blob: 8bd2f432d8f028e32c23a928820991687466c3d8 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Chris Lattner58af2a12006-02-15 07:22:58 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer14310612006-12-31 05:40:51 +000022#include "llvm/Support/CommandLine.h"
Chris Lattnerf7469af2007-01-31 04:44:08 +000023#include "llvm/ADT/SmallVector.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000024#include "llvm/ADT/STLExtras.h"
25#include "llvm/Support/MathExtras.h"
Reid Spencer481169e2006-12-01 00:33:46 +000026#include "llvm/Support/Streams.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000027#include <algorithm>
Chris Lattner58af2a12006-02-15 07:22:58 +000028#include <list>
Chris Lattner8adde282007-02-11 21:40:10 +000029#include <map>
Chris Lattner58af2a12006-02-15 07:22:58 +000030#include <utility>
Reid Spencer14310612006-12-31 05:40:51 +000031#ifndef NDEBUG
32#define YYDEBUG 1
33#endif
Chris Lattner58af2a12006-02-15 07:22:58 +000034
Reid Spencere4f47592006-08-18 17:32:55 +000035// The following is a gross hack. In order to rid the libAsmParser library of
36// exceptions, we have to have a way of getting the yyparse function to go into
37// an error situation. So, whenever we want an error to occur, the GenerateError
38// function (see bottom of file) sets TriggerError. Then, at the end of each
39// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
40// (a goto) to put YACC in error state. Furthermore, several calls to
41// GenerateError are made from inside productions and they must simulate the
42// previous exception behavior by exiting the production immediately. We have
43// replaced these with the GEN_ERROR macro which calls GeneratError and then
44// immediately invokes YYERROR. This would be so much cleaner if it was a
45// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000046static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000047#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000048#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
49
Chris Lattner58af2a12006-02-15 07:22:58 +000050int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
51int yylex(); // declaration" of xxx warnings.
52int yyparse();
53
54namespace llvm {
55 std::string CurFilename;
Reid Spencer14310612006-12-31 05:40:51 +000056#if YYDEBUG
57static cl::opt<bool>
58Debug("debug-yacc", cl::desc("Print yacc debug state changes"),
59 cl::Hidden, cl::init(false));
60#endif
Chris Lattner58af2a12006-02-15 07:22:58 +000061}
62using namespace llvm;
63
64static Module *ParserResult;
65
66// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
67// relating to upreferences in the input stream.
68//
69//#define DEBUG_UPREFS 1
70#ifdef DEBUG_UPREFS
Bill Wendlinge8156192006-12-07 01:30:32 +000071#define UR_OUT(X) cerr << X
Chris Lattner58af2a12006-02-15 07:22:58 +000072#else
73#define UR_OUT(X)
74#endif
75
76#define YYERROR_VERBOSE 1
77
Chris Lattner58af2a12006-02-15 07:22:58 +000078static GlobalVariable *CurGV;
79
80
81// This contains info used when building the body of a function. It is
82// destroyed when the function is completed.
83//
84typedef std::vector<Value *> ValueList; // Numbered defs
Reid Spencer14310612006-12-31 05:40:51 +000085
Chris Lattner58af2a12006-02-15 07:22:58 +000086static void
Reid Spencer93c40032007-03-19 18:40:50 +000087ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers=0);
Chris Lattner58af2a12006-02-15 07:22:58 +000088
89static struct PerModuleInfo {
90 Module *CurrentModule;
Reid Spencer93c40032007-03-19 18:40:50 +000091 ValueList Values; // Module level numbered definitions
92 ValueList LateResolveValues;
Reid Spencer861d9d62006-11-28 07:29:44 +000093 std::vector<PATypeHolder> Types;
94 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner58af2a12006-02-15 07:22:58 +000095
96 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner0ad19702006-06-21 16:53:00 +000097 /// how they were referenced and on which line of the input they came from so
Chris Lattner58af2a12006-02-15 07:22:58 +000098 /// that we can resolve them later and print error messages as appropriate.
99 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
100
101 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
102 // references to global values. Global values may be referenced before they
103 // are defined, and if so, the temporary object that they represent is held
104 // here. This is used for forward references of GlobalValues.
105 //
106 typedef std::map<std::pair<const PointerType *,
107 ValID>, GlobalValue*> GlobalRefsType;
108 GlobalRefsType GlobalRefs;
109
110 void ModuleDone() {
111 // If we could not resolve some functions at function compilation time
112 // (calls to functions before they are defined), resolve them now... Types
113 // are resolved when the constant pool has been completely parsed.
114 //
115 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000116 if (TriggerError)
117 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000118
119 // Check to make sure that all global value forward references have been
120 // resolved!
121 //
122 if (!GlobalRefs.empty()) {
123 std::string UndefinedReferences = "Unresolved global references exist:\n";
124
125 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
126 I != E; ++I) {
127 UndefinedReferences += " " + I->first.first->getDescription() + " " +
128 I->first.second.getName() + "\n";
129 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000130 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000131 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000132 }
133
Chris Lattner58af2a12006-02-15 07:22:58 +0000134 Values.clear(); // Clear out function local definitions
135 Types.clear();
136 CurrentModule = 0;
137 }
138
139 // GetForwardRefForGlobal - Check to see if there is a forward reference
140 // for this global. If so, remove it from the GlobalRefs map and return it.
141 // If not, just return null.
142 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
143 // Check to see if there is a forward reference to this global variable...
144 // if there is, eliminate it and patch the reference to use the new def'n.
145 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
146 GlobalValue *Ret = 0;
147 if (I != GlobalRefs.end()) {
148 Ret = I->second;
149 GlobalRefs.erase(I);
150 }
151 return Ret;
152 }
Reid Spencer8c8a2dc2007-01-02 21:54:12 +0000153
154 bool TypeIsUnresolved(PATypeHolder* PATy) {
155 // If it isn't abstract, its resolved
156 const Type* Ty = PATy->get();
157 if (!Ty->isAbstract())
158 return false;
159 // Traverse the type looking for abstract types. If it isn't abstract then
160 // we don't need to traverse that leg of the type.
161 std::vector<const Type*> WorkList, SeenList;
162 WorkList.push_back(Ty);
163 while (!WorkList.empty()) {
164 const Type* Ty = WorkList.back();
165 SeenList.push_back(Ty);
166 WorkList.pop_back();
167 if (const OpaqueType* OpTy = dyn_cast<OpaqueType>(Ty)) {
168 // Check to see if this is an unresolved type
169 std::map<ValID, PATypeHolder>::iterator I = LateResolveTypes.begin();
170 std::map<ValID, PATypeHolder>::iterator E = LateResolveTypes.end();
171 for ( ; I != E; ++I) {
172 if (I->second.get() == OpTy)
173 return true;
174 }
175 } else if (const SequentialType* SeqTy = dyn_cast<SequentialType>(Ty)) {
176 const Type* TheTy = SeqTy->getElementType();
177 if (TheTy->isAbstract() && TheTy != Ty) {
178 std::vector<const Type*>::iterator I = SeenList.begin(),
179 E = SeenList.end();
180 for ( ; I != E; ++I)
181 if (*I == TheTy)
182 break;
183 if (I == E)
184 WorkList.push_back(TheTy);
185 }
186 } else if (const StructType* StrTy = dyn_cast<StructType>(Ty)) {
187 for (unsigned i = 0; i < StrTy->getNumElements(); ++i) {
188 const Type* TheTy = StrTy->getElementType(i);
189 if (TheTy->isAbstract() && TheTy != Ty) {
190 std::vector<const Type*>::iterator I = SeenList.begin(),
191 E = SeenList.end();
192 for ( ; I != E; ++I)
193 if (*I == TheTy)
194 break;
195 if (I == E)
196 WorkList.push_back(TheTy);
197 }
198 }
199 }
200 }
201 return false;
202 }
203
204
Chris Lattner58af2a12006-02-15 07:22:58 +0000205} CurModule;
206
207static struct PerFunctionInfo {
208 Function *CurrentFunction; // Pointer to current function being created
209
Reid Spencer93c40032007-03-19 18:40:50 +0000210 ValueList Values; // Keep track of #'d definitions
211 unsigned NextValNum;
212 ValueList LateResolveValues;
Reid Spenceref9b9a72007-02-05 20:47:22 +0000213 bool isDeclare; // Is this function a forward declararation?
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000214 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000215 GlobalValue::VisibilityTypes Visibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000216
217 /// BBForwardRefs - When we see forward references to basic blocks, keep
218 /// track of them here.
Reid Spencer93c40032007-03-19 18:40:50 +0000219 std::map<ValID, BasicBlock*> BBForwardRefs;
Chris Lattner58af2a12006-02-15 07:22:58 +0000220
221 inline PerFunctionInfo() {
222 CurrentFunction = 0;
223 isDeclare = false;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000224 Linkage = GlobalValue::ExternalLinkage;
225 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000226 }
227
228 inline void FunctionStart(Function *M) {
229 CurrentFunction = M;
Reid Spencer93c40032007-03-19 18:40:50 +0000230 NextValNum = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000231 }
232
233 void FunctionDone() {
Chris Lattner58af2a12006-02-15 07:22:58 +0000234 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000235 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000236 GenerateError("Undefined reference to label " +
Reid Spencer93c40032007-03-19 18:40:50 +0000237 BBForwardRefs.begin()->second->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000238 return;
239 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000240
241 // Resolve all forward references now.
242 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
243
244 Values.clear(); // Clear out function local definitions
Reid Spencer93c40032007-03-19 18:40:50 +0000245 BBForwardRefs.clear();
Chris Lattner58af2a12006-02-15 07:22:58 +0000246 CurrentFunction = 0;
247 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000248 Linkage = GlobalValue::ExternalLinkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000249 Visibility = GlobalValue::DefaultVisibility;
Chris Lattner58af2a12006-02-15 07:22:58 +0000250 }
251} CurFun; // Info for the current function...
252
253static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
254
255
256//===----------------------------------------------------------------------===//
257// Code to handle definitions of all the types
258//===----------------------------------------------------------------------===//
259
Reid Spencer93c40032007-03-19 18:40:50 +0000260static void InsertValue(Value *V, ValueList &ValueTab = CurFun.Values) {
261 // Things that have names or are void typed don't get slot numbers
262 if (V->hasName() || (V->getType() == Type::VoidTy))
263 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000264
Reid Spencer93c40032007-03-19 18:40:50 +0000265 // In the case of function values, we have to allow for the forward reference
266 // of basic blocks, which are included in the numbering. Consequently, we keep
267 // track of the next insertion location with NextValNum. When a BB gets
268 // inserted, it could change the size of the CurFun.Values vector.
269 if (&ValueTab == &CurFun.Values) {
270 if (ValueTab.size() <= CurFun.NextValNum)
271 ValueTab.resize(CurFun.NextValNum+1);
272 ValueTab[CurFun.NextValNum++] = V;
273 return;
274 }
275 // For all other lists, its okay to just tack it on the back of the vector.
276 ValueTab.push_back(V);
Chris Lattner58af2a12006-02-15 07:22:58 +0000277}
278
279static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
280 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000281 case ValID::LocalID: // Is it a numbered definition?
Chris Lattner58af2a12006-02-15 07:22:58 +0000282 // Module constants occupy the lowest numbered slots...
Reid Spencer41dff5e2007-01-26 08:05:27 +0000283 if (D.Num < CurModule.Types.size())
284 return CurModule.Types[D.Num];
Chris Lattner58af2a12006-02-15 07:22:58 +0000285 break;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000286 case ValID::LocalName: // Is it a named definition?
Chris Lattner58af2a12006-02-15 07:22:58 +0000287 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
288 D.destroy(); // Free old strdup'd memory...
289 return N;
290 }
291 break;
292 default:
Reid Spencerb5334b02007-02-05 10:18:06 +0000293 GenerateError("Internal parser error: Invalid symbol type reference");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000294 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000295 }
296
297 // If we reached here, we referenced either a symbol that we don't know about
298 // or an id number that hasn't been read yet. We may be referencing something
299 // forward, so just create an entry to be resolved later and get to it...
300 //
301 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
302
303
304 if (inFunctionScope()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000305 if (D.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000306 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000307 return 0;
308 } else {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000309 GenerateError("Reference to an undefined type: #" + utostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000310 return 0;
311 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000312 }
313
Reid Spencer861d9d62006-11-28 07:29:44 +0000314 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner58af2a12006-02-15 07:22:58 +0000315 if (I != CurModule.LateResolveTypes.end())
Reid Spencer861d9d62006-11-28 07:29:44 +0000316 return I->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000317
Reid Spencer861d9d62006-11-28 07:29:44 +0000318 Type *Typ = OpaqueType::get();
319 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
320 return Typ;
Reid Spencera132e042006-12-03 05:46:11 +0000321 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000322
Reid Spencer93c40032007-03-19 18:40:50 +0000323// getExistingVal - Look up the value specified by the provided type and
Chris Lattner58af2a12006-02-15 07:22:58 +0000324// the provided ValID. If the value exists and has already been defined, return
325// it. Otherwise return null.
326//
Reid Spencer93c40032007-03-19 18:40:50 +0000327static Value *getExistingVal(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000328 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000329 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000330 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000331 return 0;
332 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000333
334 switch (D.Type) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000335 case ValID::LocalID: { // Is it a numbered definition?
Reid Spencer41dff5e2007-01-26 08:05:27 +0000336 // Check that the number is within bounds.
Reid Spencer93c40032007-03-19 18:40:50 +0000337 if (D.Num >= CurFun.Values.size())
338 return 0;
339 Value *Result = CurFun.Values[D.Num];
340 if (Ty != Result->getType()) {
341 GenerateError("Numbered value (%" + utostr(D.Num) + ") of type '" +
342 Result->getType()->getDescription() + "' does not match "
343 "expected type, '" + Ty->getDescription() + "'");
344 return 0;
345 }
346 return Result;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000347 }
348 case ValID::GlobalID: { // Is it a numbered definition?
Reid Spencer93c40032007-03-19 18:40:50 +0000349 if (D.Num >= CurModule.Values.size())
Reid Spenceref9b9a72007-02-05 20:47:22 +0000350 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000351 Value *Result = CurModule.Values[D.Num];
352 if (Ty != Result->getType()) {
353 GenerateError("Numbered value (@" + utostr(D.Num) + ") of type '" +
354 Result->getType()->getDescription() + "' does not match "
355 "expected type, '" + Ty->getDescription() + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000356 return 0;
Reid Spencer93c40032007-03-19 18:40:50 +0000357 }
358 return Result;
Chris Lattner58af2a12006-02-15 07:22:58 +0000359 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000360
361 case ValID::LocalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000362 if (!inFunctionScope())
363 return 0;
364 ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable();
365 Value *N = SymTab.lookup(D.Name);
366 if (N == 0)
367 return 0;
368 if (N->getType() != Ty)
369 return 0;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000370
371 D.destroy(); // Free old strdup'd memory...
372 return N;
373 }
374 case ValID::GlobalName: { // Is it a named definition?
Reid Spenceref9b9a72007-02-05 20:47:22 +0000375 ValueSymbolTable &SymTab = CurModule.CurrentModule->getValueSymbolTable();
376 Value *N = SymTab.lookup(D.Name);
377 if (N == 0)
378 return 0;
379 if (N->getType() != Ty)
380 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000381
382 D.destroy(); // Free old strdup'd memory...
383 return N;
384 }
385
386 // Check to make sure that "Ty" is an integral type, and that our
387 // value will fit into the specified type...
388 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000389 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000390 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000391 itostr(D.ConstPool64) + "' is invalid for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000392 Ty->getDescription() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000393 return 0;
394 }
Reid Spencer49d273e2007-03-19 20:40:51 +0000395 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000396
397 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000398 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
399 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000400 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Reid Spencerb5334b02007-02-05 10:18:06 +0000401 "' is invalid or out of range");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000402 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000403 } else { // This is really a signed reference. Transmogrify.
Reid Spencer49d273e2007-03-19 20:40:51 +0000404 return ConstantInt::get(Ty, D.ConstPool64, true);
Chris Lattner58af2a12006-02-15 07:22:58 +0000405 }
406 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000407 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000408 }
409
410 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000411 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000412 GenerateError("FP constant invalid for type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000413 return 0;
414 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000415 return ConstantFP::get(Ty, D.ConstPoolFP);
416
417 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000418 if (!isa<PointerType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000419 GenerateError("Cannot create a a non pointer null");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000420 return 0;
421 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000422 return ConstantPointerNull::get(cast<PointerType>(Ty));
423
424 case ValID::ConstUndefVal: // Is it an undef value?
425 return UndefValue::get(Ty);
426
427 case ValID::ConstZeroVal: // Is it a zero value?
428 return Constant::getNullValue(Ty);
429
430 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000431 if (D.ConstantValue->getType() != Ty) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000432 GenerateError("Constant expression type different from required type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000433 return 0;
434 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000435 return D.ConstantValue;
436
437 case ValID::InlineAsmVal: { // Inline asm expression
438 const PointerType *PTy = dyn_cast<PointerType>(Ty);
439 const FunctionType *FTy =
440 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000441 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000442 GenerateError("Invalid type for asm constraint string");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000443 return 0;
444 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000445 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
446 D.IAD->HasSideEffects);
447 D.destroy(); // Free InlineAsmDescriptor.
448 return IA;
449 }
450 default:
Reid Spencera9720f52007-02-05 17:04:00 +0000451 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000452 return 0;
453 } // End of switch
454
Reid Spencera9720f52007-02-05 17:04:00 +0000455 assert(0 && "Unhandled case!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000456 return 0;
457}
458
Reid Spencer93c40032007-03-19 18:40:50 +0000459// getVal - This function is identical to getExistingVal, except that if a
Chris Lattner58af2a12006-02-15 07:22:58 +0000460// value is not already defined, it "improvises" by creating a placeholder var
461// that looks and acts just like the requested variable. When the value is
462// defined later, all uses of the placeholder variable are replaced with the
463// real thing.
464//
465static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000466 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000467 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000468 return 0;
469 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000470
471 // See if the value has already been defined.
Reid Spencer93c40032007-03-19 18:40:50 +0000472 Value *V = getExistingVal(Ty, ID);
Chris Lattner58af2a12006-02-15 07:22:58 +0000473 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000474 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000475
Reid Spencer5b7e7532006-09-28 19:28:24 +0000476 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000477 GenerateError("Invalid use of a composite type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000478 return 0;
479 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000480
481 // If we reached here, we referenced either a symbol that we don't know about
482 // or an id number that hasn't been read yet. We may be referencing something
483 // forward, so just create an entry to be resolved later and get to it...
484 //
485 V = new Argument(Ty);
486
487 // Remember where this forward reference came from. FIXME, shouldn't we try
488 // to recycle these things??
489 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
490 llvmAsmlineno)));
491
492 if (inFunctionScope())
493 InsertValue(V, CurFun.LateResolveValues);
494 else
495 InsertValue(V, CurModule.LateResolveValues);
496 return V;
497}
498
Reid Spencer93c40032007-03-19 18:40:50 +0000499/// defineBBVal - This is a definition of a new basic block with the specified
500/// identifier which must be the same as CurFun.NextValNum, if its numeric.
501static BasicBlock *defineBBVal(const ValID &ID) {
Reid Spencera9720f52007-02-05 17:04:00 +0000502 assert(inFunctionScope() && "Can't get basic block at global scope!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000503
Chris Lattner58af2a12006-02-15 07:22:58 +0000504 BasicBlock *BB = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000505
Reid Spencer93c40032007-03-19 18:40:50 +0000506 // First, see if this was forward referenced
Chris Lattner58af2a12006-02-15 07:22:58 +0000507
Reid Spencer93c40032007-03-19 18:40:50 +0000508 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
509 if (BBI != CurFun.BBForwardRefs.end()) {
510 BB = BBI->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000511 // The forward declaration could have been inserted anywhere in the
512 // function: insert it into the correct place now.
513 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
514 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Reid Spencer93c40032007-03-19 18:40:50 +0000515
Reid Spencer66728ef2007-03-20 01:13:36 +0000516 // We're about to erase the entry, save the key so we can clean it up.
517 ValID Tmp = BBI->first;
518
Reid Spencer93c40032007-03-19 18:40:50 +0000519 // Erase the forward ref from the map as its no longer "forward"
520 CurFun.BBForwardRefs.erase(ID);
521
Reid Spencer66728ef2007-03-20 01:13:36 +0000522 // The key has been removed from the map but so we don't want to leave
523 // strdup'd memory around so destroy it too.
524 Tmp.destroy();
525
Reid Spencer93c40032007-03-19 18:40:50 +0000526 // If its a numbered definition, bump the number and set the BB value.
527 if (ID.Type == ValID::LocalID) {
528 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
529 InsertValue(BB);
530 }
531
532 ID.destroy();
533 return BB;
534 }
535
536 // We haven't seen this BB before and its first mention is a definition.
537 // Just create it and return it.
538 std::string Name (ID.Type == ValID::LocalName ? ID.Name : "");
539 BB = new BasicBlock(Name, CurFun.CurrentFunction);
540 if (ID.Type == ValID::LocalID) {
541 assert(ID.Num == CurFun.NextValNum && "Invalid new block number");
542 InsertValue(BB);
Chris Lattner58af2a12006-02-15 07:22:58 +0000543 }
Reid Spencer93c40032007-03-19 18:40:50 +0000544
545 ID.destroy(); // Free strdup'd memory
546 return BB;
547}
548
549/// getBBVal - get an existing BB value or create a forward reference for it.
550///
551static BasicBlock *getBBVal(const ValID &ID) {
552 assert(inFunctionScope() && "Can't get basic block at global scope!");
553
554 BasicBlock *BB = 0;
555
556 std::map<ValID, BasicBlock*>::iterator BBI = CurFun.BBForwardRefs.find(ID);
557 if (BBI != CurFun.BBForwardRefs.end()) {
558 BB = BBI->second;
559 } if (ID.Type == ValID::LocalName) {
560 std::string Name = ID.Name;
561 Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name);
562 if (N)
563 if (N->getType()->getTypeID() == Type::LabelTyID)
564 BB = cast<BasicBlock>(N);
565 else
566 GenerateError("Reference to label '" + Name + "' is actually of type '"+
567 N->getType()->getDescription() + "'");
568 } else if (ID.Type == ValID::LocalID) {
569 if (ID.Num < CurFun.NextValNum && ID.Num < CurFun.Values.size()) {
570 if (CurFun.Values[ID.Num]->getType()->getTypeID() == Type::LabelTyID)
571 BB = cast<BasicBlock>(CurFun.Values[ID.Num]);
572 else
573 GenerateError("Reference to label '%" + utostr(ID.Num) +
574 "' is actually of type '"+
575 CurFun.Values[ID.Num]->getType()->getDescription() + "'");
576 }
577 } else {
578 GenerateError("Illegal label reference " + ID.getName());
579 return 0;
580 }
581
582 // If its already been defined, return it now.
583 if (BB) {
584 ID.destroy(); // Free strdup'd memory.
585 return BB;
586 }
587
588 // Otherwise, this block has not been seen before, create it.
589 std::string Name;
590 if (ID.Type == ValID::LocalName)
591 Name = ID.Name;
592 BB = new BasicBlock(Name, CurFun.CurrentFunction);
593
594 // Insert it in the forward refs map.
595 CurFun.BBForwardRefs[ID] = BB;
596
Chris Lattner58af2a12006-02-15 07:22:58 +0000597 return BB;
598}
599
600
601//===----------------------------------------------------------------------===//
602// Code to handle forward references in instructions
603//===----------------------------------------------------------------------===//
604//
605// This code handles the late binding needed with statements that reference
606// values not defined yet... for example, a forward branch, or the PHI node for
607// a loop body.
608//
609// This keeps a table (CurFun.LateResolveValues) of all such forward references
610// and back patchs after we are done.
611//
612
613// ResolveDefinitions - If we could not resolve some defs at parsing
614// time (forward branches, phi functions for loops, etc...) resolve the
615// defs now...
616//
617static void
Reid Spencer93c40032007-03-19 18:40:50 +0000618ResolveDefinitions(ValueList &LateResolvers, ValueList *FutureLateResolvers) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000619 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Reid Spencer93c40032007-03-19 18:40:50 +0000620 while (!LateResolvers.empty()) {
621 Value *V = LateResolvers.back();
622 LateResolvers.pop_back();
Chris Lattner58af2a12006-02-15 07:22:58 +0000623
Reid Spencer93c40032007-03-19 18:40:50 +0000624 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
625 CurModule.PlaceHolderInfo.find(V);
626 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000627
Reid Spencer93c40032007-03-19 18:40:50 +0000628 ValID &DID = PHI->second.first;
Chris Lattner58af2a12006-02-15 07:22:58 +0000629
Reid Spencer93c40032007-03-19 18:40:50 +0000630 Value *TheRealValue = getExistingVal(V->getType(), DID);
631 if (TriggerError)
632 return;
633 if (TheRealValue) {
634 V->replaceAllUsesWith(TheRealValue);
635 delete V;
636 CurModule.PlaceHolderInfo.erase(PHI);
637 } else if (FutureLateResolvers) {
638 // Functions have their unresolved items forwarded to the module late
639 // resolver table
640 InsertValue(V, *FutureLateResolvers);
641 } else {
642 if (DID.Type == ValID::LocalName || DID.Type == ValID::GlobalName) {
643 GenerateError("Reference to an invalid definition: '" +DID.getName()+
644 "' of type '" + V->getType()->getDescription() + "'",
645 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000646 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000647 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000648 GenerateError("Reference to an invalid definition: #" +
649 itostr(DID.Num) + " of type '" +
650 V->getType()->getDescription() + "'",
651 PHI->second.second);
652 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000653 }
654 }
655 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000656 LateResolvers.clear();
657}
658
659// ResolveTypeTo - A brand new type was just declared. This means that (if
660// name is not null) things referencing Name can be resolved. Otherwise, things
661// refering to the number can be resolved. Do this now.
662//
663static void ResolveTypeTo(char *Name, const Type *ToTy) {
664 ValID D;
Reid Spencer41dff5e2007-01-26 08:05:27 +0000665 if (Name) D = ValID::createLocalName(Name);
666 else D = ValID::createLocalID(CurModule.Types.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000667
Reid Spencer861d9d62006-11-28 07:29:44 +0000668 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000669 CurModule.LateResolveTypes.find(D);
670 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000671 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000672 CurModule.LateResolveTypes.erase(I);
673 }
674}
675
676// setValueName - Set the specified value to the name given. The name may be
677// null potentially, in which case this is a noop. The string passed in is
678// assumed to be a malloc'd string buffer, and is free'd by this function.
679//
680static void setValueName(Value *V, char *NameStr) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000681 if (!NameStr) return;
682 std::string Name(NameStr); // Copy string
683 free(NameStr); // Free old string
Chris Lattner58af2a12006-02-15 07:22:58 +0000684
Reid Spencer41dff5e2007-01-26 08:05:27 +0000685 if (V->getType() == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000686 GenerateError("Can't assign name '" + Name+"' to value with void type");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000687 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000688 }
Reid Spencer41dff5e2007-01-26 08:05:27 +0000689
Reid Spencera9720f52007-02-05 17:04:00 +0000690 assert(inFunctionScope() && "Must be in function scope!");
Reid Spenceref9b9a72007-02-05 20:47:22 +0000691 ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable();
692 if (ST.lookup(Name)) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000693 GenerateError("Redefinition of value '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000694 V->getType()->getDescription() + "'");
Reid Spencer41dff5e2007-01-26 08:05:27 +0000695 return;
696 }
697
698 // Set the name.
699 V->setName(Name);
Chris Lattner58af2a12006-02-15 07:22:58 +0000700}
701
702/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
703/// this is a declaration, otherwise it is a definition.
704static GlobalVariable *
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000705ParseGlobalVariable(char *NameStr,
706 GlobalValue::LinkageTypes Linkage,
707 GlobalValue::VisibilityTypes Visibility,
Chris Lattner58af2a12006-02-15 07:22:58 +0000708 bool isConstantGlobal, const Type *Ty,
709 Constant *Initializer) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000710 if (isa<FunctionType>(Ty)) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000711 GenerateError("Cannot declare global vars of function type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000712 return 0;
713 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000714
715 const PointerType *PTy = PointerType::get(Ty);
716
717 std::string Name;
718 if (NameStr) {
719 Name = NameStr; // Copy string
720 free(NameStr); // Free old string
721 }
722
723 // See if this global value was forward referenced. If so, recycle the
724 // object.
725 ValID ID;
726 if (!Name.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +0000727 ID = ValID::createGlobalName((char*)Name.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +0000728 } else {
Reid Spencer93c40032007-03-19 18:40:50 +0000729 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +0000730 }
731
732 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
733 // Move the global to the end of the list, from whereever it was
734 // previously inserted.
735 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
736 CurModule.CurrentModule->getGlobalList().remove(GV);
737 CurModule.CurrentModule->getGlobalList().push_back(GV);
738 GV->setInitializer(Initializer);
739 GV->setLinkage(Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000740 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000741 GV->setConstant(isConstantGlobal);
742 InsertValue(GV, CurModule.Values);
743 return GV;
744 }
745
Reid Spenceref9b9a72007-02-05 20:47:22 +0000746 // If this global has a name
Chris Lattner58af2a12006-02-15 07:22:58 +0000747 if (!Name.empty()) {
Reid Spenceref9b9a72007-02-05 20:47:22 +0000748 // if the global we're parsing has an initializer (is a definition) and
749 // has external linkage.
750 if (Initializer && Linkage != GlobalValue::InternalLinkage)
751 // If there is already a global with external linkage with this name
752 if (CurModule.CurrentModule->getGlobalVariable(Name, false)) {
753 // If we allow this GVar to get created, it will be renamed in the
754 // symbol table because it conflicts with an existing GVar. We can't
755 // allow redefinition of GVars whose linking indicates that their name
756 // must stay the same. Issue the error.
757 GenerateError("Redefinition of global variable named '" + Name +
758 "' of type '" + Ty->getDescription() + "'");
759 return 0;
760 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000761 }
762
763 // Otherwise there is no existing GV to use, create one now.
764 GlobalVariable *GV =
765 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
766 CurModule.CurrentModule);
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000767 GV->setVisibility(Visibility);
Chris Lattner58af2a12006-02-15 07:22:58 +0000768 InsertValue(GV, CurModule.Values);
769 return GV;
770}
771
772// setTypeName - Set the specified type to the name given. The name may be
773// null potentially, in which case this is a noop. The string passed in is
774// assumed to be a malloc'd string buffer, and is freed by this function.
775//
776// This function returns true if the type has already been defined, but is
777// allowed to be redefined in the specified context. If the name is a new name
778// for the type plane, it is inserted and false is returned.
779static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencera9720f52007-02-05 17:04:00 +0000780 assert(!inFunctionScope() && "Can't give types function-local names!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000781 if (NameStr == 0) return false;
782
783 std::string Name(NameStr); // Copy string
784 free(NameStr); // Free old string
785
786 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000787 if (T == Type::VoidTy) {
Reid Spencerb5334b02007-02-05 10:18:06 +0000788 GenerateError("Can't assign name '" + Name + "' to the void type");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000789 return false;
790 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000791
792 // Set the type name, checking for conflicts as we do so.
793 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
794
795 if (AlreadyExists) { // Inserting a name that is already defined???
796 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
Reid Spencera9720f52007-02-05 17:04:00 +0000797 assert(Existing && "Conflict but no matching type?!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000798
799 // There is only one case where this is allowed: when we are refining an
800 // opaque type. In this case, Existing will be an opaque type.
801 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
802 // We ARE replacing an opaque type!
803 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
804 return true;
805 }
806
807 // Otherwise, this is an attempt to redefine a type. That's okay if
808 // the redefinition is identical to the original. This will be so if
809 // Existing and T point to the same Type object. In this one case we
810 // allow the equivalent redefinition.
811 if (Existing == T) return true; // Yes, it's equal.
812
813 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer63c34452007-01-05 21:51:07 +0000814 GenerateError("Redefinition of type named '" + Name + "' of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +0000815 T->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +0000816 }
817
818 return false;
819}
820
821//===----------------------------------------------------------------------===//
822// Code for handling upreferences in type names...
823//
824
825// TypeContains - Returns true if Ty directly contains E in it.
826//
827static bool TypeContains(const Type *Ty, const Type *E) {
828 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
829 E) != Ty->subtype_end();
830}
831
832namespace {
833 struct UpRefRecord {
834 // NestingLevel - The number of nesting levels that need to be popped before
835 // this type is resolved.
836 unsigned NestingLevel;
837
838 // LastContainedTy - This is the type at the current binding level for the
839 // type. Every time we reduce the nesting level, this gets updated.
840 const Type *LastContainedTy;
841
842 // UpRefTy - This is the actual opaque type that the upreference is
843 // represented with.
844 OpaqueType *UpRefTy;
845
846 UpRefRecord(unsigned NL, OpaqueType *URTy)
847 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
848 };
849}
850
851// UpRefs - A list of the outstanding upreferences that need to be resolved.
852static std::vector<UpRefRecord> UpRefs;
853
854/// HandleUpRefs - Every time we finish a new layer of types, this function is
855/// called. It loops through the UpRefs vector, which is a list of the
856/// currently active types. For each type, if the up reference is contained in
857/// the newly completed type, we decrement the level count. When the level
858/// count reaches zero, the upreferenced type is the type that is passed in:
859/// thus we can complete the cycle.
860///
861static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000862 // If Ty isn't abstract, or if there are no up-references in it, then there is
863 // nothing to resolve here.
864 if (!ty->isAbstract() || UpRefs.empty()) return ty;
865
Chris Lattner58af2a12006-02-15 07:22:58 +0000866 PATypeHolder Ty(ty);
867 UR_OUT("Type '" << Ty->getDescription() <<
868 "' newly formed. Resolving upreferences.\n" <<
869 UpRefs.size() << " upreferences active!\n");
870
871 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
872 // to zero), we resolve them all together before we resolve them to Ty. At
873 // the end of the loop, if there is anything to resolve to Ty, it will be in
874 // this variable.
875 OpaqueType *TypeToResolve = 0;
876
877 for (unsigned i = 0; i != UpRefs.size(); ++i) {
878 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
879 << UpRefs[i].second->getDescription() << ") = "
880 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
881 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
882 // Decrement level of upreference
883 unsigned Level = --UpRefs[i].NestingLevel;
884 UpRefs[i].LastContainedTy = Ty;
885 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
886 if (Level == 0) { // Upreference should be resolved!
887 if (!TypeToResolve) {
888 TypeToResolve = UpRefs[i].UpRefTy;
889 } else {
890 UR_OUT(" * Resolving upreference for "
891 << UpRefs[i].second->getDescription() << "\n";
892 std::string OldName = UpRefs[i].UpRefTy->getDescription());
893 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
894 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
895 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
896 }
897 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
898 --i; // Do not skip the next element...
899 }
900 }
901 }
902
903 if (TypeToResolve) {
904 UR_OUT(" * Resolving upreference for "
905 << UpRefs[i].second->getDescription() << "\n";
906 std::string OldName = TypeToResolve->getDescription());
907 TypeToResolve->refineAbstractTypeTo(Ty);
908 }
909
910 return Ty;
911}
912
Chris Lattner58af2a12006-02-15 07:22:58 +0000913//===----------------------------------------------------------------------===//
914// RunVMAsmParser - Define an interface to this parser
915//===----------------------------------------------------------------------===//
916//
Reid Spencer14310612006-12-31 05:40:51 +0000917static Module* RunParser(Module * M);
918
Chris Lattner58af2a12006-02-15 07:22:58 +0000919Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
920 set_scan_file(F);
921
922 CurFilename = Filename;
923 return RunParser(new Module(CurFilename));
924}
925
926Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
927 set_scan_string(AsmString);
928
929 CurFilename = "from_memory";
930 if (M == NULL) {
931 return RunParser(new Module (CurFilename));
932 } else {
933 return RunParser(M);
934 }
935}
936
937%}
938
939%union {
940 llvm::Module *ModuleVal;
941 llvm::Function *FunctionVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000942 llvm::BasicBlock *BasicBlockVal;
943 llvm::TerminatorInst *TermInstVal;
944 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000945 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000946
Reid Spencera132e042006-12-03 05:46:11 +0000947 const llvm::Type *PrimType;
Reid Spencer14310612006-12-31 05:40:51 +0000948 std::list<llvm::PATypeHolder> *TypeList;
Reid Spencera132e042006-12-03 05:46:11 +0000949 llvm::PATypeHolder *TypeVal;
950 llvm::Value *ValueVal;
Reid Spencera132e042006-12-03 05:46:11 +0000951 std::vector<llvm::Value*> *ValueList;
Reid Spencer14310612006-12-31 05:40:51 +0000952 llvm::ArgListType *ArgList;
953 llvm::TypeWithAttrs TypeWithAttrs;
954 llvm::TypeWithAttrsList *TypeWithAttrsList;
955 llvm::ValueRefList *ValueRefList;
956
Chris Lattner58af2a12006-02-15 07:22:58 +0000957 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000958 std::list<std::pair<llvm::Value*,
959 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000960 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +0000961 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +0000962
963 llvm::GlobalValue::LinkageTypes Linkage;
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000964 llvm::GlobalValue::VisibilityTypes Visibility;
Reid Spencer14310612006-12-31 05:40:51 +0000965 llvm::FunctionType::ParameterAttributes ParamAttrs;
Reid Spencer38c91a92007-02-28 02:24:54 +0000966 llvm::APInt *APIntVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000967 int64_t SInt64Val;
968 uint64_t UInt64Val;
969 int SIntVal;
970 unsigned UIntVal;
971 double FPVal;
972 bool BoolVal;
973
974 char *StrVal; // This memory is strdup'd!
Reid Spencer1628cec2006-10-26 06:15:43 +0000975 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner58af2a12006-02-15 07:22:58 +0000976
Reid Spencera132e042006-12-03 05:46:11 +0000977 llvm::Instruction::BinaryOps BinaryOpVal;
978 llvm::Instruction::TermOps TermOpVal;
979 llvm::Instruction::MemoryOps MemOpVal;
980 llvm::Instruction::CastOps CastOpVal;
981 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencera132e042006-12-03 05:46:11 +0000982 llvm::ICmpInst::Predicate IPredicate;
983 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +0000984}
985
Reid Spencer14310612006-12-31 05:40:51 +0000986%type <ModuleVal> Module
Chris Lattner58af2a12006-02-15 07:22:58 +0000987%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
988%type <BasicBlockVal> BasicBlock InstructionList
989%type <TermInstVal> BBTerminatorInst
990%type <InstVal> Inst InstVal MemoryInst
991%type <ConstVal> ConstVal ConstExpr
992%type <ConstVector> ConstVector
993%type <ArgList> ArgList ArgListH
Chris Lattner58af2a12006-02-15 07:22:58 +0000994%type <PHIList> PHIList
Reid Spencer14310612006-12-31 05:40:51 +0000995%type <ValueRefList> ValueRefList // For call param lists & GEP indices
996%type <ValueList> IndexList // For GEP indices
997%type <TypeList> TypeListI
998%type <TypeWithAttrsList> ArgTypeList ArgTypeListI
Reid Spencer218ded22007-01-05 17:07:23 +0000999%type <TypeWithAttrs> ArgType
Chris Lattner58af2a12006-02-15 07:22:58 +00001000%type <JumpTable> JumpTable
1001%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1002%type <BoolVal> OptVolatile // 'volatile' or not
1003%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1004%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Reid Spencer14310612006-12-31 05:40:51 +00001005%type <Linkage> GVInternalLinkage GVExternalLinkage
1006%type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001007%type <Visibility> GVVisibilityStyle
Chris Lattner58af2a12006-02-15 07:22:58 +00001008
1009// ValueRef - Unresolved reference to a definition or BB
1010%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1011%type <ValueVal> ResolvedVal // <type> <valref> pair
1012// Tokens and types for handling constant integer values
1013//
1014// ESINT64VAL - A negative number within long long range
1015%token <SInt64Val> ESINT64VAL
1016
1017// EUINT64VAL - A positive number within uns. long long range
1018%token <UInt64Val> EUINT64VAL
Chris Lattner58af2a12006-02-15 07:22:58 +00001019
Reid Spencer38c91a92007-02-28 02:24:54 +00001020// ESAPINTVAL - A negative number with arbitrary precision
1021%token <APIntVal> ESAPINTVAL
1022
1023// EUAPINTVAL - A positive number with arbitrary precision
1024%token <APIntVal> EUAPINTVAL
1025
Reid Spencer41dff5e2007-01-26 08:05:27 +00001026%token <UIntVal> LOCALVAL_ID GLOBALVAL_ID // %123 @123
Chris Lattner58af2a12006-02-15 07:22:58 +00001027%token <FPVal> FPVAL // Float or Double constant
1028
1029// Built in types...
Reid Spencer218ded22007-01-05 17:07:23 +00001030%type <TypeVal> Types ResultTypes
Reid Spencer14310612006-12-31 05:40:51 +00001031%type <PrimType> IntType FPType PrimType // Classifications
Reid Spencer6f407902007-01-13 05:00:46 +00001032%token <PrimType> VOID INTTYPE
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001033%token <PrimType> FLOAT DOUBLE LABEL
1034%token TYPE
Chris Lattner58af2a12006-02-15 07:22:58 +00001035
Reid Spencer41dff5e2007-01-26 08:05:27 +00001036%token<StrVal> LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT
1037%type <StrVal> LocalName OptLocalName OptLocalAssign
1038%type <StrVal> GlobalName OptGlobalAssign
1039%type <UIntVal> OptAlign OptCAlign
Chris Lattner58af2a12006-02-15 07:22:58 +00001040%type <StrVal> OptSection SectionString
1041
1042%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001043%token DECLARE DEFINE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencer14310612006-12-31 05:40:51 +00001044%token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001045%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Reid Spencer41dff5e2007-01-26 08:05:27 +00001046%token OPAQUE EXTERNAL TARGET TRIPLE ALIGN
Chris Lattner58af2a12006-02-15 07:22:58 +00001047%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001048%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner1ae022f2006-10-22 06:08:13 +00001049%token DATALAYOUT
Chris Lattner58af2a12006-02-15 07:22:58 +00001050%type <UIntVal> OptCallingConv
Reid Spencer218ded22007-01-05 17:07:23 +00001051%type <ParamAttrs> OptParamAttrs ParamAttr
1052%type <ParamAttrs> OptFuncAttrs FuncAttr
Chris Lattner58af2a12006-02-15 07:22:58 +00001053
1054// Basic Block Terminating Operators
1055%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1056
1057// Binary Operators
Reid Spencere4d87aa2006-12-23 06:05:41 +00001058%type <BinaryOpVal> ArithmeticOps LogicalOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001059%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer832254e2007-02-02 02:16:23 +00001060%token <BinaryOpVal> SHL LSHR ASHR
1061
Reid Spencera132e042006-12-03 05:46:11 +00001062%token <OtherOpVal> ICMP FCMP
Reid Spencera132e042006-12-03 05:46:11 +00001063%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001064%type <FPredicate> FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001065%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1066%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001067
1068// Memory Instructions
1069%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1070
Reid Spencer3da59db2006-11-27 01:05:10 +00001071// Cast Operators
1072%type <CastOpVal> CastOps
1073%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1074%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1075
Chris Lattner58af2a12006-02-15 07:22:58 +00001076// Other Operators
Reid Spencer832254e2007-02-02 02:16:23 +00001077%token <OtherOpVal> PHI_TOK SELECT VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001078%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattner58af2a12006-02-15 07:22:58 +00001079
Reid Spencer218ded22007-01-05 17:07:23 +00001080// Function Attributes
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001081%token NORETURN INREG SRET
Chris Lattner58af2a12006-02-15 07:22:58 +00001082
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001083// Visibility Styles
1084%token DEFAULT HIDDEN
1085
Chris Lattner58af2a12006-02-15 07:22:58 +00001086%start Module
1087%%
1088
Chris Lattner58af2a12006-02-15 07:22:58 +00001089
Chris Lattner58af2a12006-02-15 07:22:58 +00001090// Operations that are notably excluded from this list include:
1091// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1092//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001093ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Reid Spencer832254e2007-02-02 02:16:23 +00001094LogicalOps : SHL | LSHR | ASHR | AND | OR | XOR;
Reid Spencer3da59db2006-11-27 01:05:10 +00001095CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1096 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
Reid Spencer832254e2007-02-02 02:16:23 +00001097
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001098IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001099 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001100 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1101 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1102 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1103 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1104 ;
1105
1106FPredicates
1107 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1108 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1109 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1110 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1111 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1112 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1113 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1114 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1115 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1116 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001117
1118// These are some types that allow classification if we only want a particular
1119// thing... for example, only a signed, unsigned, or integral type.
Reid Spencera54b7cb2007-01-12 07:05:14 +00001120IntType : INTTYPE;
Chris Lattner58af2a12006-02-15 07:22:58 +00001121FPType : FLOAT | DOUBLE;
1122
Reid Spencer41dff5e2007-01-26 08:05:27 +00001123LocalName : LOCALVAR | STRINGCONSTANT;
1124OptLocalName : LocalName | /*empty*/ { $$ = 0; };
1125
1126/// OptLocalAssign - Value producing statements have an optional assignment
1127/// component.
1128OptLocalAssign : LocalName '=' {
1129 $$ = $1;
1130 CHECK_FOR_ERROR
1131 }
1132 | /*empty*/ {
1133 $$ = 0;
1134 CHECK_FOR_ERROR
1135 };
1136
1137GlobalName : GLOBALVAR | ATSTRINGCONSTANT;
1138
1139OptGlobalAssign : GlobalName '=' {
Chris Lattner58af2a12006-02-15 07:22:58 +00001140 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001141 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001142 }
1143 | /*empty*/ {
1144 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001145 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001146 };
1147
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001148GVInternalLinkage
1149 : INTERNAL { $$ = GlobalValue::InternalLinkage; }
1150 | WEAK { $$ = GlobalValue::WeakLinkage; }
1151 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1152 | APPENDING { $$ = GlobalValue::AppendingLinkage; }
1153 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
1154 ;
1155
1156GVExternalLinkage
1157 : DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1158 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
1159 | EXTERNAL { $$ = GlobalValue::ExternalLinkage; }
1160 ;
1161
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001162GVVisibilityStyle
1163 : /*empty*/ { $$ = GlobalValue::DefaultVisibility; }
1164 | HIDDEN { $$ = GlobalValue::HiddenVisibility; }
1165 ;
1166
Reid Spencer14310612006-12-31 05:40:51 +00001167FunctionDeclareLinkage
1168 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1169 | DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; }
1170 | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001171 ;
1172
Reid Spencer14310612006-12-31 05:40:51 +00001173FunctionDefineLinkage
1174 : /*empty*/ { $$ = GlobalValue::ExternalLinkage; }
1175 | INTERNAL { $$ = GlobalValue::InternalLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001176 | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; }
1177 | WEAK { $$ = GlobalValue::WeakLinkage; }
1178 | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001179 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001180
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001181OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1182 CCC_TOK { $$ = CallingConv::C; } |
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001183 FASTCC_TOK { $$ = CallingConv::Fast; } |
1184 COLDCC_TOK { $$ = CallingConv::Cold; } |
1185 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1186 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1187 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001188 if ((unsigned)$2 != $2)
Reid Spencerb5334b02007-02-05 10:18:06 +00001189 GEN_ERROR("Calling conv too large");
Chris Lattner58af2a12006-02-15 07:22:58 +00001190 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001191 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001192 };
1193
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001194ParamAttr : ZEXT { $$ = FunctionType::ZExtAttribute; }
1195 | SEXT { $$ = FunctionType::SExtAttribute; }
1196 | INREG { $$ = FunctionType::InRegAttribute; }
1197 | SRET { $$ = FunctionType::StructRetAttribute; }
Reid Spencer14310612006-12-31 05:40:51 +00001198 ;
1199
Reid Spencer218ded22007-01-05 17:07:23 +00001200OptParamAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1201 | OptParamAttrs ParamAttr {
1202 $$ = FunctionType::ParameterAttributes($1 | $2);
Reid Spencer14310612006-12-31 05:40:51 +00001203 }
1204 ;
1205
Reid Spencer218ded22007-01-05 17:07:23 +00001206FuncAttr : NORETURN { $$ = FunctionType::NoReturnAttribute; }
1207 | ParamAttr
1208 ;
1209
1210OptFuncAttrs : /* empty */ { $$ = FunctionType::NoAttributeSet; }
1211 | OptFuncAttrs FuncAttr {
1212 $$ = FunctionType::ParameterAttributes($1 | $2);
1213 }
Reid Spencer14310612006-12-31 05:40:51 +00001214 ;
1215
Chris Lattner58af2a12006-02-15 07:22:58 +00001216// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1217// a comma before it.
1218OptAlign : /*empty*/ { $$ = 0; } |
1219 ALIGN EUINT64VAL {
1220 $$ = $2;
1221 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001222 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001223 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001224};
1225OptCAlign : /*empty*/ { $$ = 0; } |
1226 ',' ALIGN EUINT64VAL {
1227 $$ = $3;
1228 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencerb5334b02007-02-05 10:18:06 +00001229 GEN_ERROR("Alignment must be a power of two");
Reid Spencer61c83e02006-08-18 08:43:06 +00001230 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001231};
1232
1233
1234SectionString : SECTION STRINGCONSTANT {
1235 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1236 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencerb5334b02007-02-05 10:18:06 +00001237 GEN_ERROR("Invalid character in section name");
Chris Lattner58af2a12006-02-15 07:22:58 +00001238 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001239 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001240};
1241
1242OptSection : /*empty*/ { $$ = 0; } |
1243 SectionString { $$ = $1; };
1244
1245// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1246// is set to be the global we are processing.
1247//
1248GlobalVarAttributes : /* empty */ {} |
1249 ',' GlobalVarAttribute GlobalVarAttributes {};
1250GlobalVarAttribute : SectionString {
1251 CurGV->setSection($1);
1252 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001253 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001254 }
1255 | ALIGN EUINT64VAL {
1256 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001257 GEN_ERROR("Alignment must be a power of two");
Chris Lattner58af2a12006-02-15 07:22:58 +00001258 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001259 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001260 };
1261
1262//===----------------------------------------------------------------------===//
1263// Types includes all predefined types... except void, because it can only be
Reid Spencer14310612006-12-31 05:40:51 +00001264// used in specific contexts (function returning void for example).
Chris Lattner58af2a12006-02-15 07:22:58 +00001265
1266// Derived types are added later...
1267//
Reid Spencer6f407902007-01-13 05:00:46 +00001268PrimType : INTTYPE | FLOAT | DOUBLE | LABEL ;
Reid Spencer14310612006-12-31 05:40:51 +00001269
1270Types
1271 : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001272 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001273 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001274 }
1275 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001276 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001277 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00001278 }
1279 | Types '*' { // Pointer type?
1280 if (*$1 == Type::LabelTy)
1281 GEN_ERROR("Cannot form a pointer to a basic block");
1282 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1283 delete $1;
1284 CHECK_FOR_ERROR
1285 }
1286 | SymbolicValueRef { // Named types are also simple types...
1287 const Type* tmp = getTypeVal($1);
1288 CHECK_FOR_ERROR
1289 $$ = new PATypeHolder(tmp);
1290 }
1291 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerb5334b02007-02-05 10:18:06 +00001292 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range");
Chris Lattner58af2a12006-02-15 07:22:58 +00001293 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1294 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001295 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001296 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001297 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001298 }
Reid Spencer218ded22007-01-05 17:07:23 +00001299 | Types '(' ArgTypeListI ')' OptFuncAttrs {
Chris Lattner58af2a12006-02-15 07:22:58 +00001300 std::vector<const Type*> Params;
Reid Spencer14310612006-12-31 05:40:51 +00001301 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencer218ded22007-01-05 17:07:23 +00001302 Attrs.push_back($5);
1303 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001304 const Type *Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001305 Params.push_back(Ty);
1306 if (Ty != Type::VoidTy)
Reid Spencer14310612006-12-31 05:40:51 +00001307 Attrs.push_back(I->Attrs);
1308 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001309 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1310 if (isVarArg) Params.pop_back();
1311
Reid Spencer14310612006-12-31 05:40:51 +00001312 FunctionType *FT = FunctionType::get(*$1, Params, isVarArg, Attrs);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001313 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001314 delete $1; // Delete the return type handle
1315 $$ = new PATypeHolder(HandleUpRefs(FT));
Reid Spencer61c83e02006-08-18 08:43:06 +00001316 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001317 }
Reid Spencer218ded22007-01-05 17:07:23 +00001318 | VOID '(' ArgTypeListI ')' OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00001319 std::vector<const Type*> Params;
1320 std::vector<FunctionType::ParameterAttributes> Attrs;
Reid Spencer218ded22007-01-05 17:07:23 +00001321 Attrs.push_back($5);
1322 for (TypeWithAttrsList::iterator I=$3->begin(), E=$3->end(); I != E; ++I) {
Reid Spencer66728ef2007-03-20 01:13:36 +00001323 const Type* Ty = I->Ty->get();
Reid Spencer66728ef2007-03-20 01:13:36 +00001324 Params.push_back(Ty);
1325 if (Ty != Type::VoidTy)
Reid Spencer14310612006-12-31 05:40:51 +00001326 Attrs.push_back(I->Attrs);
1327 }
1328 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1329 if (isVarArg) Params.pop_back();
1330
1331 FunctionType *FT = FunctionType::get($1, Params, isVarArg, Attrs);
Reid Spencer218ded22007-01-05 17:07:23 +00001332 delete $3; // Delete the argument list
Reid Spencer14310612006-12-31 05:40:51 +00001333 $$ = new PATypeHolder(HandleUpRefs(FT));
1334 CHECK_FOR_ERROR
1335 }
1336
1337 | '[' EUINT64VAL 'x' Types ']' { // Sized array type?
Reid Spencera132e042006-12-03 05:46:11 +00001338 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1339 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001340 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001341 }
Chris Lattner32980692007-02-19 07:44:24 +00001342 | '<' EUINT64VAL 'x' Types '>' { // Vector type?
Reid Spencera132e042006-12-03 05:46:11 +00001343 const llvm::Type* ElemTy = $4->get();
1344 if ((unsigned)$2 != $2)
1345 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner42a75512007-01-15 02:27:26 +00001346 if (!ElemTy->isFloatingPoint() && !ElemTy->isInteger())
Reid Spencer9d6565a2007-02-15 02:26:10 +00001347 GEN_ERROR("Element type of a VectorType must be primitive");
Reid Spencera132e042006-12-03 05:46:11 +00001348 if (!isPowerOf2_32($2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001349 GEN_ERROR("Vector length should be a power of 2");
Reid Spencer9d6565a2007-02-15 02:26:10 +00001350 $$ = new PATypeHolder(HandleUpRefs(VectorType::get(*$4, (unsigned)$2)));
Reid Spencera132e042006-12-03 05:46:11 +00001351 delete $4;
1352 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001353 }
1354 | '{' TypeListI '}' { // Structure type?
1355 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001356 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001357 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001358 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001359
Reid Spencera132e042006-12-03 05:46:11 +00001360 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001361 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001362 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001363 }
1364 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001365 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001366 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001367 }
Andrew Lenharth6353e052006-12-08 18:07:09 +00001368 | '<' '{' TypeListI '}' '>' {
1369 std::vector<const Type*> Elements;
1370 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1371 E = $3->end(); I != E; ++I)
1372 Elements.push_back(*I);
1373
1374 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements, true)));
1375 delete $3;
1376 CHECK_FOR_ERROR
1377 }
1378 | '<' '{' '}' '>' { // Empty structure type?
1379 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>(), true));
1380 CHECK_FOR_ERROR
1381 }
Reid Spencer14310612006-12-31 05:40:51 +00001382 ;
1383
1384ArgType
1385 : Types OptParamAttrs {
1386 $$.Ty = $1;
1387 $$.Attrs = $2;
1388 }
1389 ;
1390
Reid Spencer218ded22007-01-05 17:07:23 +00001391ResultTypes
1392 : Types {
Reid Spencer14310612006-12-31 05:40:51 +00001393 if (!UpRefs.empty())
1394 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1395 if (!(*$1)->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001396 GEN_ERROR("LLVM functions cannot return aggregate types");
Reid Spencer218ded22007-01-05 17:07:23 +00001397 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00001398 }
Reid Spencer218ded22007-01-05 17:07:23 +00001399 | VOID {
1400 $$ = new PATypeHolder(Type::VoidTy);
Reid Spencer14310612006-12-31 05:40:51 +00001401 }
1402 ;
1403
1404ArgTypeList : ArgType {
1405 $$ = new TypeWithAttrsList();
1406 $$->push_back($1);
1407 CHECK_FOR_ERROR
1408 }
1409 | ArgTypeList ',' ArgType {
1410 ($$=$1)->push_back($3);
1411 CHECK_FOR_ERROR
1412 }
1413 ;
1414
1415ArgTypeListI
1416 : ArgTypeList
1417 | ArgTypeList ',' DOTDOTDOT {
1418 $$=$1;
1419 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1420 TWA.Ty = new PATypeHolder(Type::VoidTy);
1421 $$->push_back(TWA);
1422 CHECK_FOR_ERROR
1423 }
1424 | DOTDOTDOT {
1425 $$ = new TypeWithAttrsList;
1426 TypeWithAttrs TWA; TWA.Attrs = FunctionType::NoAttributeSet;
1427 TWA.Ty = new PATypeHolder(Type::VoidTy);
1428 $$->push_back(TWA);
1429 CHECK_FOR_ERROR
1430 }
1431 | /*empty*/ {
1432 $$ = new TypeWithAttrsList();
Reid Spencer61c83e02006-08-18 08:43:06 +00001433 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001434 };
1435
1436// TypeList - Used for struct declarations and as a basis for function type
1437// declaration type lists
1438//
Reid Spencer14310612006-12-31 05:40:51 +00001439TypeListI : Types {
Reid Spencera132e042006-12-03 05:46:11 +00001440 $$ = new std::list<PATypeHolder>();
Reid Spencer66728ef2007-03-20 01:13:36 +00001441 $$->push_back(*$1);
1442 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001443 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001444 }
Reid Spencer14310612006-12-31 05:40:51 +00001445 | TypeListI ',' Types {
Reid Spencer66728ef2007-03-20 01:13:36 +00001446 ($$=$1)->push_back(*$3);
1447 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001448 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001449 };
1450
Chris Lattner58af2a12006-02-15 07:22:58 +00001451// ConstVal - The various declarations that go into the constant pool. This
1452// production is used ONLY to represent constants that show up AFTER a 'const',
1453// 'constant' or 'global' token at global scope. Constants that can be inlined
1454// into other expressions (such as integers and constexprs) are handled by the
1455// ResolvedVal, ValueRef and ConstValueRef productions.
1456//
1457ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001458 if (!UpRefs.empty())
1459 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001460 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001461 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001462 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001463 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001464 const Type *ETy = ATy->getElementType();
1465 int NumElements = ATy->getNumElements();
1466
1467 // Verify that we have the correct size...
1468 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001469 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001470 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001471 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001472
1473 // Verify all elements are correct type!
1474 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001475 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001476 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001477 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001478 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001479 }
1480
Reid Spencera132e042006-12-03 05:46:11 +00001481 $$ = ConstantArray::get(ATy, *$3);
1482 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001483 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001484 }
1485 | Types '[' ']' {
Reid Spencer14310612006-12-31 05:40:51 +00001486 if (!UpRefs.empty())
1487 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001488 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001489 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001490 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001491 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001492
1493 int NumElements = ATy->getNumElements();
1494 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001495 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Reid Spencerb5334b02007-02-05 10:18:06 +00001496 " arguments, but has size of " + itostr(NumElements) +"");
Reid Spencera132e042006-12-03 05:46:11 +00001497 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1498 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001499 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001500 }
1501 | Types 'c' STRINGCONSTANT {
Reid Spencer14310612006-12-31 05:40:51 +00001502 if (!UpRefs.empty())
1503 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001504 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001505 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001506 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001507 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001508
1509 int NumElements = ATy->getNumElements();
1510 const Type *ETy = ATy->getElementType();
1511 char *EndStr = UnEscapeLexed($3, true);
1512 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001513 GEN_ERROR("Can't build string constant of size " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001514 itostr((int)(EndStr-$3)) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001515 " when array has size " + itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001516 std::vector<Constant*> Vals;
Reid Spencer14310612006-12-31 05:40:51 +00001517 if (ETy == Type::Int8Ty) {
Chris Lattner58af2a12006-02-15 07:22:58 +00001518 for (unsigned char *C = (unsigned char *)$3;
Reid Spencer14310612006-12-31 05:40:51 +00001519 C != (unsigned char*)EndStr; ++C)
1520 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner58af2a12006-02-15 07:22:58 +00001521 } else {
1522 free($3);
Reid Spencerb5334b02007-02-05 10:18:06 +00001523 GEN_ERROR("Cannot build string arrays of non byte sized elements");
Chris Lattner58af2a12006-02-15 07:22:58 +00001524 }
1525 free($3);
Reid Spencera132e042006-12-03 05:46:11 +00001526 $$ = ConstantArray::get(ATy, Vals);
1527 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001528 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001529 }
1530 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencer14310612006-12-31 05:40:51 +00001531 if (!UpRefs.empty())
1532 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00001533 const VectorType *PTy = dyn_cast<VectorType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001534 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001535 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001536 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001537 const Type *ETy = PTy->getElementType();
1538 int NumElements = PTy->getNumElements();
1539
1540 // Verify that we have the correct size...
1541 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001542 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001543 utostr($3->size()) + " arguments, but has size of " +
Reid Spencerb5334b02007-02-05 10:18:06 +00001544 itostr(NumElements) + "");
Chris Lattner58af2a12006-02-15 07:22:58 +00001545
1546 // Verify all elements are correct type!
1547 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001548 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001549 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001550 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001551 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001552 }
1553
Reid Spencer9d6565a2007-02-15 02:26:10 +00001554 $$ = ConstantVector::get(PTy, *$3);
Reid Spencera132e042006-12-03 05:46:11 +00001555 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001556 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001557 }
1558 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001559 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001560 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001561 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001562 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001563
1564 if ($3->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001565 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001566
1567 // Check to ensure that constants are compatible with the type initializer!
1568 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001569 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001570 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001571 STy->getElementType(i)->getDescription() +
1572 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001573 " of structure initializer");
Chris Lattner58af2a12006-02-15 07:22:58 +00001574
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001575 // Check to ensure that Type is not packed
1576 if (STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001577 GEN_ERROR("Unpacked Initializer to vector type '" + STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001578
Reid Spencera132e042006-12-03 05:46:11 +00001579 $$ = ConstantStruct::get(STy, *$3);
1580 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001581 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001582 }
1583 | Types '{' '}' {
Reid Spencer14310612006-12-31 05:40:51 +00001584 if (!UpRefs.empty())
1585 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001586 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001587 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001588 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001589 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001590
1591 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001592 GEN_ERROR("Illegal number of initializers for structure type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001593
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001594 // Check to ensure that Type is not packed
1595 if (STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001596 GEN_ERROR("Unpacked Initializer to vector type '" + STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001597
1598 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1599 delete $1;
1600 CHECK_FOR_ERROR
1601 }
1602 | Types '<' '{' ConstVector '}' '>' {
1603 const StructType *STy = dyn_cast<StructType>($1->get());
1604 if (STy == 0)
1605 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001606 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001607
1608 if ($4->size() != STy->getNumContainedTypes())
Reid Spencerb5334b02007-02-05 10:18:06 +00001609 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001610
1611 // Check to ensure that constants are compatible with the type initializer!
1612 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1613 if ((*$4)[i]->getType() != STy->getElementType(i))
1614 GEN_ERROR("Expected type '" +
1615 STy->getElementType(i)->getDescription() +
1616 "' for element #" + utostr(i) +
Reid Spencerb5334b02007-02-05 10:18:06 +00001617 " of structure initializer");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001618
1619 // Check to ensure that Type is packed
1620 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001621 GEN_ERROR("Vector initializer to non-vector type '" +
1622 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001623
1624 $$ = ConstantStruct::get(STy, *$4);
1625 delete $1; delete $4;
1626 CHECK_FOR_ERROR
1627 }
1628 | Types '<' '{' '}' '>' {
1629 if (!UpRefs.empty())
1630 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
1631 const StructType *STy = dyn_cast<StructType>($1->get());
1632 if (STy == 0)
1633 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001634 (*$1)->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001635
1636 if (STy->getNumContainedTypes() != 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001637 GEN_ERROR("Illegal number of initializers for structure type");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001638
1639 // Check to ensure that Type is packed
1640 if (!STy->isPacked())
Chris Lattner32980692007-02-19 07:44:24 +00001641 GEN_ERROR("Vector initializer to non-vector type '" +
1642 STy->getDescription() + "'");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001643
Reid Spencera132e042006-12-03 05:46:11 +00001644 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1645 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001646 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001647 }
1648 | Types NULL_TOK {
Reid Spencer14310612006-12-31 05:40:51 +00001649 if (!UpRefs.empty())
1650 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001651 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001652 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001653 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001654 (*$1)->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00001655
Reid Spencera132e042006-12-03 05:46:11 +00001656 $$ = ConstantPointerNull::get(PTy);
1657 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001658 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001659 }
1660 | Types UNDEF {
Reid Spencer14310612006-12-31 05:40:51 +00001661 if (!UpRefs.empty())
1662 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001663 $$ = UndefValue::get($1->get());
1664 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001665 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001666 }
1667 | Types SymbolicValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00001668 if (!UpRefs.empty())
1669 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001670 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001671 if (Ty == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001672 GEN_ERROR("Global const reference must be a pointer type");
Chris Lattner58af2a12006-02-15 07:22:58 +00001673
1674 // ConstExprs can exist in the body of a function, thus creating
1675 // GlobalValues whenever they refer to a variable. Because we are in
Reid Spencer93c40032007-03-19 18:40:50 +00001676 // the context of a function, getExistingVal will search the functions
Chris Lattner58af2a12006-02-15 07:22:58 +00001677 // symbol table instead of the module symbol table for the global symbol,
1678 // which throws things all off. To get around this, we just tell
Reid Spencer93c40032007-03-19 18:40:50 +00001679 // getExistingVal that we are at global scope here.
Chris Lattner58af2a12006-02-15 07:22:58 +00001680 //
1681 Function *SavedCurFn = CurFun.CurrentFunction;
1682 CurFun.CurrentFunction = 0;
1683
Reid Spencer93c40032007-03-19 18:40:50 +00001684 Value *V = getExistingVal(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001685 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001686
1687 CurFun.CurrentFunction = SavedCurFn;
1688
1689 // If this is an initializer for a constant pointer, which is referencing a
1690 // (currently) undefined variable, create a stub now that shall be replaced
1691 // in the future with the right type of variable.
1692 //
1693 if (V == 0) {
Reid Spencera9720f52007-02-05 17:04:00 +00001694 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001695 const PointerType *PT = cast<PointerType>(Ty);
1696
1697 // First check to see if the forward references value is already created!
1698 PerModuleInfo::GlobalRefsType::iterator I =
1699 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1700
1701 if (I != CurModule.GlobalRefs.end()) {
1702 V = I->second; // Placeholder already exists, use it...
1703 $2.destroy();
1704 } else {
1705 std::string Name;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001706 if ($2.Type == ValID::GlobalName)
1707 Name = $2.Name;
1708 else if ($2.Type != ValID::GlobalID)
1709 GEN_ERROR("Invalid reference to global");
Chris Lattner58af2a12006-02-15 07:22:58 +00001710
1711 // Create the forward referenced global.
1712 GlobalValue *GV;
1713 if (const FunctionType *FTy =
1714 dyn_cast<FunctionType>(PT->getElementType())) {
1715 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1716 CurModule.CurrentModule);
1717 } else {
1718 GV = new GlobalVariable(PT->getElementType(), false,
1719 GlobalValue::ExternalLinkage, 0,
1720 Name, CurModule.CurrentModule);
1721 }
1722
1723 // Keep track of the fact that we have a forward ref to recycle it
1724 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1725 V = GV;
1726 }
1727 }
1728
Reid Spencera132e042006-12-03 05:46:11 +00001729 $$ = cast<GlobalValue>(V);
1730 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001731 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001732 }
1733 | Types ConstExpr {
Reid Spencer14310612006-12-31 05:40:51 +00001734 if (!UpRefs.empty())
1735 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001736 if ($1->get() != $2->getType())
Reid Spencere68853b2007-01-04 00:06:14 +00001737 GEN_ERROR("Mismatched types for constant expression: " +
1738 (*$1)->getDescription() + " and " + $2->getType()->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001739 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001740 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001741 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001742 }
1743 | Types ZEROINITIALIZER {
Reid Spencer14310612006-12-31 05:40:51 +00001744 if (!UpRefs.empty())
1745 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001746 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001747 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencerb5334b02007-02-05 10:18:06 +00001748 GEN_ERROR("Cannot create a null initialized value of this type");
Reid Spencera132e042006-12-03 05:46:11 +00001749 $$ = Constant::getNullValue(Ty);
1750 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001751 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001752 }
Reid Spencer14310612006-12-31 05:40:51 +00001753 | IntType ESINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001754 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001755 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001756 $$ = ConstantInt::get($1, $2, true);
Reid Spencer38c91a92007-02-28 02:24:54 +00001757 CHECK_FOR_ERROR
1758 }
1759 | IntType ESAPINTVAL { // arbitrary precision integer constants
1760 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1761 if ($2->getBitWidth() > BitWidth) {
1762 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001763 }
1764 $2->sextOrTrunc(BitWidth);
1765 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001766 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001767 CHECK_FOR_ERROR
1768 }
Reid Spencer14310612006-12-31 05:40:51 +00001769 | IntType EUINT64VAL { // integral constants
Reid Spencere4d87aa2006-12-23 06:05:41 +00001770 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001771 GEN_ERROR("Constant value doesn't fit in type");
Reid Spencer49d273e2007-03-19 20:40:51 +00001772 $$ = ConstantInt::get($1, $2, false);
Reid Spencer38c91a92007-02-28 02:24:54 +00001773 CHECK_FOR_ERROR
1774 }
1775 | IntType EUAPINTVAL { // arbitrary precision integer constants
1776 uint32_t BitWidth = cast<IntegerType>($1)->getBitWidth();
1777 if ($2->getBitWidth() > BitWidth) {
1778 GEN_ERROR("Constant value does not fit in type");
Reid Spencer10794272007-03-01 19:41:47 +00001779 }
1780 $2->zextOrTrunc(BitWidth);
1781 $$ = ConstantInt::get(*$2);
Reid Spencer38c91a92007-02-28 02:24:54 +00001782 delete $2;
Reid Spencere4d87aa2006-12-23 06:05:41 +00001783 CHECK_FOR_ERROR
1784 }
Reid Spencer6f407902007-01-13 05:00:46 +00001785 | INTTYPE TRUETOK { // Boolean constants
1786 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001787 $$ = ConstantInt::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001788 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001789 }
Reid Spencer6f407902007-01-13 05:00:46 +00001790 | INTTYPE FALSETOK { // Boolean constants
1791 assert(cast<IntegerType>($1)->getBitWidth() == 1 && "Not Bool?");
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00001792 $$ = ConstantInt::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001793 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001794 }
1795 | FPType FPVAL { // Float & Double constants
Reid Spencera132e042006-12-03 05:46:11 +00001796 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencerb5334b02007-02-05 10:18:06 +00001797 GEN_ERROR("Floating point constant invalid for type");
Reid Spencera132e042006-12-03 05:46:11 +00001798 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001799 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001800 };
1801
1802
Reid Spencer3da59db2006-11-27 01:05:10 +00001803ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer14310612006-12-31 05:40:51 +00001804 if (!UpRefs.empty())
1805 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00001806 Constant *Val = $3;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001807 const Type *DestTy = $5->get();
1808 if (!CastInst::castIsValid($1, $3, DestTy))
1809 GEN_ERROR("invalid cast opcode for cast from '" +
1810 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00001811 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00001812 $$ = ConstantExpr::getCast($1, $3, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00001813 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001814 }
1815 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001816 if (!isa<PointerType>($3->getType()))
Reid Spencerb5334b02007-02-05 10:18:06 +00001817 GEN_ERROR("GetElementPtr requires a pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00001818
Reid Spencera132e042006-12-03 05:46:11 +00001819 const Type *IdxTy =
Chris Lattner7d9801d2007-02-13 00:58:01 +00001820 GetElementPtrInst::getIndexedType($3->getType(), &(*$4)[0], $4->size(),
1821 true);
Reid Spencera132e042006-12-03 05:46:11 +00001822 if (!IdxTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00001823 GEN_ERROR("Index list invalid for constant getelementptr");
Reid Spencera132e042006-12-03 05:46:11 +00001824
Chris Lattnerf7469af2007-01-31 04:44:08 +00001825 SmallVector<Constant*, 8> IdxVec;
Reid Spencera132e042006-12-03 05:46:11 +00001826 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1827 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001828 IdxVec.push_back(C);
1829 else
Reid Spencerb5334b02007-02-05 10:18:06 +00001830 GEN_ERROR("Indices to constant getelementptr must be constants");
Chris Lattner58af2a12006-02-15 07:22:58 +00001831
1832 delete $4;
1833
Chris Lattnerf7469af2007-01-31 04:44:08 +00001834 $$ = ConstantExpr::getGetElementPtr($3, &IdxVec[0], IdxVec.size());
Reid Spencer61c83e02006-08-18 08:43:06 +00001835 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001836 }
1837 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencer4fe16d62007-01-11 18:21:29 +00001838 if ($3->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00001839 GEN_ERROR("Select condition must be of boolean type");
Reid Spencera132e042006-12-03 05:46:11 +00001840 if ($5->getType() != $7->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001841 GEN_ERROR("Select operand types must match");
Reid Spencera132e042006-12-03 05:46:11 +00001842 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001843 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001844 }
1845 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001846 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001847 GEN_ERROR("Binary operator types must match");
Reid Spencer1628cec2006-10-26 06:15:43 +00001848 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00001849 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001850 }
1851 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001852 if ($3->getType() != $5->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001853 GEN_ERROR("Logical operator types must match");
Chris Lattner42a75512007-01-15 02:27:26 +00001854 if (!$3->getType()->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00001855 if (Instruction::isShift($1) || !isa<VectorType>($3->getType()) ||
1856 !cast<VectorType>($3->getType())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00001857 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00001858 }
Reid Spencera132e042006-12-03 05:46:11 +00001859 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001860 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001861 }
Reid Spencer4012e832006-12-04 05:24:24 +00001862 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1863 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001864 GEN_ERROR("icmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001865 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001866 }
Reid Spencer4012e832006-12-04 05:24:24 +00001867 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1868 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00001869 GEN_ERROR("fcmp operand types must match");
Reid Spencer4012e832006-12-04 05:24:24 +00001870 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001871 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001872 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001873 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencerb5334b02007-02-05 10:18:06 +00001874 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001875 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001876 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001877 }
1878 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001879 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001880 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00001881 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001882 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001883 }
1884 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001885 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencerb5334b02007-02-05 10:18:06 +00001886 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00001887 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001888 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001889 };
1890
Chris Lattnerd25db202006-04-08 03:55:17 +00001891
Chris Lattner58af2a12006-02-15 07:22:58 +00001892// ConstVector - A list of comma separated constants.
1893ConstVector : ConstVector ',' ConstVal {
1894 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001895 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001896 }
1897 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00001898 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00001899 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001900 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001901 };
1902
1903
1904// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1905GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1906
1907
1908//===----------------------------------------------------------------------===//
1909// Rules to match Modules
1910//===----------------------------------------------------------------------===//
1911
1912// Module rule: Capture the result of parsing the whole file into a result
1913// variable...
1914//
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001915Module
1916 : DefinitionList {
1917 $$ = ParserResult = CurModule.CurrentModule;
1918 CurModule.ModuleDone();
1919 CHECK_FOR_ERROR;
1920 }
1921 | /*empty*/ {
1922 $$ = ParserResult = CurModule.CurrentModule;
1923 CurModule.ModuleDone();
1924 CHECK_FOR_ERROR;
1925 }
1926 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001927
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001928DefinitionList
1929 : Definition
1930 | DefinitionList Definition
1931 ;
1932
1933Definition
Jeff Cohen361c3ef2007-01-21 19:19:31 +00001934 : DEFINE { CurFun.isDeclare = false; } Function {
Chris Lattner58af2a12006-02-15 07:22:58 +00001935 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001936 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001937 }
1938 | DECLARE { CurFun.isDeclare = true; } FunctionProto {
Reid Spencer61c83e02006-08-18 08:43:06 +00001939 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001940 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001941 | MODULE ASM_TOK AsmBlock {
Reid Spencer61c83e02006-08-18 08:43:06 +00001942 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001943 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001944 | IMPLEMENTATION {
Chris Lattner58af2a12006-02-15 07:22:58 +00001945 // Emit an error if there are any unresolved types left.
1946 if (!CurModule.LateResolveTypes.empty()) {
1947 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer41dff5e2007-01-26 08:05:27 +00001948 if (DID.Type == ValID::LocalName) {
Reid Spencer61c83e02006-08-18 08:43:06 +00001949 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1950 } else {
1951 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1952 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001953 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001954 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001955 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00001956 | OptLocalAssign TYPE Types {
Reid Spencer14310612006-12-31 05:40:51 +00001957 if (!UpRefs.empty())
1958 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001959 // Eagerly resolve types. This is not an optimization, this is a
1960 // requirement that is due to the fact that we could have this:
1961 //
1962 // %list = type { %list * }
1963 // %list = type { %list * } ; repeated type decl
1964 //
1965 // If types are not resolved eagerly, then the two types will not be
1966 // determined to be the same type!
1967 //
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001968 ResolveTypeTo($1, *$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00001969
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001970 if (!setTypeName(*$3, $1) && !$1) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001971 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001972 // If this is a named type that is not a redefinition, add it to the slot
1973 // table.
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001974 CurModule.Types.push_back(*$3);
Chris Lattner58af2a12006-02-15 07:22:58 +00001975 }
Reid Spencera132e042006-12-03 05:46:11 +00001976
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001977 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001978 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001979 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00001980 | OptLocalAssign TYPE VOID {
Reid Spencer14310612006-12-31 05:40:51 +00001981 ResolveTypeTo($1, $3);
1982
1983 if (!setTypeName($3, $1) && !$1) {
1984 CHECK_FOR_ERROR
1985 // If this is a named type that is not a redefinition, add it to the slot
1986 // table.
1987 CurModule.Types.push_back($3);
1988 }
1989 CHECK_FOR_ERROR
1990 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00001991 | OptGlobalAssign GVVisibilityStyle GlobalType ConstVal {
1992 /* "Externally Visible" Linkage */
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001993 if ($4 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00001994 GEN_ERROR("Global value initializer is not a constant");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001995 CurGV = ParseGlobalVariable($1, GlobalValue::ExternalLinkage,
1996 $2, $3, $4->getType(), $4);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001997 CHECK_FOR_ERROR
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001998 } GlobalVarAttributes {
1999 CurGV = 0;
2000 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002001 | OptGlobalAssign GVInternalLinkage GVVisibilityStyle GlobalType ConstVal {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002002 if ($5 == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002003 GEN_ERROR("Global value initializer is not a constant");
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002004 CurGV = ParseGlobalVariable($1, $2, $3, $4, $5->getType(), $5);
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002005 CHECK_FOR_ERROR
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002006 } GlobalVarAttributes {
2007 CurGV = 0;
2008 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002009 | OptGlobalAssign GVExternalLinkage GVVisibilityStyle GlobalType Types {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002010 if (!UpRefs.empty())
2011 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
2012 CurGV = ParseGlobalVariable($1, $2, $3, $4, *$5, 0);
2013 CHECK_FOR_ERROR
2014 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002015 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002016 CurGV = 0;
2017 CHECK_FOR_ERROR
2018 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002019 | TARGET TargetDefinition {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002020 CHECK_FOR_ERROR
2021 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002022 | DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00002023 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002024 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00002025 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002026
2027
2028AsmBlock : STRINGCONSTANT {
2029 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
2030 char *EndStr = UnEscapeLexed($1, true);
2031 std::string NewAsm($1, EndStr);
2032 free($1);
2033
2034 if (AsmSoFar.empty())
2035 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
2036 else
2037 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00002038 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002039};
2040
Reid Spencer41dff5e2007-01-26 08:05:27 +00002041TargetDefinition : TRIPLE '=' STRINGCONSTANT {
Chris Lattner58af2a12006-02-15 07:22:58 +00002042 CurModule.CurrentModule->setTargetTriple($3);
2043 free($3);
John Criswell2f6a8b12006-10-24 19:09:48 +00002044 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00002045 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson1dc69692006-10-18 02:21:48 +00002046 CurModule.CurrentModule->setDataLayout($3);
2047 free($3);
Owen Anderson1dc69692006-10-18 02:21:48 +00002048 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002049
2050LibrariesDefinition : '[' LibList ']';
2051
2052LibList : LibList ',' STRINGCONSTANT {
2053 CurModule.CurrentModule->addLibrary($3);
2054 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002055 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002056 }
2057 | STRINGCONSTANT {
2058 CurModule.CurrentModule->addLibrary($1);
2059 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002060 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002061 }
2062 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00002063 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002064 }
2065 ;
2066
2067//===----------------------------------------------------------------------===//
2068// Rules to match Function Headers
2069//===----------------------------------------------------------------------===//
2070
Reid Spencer41dff5e2007-01-26 08:05:27 +00002071ArgListH : ArgListH ',' Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002072 if (!UpRefs.empty())
2073 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
2074 if (*$3 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002075 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002076 ArgListEntry E; E.Attrs = $4; E.Ty = $3; E.Name = $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002077 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002078 $1->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002079 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002080 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002081 | Types OptParamAttrs OptLocalName {
Reid Spencer14310612006-12-31 05:40:51 +00002082 if (!UpRefs.empty())
2083 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2084 if (*$1 == Type::VoidTy)
Reid Spencerb5334b02007-02-05 10:18:06 +00002085 GEN_ERROR("void typed arguments are invalid");
Reid Spencer14310612006-12-31 05:40:51 +00002086 ArgListEntry E; E.Attrs = $2; E.Ty = $1; E.Name = $3;
2087 $$ = new ArgListType;
2088 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002089 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002090 };
2091
2092ArgList : ArgListH {
2093 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002094 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002095 }
2096 | ArgListH ',' DOTDOTDOT {
2097 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002098 struct ArgListEntry E;
2099 E.Ty = new PATypeHolder(Type::VoidTy);
2100 E.Name = 0;
2101 E.Attrs = FunctionType::NoAttributeSet;
2102 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002103 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002104 }
2105 | DOTDOTDOT {
Reid Spencer14310612006-12-31 05:40:51 +00002106 $$ = new ArgListType;
2107 struct ArgListEntry E;
2108 E.Ty = new PATypeHolder(Type::VoidTy);
2109 E.Name = 0;
2110 E.Attrs = FunctionType::NoAttributeSet;
2111 $$->push_back(E);
Reid Spencer61c83e02006-08-18 08:43:06 +00002112 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002113 }
2114 | /* empty */ {
2115 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002116 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002117 };
2118
Reid Spencer41dff5e2007-01-26 08:05:27 +00002119FunctionHeaderH : OptCallingConv ResultTypes GlobalName '(' ArgList ')'
Reid Spencer218ded22007-01-05 17:07:23 +00002120 OptFuncAttrs OptSection OptAlign {
Chris Lattner58af2a12006-02-15 07:22:58 +00002121 UnEscapeLexed($3);
2122 std::string FunctionName($3);
2123 free($3); // Free strdup'd memory!
2124
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002125 // Check the function result for abstractness if this is a define. We should
2126 // have no abstract types at this point
Reid Spencer218ded22007-01-05 17:07:23 +00002127 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved($2))
2128 GEN_ERROR("Reference to abstract result: "+ $2->get()->getDescription());
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002129
Chris Lattner58af2a12006-02-15 07:22:58 +00002130 std::vector<const Type*> ParamTypeList;
Reid Spencer14310612006-12-31 05:40:51 +00002131 std::vector<FunctionType::ParameterAttributes> ParamAttrs;
Reid Spencer218ded22007-01-05 17:07:23 +00002132 ParamAttrs.push_back($7);
Chris Lattner58af2a12006-02-15 07:22:58 +00002133 if ($5) { // If there are arguments...
Reid Spencer14310612006-12-31 05:40:51 +00002134 for (ArgListType::iterator I = $5->begin(); I != $5->end(); ++I) {
2135 const Type* Ty = I->Ty->get();
Reid Spencer8c8a2dc2007-01-02 21:54:12 +00002136 if (!CurFun.isDeclare && CurModule.TypeIsUnresolved(I->Ty))
2137 GEN_ERROR("Reference to abstract argument: " + Ty->getDescription());
Reid Spencer14310612006-12-31 05:40:51 +00002138 ParamTypeList.push_back(Ty);
2139 if (Ty != Type::VoidTy)
2140 ParamAttrs.push_back(I->Attrs);
2141 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002142 }
2143
2144 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2145 if (isVarArg) ParamTypeList.pop_back();
2146
Reid Spencer218ded22007-01-05 17:07:23 +00002147 FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg,
Reid Spencer14310612006-12-31 05:40:51 +00002148 ParamAttrs);
Chris Lattner58af2a12006-02-15 07:22:58 +00002149 const PointerType *PFT = PointerType::get(FT);
Reid Spencer218ded22007-01-05 17:07:23 +00002150 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002151
2152 ValID ID;
2153 if (!FunctionName.empty()) {
Reid Spencer41dff5e2007-01-26 08:05:27 +00002154 ID = ValID::createGlobalName((char*)FunctionName.c_str());
Chris Lattner58af2a12006-02-15 07:22:58 +00002155 } else {
Reid Spencer93c40032007-03-19 18:40:50 +00002156 ID = ValID::createGlobalID(CurModule.Values.size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002157 }
2158
2159 Function *Fn = 0;
2160 // See if this function was forward referenced. If so, recycle the object.
2161 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2162 // Move the function to the end of the list, from whereever it was
2163 // previously inserted.
2164 Fn = cast<Function>(FWRef);
2165 CurModule.CurrentModule->getFunctionList().remove(Fn);
2166 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2167 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
Reid Spenceref9b9a72007-02-05 20:47:22 +00002168 (Fn = CurModule.CurrentModule->getFunction(FunctionName))) {
2169 if (Fn->getFunctionType() != FT ) {
2170 // The existing function doesn't have the same type. This is an overload
2171 // error.
2172 GEN_ERROR("Overload of function '" + FunctionName + "' not permitted.");
2173 } else if (!CurFun.isDeclare && !Fn->isDeclaration()) {
2174 // Neither the existing or the current function is a declaration and they
2175 // have the same name and same type. Clearly this is a redefinition.
Reid Spencerb5334b02007-02-05 10:18:06 +00002176 GEN_ERROR("Redefinition of function '" + FunctionName + "'");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002177 } if (Fn->isDeclaration()) {
2178 // Make sure to strip off any argument names so we can't get conflicts.
Chris Lattner58af2a12006-02-15 07:22:58 +00002179 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2180 AI != AE; ++AI)
2181 AI->setName("");
Reid Spenceref9b9a72007-02-05 20:47:22 +00002182 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002183 } else { // Not already defined?
2184 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
2185 CurModule.CurrentModule);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002186
Chris Lattner58af2a12006-02-15 07:22:58 +00002187 InsertValue(Fn, CurModule.Values);
2188 }
2189
2190 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002191
2192 if (CurFun.isDeclare) {
2193 // If we have declaration, always overwrite linkage. This will allow us to
2194 // correctly handle cases, when pointer to function is passed as argument to
2195 // another function.
2196 Fn->setLinkage(CurFun.Linkage);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002197 Fn->setVisibility(CurFun.Visibility);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002198 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002199 Fn->setCallingConv($1);
Reid Spencer218ded22007-01-05 17:07:23 +00002200 Fn->setAlignment($9);
2201 if ($8) {
2202 Fn->setSection($8);
2203 free($8);
Chris Lattner58af2a12006-02-15 07:22:58 +00002204 }
2205
2206 // Add all of the arguments we parsed to the function...
2207 if ($5) { // Is null if empty...
2208 if (isVarArg) { // Nuke the last entry
Reid Spenceref9b9a72007-02-05 20:47:22 +00002209 assert($5->back().Ty->get() == Type::VoidTy && $5->back().Name == 0 &&
Reid Spencera9720f52007-02-05 17:04:00 +00002210 "Not a varargs marker!");
Reid Spencer14310612006-12-31 05:40:51 +00002211 delete $5->back().Ty;
Chris Lattner58af2a12006-02-15 07:22:58 +00002212 $5->pop_back(); // Delete the last entry
2213 }
2214 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spenceref9b9a72007-02-05 20:47:22 +00002215 Function::arg_iterator ArgEnd = Fn->arg_end();
Reid Spencer14310612006-12-31 05:40:51 +00002216 unsigned Idx = 1;
Reid Spenceref9b9a72007-02-05 20:47:22 +00002217 for (ArgListType::iterator I = $5->begin();
2218 I != $5->end() && ArgIt != ArgEnd; ++I, ++ArgIt) {
Reid Spencer14310612006-12-31 05:40:51 +00002219 delete I->Ty; // Delete the typeholder...
Reid Spenceref9b9a72007-02-05 20:47:22 +00002220 setValueName(ArgIt, I->Name); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002221 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002222 InsertValue(ArgIt);
Reid Spencer14310612006-12-31 05:40:51 +00002223 Idx++;
Chris Lattner58af2a12006-02-15 07:22:58 +00002224 }
Reid Spencera132e042006-12-03 05:46:11 +00002225
Chris Lattner58af2a12006-02-15 07:22:58 +00002226 delete $5; // We're now done with the argument list
2227 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002228 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002229};
2230
2231BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2232
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002233FunctionHeader : FunctionDefineLinkage GVVisibilityStyle FunctionHeaderH BEGIN {
Chris Lattner58af2a12006-02-15 07:22:58 +00002234 $$ = CurFun.CurrentFunction;
2235
2236 // Make sure that we keep track of the linkage type even if there was a
2237 // previous "declare".
2238 $$->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002239 $$->setVisibility($2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002240};
2241
2242END : ENDTOK | '}'; // Allow end of '}' to end a function
2243
2244Function : BasicBlockList END {
2245 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002246 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002247};
2248
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002249FunctionProto : FunctionDeclareLinkage GVVisibilityStyle FunctionHeaderH {
Reid Spencer14310612006-12-31 05:40:51 +00002250 CurFun.CurrentFunction->setLinkage($1);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002251 CurFun.CurrentFunction->setVisibility($2);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002252 $$ = CurFun.CurrentFunction;
2253 CurFun.FunctionDone();
2254 CHECK_FOR_ERROR
2255 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002256
2257//===----------------------------------------------------------------------===//
2258// Rules to match Basic Blocks
2259//===----------------------------------------------------------------------===//
2260
2261OptSideEffect : /* empty */ {
2262 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002263 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002264 }
2265 | SIDEEFFECT {
2266 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002267 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002268 };
2269
2270ConstValueRef : ESINT64VAL { // A reference to a direct constant
2271 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002272 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002273 }
2274 | EUINT64VAL {
2275 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002276 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002277 }
2278 | FPVAL { // Perhaps it's an FP constant?
2279 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002280 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002281 }
2282 | TRUETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002283 $$ = ValID::create(ConstantInt::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002284 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002285 }
2286 | FALSETOK {
Zhou Sheng6b6b6ef2007-01-11 12:24:14 +00002287 $$ = ValID::create(ConstantInt::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002288 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002289 }
2290 | NULL_TOK {
2291 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002292 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002293 }
2294 | UNDEF {
2295 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002296 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002297 }
2298 | ZEROINITIALIZER { // A vector zero constant.
2299 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002300 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002301 }
2302 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002303 const Type *ETy = (*$2)[0]->getType();
Chris Lattner58af2a12006-02-15 07:22:58 +00002304 int NumElements = $2->size();
2305
Reid Spencer9d6565a2007-02-15 02:26:10 +00002306 VectorType* pt = VectorType::get(ETy, NumElements);
Chris Lattner58af2a12006-02-15 07:22:58 +00002307 PATypeHolder* PTy = new PATypeHolder(
Reid Spencera132e042006-12-03 05:46:11 +00002308 HandleUpRefs(
Reid Spencer9d6565a2007-02-15 02:26:10 +00002309 VectorType::get(
Reid Spencera132e042006-12-03 05:46:11 +00002310 ETy,
2311 NumElements)
2312 )
2313 );
Chris Lattner58af2a12006-02-15 07:22:58 +00002314
2315 // Verify all elements are correct type!
2316 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002317 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002318 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002319 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002320 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002321 }
2322
Reid Spencer9d6565a2007-02-15 02:26:10 +00002323 $$ = ValID::create(ConstantVector::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002324 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002325 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002326 }
2327 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002328 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002329 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002330 }
2331 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2332 char *End = UnEscapeLexed($3, true);
2333 std::string AsmStr = std::string($3, End);
2334 End = UnEscapeLexed($5, true);
2335 std::string Constraints = std::string($5, End);
2336 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2337 free($3);
2338 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002339 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002340 };
2341
2342// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2343// another value.
2344//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002345SymbolicValueRef : LOCALVAL_ID { // Is it an integer reference...?
2346 $$ = ValID::createLocalID($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002347 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002348 }
Reid Spencer41dff5e2007-01-26 08:05:27 +00002349 | GLOBALVAL_ID {
2350 $$ = ValID::createGlobalID($1);
2351 CHECK_FOR_ERROR
2352 }
2353 | LocalName { // Is it a named reference...?
2354 $$ = ValID::createLocalName($1);
2355 CHECK_FOR_ERROR
2356 }
2357 | GlobalName { // Is it a named reference...?
2358 $$ = ValID::createGlobalName($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002359 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002360 };
2361
2362// ValueRef - A reference to a definition... either constant or symbolic
2363ValueRef : SymbolicValueRef | ConstValueRef;
2364
2365
2366// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2367// type immediately preceeds the value reference, and allows complex constant
2368// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2369ResolvedVal : Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002370 if (!UpRefs.empty())
2371 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2372 $$ = getVal(*$1, $2);
2373 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002374 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002375 }
2376 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002377
2378BasicBlockList : BasicBlockList BasicBlock {
2379 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002380 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002381 }
2382 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2383 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002384 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002385 };
2386
2387
2388// Basic blocks are terminated by branching instructions:
2389// br, br/cc, switch, ret
2390//
Reid Spencer41dff5e2007-01-26 08:05:27 +00002391BasicBlock : InstructionList OptLocalAssign BBTerminatorInst {
Chris Lattner58af2a12006-02-15 07:22:58 +00002392 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002393 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002394 InsertValue($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002395 $1->getInstList().push_back($3);
Chris Lattner58af2a12006-02-15 07:22:58 +00002396 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002397 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002398 };
2399
2400InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002401 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2402 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2403 if (CI2->getParent() == 0)
2404 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002405 $1->getInstList().push_back($2);
2406 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002407 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002408 }
Reid Spencer93c40032007-03-19 18:40:50 +00002409 | /* empty */ { // Empty space between instruction lists
2410 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
Reid Spencer61c83e02006-08-18 08:43:06 +00002411 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002412 }
Reid Spencer93c40032007-03-19 18:40:50 +00002413 | LABELSTR { // Labelled (named) basic block
2414 $$ = defineBBVal(ValID::createLocalName($1));
Reid Spencer61c83e02006-08-18 08:43:06 +00002415 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002416 };
2417
2418BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencera132e042006-12-03 05:46:11 +00002419 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002420 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002421 }
Reid Spencer93c40032007-03-19 18:40:50 +00002422 | RET VOID { // Return with no result...
Chris Lattner58af2a12006-02-15 07:22:58 +00002423 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002424 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002425 }
Reid Spencer93c40032007-03-19 18:40:50 +00002426 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002427 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002428 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002429 $$ = new BranchInst(tmpBB);
Reid Spencer93c40032007-03-19 18:40:50 +00002430 } // Conditional Branch...
Reid Spencer6f407902007-01-13 05:00:46 +00002431 | BR INTTYPE ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
2432 assert(cast<IntegerType>($2)->getBitWidth() == 1 && "Not Bool?");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002433 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002434 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002435 BasicBlock* tmpBBB = getBBVal($9);
2436 CHECK_FOR_ERROR
Reid Spencer4fe16d62007-01-11 18:21:29 +00002437 Value* tmpVal = getVal(Type::Int1Ty, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002438 CHECK_FOR_ERROR
2439 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002440 }
2441 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002442 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002443 CHECK_FOR_ERROR
2444 BasicBlock* tmpBB = getBBVal($6);
2445 CHECK_FOR_ERROR
2446 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002447 $$ = S;
2448
2449 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2450 E = $8->end();
2451 for (; I != E; ++I) {
2452 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2453 S->addCase(CI, I->second);
2454 else
Reid Spencerb5334b02007-02-05 10:18:06 +00002455 GEN_ERROR("Switch case is constant, but not a simple integer");
Chris Lattner58af2a12006-02-15 07:22:58 +00002456 }
2457 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002458 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002459 }
2460 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002461 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002462 CHECK_FOR_ERROR
2463 BasicBlock* tmpBB = getBBVal($6);
2464 CHECK_FOR_ERROR
2465 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002466 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002467 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002468 }
Reid Spencer218ded22007-01-05 17:07:23 +00002469 | INVOKE OptCallingConv ResultTypes ValueRef '(' ValueRefList ')' OptFuncAttrs
Chris Lattner58af2a12006-02-15 07:22:58 +00002470 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattner58af2a12006-02-15 07:22:58 +00002471
Reid Spencer14310612006-12-31 05:40:51 +00002472 // Handle the short syntax
2473 const PointerType *PFTy = 0;
2474 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002475 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002476 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2477 // Pull out the types of all of the arguments...
2478 std::vector<const Type*> ParamTypes;
Reid Spencer14310612006-12-31 05:40:51 +00002479 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencer218ded22007-01-05 17:07:23 +00002480 ParamAttrs.push_back($8);
Reid Spencer14310612006-12-31 05:40:51 +00002481 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2482 const Type *Ty = I->Val->getType();
2483 if (Ty == Type::VoidTy)
2484 GEN_ERROR("Short call syntax cannot be used with varargs");
2485 ParamTypes.push_back(Ty);
2486 ParamAttrs.push_back(I->Attrs);
Chris Lattner58af2a12006-02-15 07:22:58 +00002487 }
2488
Reid Spencer218ded22007-01-05 17:07:23 +00002489 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattner58af2a12006-02-15 07:22:58 +00002490 PFTy = PointerType::get(Ty);
2491 }
2492
Reid Spencer66728ef2007-03-20 01:13:36 +00002493 delete $3;
2494
Chris Lattner58af2a12006-02-15 07:22:58 +00002495 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002496 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002497 BasicBlock *Normal = getBBVal($11);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002498 CHECK_FOR_ERROR
Reid Spencer218ded22007-01-05 17:07:23 +00002499 BasicBlock *Except = getBBVal($14);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002500 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002501
Reid Spencer14310612006-12-31 05:40:51 +00002502 // Check the arguments
2503 ValueList Args;
2504 if ($6->empty()) { // Has no arguments?
2505 // Make sure no arguments is a good thing!
2506 if (Ty->getNumParams() != 0)
2507 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002508 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002509 } else { // Has arguments?
2510 // Loop through FunctionType's arguments and ensure they are specified
2511 // correctly!
Chris Lattner58af2a12006-02-15 07:22:58 +00002512 FunctionType::param_iterator I = Ty->param_begin();
2513 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer14310612006-12-31 05:40:51 +00002514 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner58af2a12006-02-15 07:22:58 +00002515
Reid Spencer14310612006-12-31 05:40:51 +00002516 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2517 if (ArgI->Val->getType() != *I)
2518 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002519 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002520 Args.push_back(ArgI->Val);
2521 }
Reid Spencera132e042006-12-03 05:46:11 +00002522
Reid Spencer14310612006-12-31 05:40:51 +00002523 if (Ty->isVarArg()) {
2524 if (I == E)
2525 for (; ArgI != ArgE; ++ArgI)
2526 Args.push_back(ArgI->Val); // push the remaining varargs
2527 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002528 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002529 }
Reid Spencer14310612006-12-31 05:40:51 +00002530
2531 // Create the InvokeInst
Chris Lattner9d2fda62007-02-13 05:53:56 +00002532 InvokeInst *II = new InvokeInst(V, Normal, Except, &Args[0], Args.size());
Reid Spencer14310612006-12-31 05:40:51 +00002533 II->setCallingConv($2);
2534 $$ = II;
Chris Lattner58af2a12006-02-15 07:22:58 +00002535 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002536 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002537 }
2538 | UNWIND {
2539 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002540 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002541 }
2542 | UNREACHABLE {
2543 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002544 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002545 };
2546
2547
2548
2549JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2550 $$ = $1;
Reid Spencer93c40032007-03-19 18:40:50 +00002551 Constant *V = cast<Constant>(getExistingVal($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002552 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002553 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002554 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002555
Reid Spencer5b7e7532006-09-28 19:28:24 +00002556 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002557 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002558 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002559 }
2560 | IntType ConstValueRef ',' LABEL ValueRef {
2561 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencer93c40032007-03-19 18:40:50 +00002562 Constant *V = cast<Constant>(getExistingVal($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002563 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002564
2565 if (V == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002566 GEN_ERROR("May only switch on a constant pool value");
Chris Lattner58af2a12006-02-15 07:22:58 +00002567
Reid Spencer5b7e7532006-09-28 19:28:24 +00002568 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002569 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002570 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002571 };
2572
Reid Spencer41dff5e2007-01-26 08:05:27 +00002573Inst : OptLocalAssign InstVal {
Reid Spenceref9b9a72007-02-05 20:47:22 +00002574 // Is this definition named?? if so, assign the name...
2575 setValueName($2, $1);
2576 CHECK_FOR_ERROR
2577 InsertValue($2);
2578 $$ = $2;
2579 CHECK_FOR_ERROR
2580 };
2581
Chris Lattner58af2a12006-02-15 07:22:58 +00002582
2583PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer14310612006-12-31 05:40:51 +00002584 if (!UpRefs.empty())
2585 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002586 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002587 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002588 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002589 BasicBlock* tmpBB = getBBVal($5);
2590 CHECK_FOR_ERROR
2591 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00002592 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002593 }
2594 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2595 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002596 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002597 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002598 BasicBlock* tmpBB = getBBVal($6);
2599 CHECK_FOR_ERROR
2600 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002601 };
2602
2603
Reid Spencer14310612006-12-31 05:40:51 +00002604ValueRefList : Types ValueRef OptParamAttrs {
2605 if (!UpRefs.empty())
2606 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
2607 // Used for call and invoke instructions
2608 $$ = new ValueRefList();
2609 ValueRefListEntry E; E.Attrs = $3; E.Val = getVal($1->get(), $2);
2610 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002611 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002612 }
Reid Spencer14310612006-12-31 05:40:51 +00002613 | ValueRefList ',' Types ValueRef OptParamAttrs {
2614 if (!UpRefs.empty())
2615 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002616 $$ = $1;
Reid Spencer14310612006-12-31 05:40:51 +00002617 ValueRefListEntry E; E.Attrs = $5; E.Val = getVal($3->get(), $4);
2618 $$->push_back(E);
Reid Spencer66728ef2007-03-20 01:13:36 +00002619 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002620 CHECK_FOR_ERROR
Reid Spencer14310612006-12-31 05:40:51 +00002621 }
2622 | /*empty*/ { $$ = new ValueRefList(); };
Chris Lattner58af2a12006-02-15 07:22:58 +00002623
Reid Spencer14310612006-12-31 05:40:51 +00002624IndexList // Used for gep instructions and constant expressions
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002625 : /*empty*/ { $$ = new std::vector<Value*>(); }
Reid Spencer14310612006-12-31 05:40:51 +00002626 | IndexList ',' ResolvedVal {
2627 $$ = $1;
2628 $$->push_back($3);
2629 CHECK_FOR_ERROR
2630 }
Reid Spencerc6c59fd2006-12-31 21:47:02 +00002631 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00002632
2633OptTailCall : TAIL CALL {
2634 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002635 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002636 }
2637 | CALL {
2638 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002639 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002640 };
2641
Chris Lattner58af2a12006-02-15 07:22:58 +00002642InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002643 if (!UpRefs.empty())
2644 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002645 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
Reid Spencer9d6565a2007-02-15 02:26:10 +00002646 !isa<VectorType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002647 GEN_ERROR(
Reid Spencerb5334b02007-02-05 10:18:06 +00002648 "Arithmetic operator requires integer, FP, or packed operands");
Reid Spencer9d6565a2007-02-15 02:26:10 +00002649 if (isa<VectorType>((*$2).get()) &&
Reid Spencera132e042006-12-03 05:46:11 +00002650 ($1 == Instruction::URem ||
2651 $1 == Instruction::SRem ||
2652 $1 == Instruction::FRem))
Chris Lattner32980692007-02-19 07:44:24 +00002653 GEN_ERROR("Remainder not supported on vector types");
Reid Spencera132e042006-12-03 05:46:11 +00002654 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002655 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002656 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002657 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002658 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002659 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002660 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002661 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002662 }
2663 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002664 if (!UpRefs.empty())
2665 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Chris Lattner42a75512007-01-15 02:27:26 +00002666 if (!(*$2)->isInteger()) {
Reid Spencer9d6565a2007-02-15 02:26:10 +00002667 if (Instruction::isShift($1) || !isa<VectorType>($2->get()) ||
2668 !cast<VectorType>($2->get())->getElementType()->isInteger())
Reid Spencerb5334b02007-02-05 10:18:06 +00002669 GEN_ERROR("Logical operator requires integral operands");
Chris Lattner58af2a12006-02-15 07:22:58 +00002670 }
Reid Spencera132e042006-12-03 05:46:11 +00002671 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002672 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002673 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002674 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002675 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002676 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002677 GEN_ERROR("binary operator returned null");
Reid Spencera132e042006-12-03 05:46:11 +00002678 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002679 }
Reid Spencera132e042006-12-03 05:46:11 +00002680 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002681 if (!UpRefs.empty())
2682 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002683 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002684 GEN_ERROR("Vector types not supported by icmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002685 Value* tmpVal1 = getVal(*$3, $4);
2686 CHECK_FOR_ERROR
2687 Value* tmpVal2 = getVal(*$3, $6);
2688 CHECK_FOR_ERROR
2689 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2690 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002691 GEN_ERROR("icmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00002692 delete $3;
Reid Spencera132e042006-12-03 05:46:11 +00002693 }
2694 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002695 if (!UpRefs.empty())
2696 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencer9d6565a2007-02-15 02:26:10 +00002697 if (isa<VectorType>((*$3).get()))
Chris Lattner32980692007-02-19 07:44:24 +00002698 GEN_ERROR("Vector types not supported by fcmp instruction");
Reid Spencera132e042006-12-03 05:46:11 +00002699 Value* tmpVal1 = getVal(*$3, $4);
2700 CHECK_FOR_ERROR
2701 Value* tmpVal2 = getVal(*$3, $6);
2702 CHECK_FOR_ERROR
2703 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2704 if ($$ == 0)
Reid Spencerb5334b02007-02-05 10:18:06 +00002705 GEN_ERROR("fcmp operator returned null");
Reid Spencer66728ef2007-03-20 01:13:36 +00002706 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002707 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002708 | CastOps ResolvedVal TO Types {
Reid Spencer14310612006-12-31 05:40:51 +00002709 if (!UpRefs.empty())
2710 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002711 Value* Val = $2;
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002712 const Type* DestTy = $4->get();
2713 if (!CastInst::castIsValid($1, Val, DestTy))
2714 GEN_ERROR("invalid cast opcode for cast from '" +
2715 Val->getType()->getDescription() + "' to '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002716 DestTy->getDescription() + "'");
Reid Spencerb0fcf8f2007-01-17 02:48:45 +00002717 $$ = CastInst::create($1, Val, DestTy);
Reid Spencera132e042006-12-03 05:46:11 +00002718 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002719 }
2720 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencer4fe16d62007-01-11 18:21:29 +00002721 if ($2->getType() != Type::Int1Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002722 GEN_ERROR("select condition must be boolean");
Reid Spencera132e042006-12-03 05:46:11 +00002723 if ($4->getType() != $6->getType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002724 GEN_ERROR("select value types should match");
Reid Spencera132e042006-12-03 05:46:11 +00002725 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002726 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002727 }
2728 | VAARG ResolvedVal ',' Types {
Reid Spencer14310612006-12-31 05:40:51 +00002729 if (!UpRefs.empty())
2730 GEN_ERROR("Invalid upreference in type: " + (*$4)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002731 $$ = new VAArgInst($2, *$4);
2732 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002733 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002734 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002735 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002736 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencerb5334b02007-02-05 10:18:06 +00002737 GEN_ERROR("Invalid extractelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002738 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002739 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002740 }
2741 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002742 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002743 GEN_ERROR("Invalid insertelement operands");
Reid Spencera132e042006-12-03 05:46:11 +00002744 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002745 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002746 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00002747 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002748 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencerb5334b02007-02-05 10:18:06 +00002749 GEN_ERROR("Invalid shufflevector operands");
Reid Spencera132e042006-12-03 05:46:11 +00002750 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002751 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00002752 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002753 | PHI_TOK PHIList {
2754 const Type *Ty = $2->front().first->getType();
2755 if (!Ty->isFirstClassType())
Reid Spencerb5334b02007-02-05 10:18:06 +00002756 GEN_ERROR("PHI node operands must be of first class type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002757 $$ = new PHINode(Ty);
2758 ((PHINode*)$$)->reserveOperandSpace($2->size());
2759 while ($2->begin() != $2->end()) {
2760 if ($2->front().first->getType() != Ty)
Reid Spencerb5334b02007-02-05 10:18:06 +00002761 GEN_ERROR("All elements of a PHI node must be of the same type");
Chris Lattner58af2a12006-02-15 07:22:58 +00002762 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2763 $2->pop_front();
2764 }
2765 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002766 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002767 }
Reid Spencer218ded22007-01-05 17:07:23 +00002768 | OptTailCall OptCallingConv ResultTypes ValueRef '(' ValueRefList ')'
2769 OptFuncAttrs {
Reid Spencer14310612006-12-31 05:40:51 +00002770
2771 // Handle the short syntax
Reid Spencer3da59db2006-11-27 01:05:10 +00002772 const PointerType *PFTy = 0;
2773 const FunctionType *Ty = 0;
Reid Spencer218ded22007-01-05 17:07:23 +00002774 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002775 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2776 // Pull out the types of all of the arguments...
2777 std::vector<const Type*> ParamTypes;
Reid Spencer14310612006-12-31 05:40:51 +00002778 FunctionType::ParamAttrsList ParamAttrs;
Reid Spencer218ded22007-01-05 17:07:23 +00002779 ParamAttrs.push_back($8);
Reid Spencer14310612006-12-31 05:40:51 +00002780 for (ValueRefList::iterator I = $6->begin(), E = $6->end(); I != E; ++I) {
2781 const Type *Ty = I->Val->getType();
2782 if (Ty == Type::VoidTy)
2783 GEN_ERROR("Short call syntax cannot be used with varargs");
2784 ParamTypes.push_back(Ty);
2785 ParamAttrs.push_back(I->Attrs);
Chris Lattner58af2a12006-02-15 07:22:58 +00002786 }
2787
Reid Spencer218ded22007-01-05 17:07:23 +00002788 Ty = FunctionType::get($3->get(), ParamTypes, false, ParamAttrs);
Chris Lattner58af2a12006-02-15 07:22:58 +00002789 PFTy = PointerType::get(Ty);
2790 }
2791
2792 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002793 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002794
Reid Spencer14310612006-12-31 05:40:51 +00002795 // Check the arguments
2796 ValueList Args;
2797 if ($6->empty()) { // Has no arguments?
Chris Lattner58af2a12006-02-15 07:22:58 +00002798 // Make sure no arguments is a good thing!
2799 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002800 GEN_ERROR("No arguments passed to a function that "
Reid Spencerb5334b02007-02-05 10:18:06 +00002801 "expects arguments");
Chris Lattner58af2a12006-02-15 07:22:58 +00002802 } else { // Has arguments?
2803 // Loop through FunctionType's arguments and ensure they are specified
2804 // correctly!
2805 //
2806 FunctionType::param_iterator I = Ty->param_begin();
2807 FunctionType::param_iterator E = Ty->param_end();
Reid Spencer14310612006-12-31 05:40:51 +00002808 ValueRefList::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner58af2a12006-02-15 07:22:58 +00002809
Reid Spencer14310612006-12-31 05:40:51 +00002810 for (; ArgI != ArgE && I != E; ++ArgI, ++I) {
2811 if (ArgI->Val->getType() != *I)
2812 GEN_ERROR("Parameter " + ArgI->Val->getName()+ " is not of type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002813 (*I)->getDescription() + "'");
Reid Spencer14310612006-12-31 05:40:51 +00002814 Args.push_back(ArgI->Val);
2815 }
2816 if (Ty->isVarArg()) {
2817 if (I == E)
2818 for (; ArgI != ArgE; ++ArgI)
2819 Args.push_back(ArgI->Val); // push the remaining varargs
2820 } else if (I != E || ArgI != ArgE)
Reid Spencerb5334b02007-02-05 10:18:06 +00002821 GEN_ERROR("Invalid number of parameters detected");
Chris Lattner58af2a12006-02-15 07:22:58 +00002822 }
Reid Spencer14310612006-12-31 05:40:51 +00002823 // Create the call node
Chris Lattner9d2fda62007-02-13 05:53:56 +00002824 CallInst *CI = new CallInst(V, &Args[0], Args.size());
Reid Spencer14310612006-12-31 05:40:51 +00002825 CI->setTailCall($1);
2826 CI->setCallingConv($2);
2827 $$ = CI;
Chris Lattner58af2a12006-02-15 07:22:58 +00002828 delete $6;
Reid Spencer41dff5e2007-01-26 08:05:27 +00002829 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00002830 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002831 }
2832 | MemoryInst {
2833 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002834 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002835 };
2836
Chris Lattner58af2a12006-02-15 07:22:58 +00002837OptVolatile : VOLATILE {
2838 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002839 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002840 }
2841 | /* empty */ {
2842 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002843 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002844 };
2845
2846
2847
2848MemoryInst : MALLOC Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00002849 if (!UpRefs.empty())
2850 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002851 $$ = new MallocInst(*$2, 0, $3);
2852 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002853 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002854 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002855 | MALLOC Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00002856 if (!UpRefs.empty())
2857 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002858 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002859 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002860 $$ = new MallocInst(*$2, tmpVal, $6);
2861 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002862 }
2863 | ALLOCA Types OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00002864 if (!UpRefs.empty())
2865 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002866 $$ = new AllocaInst(*$2, 0, $3);
2867 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002868 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002869 }
Reid Spencera54b7cb2007-01-12 07:05:14 +00002870 | ALLOCA Types ',' INTTYPE ValueRef OptCAlign {
Reid Spencer14310612006-12-31 05:40:51 +00002871 if (!UpRefs.empty())
2872 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002873 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002874 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002875 $$ = new AllocaInst(*$2, tmpVal, $6);
2876 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002877 }
2878 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002879 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002880 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencerb5334b02007-02-05 10:18:06 +00002881 $2->getType()->getDescription() + "");
Reid Spencera132e042006-12-03 05:46:11 +00002882 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002883 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002884 }
2885
2886 | OptVolatile LOAD Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002887 if (!UpRefs.empty())
2888 GEN_ERROR("Invalid upreference in type: " + (*$3)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002889 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002890 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00002891 (*$3)->getDescription());
2892 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002893 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00002894 (*$3)->getDescription());
2895 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002896 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002897 $$ = new LoadInst(tmpVal, "", $1);
Reid Spencera132e042006-12-03 05:46:11 +00002898 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002899 }
2900 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer14310612006-12-31 05:40:51 +00002901 if (!UpRefs.empty())
2902 GEN_ERROR("Invalid upreference in type: " + (*$5)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002903 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00002904 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00002905 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00002906 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002907 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00002908 if (ElTy != $3->getType())
2909 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Reid Spencerb5334b02007-02-05 10:18:06 +00002910 "' into space of type '" + ElTy->getDescription() + "'");
Chris Lattner58af2a12006-02-15 07:22:58 +00002911
Reid Spencera132e042006-12-03 05:46:11 +00002912 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002913 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002914 $$ = new StoreInst($3, tmpVal, $1);
2915 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002916 }
2917 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer14310612006-12-31 05:40:51 +00002918 if (!UpRefs.empty())
2919 GEN_ERROR("Invalid upreference in type: " + (*$2)->getDescription());
Reid Spencera132e042006-12-03 05:46:11 +00002920 if (!isa<PointerType>($2->get()))
Reid Spencerb5334b02007-02-05 10:18:06 +00002921 GEN_ERROR("getelementptr insn requires pointer operand");
Chris Lattner58af2a12006-02-15 07:22:58 +00002922
Chris Lattner7d9801d2007-02-13 00:58:01 +00002923 if (!GetElementPtrInst::getIndexedType(*$2, &(*$4)[0], $4->size(), true))
Reid Spencer61c83e02006-08-18 08:43:06 +00002924 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencerb5334b02007-02-05 10:18:06 +00002925 (*$2)->getDescription()+ "'");
Reid Spencera132e042006-12-03 05:46:11 +00002926 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002927 CHECK_FOR_ERROR
Chris Lattner7d9801d2007-02-13 00:58:01 +00002928 $$ = new GetElementPtrInst(tmpVal, &(*$4)[0], $4->size());
Reid Spencera132e042006-12-03 05:46:11 +00002929 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002930 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002931 };
2932
2933
2934%%
Reid Spencer61c83e02006-08-18 08:43:06 +00002935
Reid Spencer14310612006-12-31 05:40:51 +00002936// common code from the two 'RunVMAsmParser' functions
2937static Module* RunParser(Module * M) {
2938
2939 llvmAsmlineno = 1; // Reset the current line number...
2940 CurModule.CurrentModule = M;
2941#if YYDEBUG
2942 yydebug = Debug;
2943#endif
2944
2945 // Check to make sure the parser succeeded
2946 if (yyparse()) {
2947 if (ParserResult)
2948 delete ParserResult;
2949 return 0;
2950 }
2951
2952 // Check to make sure that parsing produced a result
2953 if (!ParserResult)
2954 return 0;
2955
2956 // Reset ParserResult variable while saving its value for the result.
2957 Module *Result = ParserResult;
2958 ParserResult = 0;
2959
2960 return Result;
2961}
2962
Reid Spencer61c83e02006-08-18 08:43:06 +00002963void llvm::GenerateError(const std::string &message, int LineNo) {
2964 if (LineNo == -1) LineNo = llvmAsmlineno;
2965 // TODO: column number in exception
2966 if (TheParseError)
2967 TheParseError->setError(CurFilename, message, LineNo);
2968 TriggerError = 1;
2969}
2970
Chris Lattner58af2a12006-02-15 07:22:58 +00002971int yyerror(const char *ErrorMsg) {
2972 std::string where
2973 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2974 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Reid Spenceref9b9a72007-02-05 20:47:22 +00002975 std::string errMsg = where + "error: " + std::string(ErrorMsg);
2976 if (yychar != YYEMPTY && yychar != 0)
2977 errMsg += " while reading token: '" + std::string(llvmAsmtext, llvmAsmleng)+
2978 "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00002979 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00002980 return 0;
2981}