blob: 6606b3a1038b6603686c68a7ee49a522ae077532 [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"
20#include "llvm/SymbolTable.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/Support/MathExtras.h"
Reid Spencer481169e2006-12-01 00:33:46 +000024#include "llvm/Support/Streams.h"
Chris Lattner58af2a12006-02-15 07:22:58 +000025#include <algorithm>
Chris Lattner58af2a12006-02-15 07:22:58 +000026#include <list>
27#include <utility>
28
Reid Spencere4f47592006-08-18 17:32:55 +000029// The following is a gross hack. In order to rid the libAsmParser library of
30// exceptions, we have to have a way of getting the yyparse function to go into
31// an error situation. So, whenever we want an error to occur, the GenerateError
32// function (see bottom of file) sets TriggerError. Then, at the end of each
33// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
34// (a goto) to put YACC in error state. Furthermore, several calls to
35// GenerateError are made from inside productions and they must simulate the
36// previous exception behavior by exiting the production immediately. We have
37// replaced these with the GEN_ERROR macro which calls GeneratError and then
38// immediately invokes YYERROR. This would be so much cleaner if it was a
39// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000040static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000041#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000042#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
43
Chris Lattner58af2a12006-02-15 07:22:58 +000044int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
45int yylex(); // declaration" of xxx warnings.
46int yyparse();
47
48namespace llvm {
49 std::string CurFilename;
50}
51using namespace llvm;
52
53static Module *ParserResult;
54
55// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
56// relating to upreferences in the input stream.
57//
58//#define DEBUG_UPREFS 1
59#ifdef DEBUG_UPREFS
Reid Spencer481169e2006-12-01 00:33:46 +000060#define UR_OUT(X) llvm_cerr << X
Chris Lattner58af2a12006-02-15 07:22:58 +000061#else
62#define UR_OUT(X)
63#endif
64
65#define YYERROR_VERBOSE 1
66
67static bool ObsoleteVarArgs;
68static bool NewVarArgs;
69static BasicBlock *CurBB;
70static GlobalVariable *CurGV;
71
72
73// This contains info used when building the body of a function. It is
74// destroyed when the function is completed.
75//
76typedef std::vector<Value *> ValueList; // Numbered defs
77static void
78ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
79 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
80
81static struct PerModuleInfo {
82 Module *CurrentModule;
83 std::map<const Type *, ValueList> Values; // Module level numbered definitions
84 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencer861d9d62006-11-28 07:29:44 +000085 std::vector<PATypeHolder> Types;
86 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner58af2a12006-02-15 07:22:58 +000087
88 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner0ad19702006-06-21 16:53:00 +000089 /// how they were referenced and on which line of the input they came from so
Chris Lattner58af2a12006-02-15 07:22:58 +000090 /// that we can resolve them later and print error messages as appropriate.
91 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
92
93 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
94 // references to global values. Global values may be referenced before they
95 // are defined, and if so, the temporary object that they represent is held
96 // here. This is used for forward references of GlobalValues.
97 //
98 typedef std::map<std::pair<const PointerType *,
99 ValID>, GlobalValue*> GlobalRefsType;
100 GlobalRefsType GlobalRefs;
101
102 void ModuleDone() {
103 // If we could not resolve some functions at function compilation time
104 // (calls to functions before they are defined), resolve them now... Types
105 // are resolved when the constant pool has been completely parsed.
106 //
107 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000108 if (TriggerError)
109 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000110
111 // Check to make sure that all global value forward references have been
112 // resolved!
113 //
114 if (!GlobalRefs.empty()) {
115 std::string UndefinedReferences = "Unresolved global references exist:\n";
116
117 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
118 I != E; ++I) {
119 UndefinedReferences += " " + I->first.first->getDescription() + " " +
120 I->first.second.getName() + "\n";
121 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000122 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000123 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000124 }
125
Chris Lattner58af2a12006-02-15 07:22:58 +0000126 Values.clear(); // Clear out function local definitions
127 Types.clear();
128 CurrentModule = 0;
129 }
130
131 // GetForwardRefForGlobal - Check to see if there is a forward reference
132 // for this global. If so, remove it from the GlobalRefs map and return it.
133 // If not, just return null.
134 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
135 // Check to see if there is a forward reference to this global variable...
136 // if there is, eliminate it and patch the reference to use the new def'n.
137 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
138 GlobalValue *Ret = 0;
139 if (I != GlobalRefs.end()) {
140 Ret = I->second;
141 GlobalRefs.erase(I);
142 }
143 return Ret;
144 }
145} CurModule;
146
147static struct PerFunctionInfo {
148 Function *CurrentFunction; // Pointer to current function being created
149
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000150 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattner58af2a12006-02-15 07:22:58 +0000151 std::map<const Type*, ValueList> LateResolveValues;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000152 bool isDeclare; // Is this function a forward declararation?
153 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Chris Lattner58af2a12006-02-15 07:22:58 +0000154
155 /// BBForwardRefs - When we see forward references to basic blocks, keep
156 /// track of them here.
157 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
158 std::vector<BasicBlock*> NumberedBlocks;
159 unsigned NextBBNum;
160
161 inline PerFunctionInfo() {
162 CurrentFunction = 0;
163 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000164 Linkage = GlobalValue::ExternalLinkage;
Chris Lattner58af2a12006-02-15 07:22:58 +0000165 }
166
167 inline void FunctionStart(Function *M) {
168 CurrentFunction = M;
169 NextBBNum = 0;
170 }
171
172 void FunctionDone() {
173 NumberedBlocks.clear();
174
175 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000176 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000177 GenerateError("Undefined reference to label " +
Chris Lattner58af2a12006-02-15 07:22:58 +0000178 BBForwardRefs.begin()->first->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000179 return;
180 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000181
182 // Resolve all forward references now.
183 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
184
185 Values.clear(); // Clear out function local definitions
186 CurrentFunction = 0;
187 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000188 Linkage = GlobalValue::ExternalLinkage;
Chris Lattner58af2a12006-02-15 07:22:58 +0000189 }
190} CurFun; // Info for the current function...
191
192static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
193
194
195//===----------------------------------------------------------------------===//
196// Code to handle definitions of all the types
197//===----------------------------------------------------------------------===//
198
199static int InsertValue(Value *V,
200 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
201 if (V->hasName()) return -1; // Is this a numbered definition?
202
203 // Yes, insert the value into the value table...
204 ValueList &List = ValueTab[V->getType()];
205 List.push_back(V);
206 return List.size()-1;
207}
208
209static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
210 switch (D.Type) {
211 case ValID::NumberVal: // Is it a numbered definition?
212 // Module constants occupy the lowest numbered slots...
213 if ((unsigned)D.Num < CurModule.Types.size())
Reid Spencer861d9d62006-11-28 07:29:44 +0000214 return CurModule.Types[(unsigned)D.Num];
Chris Lattner58af2a12006-02-15 07:22:58 +0000215 break;
216 case ValID::NameVal: // Is it a named definition?
217 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
218 D.destroy(); // Free old strdup'd memory...
219 return N;
220 }
221 break;
222 default:
Reid Spencer61c83e02006-08-18 08:43:06 +0000223 GenerateError("Internal parser error: Invalid symbol type reference!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000224 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000225 }
226
227 // If we reached here, we referenced either a symbol that we don't know about
228 // or an id number that hasn't been read yet. We may be referencing something
229 // forward, so just create an entry to be resolved later and get to it...
230 //
231 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
232
233
234 if (inFunctionScope()) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000235 if (D.Type == ValID::NameVal) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000236 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000237 return 0;
238 } else {
Reid Spencer61c83e02006-08-18 08:43:06 +0000239 GenerateError("Reference to an undefined type: #" + itostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000240 return 0;
241 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000242 }
243
Reid Spencer861d9d62006-11-28 07:29:44 +0000244 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner58af2a12006-02-15 07:22:58 +0000245 if (I != CurModule.LateResolveTypes.end())
Reid Spencer861d9d62006-11-28 07:29:44 +0000246 return I->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000247
Reid Spencer861d9d62006-11-28 07:29:44 +0000248 Type *Typ = OpaqueType::get();
249 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
250 return Typ;
Reid Spencera132e042006-12-03 05:46:11 +0000251 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000252
253static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
254 SymbolTable &SymTab =
255 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
256 CurModule.CurrentModule->getSymbolTable();
257 return SymTab.lookup(Ty, Name);
258}
259
260// getValNonImprovising - Look up the value specified by the provided type and
261// the provided ValID. If the value exists and has already been defined, return
262// it. Otherwise return null.
263//
264static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000265 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000266 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000267 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000268 return 0;
269 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000270
271 switch (D.Type) {
272 case ValID::NumberVal: { // Is it a numbered definition?
273 unsigned Num = (unsigned)D.Num;
274
275 // Module constants occupy the lowest numbered slots...
276 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
277 if (VI != CurModule.Values.end()) {
278 if (Num < VI->second.size())
279 return VI->second[Num];
280 Num -= VI->second.size();
281 }
282
283 // Make sure that our type is within bounds
284 VI = CurFun.Values.find(Ty);
285 if (VI == CurFun.Values.end()) return 0;
286
287 // Check that the number is within bounds...
288 if (VI->second.size() <= Num) return 0;
289
290 return VI->second[Num];
291 }
292
293 case ValID::NameVal: { // Is it a named definition?
294 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
295 if (N == 0) return 0;
296
297 D.destroy(); // Free old strdup'd memory...
298 return N;
299 }
300
301 // Check to make sure that "Ty" is an integral type, and that our
302 // value will fit into the specified type...
303 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000304 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000305 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000306 itostr(D.ConstPool64) + "' is invalid for type '" +
307 Ty->getDescription() + "'!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000308 return 0;
309 }
Reid Spencerb83eb642006-10-20 07:07:24 +0000310 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000311
312 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000313 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
314 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000315 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattner58af2a12006-02-15 07:22:58 +0000316 "' is invalid or out of range!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000317 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000318 } else { // This is really a signed reference. Transmogrify.
Reid Spencerb83eb642006-10-20 07:07:24 +0000319 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000320 }
321 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000322 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000323 }
324
325 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000326 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000327 GenerateError("FP constant invalid for type!!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000328 return 0;
329 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000330 return ConstantFP::get(Ty, D.ConstPoolFP);
331
332 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000333 if (!isa<PointerType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000334 GenerateError("Cannot create a a non pointer null!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000335 return 0;
336 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000337 return ConstantPointerNull::get(cast<PointerType>(Ty));
338
339 case ValID::ConstUndefVal: // Is it an undef value?
340 return UndefValue::get(Ty);
341
342 case ValID::ConstZeroVal: // Is it a zero value?
343 return Constant::getNullValue(Ty);
344
345 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000346 if (D.ConstantValue->getType() != Ty) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000347 GenerateError("Constant expression type different from required type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000348 return 0;
349 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000350 return D.ConstantValue;
351
352 case ValID::InlineAsmVal: { // Inline asm expression
353 const PointerType *PTy = dyn_cast<PointerType>(Ty);
354 const FunctionType *FTy =
355 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000356 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000357 GenerateError("Invalid type for asm constraint string!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000358 return 0;
359 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000360 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
361 D.IAD->HasSideEffects);
362 D.destroy(); // Free InlineAsmDescriptor.
363 return IA;
364 }
365 default:
366 assert(0 && "Unhandled case!");
367 return 0;
368 } // End of switch
369
370 assert(0 && "Unhandled case!");
371 return 0;
372}
373
374// getVal - This function is identical to getValNonImprovising, except that if a
375// value is not already defined, it "improvises" by creating a placeholder var
376// that looks and acts just like the requested variable. When the value is
377// defined later, all uses of the placeholder variable are replaced with the
378// real thing.
379//
380static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000381 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000382 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000383 return 0;
384 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000385
386 // See if the value has already been defined.
387 Value *V = getValNonImprovising(Ty, ID);
388 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000389 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000390
Reid Spencer5b7e7532006-09-28 19:28:24 +0000391 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000392 GenerateError("Invalid use of a composite type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000393 return 0;
394 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000395
396 // If we reached here, we referenced either a symbol that we don't know about
397 // or an id number that hasn't been read yet. We may be referencing something
398 // forward, so just create an entry to be resolved later and get to it...
399 //
400 V = new Argument(Ty);
401
402 // Remember where this forward reference came from. FIXME, shouldn't we try
403 // to recycle these things??
404 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
405 llvmAsmlineno)));
406
407 if (inFunctionScope())
408 InsertValue(V, CurFun.LateResolveValues);
409 else
410 InsertValue(V, CurModule.LateResolveValues);
411 return V;
412}
413
414/// getBBVal - This is used for two purposes:
415/// * If isDefinition is true, a new basic block with the specified ID is being
416/// defined.
417/// * If isDefinition is true, this is a reference to a basic block, which may
418/// or may not be a forward reference.
419///
420static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
421 assert(inFunctionScope() && "Can't get basic block at global scope!");
422
423 std::string Name;
424 BasicBlock *BB = 0;
425 switch (ID.Type) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000426 default:
427 GenerateError("Illegal label reference " + ID.getName());
428 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000429 case ValID::NumberVal: // Is it a numbered definition?
430 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
431 CurFun.NumberedBlocks.resize(ID.Num+1);
432 BB = CurFun.NumberedBlocks[ID.Num];
433 break;
434 case ValID::NameVal: // Is it a named definition?
435 Name = ID.Name;
436 if (Value *N = CurFun.CurrentFunction->
437 getSymbolTable().lookup(Type::LabelTy, Name))
438 BB = cast<BasicBlock>(N);
439 break;
440 }
441
442 // See if the block has already been defined.
443 if (BB) {
444 // If this is the definition of the block, make sure the existing value was
445 // just a forward reference. If it was a forward reference, there will be
446 // an entry for it in the PlaceHolderInfo map.
Reid Spencer5b7e7532006-09-28 19:28:24 +0000447 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000448 // The existing value was a definition, not a forward reference.
Reid Spencer61c83e02006-08-18 08:43:06 +0000449 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000450 return 0;
451 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000452
453 ID.destroy(); // Free strdup'd memory.
454 return BB;
455 }
456
457 // Otherwise this block has not been seen before.
458 BB = new BasicBlock("", CurFun.CurrentFunction);
459 if (ID.Type == ValID::NameVal) {
460 BB->setName(ID.Name);
461 } else {
462 CurFun.NumberedBlocks[ID.Num] = BB;
463 }
464
465 // If this is not a definition, keep track of it so we can use it as a forward
466 // reference.
467 if (!isDefinition) {
468 // Remember where this forward reference came from.
469 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
470 } else {
471 // The forward declaration could have been inserted anywhere in the
472 // function: insert it into the correct place now.
473 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
474 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
475 }
476 ID.destroy();
477 return BB;
478}
479
480
481//===----------------------------------------------------------------------===//
482// Code to handle forward references in instructions
483//===----------------------------------------------------------------------===//
484//
485// This code handles the late binding needed with statements that reference
486// values not defined yet... for example, a forward branch, or the PHI node for
487// a loop body.
488//
489// This keeps a table (CurFun.LateResolveValues) of all such forward references
490// and back patchs after we are done.
491//
492
493// ResolveDefinitions - If we could not resolve some defs at parsing
494// time (forward branches, phi functions for loops, etc...) resolve the
495// defs now...
496//
497static void
498ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
499 std::map<const Type*,ValueList> *FutureLateResolvers) {
500 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
501 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
502 E = LateResolvers.end(); LRI != E; ++LRI) {
503 ValueList &List = LRI->second;
504 while (!List.empty()) {
505 Value *V = List.back();
506 List.pop_back();
507
508 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
509 CurModule.PlaceHolderInfo.find(V);
510 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
511
512 ValID &DID = PHI->second.first;
513
514 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000515 if (TriggerError)
516 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000517 if (TheRealValue) {
518 V->replaceAllUsesWith(TheRealValue);
519 delete V;
520 CurModule.PlaceHolderInfo.erase(PHI);
521 } else if (FutureLateResolvers) {
522 // Functions have their unresolved items forwarded to the module late
523 // resolver table
524 InsertValue(V, *FutureLateResolvers);
525 } else {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000526 if (DID.Type == ValID::NameVal) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000527 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Chris Lattner58af2a12006-02-15 07:22:58 +0000528 "' of type '" + V->getType()->getDescription() + "'",
529 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000530 return;
531 } else {
Reid Spencer61c83e02006-08-18 08:43:06 +0000532 GenerateError("Reference to an invalid definition: #" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000533 itostr(DID.Num) + " of type '" +
534 V->getType()->getDescription() + "'",
535 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000536 return;
537 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000538 }
539 }
540 }
541
542 LateResolvers.clear();
543}
544
545// ResolveTypeTo - A brand new type was just declared. This means that (if
546// name is not null) things referencing Name can be resolved. Otherwise, things
547// refering to the number can be resolved. Do this now.
548//
549static void ResolveTypeTo(char *Name, const Type *ToTy) {
550 ValID D;
551 if (Name) D = ValID::create(Name);
552 else D = ValID::create((int)CurModule.Types.size());
553
Reid Spencer861d9d62006-11-28 07:29:44 +0000554 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000555 CurModule.LateResolveTypes.find(D);
556 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000557 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000558 CurModule.LateResolveTypes.erase(I);
559 }
560}
561
562// setValueName - Set the specified value to the name given. The name may be
563// null potentially, in which case this is a noop. The string passed in is
564// assumed to be a malloc'd string buffer, and is free'd by this function.
565//
566static void setValueName(Value *V, char *NameStr) {
567 if (NameStr) {
568 std::string Name(NameStr); // Copy string
569 free(NameStr); // Free old string
570
Reid Spencer5b7e7532006-09-28 19:28:24 +0000571 if (V->getType() == Type::VoidTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000572 GenerateError("Can't assign name '" + Name+"' to value with void type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000573 return;
574 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000575
576 assert(inFunctionScope() && "Must be in function scope!");
577 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
Reid Spencer5b7e7532006-09-28 19:28:24 +0000578 if (ST.lookup(V->getType(), Name)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000579 GenerateError("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000580 V->getType()->getDescription() + "' type plane!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000581 return;
582 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000583
584 // Set the name.
585 V->setName(Name);
586 }
587}
588
589/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
590/// this is a declaration, otherwise it is a definition.
591static GlobalVariable *
592ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
593 bool isConstantGlobal, const Type *Ty,
594 Constant *Initializer) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000595 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000596 GenerateError("Cannot declare global vars of function type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000597 return 0;
598 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000599
600 const PointerType *PTy = PointerType::get(Ty);
601
602 std::string Name;
603 if (NameStr) {
604 Name = NameStr; // Copy string
605 free(NameStr); // Free old string
606 }
607
608 // See if this global value was forward referenced. If so, recycle the
609 // object.
610 ValID ID;
611 if (!Name.empty()) {
612 ID = ValID::create((char*)Name.c_str());
613 } else {
614 ID = ValID::create((int)CurModule.Values[PTy].size());
615 }
616
617 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
618 // Move the global to the end of the list, from whereever it was
619 // previously inserted.
620 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
621 CurModule.CurrentModule->getGlobalList().remove(GV);
622 CurModule.CurrentModule->getGlobalList().push_back(GV);
623 GV->setInitializer(Initializer);
624 GV->setLinkage(Linkage);
625 GV->setConstant(isConstantGlobal);
626 InsertValue(GV, CurModule.Values);
627 return GV;
628 }
629
630 // If this global has a name, check to see if there is already a definition
631 // of this global in the module. If so, merge as appropriate. Note that
632 // this is really just a hack around problems in the CFE. :(
633 if (!Name.empty()) {
634 // We are a simple redefinition of a value, check to see if it is defined
635 // the same as the old one.
636 if (GlobalVariable *EGV =
637 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
638 // We are allowed to redefine a global variable in two circumstances:
639 // 1. If at least one of the globals is uninitialized or
640 // 2. If both initializers have the same value.
641 //
642 if (!EGV->hasInitializer() || !Initializer ||
643 EGV->getInitializer() == Initializer) {
644
645 // Make sure the existing global version gets the initializer! Make
646 // sure that it also gets marked const if the new version is.
647 if (Initializer && !EGV->hasInitializer())
648 EGV->setInitializer(Initializer);
649 if (isConstantGlobal)
650 EGV->setConstant(true);
651 EGV->setLinkage(Linkage);
652 return EGV;
653 }
654
Reid Spencer61c83e02006-08-18 08:43:06 +0000655 GenerateError("Redefinition of global variable named '" + Name +
Chris Lattner58af2a12006-02-15 07:22:58 +0000656 "' in the '" + Ty->getDescription() + "' type plane!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000657 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000658 }
659 }
660
661 // Otherwise there is no existing GV to use, create one now.
662 GlobalVariable *GV =
663 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
664 CurModule.CurrentModule);
665 InsertValue(GV, CurModule.Values);
666 return GV;
667}
668
669// setTypeName - Set the specified type to the name given. The name may be
670// null potentially, in which case this is a noop. The string passed in is
671// assumed to be a malloc'd string buffer, and is freed by this function.
672//
673// This function returns true if the type has already been defined, but is
674// allowed to be redefined in the specified context. If the name is a new name
675// for the type plane, it is inserted and false is returned.
676static bool setTypeName(const Type *T, char *NameStr) {
677 assert(!inFunctionScope() && "Can't give types function-local names!");
678 if (NameStr == 0) return false;
679
680 std::string Name(NameStr); // Copy string
681 free(NameStr); // Free old string
682
683 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000684 if (T == Type::VoidTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000685 GenerateError("Can't assign name '" + Name + "' to the void type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000686 return false;
687 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000688
689 // Set the type name, checking for conflicts as we do so.
690 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
691
692 if (AlreadyExists) { // Inserting a name that is already defined???
693 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
694 assert(Existing && "Conflict but no matching type?");
695
696 // There is only one case where this is allowed: when we are refining an
697 // opaque type. In this case, Existing will be an opaque type.
698 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
699 // We ARE replacing an opaque type!
700 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
701 return true;
702 }
703
704 // Otherwise, this is an attempt to redefine a type. That's okay if
705 // the redefinition is identical to the original. This will be so if
706 // Existing and T point to the same Type object. In this one case we
707 // allow the equivalent redefinition.
708 if (Existing == T) return true; // Yes, it's equal.
709
710 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer61c83e02006-08-18 08:43:06 +0000711 GenerateError("Redefinition of type named '" + Name + "' in the '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000712 T->getDescription() + "' type plane!");
713 }
714
715 return false;
716}
717
718//===----------------------------------------------------------------------===//
719// Code for handling upreferences in type names...
720//
721
722// TypeContains - Returns true if Ty directly contains E in it.
723//
724static bool TypeContains(const Type *Ty, const Type *E) {
725 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
726 E) != Ty->subtype_end();
727}
728
729namespace {
730 struct UpRefRecord {
731 // NestingLevel - The number of nesting levels that need to be popped before
732 // this type is resolved.
733 unsigned NestingLevel;
734
735 // LastContainedTy - This is the type at the current binding level for the
736 // type. Every time we reduce the nesting level, this gets updated.
737 const Type *LastContainedTy;
738
739 // UpRefTy - This is the actual opaque type that the upreference is
740 // represented with.
741 OpaqueType *UpRefTy;
742
743 UpRefRecord(unsigned NL, OpaqueType *URTy)
744 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
745 };
746}
747
748// UpRefs - A list of the outstanding upreferences that need to be resolved.
749static std::vector<UpRefRecord> UpRefs;
750
751/// HandleUpRefs - Every time we finish a new layer of types, this function is
752/// called. It loops through the UpRefs vector, which is a list of the
753/// currently active types. For each type, if the up reference is contained in
754/// the newly completed type, we decrement the level count. When the level
755/// count reaches zero, the upreferenced type is the type that is passed in:
756/// thus we can complete the cycle.
757///
758static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000759 // If Ty isn't abstract, or if there are no up-references in it, then there is
760 // nothing to resolve here.
761 if (!ty->isAbstract() || UpRefs.empty()) return ty;
762
Chris Lattner58af2a12006-02-15 07:22:58 +0000763 PATypeHolder Ty(ty);
764 UR_OUT("Type '" << Ty->getDescription() <<
765 "' newly formed. Resolving upreferences.\n" <<
766 UpRefs.size() << " upreferences active!\n");
767
768 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
769 // to zero), we resolve them all together before we resolve them to Ty. At
770 // the end of the loop, if there is anything to resolve to Ty, it will be in
771 // this variable.
772 OpaqueType *TypeToResolve = 0;
773
774 for (unsigned i = 0; i != UpRefs.size(); ++i) {
775 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
776 << UpRefs[i].second->getDescription() << ") = "
777 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
778 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
779 // Decrement level of upreference
780 unsigned Level = --UpRefs[i].NestingLevel;
781 UpRefs[i].LastContainedTy = Ty;
782 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
783 if (Level == 0) { // Upreference should be resolved!
784 if (!TypeToResolve) {
785 TypeToResolve = UpRefs[i].UpRefTy;
786 } else {
787 UR_OUT(" * Resolving upreference for "
788 << UpRefs[i].second->getDescription() << "\n";
789 std::string OldName = UpRefs[i].UpRefTy->getDescription());
790 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
791 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
792 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
793 }
794 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
795 --i; // Do not skip the next element...
796 }
797 }
798 }
799
800 if (TypeToResolve) {
801 UR_OUT(" * Resolving upreference for "
802 << UpRefs[i].second->getDescription() << "\n";
803 std::string OldName = TypeToResolve->getDescription());
804 TypeToResolve->refineAbstractTypeTo(Ty);
805 }
806
807 return Ty;
808}
809
Chris Lattner58af2a12006-02-15 07:22:58 +0000810// common code from the two 'RunVMAsmParser' functions
Reid Spencer5b7e7532006-09-28 19:28:24 +0000811static Module* RunParser(Module * M) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000812
813 llvmAsmlineno = 1; // Reset the current line number...
814 ObsoleteVarArgs = false;
815 NewVarArgs = false;
Chris Lattner58af2a12006-02-15 07:22:58 +0000816 CurModule.CurrentModule = M;
Reid Spencerf63697d2006-10-09 17:36:59 +0000817
818 // Check to make sure the parser succeeded
819 if (yyparse()) {
820 if (ParserResult)
821 delete ParserResult;
822 return 0;
823 }
824
825 // Check to make sure that parsing produced a result
Reid Spencer61c83e02006-08-18 08:43:06 +0000826 if (!ParserResult)
827 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000828
Reid Spencerf63697d2006-10-09 17:36:59 +0000829 // Reset ParserResult variable while saving its value for the result.
Chris Lattner58af2a12006-02-15 07:22:58 +0000830 Module *Result = ParserResult;
831 ParserResult = 0;
832
833 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
834 {
835 Function* F;
836 if ((F = Result->getNamedFunction("llvm.va_start"))
837 && F->getFunctionType()->getNumParams() == 0)
838 ObsoleteVarArgs = true;
839 if((F = Result->getNamedFunction("llvm.va_copy"))
840 && F->getFunctionType()->getNumParams() == 1)
841 ObsoleteVarArgs = true;
842 }
843
Reid Spencer5b7e7532006-09-28 19:28:24 +0000844 if (ObsoleteVarArgs && NewVarArgs) {
845 GenerateError(
846 "This file is corrupt: it uses both new and old style varargs");
847 return 0;
848 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000849
850 if(ObsoleteVarArgs) {
851 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000852 if (F->arg_size() != 0) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000853 GenerateError("Obsolete va_start takes 0 argument!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000854 return 0;
855 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000856
857 //foo = va_start()
858 // ->
859 //bar = alloca typeof(foo)
860 //va_start(bar)
861 //foo = load bar
862
863 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
864 const Type* ArgTy = F->getFunctionType()->getReturnType();
865 const Type* ArgTyPtr = PointerType::get(ArgTy);
866 Function* NF = Result->getOrInsertFunction("llvm.va_start",
867 RetTy, ArgTyPtr, (Type *)0);
868
869 while (!F->use_empty()) {
870 CallInst* CI = cast<CallInst>(F->use_back());
871 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
872 new CallInst(NF, bar, "", CI);
873 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
874 CI->replaceAllUsesWith(foo);
875 CI->getParent()->getInstList().erase(CI);
876 }
877 Result->getFunctionList().erase(F);
878 }
879
880 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000881 if(F->arg_size() != 1) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000882 GenerateError("Obsolete va_end takes 1 argument!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000883 return 0;
884 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000885
886 //vaend foo
887 // ->
888 //bar = alloca 1 of typeof(foo)
889 //vaend bar
890 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
891 const Type* ArgTy = F->getFunctionType()->getParamType(0);
892 const Type* ArgTyPtr = PointerType::get(ArgTy);
893 Function* NF = Result->getOrInsertFunction("llvm.va_end",
894 RetTy, ArgTyPtr, (Type *)0);
895
896 while (!F->use_empty()) {
897 CallInst* CI = cast<CallInst>(F->use_back());
898 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
899 new StoreInst(CI->getOperand(1), bar, CI);
900 new CallInst(NF, bar, "", CI);
901 CI->getParent()->getInstList().erase(CI);
902 }
903 Result->getFunctionList().erase(F);
904 }
905
906 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000907 if(F->arg_size() != 1) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000908 GenerateError("Obsolete va_copy takes 1 argument!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000909 return 0;
910 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000911 //foo = vacopy(bar)
912 // ->
913 //a = alloca 1 of typeof(foo)
914 //b = alloca 1 of typeof(foo)
915 //store bar -> b
916 //vacopy(a, b)
917 //foo = load a
918
919 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
920 const Type* ArgTy = F->getFunctionType()->getReturnType();
921 const Type* ArgTyPtr = PointerType::get(ArgTy);
922 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
923 RetTy, ArgTyPtr, ArgTyPtr,
924 (Type *)0);
925
926 while (!F->use_empty()) {
927 CallInst* CI = cast<CallInst>(F->use_back());
928 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
929 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
930 new StoreInst(CI->getOperand(1), b, CI);
931 new CallInst(NF, a, b, "", CI);
932 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
933 CI->replaceAllUsesWith(foo);
934 CI->getParent()->getInstList().erase(CI);
935 }
936 Result->getFunctionList().erase(F);
937 }
938 }
939
940 return Result;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000941}
Chris Lattner58af2a12006-02-15 07:22:58 +0000942
943//===----------------------------------------------------------------------===//
944// RunVMAsmParser - Define an interface to this parser
945//===----------------------------------------------------------------------===//
946//
947Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
948 set_scan_file(F);
949
950 CurFilename = Filename;
951 return RunParser(new Module(CurFilename));
952}
953
954Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
955 set_scan_string(AsmString);
956
957 CurFilename = "from_memory";
958 if (M == NULL) {
959 return RunParser(new Module (CurFilename));
960 } else {
961 return RunParser(M);
962 }
963}
964
965%}
966
967%union {
968 llvm::Module *ModuleVal;
969 llvm::Function *FunctionVal;
Reid Spencera132e042006-12-03 05:46:11 +0000970 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000971 llvm::BasicBlock *BasicBlockVal;
972 llvm::TerminatorInst *TermInstVal;
973 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000974 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000975
Reid Spencera132e042006-12-03 05:46:11 +0000976 const llvm::Type *PrimType;
977 llvm::PATypeHolder *TypeVal;
978 llvm::Value *ValueVal;
979
980 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
981 std::vector<llvm::Value*> *ValueList;
982 std::list<llvm::PATypeHolder> *TypeList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000983 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000984 std::list<std::pair<llvm::Value*,
985 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000986 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +0000987 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +0000988
989 llvm::GlobalValue::LinkageTypes Linkage;
990 int64_t SInt64Val;
991 uint64_t UInt64Val;
992 int SIntVal;
993 unsigned UIntVal;
994 double FPVal;
995 bool BoolVal;
996
997 char *StrVal; // This memory is strdup'd!
Reid Spencer1628cec2006-10-26 06:15:43 +0000998 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner58af2a12006-02-15 07:22:58 +0000999
Reid Spencera132e042006-12-03 05:46:11 +00001000 llvm::Instruction::BinaryOps BinaryOpVal;
1001 llvm::Instruction::TermOps TermOpVal;
1002 llvm::Instruction::MemoryOps MemOpVal;
1003 llvm::Instruction::CastOps CastOpVal;
1004 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer1628cec2006-10-26 06:15:43 +00001005 llvm::Module::Endianness Endianness;
Reid Spencera132e042006-12-03 05:46:11 +00001006 llvm::ICmpInst::Predicate IPredicate;
1007 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +00001008}
1009
1010%type <ModuleVal> Module FunctionList
1011%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1012%type <BasicBlockVal> BasicBlock InstructionList
1013%type <TermInstVal> BBTerminatorInst
1014%type <InstVal> Inst InstVal MemoryInst
1015%type <ConstVal> ConstVal ConstExpr
1016%type <ConstVector> ConstVector
1017%type <ArgList> ArgList ArgListH
1018%type <ArgVal> ArgVal
1019%type <PHIList> PHIList
1020%type <ValueList> ValueRefList ValueRefListE // For call param lists
1021%type <ValueList> IndexList // For GEP derived indices
1022%type <TypeList> TypeListI ArgTypeListI
1023%type <JumpTable> JumpTable
1024%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1025%type <BoolVal> OptVolatile // 'volatile' or not
1026%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1027%type <BoolVal> OptSideEffect // 'sideeffect' or not.
1028%type <Linkage> OptLinkage
1029%type <Endianness> BigOrLittle
1030
1031// ValueRef - Unresolved reference to a definition or BB
1032%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1033%type <ValueVal> ResolvedVal // <type> <valref> pair
1034// Tokens and types for handling constant integer values
1035//
1036// ESINT64VAL - A negative number within long long range
1037%token <SInt64Val> ESINT64VAL
1038
1039// EUINT64VAL - A positive number within uns. long long range
1040%token <UInt64Val> EUINT64VAL
1041%type <SInt64Val> EINT64VAL
1042
1043%token <SIntVal> SINTVAL // Signed 32 bit ints...
1044%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
1045%type <SIntVal> INTVAL
1046%token <FPVal> FPVAL // Float or Double constant
1047
1048// Built in types...
1049%type <TypeVal> Types TypesV UpRTypes UpRTypesV
Reid Spencera132e042006-12-03 05:46:11 +00001050%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
1051%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
1052%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner58af2a12006-02-15 07:22:58 +00001053
1054%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
1055%type <StrVal> Name OptName OptAssign
1056%type <UIntVal> OptAlign OptCAlign
1057%type <StrVal> OptSection SectionString
1058
1059%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1060%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001061%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1062%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Chris Lattner58af2a12006-02-15 07:22:58 +00001063%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
1064%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner75466192006-05-19 21:28:53 +00001065%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001066%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner1ae022f2006-10-22 06:08:13 +00001067%token DATALAYOUT
Chris Lattner58af2a12006-02-15 07:22:58 +00001068%type <UIntVal> OptCallingConv
1069
1070// Basic Block Terminating Operators
1071%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1072
1073// Binary Operators
1074%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001075%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer1628cec2006-10-26 06:15:43 +00001076%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencera132e042006-12-03 05:46:11 +00001077%token <OtherOpVal> ICMP FCMP
Reid Spencera132e042006-12-03 05:46:11 +00001078%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +00001079%type <FPredicate> FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001080%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
1081%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +00001082
1083// Memory Instructions
1084%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1085
Reid Spencer3da59db2006-11-27 01:05:10 +00001086// Cast Operators
1087%type <CastOpVal> CastOps
1088%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
1089%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
1090
Chris Lattner58af2a12006-02-15 07:22:58 +00001091// Other Operators
1092%type <OtherOpVal> ShiftOps
Reid Spencer3da59db2006-11-27 01:05:10 +00001093%token <OtherOpVal> PHI_TOK SELECT SHL LSHR ASHR VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001094%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattner58af2a12006-02-15 07:22:58 +00001095%token VAARG_old VANEXT_old //OBSOLETE
1096
1097
1098%start Module
1099%%
1100
1101// Handle constant integer size restriction and conversion...
1102//
1103INTVAL : SINTVAL;
1104INTVAL : UINTVAL {
1105 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001106 GEN_ERROR("Value too large for type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001107 $$ = (int32_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001108 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001109};
1110
1111
1112EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
1113EINT64VAL : EUINT64VAL {
1114 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001115 GEN_ERROR("Value too large for type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001116 $$ = (int64_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001117 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001118};
1119
1120// Operations that are notably excluded from this list include:
1121// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1122//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001123ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Chris Lattner58af2a12006-02-15 07:22:58 +00001124LogicalOps : AND | OR | XOR;
1125SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer3da59db2006-11-27 01:05:10 +00001126CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1127 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
1128ShiftOps : SHL | LSHR | ASHR;
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001129IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001130 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001131 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1132 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1133 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1134 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1135 ;
1136
1137FPredicates
1138 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1139 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1140 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1141 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1142 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1143 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1144 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1145 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1146 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1147 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001148
1149// These are some types that allow classification if we only want a particular
1150// thing... for example, only a signed, unsigned, or integral type.
1151SIntType : LONG | INT | SHORT | SBYTE;
1152UIntType : ULONG | UINT | USHORT | UBYTE;
1153IntType : SIntType | UIntType;
1154FPType : FLOAT | DOUBLE;
1155
1156// OptAssign - Value producing statements have an optional assignment component
1157OptAssign : Name '=' {
1158 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001159 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001160 }
1161 | /*empty*/ {
1162 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001163 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001164 };
1165
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001166OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1167 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
1168 WEAK { $$ = GlobalValue::WeakLinkage; } |
1169 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1170 DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; } |
1171 DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; } |
1172 EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } |
1173 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner58af2a12006-02-15 07:22:58 +00001174
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001175OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1176 CCC_TOK { $$ = CallingConv::C; } |
1177 CSRETCC_TOK { $$ = CallingConv::CSRet; } |
1178 FASTCC_TOK { $$ = CallingConv::Fast; } |
1179 COLDCC_TOK { $$ = CallingConv::Cold; } |
1180 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1181 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1182 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001183 if ((unsigned)$2 != $2)
Reid Spencer61c83e02006-08-18 08:43:06 +00001184 GEN_ERROR("Calling conv too large!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001185 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001186 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001187 };
1188
1189// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1190// a comma before it.
1191OptAlign : /*empty*/ { $$ = 0; } |
1192 ALIGN EUINT64VAL {
1193 $$ = $2;
1194 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001195 GEN_ERROR("Alignment must be a power of two!");
1196 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001197};
1198OptCAlign : /*empty*/ { $$ = 0; } |
1199 ',' ALIGN EUINT64VAL {
1200 $$ = $3;
1201 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001202 GEN_ERROR("Alignment must be a power of two!");
1203 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001204};
1205
1206
1207SectionString : SECTION STRINGCONSTANT {
1208 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1209 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer61c83e02006-08-18 08:43:06 +00001210 GEN_ERROR("Invalid character in section name!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001211 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001212 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001213};
1214
1215OptSection : /*empty*/ { $$ = 0; } |
1216 SectionString { $$ = $1; };
1217
1218// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1219// is set to be the global we are processing.
1220//
1221GlobalVarAttributes : /* empty */ {} |
1222 ',' GlobalVarAttribute GlobalVarAttributes {};
1223GlobalVarAttribute : SectionString {
1224 CurGV->setSection($1);
1225 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001226 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001227 }
1228 | ALIGN EUINT64VAL {
1229 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001230 GEN_ERROR("Alignment must be a power of two!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001231 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001232 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001233 };
1234
1235//===----------------------------------------------------------------------===//
1236// Types includes all predefined types... except void, because it can only be
1237// used in specific contexts (function returning void for example). To have
1238// access to it, a user must explicitly use TypesV.
1239//
1240
1241// TypesV includes all of 'Types', but it also includes the void type.
Reid Spencera132e042006-12-03 05:46:11 +00001242TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1243UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner58af2a12006-02-15 07:22:58 +00001244
1245Types : UpRTypes {
1246 if (!UpRefs.empty())
Reid Spencera132e042006-12-03 05:46:11 +00001247 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001248 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001249 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001250 };
Chris Lattner58af2a12006-02-15 07:22:58 +00001251
1252
1253// Derived types are added later...
1254//
1255PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1256PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
1257UpRTypes : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001258 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001259 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001260 }
1261 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001262 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001263 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001264 };
1265UpRTypes : SymbolicValueRef { // Named types are also simple types...
Reid Spencer5b7e7532006-09-28 19:28:24 +00001266 const Type* tmp = getTypeVal($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001267 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001268 $$ = new PATypeHolder(tmp);
Chris Lattner58af2a12006-02-15 07:22:58 +00001269};
1270
1271// Include derived types in the Types production.
1272//
1273UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencer61c83e02006-08-18 08:43:06 +00001274 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001275 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1276 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001277 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001278 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001279 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001280 }
1281 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
1282 std::vector<const Type*> Params;
Reid Spencera132e042006-12-03 05:46:11 +00001283 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001284 E = $3->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001285 Params.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001286 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1287 if (isVarArg) Params.pop_back();
1288
Reid Spencera132e042006-12-03 05:46:11 +00001289 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001290 delete $3; // Delete the argument list
Reid Spencera132e042006-12-03 05:46:11 +00001291 delete $1; // Delete the return type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001292 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001293 }
1294 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencera132e042006-12-03 05:46:11 +00001295 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1296 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001297 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001298 }
1299 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencera132e042006-12-03 05:46:11 +00001300 const llvm::Type* ElemTy = $4->get();
1301 if ((unsigned)$2 != $2)
1302 GEN_ERROR("Unsigned result not equal to signed result");
1303 if (!ElemTy->isPrimitiveType())
1304 GEN_ERROR("Elemental type of a PackedType must be primitive");
1305 if (!isPowerOf2_32($2))
1306 GEN_ERROR("Vector length should be a power of 2!");
1307 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1308 delete $4;
1309 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001310 }
1311 | '{' TypeListI '}' { // Structure type?
1312 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001313 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001314 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001315 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001316
Reid Spencera132e042006-12-03 05:46:11 +00001317 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001318 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001319 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001320 }
1321 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001322 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001323 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001324 }
1325 | UpRTypes '*' { // Pointer type?
Reid Spencera132e042006-12-03 05:46:11 +00001326 if (*$1 == Type::LabelTy)
Chris Lattner59c85e92006-10-15 23:27:25 +00001327 GEN_ERROR("Cannot form a pointer to a basic block");
Reid Spencera132e042006-12-03 05:46:11 +00001328 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1329 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001330 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001331 };
1332
1333// TypeList - Used for struct declarations and as a basis for function type
1334// declaration type lists
1335//
1336TypeListI : UpRTypes {
Reid Spencera132e042006-12-03 05:46:11 +00001337 $$ = new std::list<PATypeHolder>();
1338 $$->push_back(*$1); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001339 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001340 }
1341 | TypeListI ',' UpRTypes {
Reid Spencera132e042006-12-03 05:46:11 +00001342 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001343 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001344 };
1345
1346// ArgTypeList - List of types for a function type declaration...
1347ArgTypeListI : TypeListI
1348 | TypeListI ',' DOTDOTDOT {
Reid Spencera132e042006-12-03 05:46:11 +00001349 ($$=$1)->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001350 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001351 }
1352 | DOTDOTDOT {
Reid Spencera132e042006-12-03 05:46:11 +00001353 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001354 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001355 }
1356 | /*empty*/ {
Reid Spencera132e042006-12-03 05:46:11 +00001357 $$ = new std::list<PATypeHolder>();
Reid Spencer61c83e02006-08-18 08:43:06 +00001358 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001359 };
1360
1361// ConstVal - The various declarations that go into the constant pool. This
1362// production is used ONLY to represent constants that show up AFTER a 'const',
1363// 'constant' or 'global' token at global scope. Constants that can be inlined
1364// into other expressions (such as integers and constexprs) are handled by the
1365// ResolvedVal, ValueRef and ConstValueRef productions.
1366//
1367ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencera132e042006-12-03 05:46:11 +00001368 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001369 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001370 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001371 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001372 const Type *ETy = ATy->getElementType();
1373 int NumElements = ATy->getNumElements();
1374
1375 // Verify that we have the correct size...
1376 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001377 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001378 utostr($3->size()) + " arguments, but has size of " +
1379 itostr(NumElements) + "!");
1380
1381 // Verify all elements are correct type!
1382 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001383 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001384 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001385 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001386 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001387 }
1388
Reid Spencera132e042006-12-03 05:46:11 +00001389 $$ = ConstantArray::get(ATy, *$3);
1390 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001391 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001392 }
1393 | Types '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00001394 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001395 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001396 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001397 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001398
1399 int NumElements = ATy->getNumElements();
1400 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001401 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Chris Lattner58af2a12006-02-15 07:22:58 +00001402 " arguments, but has size of " + itostr(NumElements) +"!");
Reid Spencera132e042006-12-03 05:46:11 +00001403 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1404 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001405 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001406 }
1407 | Types 'c' STRINGCONSTANT {
Reid Spencera132e042006-12-03 05:46:11 +00001408 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001409 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001410 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001411 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001412
1413 int NumElements = ATy->getNumElements();
1414 const Type *ETy = ATy->getElementType();
1415 char *EndStr = UnEscapeLexed($3, true);
1416 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001417 GEN_ERROR("Can't build string constant of size " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001418 itostr((int)(EndStr-$3)) +
1419 " when array has size " + itostr(NumElements) + "!");
1420 std::vector<Constant*> Vals;
1421 if (ETy == Type::SByteTy) {
1422 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
Reid Spencerb83eb642006-10-20 07:07:24 +00001423 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner58af2a12006-02-15 07:22:58 +00001424 } else if (ETy == Type::UByteTy) {
1425 for (unsigned char *C = (unsigned char *)$3;
1426 C != (unsigned char*)EndStr; ++C)
Reid Spencerb83eb642006-10-20 07:07:24 +00001427 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner58af2a12006-02-15 07:22:58 +00001428 } else {
1429 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001430 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001431 }
1432 free($3);
Reid Spencera132e042006-12-03 05:46:11 +00001433 $$ = ConstantArray::get(ATy, Vals);
1434 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001435 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001436 }
1437 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencera132e042006-12-03 05:46:11 +00001438 const PackedType *PTy = dyn_cast<PackedType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001439 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001440 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001441 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001442 const Type *ETy = PTy->getElementType();
1443 int NumElements = PTy->getNumElements();
1444
1445 // Verify that we have the correct size...
1446 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001447 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001448 utostr($3->size()) + " arguments, but has size of " +
1449 itostr(NumElements) + "!");
1450
1451 // Verify all elements are correct type!
1452 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001453 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001454 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001455 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001456 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001457 }
1458
Reid Spencera132e042006-12-03 05:46:11 +00001459 $$ = ConstantPacked::get(PTy, *$3);
1460 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001461 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001462 }
1463 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001464 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001465 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001466 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001467 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001468
1469 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer61c83e02006-08-18 08:43:06 +00001470 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001471
1472 // Check to ensure that constants are compatible with the type initializer!
1473 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001474 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001475 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001476 STy->getElementType(i)->getDescription() +
1477 "' for element #" + utostr(i) +
1478 " of structure initializer!");
1479
Reid Spencera132e042006-12-03 05:46:11 +00001480 $$ = ConstantStruct::get(STy, *$3);
1481 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001482 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001483 }
1484 | Types '{' '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001485 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001486 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001487 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001488 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001489
1490 if (STy->getNumContainedTypes() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001491 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001492
Reid Spencera132e042006-12-03 05:46:11 +00001493 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1494 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001495 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001496 }
1497 | Types NULL_TOK {
Reid Spencera132e042006-12-03 05:46:11 +00001498 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001499 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001500 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001501 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001502
Reid Spencera132e042006-12-03 05:46:11 +00001503 $$ = ConstantPointerNull::get(PTy);
1504 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001505 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001506 }
1507 | Types UNDEF {
Reid Spencera132e042006-12-03 05:46:11 +00001508 $$ = UndefValue::get($1->get());
1509 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001510 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001511 }
1512 | Types SymbolicValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00001513 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001514 if (Ty == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001515 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001516
1517 // ConstExprs can exist in the body of a function, thus creating
1518 // GlobalValues whenever they refer to a variable. Because we are in
1519 // the context of a function, getValNonImprovising will search the functions
1520 // symbol table instead of the module symbol table for the global symbol,
1521 // which throws things all off. To get around this, we just tell
1522 // getValNonImprovising that we are at global scope here.
1523 //
1524 Function *SavedCurFn = CurFun.CurrentFunction;
1525 CurFun.CurrentFunction = 0;
1526
1527 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001528 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001529
1530 CurFun.CurrentFunction = SavedCurFn;
1531
1532 // If this is an initializer for a constant pointer, which is referencing a
1533 // (currently) undefined variable, create a stub now that shall be replaced
1534 // in the future with the right type of variable.
1535 //
1536 if (V == 0) {
1537 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1538 const PointerType *PT = cast<PointerType>(Ty);
1539
1540 // First check to see if the forward references value is already created!
1541 PerModuleInfo::GlobalRefsType::iterator I =
1542 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1543
1544 if (I != CurModule.GlobalRefs.end()) {
1545 V = I->second; // Placeholder already exists, use it...
1546 $2.destroy();
1547 } else {
1548 std::string Name;
1549 if ($2.Type == ValID::NameVal) Name = $2.Name;
1550
1551 // Create the forward referenced global.
1552 GlobalValue *GV;
1553 if (const FunctionType *FTy =
1554 dyn_cast<FunctionType>(PT->getElementType())) {
1555 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1556 CurModule.CurrentModule);
1557 } else {
1558 GV = new GlobalVariable(PT->getElementType(), false,
1559 GlobalValue::ExternalLinkage, 0,
1560 Name, CurModule.CurrentModule);
1561 }
1562
1563 // Keep track of the fact that we have a forward ref to recycle it
1564 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1565 V = GV;
1566 }
1567 }
1568
Reid Spencera132e042006-12-03 05:46:11 +00001569 $$ = cast<GlobalValue>(V);
1570 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001571 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001572 }
1573 | Types ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00001574 if ($1->get() != $2->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001575 GEN_ERROR("Mismatched types for constant expression!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001576 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001577 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001578 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001579 }
1580 | Types ZEROINITIALIZER {
Reid Spencera132e042006-12-03 05:46:11 +00001581 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001582 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +00001583 GEN_ERROR("Cannot create a null initialized value of this type!");
Reid Spencera132e042006-12-03 05:46:11 +00001584 $$ = Constant::getNullValue(Ty);
1585 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001586 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001587 }
1588 | SIntType EINT64VAL { // integral constants
1589 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001590 GEN_ERROR("Constant value doesn't fit in type!");
Reid Spencera132e042006-12-03 05:46:11 +00001591 $$ = ConstantInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001592 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001593 }
1594 | UIntType EUINT64VAL { // integral constants
Reid Spencera132e042006-12-03 05:46:11 +00001595 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001596 GEN_ERROR("Constant value doesn't fit in type!");
Reid Spencera132e042006-12-03 05:46:11 +00001597 $$ = ConstantInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001598 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001599 }
1600 | BOOL TRUETOK { // Boolean constants
Reid Spencera132e042006-12-03 05:46:11 +00001601 $$ = ConstantBool::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001602 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001603 }
1604 | BOOL FALSETOK { // Boolean constants
Reid Spencera132e042006-12-03 05:46:11 +00001605 $$ = ConstantBool::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001606 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001607 }
1608 | FPType FPVAL { // Float & Double constants
Reid Spencera132e042006-12-03 05:46:11 +00001609 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001610 GEN_ERROR("Floating point constant invalid for type!!");
Reid Spencera132e042006-12-03 05:46:11 +00001611 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001612 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001613 };
1614
1615
Reid Spencer3da59db2006-11-27 01:05:10 +00001616ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001617 Constant *Val = $3;
1618 const Type *Ty = $5->get();
Reid Spencer3da59db2006-11-27 01:05:10 +00001619 if (!Val->getType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001620 GEN_ERROR("cast constant expression from a non-primitive type: '" +
Reid Spencer3da59db2006-11-27 01:05:10 +00001621 Val->getType()->getDescription() + "'!");
1622 if (!Ty->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001623 GEN_ERROR("cast constant expression to a non-primitive type: '" +
Reid Spencer3da59db2006-11-27 01:05:10 +00001624 Ty->getDescription() + "'!");
Reid Spencera132e042006-12-03 05:46:11 +00001625 $$ = ConstantExpr::getCast($1, $3, $5->get());
1626 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001627 }
1628 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001629 if (!isa<PointerType>($3->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001630 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001631
Reid Spencera132e042006-12-03 05:46:11 +00001632 const Type *IdxTy =
1633 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1634 if (!IdxTy)
1635 GEN_ERROR("Index list invalid for constant getelementptr!");
1636
1637 std::vector<Constant*> IdxVec;
1638 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1639 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001640 IdxVec.push_back(C);
1641 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001642 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001643
1644 delete $4;
1645
Reid Spencera132e042006-12-03 05:46:11 +00001646 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Reid Spencer61c83e02006-08-18 08:43:06 +00001647 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001648 }
1649 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001650 if ($3->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001651 GEN_ERROR("Select condition must be of boolean type!");
Reid Spencera132e042006-12-03 05:46:11 +00001652 if ($5->getType() != $7->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001653 GEN_ERROR("Select operand types must match!");
Reid Spencera132e042006-12-03 05:46:11 +00001654 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001655 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001656 }
1657 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001658 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001659 GEN_ERROR("Binary operator types must match!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001660 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00001661 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001662 }
1663 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001664 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001665 GEN_ERROR("Logical operator types must match!");
Reid Spencera132e042006-12-03 05:46:11 +00001666 if (!$3->getType()->isIntegral()) {
1667 if (!isa<PackedType>($3->getType()) ||
1668 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00001669 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001670 }
Reid Spencera132e042006-12-03 05:46:11 +00001671 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001672 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001673 }
1674 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001675 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001676 GEN_ERROR("setcc operand types must match!");
Reid Spencera132e042006-12-03 05:46:11 +00001677 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001678 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001679 }
Reid Spencer4012e832006-12-04 05:24:24 +00001680 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1681 if ($4->getType() != $6->getType())
Reid Spencera132e042006-12-03 05:46:11 +00001682 GEN_ERROR("icmp operand types must match!");
Reid Spencer4012e832006-12-04 05:24:24 +00001683 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001684 }
Reid Spencer4012e832006-12-04 05:24:24 +00001685 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1686 if ($4->getType() != $6->getType())
Reid Spencera132e042006-12-03 05:46:11 +00001687 GEN_ERROR("fcmp operand types must match!");
Reid Spencer4012e832006-12-04 05:24:24 +00001688 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001689 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001690 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001691 if ($5->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001692 GEN_ERROR("Shift count for shift constant must be unsigned byte!");
Reid Spencera132e042006-12-03 05:46:11 +00001693 if (!$3->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00001694 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencer3822ff52006-11-08 06:47:33 +00001695 CHECK_FOR_ERROR;
Reid Spencera132e042006-12-03 05:46:11 +00001696 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001697 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001698 }
1699 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001700 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencer61c83e02006-08-18 08:43:06 +00001701 GEN_ERROR("Invalid extractelement operands!");
Reid Spencera132e042006-12-03 05:46:11 +00001702 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001703 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001704 }
1705 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001706 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001707 GEN_ERROR("Invalid insertelement operands!");
Reid Spencera132e042006-12-03 05:46:11 +00001708 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001709 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001710 }
1711 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001712 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001713 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencera132e042006-12-03 05:46:11 +00001714 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001715 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001716 };
1717
Chris Lattnerd25db202006-04-08 03:55:17 +00001718
Chris Lattner58af2a12006-02-15 07:22:58 +00001719// ConstVector - A list of comma separated constants.
1720ConstVector : ConstVector ',' ConstVal {
1721 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001722 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001723 }
1724 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00001725 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00001726 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001727 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001728 };
1729
1730
1731// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1732GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1733
1734
1735//===----------------------------------------------------------------------===//
1736// Rules to match Modules
1737//===----------------------------------------------------------------------===//
1738
1739// Module rule: Capture the result of parsing the whole file into a result
1740// variable...
1741//
1742Module : FunctionList {
1743 $$ = ParserResult = $1;
1744 CurModule.ModuleDone();
Reid Spencerf63697d2006-10-09 17:36:59 +00001745 CHECK_FOR_ERROR;
Chris Lattner58af2a12006-02-15 07:22:58 +00001746};
1747
1748// FunctionList - A list of functions, preceeded by a constant pool.
1749//
1750FunctionList : FunctionList Function {
1751 $$ = $1;
1752 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001753 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001754 }
1755 | FunctionList FunctionProto {
1756 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001757 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001758 }
1759 | FunctionList MODULE ASM_TOK AsmBlock {
1760 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001761 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001762 }
1763 | FunctionList IMPLEMENTATION {
1764 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001765 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001766 }
1767 | ConstPool {
1768 $$ = CurModule.CurrentModule;
1769 // Emit an error if there are any unresolved types left.
1770 if (!CurModule.LateResolveTypes.empty()) {
1771 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer61c83e02006-08-18 08:43:06 +00001772 if (DID.Type == ValID::NameVal) {
1773 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1774 } else {
1775 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1776 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001777 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001778 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001779 };
1780
1781// ConstPool - Constants with optional names assigned to them.
1782ConstPool : ConstPool OptAssign TYPE TypesV {
1783 // Eagerly resolve types. This is not an optimization, this is a
1784 // requirement that is due to the fact that we could have this:
1785 //
1786 // %list = type { %list * }
1787 // %list = type { %list * } ; repeated type decl
1788 //
1789 // If types are not resolved eagerly, then the two types will not be
1790 // determined to be the same type!
1791 //
Reid Spencera132e042006-12-03 05:46:11 +00001792 ResolveTypeTo($2, *$4);
Chris Lattner58af2a12006-02-15 07:22:58 +00001793
Reid Spencera132e042006-12-03 05:46:11 +00001794 if (!setTypeName(*$4, $2) && !$2) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001795 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001796 // If this is a named type that is not a redefinition, add it to the slot
1797 // table.
Reid Spencera132e042006-12-03 05:46:11 +00001798 CurModule.Types.push_back(*$4);
Chris Lattner58af2a12006-02-15 07:22:58 +00001799 }
Reid Spencera132e042006-12-03 05:46:11 +00001800
1801 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001802 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001803 }
1804 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001805 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001806 }
1807 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001808 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001809 }
1810 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00001811 if ($5 == 0)
Reid Spencer5b7e7532006-09-28 19:28:24 +00001812 GEN_ERROR("Global value initializer is not a constant!");
Reid Spencera132e042006-12-03 05:46:11 +00001813 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001814 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00001815 } GlobalVarAttributes {
1816 CurGV = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +00001817 }
1818 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencera132e042006-12-03 05:46:11 +00001819 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001820 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001821 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001822 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001823 CurGV = 0;
1824 CHECK_FOR_ERROR
1825 }
1826 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencera132e042006-12-03 05:46:11 +00001827 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, *$5, 0);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001828 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001829 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001830 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001831 CurGV = 0;
1832 CHECK_FOR_ERROR
1833 }
1834 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001835 CurGV =
Reid Spencera132e042006-12-03 05:46:11 +00001836 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, *$5, 0);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001837 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001838 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001839 } GlobalVarAttributes {
Chris Lattner58af2a12006-02-15 07:22:58 +00001840 CurGV = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001841 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001842 }
1843 | ConstPool TARGET TargetDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001844 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001845 }
1846 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001847 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001848 }
1849 | /* empty: end of list */ {
1850 };
1851
1852
1853AsmBlock : STRINGCONSTANT {
1854 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
1855 char *EndStr = UnEscapeLexed($1, true);
1856 std::string NewAsm($1, EndStr);
1857 free($1);
1858
1859 if (AsmSoFar.empty())
1860 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
1861 else
1862 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00001863 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001864};
1865
1866BigOrLittle : BIG { $$ = Module::BigEndian; };
1867BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1868
1869TargetDefinition : ENDIAN '=' BigOrLittle {
1870 CurModule.CurrentModule->setEndianness($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001871 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001872 }
1873 | POINTERSIZE '=' EUINT64VAL {
1874 if ($3 == 32)
1875 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1876 else if ($3 == 64)
1877 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1878 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001879 GEN_ERROR("Invalid pointer size: '" + utostr($3) + "'!");
1880 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001881 }
1882 | TRIPLE '=' STRINGCONSTANT {
1883 CurModule.CurrentModule->setTargetTriple($3);
1884 free($3);
John Criswell2f6a8b12006-10-24 19:09:48 +00001885 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00001886 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson1dc69692006-10-18 02:21:48 +00001887 CurModule.CurrentModule->setDataLayout($3);
1888 free($3);
Owen Anderson1dc69692006-10-18 02:21:48 +00001889 };
Chris Lattner58af2a12006-02-15 07:22:58 +00001890
1891LibrariesDefinition : '[' LibList ']';
1892
1893LibList : LibList ',' STRINGCONSTANT {
1894 CurModule.CurrentModule->addLibrary($3);
1895 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001896 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001897 }
1898 | STRINGCONSTANT {
1899 CurModule.CurrentModule->addLibrary($1);
1900 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001901 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001902 }
1903 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00001904 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001905 }
1906 ;
1907
1908//===----------------------------------------------------------------------===//
1909// Rules to match Function Headers
1910//===----------------------------------------------------------------------===//
1911
1912Name : VAR_ID | STRINGCONSTANT;
1913OptName : Name | /*empty*/ { $$ = 0; };
1914
1915ArgVal : Types OptName {
Reid Spencera132e042006-12-03 05:46:11 +00001916 if (*$1 == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001917 GEN_ERROR("void typed arguments are invalid!");
Reid Spencera132e042006-12-03 05:46:11 +00001918 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001919 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001920};
1921
1922ArgListH : ArgListH ',' ArgVal {
1923 $$ = $1;
1924 $1->push_back(*$3);
1925 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001926 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001927 }
1928 | ArgVal {
Reid Spencera132e042006-12-03 05:46:11 +00001929 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner58af2a12006-02-15 07:22:58 +00001930 $$->push_back(*$1);
1931 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001932 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001933 };
1934
1935ArgList : ArgListH {
1936 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001937 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001938 }
1939 | ArgListH ',' DOTDOTDOT {
1940 $$ = $1;
Reid Spencera132e042006-12-03 05:46:11 +00001941 $$->push_back(std::pair<PATypeHolder*,
1942 char*>(new PATypeHolder(Type::VoidTy), 0));
Reid Spencer61c83e02006-08-18 08:43:06 +00001943 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001944 }
1945 | DOTDOTDOT {
Reid Spencera132e042006-12-03 05:46:11 +00001946 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1947 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Reid Spencer61c83e02006-08-18 08:43:06 +00001948 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001949 }
1950 | /* empty */ {
1951 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001952 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001953 };
1954
1955FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1956 OptSection OptAlign {
1957 UnEscapeLexed($3);
1958 std::string FunctionName($3);
1959 free($3); // Free strdup'd memory!
1960
Reid Spencera132e042006-12-03 05:46:11 +00001961 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001962 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001963
1964 std::vector<const Type*> ParamTypeList;
1965 if ($5) { // If there are arguments...
Reid Spencera132e042006-12-03 05:46:11 +00001966 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
Chris Lattner58af2a12006-02-15 07:22:58 +00001967 I != $5->end(); ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001968 ParamTypeList.push_back(I->first->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001969 }
1970
1971 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1972 if (isVarArg) ParamTypeList.pop_back();
1973
Reid Spencera132e042006-12-03 05:46:11 +00001974 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattner58af2a12006-02-15 07:22:58 +00001975 const PointerType *PFT = PointerType::get(FT);
Reid Spencera132e042006-12-03 05:46:11 +00001976 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00001977
1978 ValID ID;
1979 if (!FunctionName.empty()) {
1980 ID = ValID::create((char*)FunctionName.c_str());
1981 } else {
1982 ID = ValID::create((int)CurModule.Values[PFT].size());
1983 }
1984
1985 Function *Fn = 0;
1986 // See if this function was forward referenced. If so, recycle the object.
1987 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1988 // Move the function to the end of the list, from whereever it was
1989 // previously inserted.
1990 Fn = cast<Function>(FWRef);
1991 CurModule.CurrentModule->getFunctionList().remove(Fn);
1992 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1993 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1994 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1995 // If this is the case, either we need to be a forward decl, or it needs
1996 // to be.
1997 if (!CurFun.isDeclare && !Fn->isExternal())
Reid Spencer61c83e02006-08-18 08:43:06 +00001998 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001999
2000 // Make sure to strip off any argument names so we can't get conflicts.
2001 if (Fn->isExternal())
2002 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2003 AI != AE; ++AI)
2004 AI->setName("");
Chris Lattner58af2a12006-02-15 07:22:58 +00002005 } else { // Not already defined?
2006 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
2007 CurModule.CurrentModule);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002008
Chris Lattner58af2a12006-02-15 07:22:58 +00002009 InsertValue(Fn, CurModule.Values);
2010 }
2011
2012 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002013
2014 if (CurFun.isDeclare) {
2015 // If we have declaration, always overwrite linkage. This will allow us to
2016 // correctly handle cases, when pointer to function is passed as argument to
2017 // another function.
2018 Fn->setLinkage(CurFun.Linkage);
2019 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002020 Fn->setCallingConv($1);
2021 Fn->setAlignment($8);
2022 if ($7) {
2023 Fn->setSection($7);
2024 free($7);
2025 }
2026
2027 // Add all of the arguments we parsed to the function...
2028 if ($5) { // Is null if empty...
2029 if (isVarArg) { // Nuke the last entry
Reid Spencera132e042006-12-03 05:46:11 +00002030 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
2031 "Not a varargs marker!");
2032 delete $5->back().first;
Chris Lattner58af2a12006-02-15 07:22:58 +00002033 $5->pop_back(); // Delete the last entry
2034 }
2035 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencera132e042006-12-03 05:46:11 +00002036 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
Chris Lattner58af2a12006-02-15 07:22:58 +00002037 I != $5->end(); ++I, ++ArgIt) {
Reid Spencera132e042006-12-03 05:46:11 +00002038 delete I->first; // Delete the typeholder...
2039
Chris Lattner58af2a12006-02-15 07:22:58 +00002040 setValueName(ArgIt, I->second); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002041 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002042 InsertValue(ArgIt);
2043 }
Reid Spencera132e042006-12-03 05:46:11 +00002044
Chris Lattner58af2a12006-02-15 07:22:58 +00002045 delete $5; // We're now done with the argument list
2046 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002047 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002048};
2049
2050BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2051
2052FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
2053 $$ = CurFun.CurrentFunction;
2054
2055 // Make sure that we keep track of the linkage type even if there was a
2056 // previous "declare".
2057 $$->setLinkage($1);
2058};
2059
2060END : ENDTOK | '}'; // Allow end of '}' to end a function
2061
2062Function : BasicBlockList END {
2063 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002064 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002065};
2066
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002067FnDeclareLinkage: /*default*/ |
Chris Lattnerf49c1762006-11-08 05:58:47 +00002068 DLLIMPORT { CurFun.Linkage = GlobalValue::DLLImportLinkage; } |
Reid Spencer481169e2006-12-01 00:33:46 +00002069 EXTERN_WEAK { CurFun.Linkage = GlobalValue::ExternalWeakLinkage; };
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002070
2071FunctionProto : DECLARE { CurFun.isDeclare = true; } FnDeclareLinkage FunctionHeaderH {
2072 $$ = CurFun.CurrentFunction;
2073 CurFun.FunctionDone();
2074 CHECK_FOR_ERROR
2075 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002076
2077//===----------------------------------------------------------------------===//
2078// Rules to match Basic Blocks
2079//===----------------------------------------------------------------------===//
2080
2081OptSideEffect : /* empty */ {
2082 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002083 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002084 }
2085 | SIDEEFFECT {
2086 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002087 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002088 };
2089
2090ConstValueRef : ESINT64VAL { // A reference to a direct constant
2091 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002092 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002093 }
2094 | EUINT64VAL {
2095 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002096 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002097 }
2098 | FPVAL { // Perhaps it's an FP constant?
2099 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002100 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002101 }
2102 | TRUETOK {
Chris Lattner47811b72006-09-28 23:35:22 +00002103 $$ = ValID::create(ConstantBool::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002104 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002105 }
2106 | FALSETOK {
Chris Lattner47811b72006-09-28 23:35:22 +00002107 $$ = ValID::create(ConstantBool::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002108 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002109 }
2110 | NULL_TOK {
2111 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002112 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002113 }
2114 | UNDEF {
2115 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002116 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002117 }
2118 | ZEROINITIALIZER { // A vector zero constant.
2119 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002120 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002121 }
2122 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002123 const Type *ETy = (*$2)[0]->getType();
Chris Lattner58af2a12006-02-15 07:22:58 +00002124 int NumElements = $2->size();
2125
2126 PackedType* pt = PackedType::get(ETy, NumElements);
2127 PATypeHolder* PTy = new PATypeHolder(
Reid Spencera132e042006-12-03 05:46:11 +00002128 HandleUpRefs(
2129 PackedType::get(
2130 ETy,
2131 NumElements)
2132 )
2133 );
Chris Lattner58af2a12006-02-15 07:22:58 +00002134
2135 // Verify all elements are correct type!
2136 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002137 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002138 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002139 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002140 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002141 }
2142
Reid Spencera132e042006-12-03 05:46:11 +00002143 $$ = ValID::create(ConstantPacked::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002144 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002145 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002146 }
2147 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002148 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002149 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002150 }
2151 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2152 char *End = UnEscapeLexed($3, true);
2153 std::string AsmStr = std::string($3, End);
2154 End = UnEscapeLexed($5, true);
2155 std::string Constraints = std::string($5, End);
2156 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2157 free($3);
2158 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002159 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002160 };
2161
2162// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2163// another value.
2164//
2165SymbolicValueRef : INTVAL { // Is it an integer reference...?
2166 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002167 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002168 }
2169 | Name { // Is it a named reference...?
2170 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002171 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002172 };
2173
2174// ValueRef - A reference to a definition... either constant or symbolic
2175ValueRef : SymbolicValueRef | ConstValueRef;
2176
2177
2178// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2179// type immediately preceeds the value reference, and allows complex constant
2180// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2181ResolvedVal : Types ValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00002182 $$ = getVal(*$1, $2); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002183 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002184 };
2185
2186BasicBlockList : BasicBlockList BasicBlock {
2187 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002188 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002189 }
2190 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2191 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002192 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002193 };
2194
2195
2196// Basic blocks are terminated by branching instructions:
2197// br, br/cc, switch, ret
2198//
2199BasicBlock : InstructionList OptAssign BBTerminatorInst {
2200 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002201 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002202 InsertValue($3);
2203
2204 $1->getInstList().push_back($3);
2205 InsertValue($1);
2206 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002207 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002208 };
2209
2210InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002211 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2212 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2213 if (CI2->getParent() == 0)
2214 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002215 $1->getInstList().push_back($2);
2216 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002217 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002218 }
2219 | /* empty */ {
2220 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002221 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002222
2223 // Make sure to move the basic block to the correct location in the
2224 // function, instead of leaving it inserted wherever it was first
2225 // referenced.
2226 Function::BasicBlockListType &BBL =
2227 CurFun.CurrentFunction->getBasicBlockList();
2228 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002229 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002230 }
2231 | LABELSTR {
2232 $$ = CurBB = getBBVal(ValID::create($1), true);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002233 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002234
2235 // Make sure to move the basic block to the correct location in the
2236 // function, instead of leaving it inserted wherever it was first
2237 // referenced.
2238 Function::BasicBlockListType &BBL =
2239 CurFun.CurrentFunction->getBasicBlockList();
2240 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002241 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002242 };
2243
2244BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencera132e042006-12-03 05:46:11 +00002245 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002246 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002247 }
2248 | RET VOID { // Return with no result...
2249 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002250 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002251 }
2252 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002253 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002254 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002255 $$ = new BranchInst(tmpBB);
Chris Lattner58af2a12006-02-15 07:22:58 +00002256 } // Conditional Branch...
2257 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002258 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002259 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002260 BasicBlock* tmpBBB = getBBVal($9);
2261 CHECK_FOR_ERROR
2262 Value* tmpVal = getVal(Type::BoolTy, $3);
2263 CHECK_FOR_ERROR
2264 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002265 }
2266 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002267 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002268 CHECK_FOR_ERROR
2269 BasicBlock* tmpBB = getBBVal($6);
2270 CHECK_FOR_ERROR
2271 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002272 $$ = S;
2273
2274 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2275 E = $8->end();
2276 for (; I != E; ++I) {
2277 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2278 S->addCase(CI, I->second);
2279 else
Reid Spencer61c83e02006-08-18 08:43:06 +00002280 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002281 }
2282 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002283 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002284 }
2285 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002286 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002287 CHECK_FOR_ERROR
2288 BasicBlock* tmpBB = getBBVal($6);
2289 CHECK_FOR_ERROR
2290 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002291 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002292 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002293 }
2294 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2295 TO LABEL ValueRef UNWIND LABEL ValueRef {
2296 const PointerType *PFTy;
2297 const FunctionType *Ty;
2298
Reid Spencera132e042006-12-03 05:46:11 +00002299 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002300 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2301 // Pull out the types of all of the arguments...
2302 std::vector<const Type*> ParamTypes;
2303 if ($6) {
Reid Spencera132e042006-12-03 05:46:11 +00002304 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner58af2a12006-02-15 07:22:58 +00002305 I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00002306 ParamTypes.push_back((*I)->getType());
Chris Lattner58af2a12006-02-15 07:22:58 +00002307 }
2308
2309 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2310 if (isVarArg) ParamTypes.pop_back();
2311
Reid Spencera132e042006-12-03 05:46:11 +00002312 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattner58af2a12006-02-15 07:22:58 +00002313 PFTy = PointerType::get(Ty);
2314 }
2315
2316 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002317 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002318 BasicBlock *Normal = getBBVal($10);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002319 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002320 BasicBlock *Except = getBBVal($13);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002321 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002322
2323 // Create the call node...
2324 if (!$6) { // Has no arguments?
2325 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
2326 } else { // Has arguments?
2327 // Loop through FunctionType's arguments and ensure they are specified
2328 // correctly!
2329 //
2330 FunctionType::param_iterator I = Ty->param_begin();
2331 FunctionType::param_iterator E = Ty->param_end();
Reid Spencera132e042006-12-03 05:46:11 +00002332 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner58af2a12006-02-15 07:22:58 +00002333
Reid Spencera132e042006-12-03 05:46:11 +00002334 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
2335 if ((*ArgI)->getType() != *I)
2336 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2337 (*I)->getDescription() + "'!");
2338
2339 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
2340 GEN_ERROR("Invalid number of parameters detected!");
2341
2342 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner58af2a12006-02-15 07:22:58 +00002343 }
2344 cast<InvokeInst>($$)->setCallingConv($2);
2345
Reid Spencera132e042006-12-03 05:46:11 +00002346 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002347 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002348 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002349 }
2350 | UNWIND {
2351 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002352 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002353 }
2354 | UNREACHABLE {
2355 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002356 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002357 };
2358
2359
2360
2361JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2362 $$ = $1;
Reid Spencera132e042006-12-03 05:46:11 +00002363 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002364 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002365 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002366 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002367
Reid Spencer5b7e7532006-09-28 19:28:24 +00002368 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002369 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002370 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002371 }
2372 | IntType ConstValueRef ',' LABEL ValueRef {
2373 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002374 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002375 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002376
2377 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002378 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002379
Reid Spencer5b7e7532006-09-28 19:28:24 +00002380 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002381 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002382 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002383 };
2384
2385Inst : OptAssign InstVal {
2386 // Is this definition named?? if so, assign the name...
2387 setValueName($2, $1);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002388 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002389 InsertValue($2);
2390 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002391 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002392};
2393
2394PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
2395 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002396 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002397 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002398 BasicBlock* tmpBB = getBBVal($5);
2399 CHECK_FOR_ERROR
2400 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00002401 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002402 }
2403 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2404 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002405 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002406 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002407 BasicBlock* tmpBB = getBBVal($6);
2408 CHECK_FOR_ERROR
2409 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002410 };
2411
2412
2413ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Reid Spencera132e042006-12-03 05:46:11 +00002414 $$ = new std::vector<Value*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00002415 $$->push_back($1);
2416 }
2417 | ValueRefList ',' ResolvedVal {
2418 $$ = $1;
2419 $1->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002420 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002421 };
2422
2423// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Reid Spencera132e042006-12-03 05:46:11 +00002424ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner58af2a12006-02-15 07:22:58 +00002425
2426OptTailCall : TAIL CALL {
2427 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002428 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002429 }
2430 | CALL {
2431 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002432 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002433 };
2434
Chris Lattner58af2a12006-02-15 07:22:58 +00002435InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00002436 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2437 !isa<PackedType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002438 GEN_ERROR(
Chris Lattner58af2a12006-02-15 07:22:58 +00002439 "Arithmetic operator requires integer, FP, or packed operands!");
Reid Spencera132e042006-12-03 05:46:11 +00002440 if (isa<PackedType>((*$2).get()) &&
2441 ($1 == Instruction::URem ||
2442 $1 == Instruction::SRem ||
2443 $1 == Instruction::FRem))
Reid Spencer3ed469c2006-11-02 20:25:50 +00002444 GEN_ERROR("U/S/FRem not supported on packed types!");
Reid Spencera132e042006-12-03 05:46:11 +00002445 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002446 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002447 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002448 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002449 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002450 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002451 GEN_ERROR("binary operator returned null!");
Reid Spencera132e042006-12-03 05:46:11 +00002452 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002453 }
2454 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00002455 if (!(*$2)->isIntegral()) {
2456 if (!isa<PackedType>($2->get()) ||
2457 !cast<PackedType>($2->get())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00002458 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002459 }
Reid Spencera132e042006-12-03 05:46:11 +00002460 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002461 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002462 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002463 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002464 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002465 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002466 GEN_ERROR("binary operator returned null!");
Reid Spencera132e042006-12-03 05:46:11 +00002467 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002468 }
2469 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00002470 if(isa<PackedType>((*$2).get())) {
Reid Spencer61c83e02006-08-18 08:43:06 +00002471 GEN_ERROR(
Chris Lattner58af2a12006-02-15 07:22:58 +00002472 "PackedTypes currently not supported in setcc instructions!");
2473 }
Reid Spencera132e042006-12-03 05:46:11 +00002474 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002475 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002476 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002477 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002478 $$ = new SetCondInst($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002479 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002480 GEN_ERROR("binary operator returned null!");
Reid Spencera132e042006-12-03 05:46:11 +00002481 delete $2;
2482 }
2483 | ICMP IPredicates Types ValueRef ',' ValueRef {
2484 if (isa<PackedType>((*$3).get()))
2485 GEN_ERROR("Packed types not supported by icmp instruction");
2486 Value* tmpVal1 = getVal(*$3, $4);
2487 CHECK_FOR_ERROR
2488 Value* tmpVal2 = getVal(*$3, $6);
2489 CHECK_FOR_ERROR
2490 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2491 if ($$ == 0)
2492 GEN_ERROR("icmp operator returned null!");
2493 }
2494 | FCMP FPredicates Types ValueRef ',' ValueRef {
2495 if (isa<PackedType>((*$3).get()))
2496 GEN_ERROR("Packed types not supported by fcmp instruction");
2497 Value* tmpVal1 = getVal(*$3, $4);
2498 CHECK_FOR_ERROR
2499 Value* tmpVal2 = getVal(*$3, $6);
2500 CHECK_FOR_ERROR
2501 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2502 if ($$ == 0)
2503 GEN_ERROR("fcmp operator returned null!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002504 }
2505 | NOT ResolvedVal {
Reid Spencer481169e2006-12-01 00:33:46 +00002506 llvm_cerr << "WARNING: Use of eliminated 'not' instruction:"
Chris Lattner58af2a12006-02-15 07:22:58 +00002507 << " Replacing with 'xor'.\n";
2508
Reid Spencera132e042006-12-03 05:46:11 +00002509 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
Chris Lattner58af2a12006-02-15 07:22:58 +00002510 if (Ones == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002511 GEN_ERROR("Expected integral type for not instruction!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002512
Reid Spencera132e042006-12-03 05:46:11 +00002513 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner58af2a12006-02-15 07:22:58 +00002514 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002515 GEN_ERROR("Could not create a xor instruction!");
2516 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002517 }
2518 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002519 if ($4->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002520 GEN_ERROR("Shift amount must be ubyte!");
Reid Spencera132e042006-12-03 05:46:11 +00002521 if (!$2->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00002522 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencer3822ff52006-11-08 06:47:33 +00002523 CHECK_FOR_ERROR;
Reid Spencera132e042006-12-03 05:46:11 +00002524 $$ = new ShiftInst($1, $2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002525 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002526 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002527 | CastOps ResolvedVal TO Types {
Reid Spencera132e042006-12-03 05:46:11 +00002528 Value* Val = $2;
2529 const Type* Ty = $4->get();
Reid Spencer3da59db2006-11-27 01:05:10 +00002530 if (!Val->getType()->isFirstClassType())
2531 GEN_ERROR("cast from a non-primitive type: '" +
2532 Val->getType()->getDescription() + "'!");
2533 if (!Ty->isFirstClassType())
2534 GEN_ERROR("cast to a non-primitive type: '" + Ty->getDescription() +"'!");
Reid Spencera132e042006-12-03 05:46:11 +00002535 $$ = CastInst::create($1, $2, $4->get());
2536 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002537 }
2538 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002539 if ($2->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002540 GEN_ERROR("select condition must be boolean!");
Reid Spencera132e042006-12-03 05:46:11 +00002541 if ($4->getType() != $6->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002542 GEN_ERROR("select value types should match!");
Reid Spencera132e042006-12-03 05:46:11 +00002543 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002544 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002545 }
2546 | VAARG ResolvedVal ',' Types {
2547 NewVarArgs = true;
Reid Spencera132e042006-12-03 05:46:11 +00002548 $$ = new VAArgInst($2, *$4);
2549 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002550 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002551 }
2552 | VAARG_old ResolvedVal ',' Types {
2553 ObsoleteVarArgs = true;
Reid Spencera132e042006-12-03 05:46:11 +00002554 const Type* ArgTy = $2->getType();
Chris Lattner58af2a12006-02-15 07:22:58 +00002555 Function* NF = CurModule.CurrentModule->
2556 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
2557
2558 //b = vaarg a, t ->
2559 //foo = alloca 1 of t
2560 //bar = vacopy a
2561 //store bar -> foo
2562 //b = vaarg foo, t
2563 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2564 CurBB->getInstList().push_back(foo);
Reid Spencera132e042006-12-03 05:46:11 +00002565 CallInst* bar = new CallInst(NF, $2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002566 CurBB->getInstList().push_back(bar);
2567 CurBB->getInstList().push_back(new StoreInst(bar, foo));
Reid Spencera132e042006-12-03 05:46:11 +00002568 $$ = new VAArgInst(foo, *$4);
2569 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002570 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002571 }
2572 | VANEXT_old ResolvedVal ',' Types {
2573 ObsoleteVarArgs = true;
Reid Spencera132e042006-12-03 05:46:11 +00002574 const Type* ArgTy = $2->getType();
Chris Lattner58af2a12006-02-15 07:22:58 +00002575 Function* NF = CurModule.CurrentModule->
2576 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
2577
2578 //b = vanext a, t ->
2579 //foo = alloca 1 of t
2580 //bar = vacopy a
2581 //store bar -> foo
2582 //tmp = vaarg foo, t
2583 //b = load foo
2584 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2585 CurBB->getInstList().push_back(foo);
Reid Spencera132e042006-12-03 05:46:11 +00002586 CallInst* bar = new CallInst(NF, $2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002587 CurBB->getInstList().push_back(bar);
2588 CurBB->getInstList().push_back(new StoreInst(bar, foo));
Reid Spencera132e042006-12-03 05:46:11 +00002589 Instruction* tmp = new VAArgInst(foo, *$4);
Chris Lattner58af2a12006-02-15 07:22:58 +00002590 CurBB->getInstList().push_back(tmp);
2591 $$ = new LoadInst(foo);
Reid Spencera132e042006-12-03 05:46:11 +00002592 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002593 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002594 }
2595 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002596 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencer61c83e02006-08-18 08:43:06 +00002597 GEN_ERROR("Invalid extractelement operands!");
Reid Spencera132e042006-12-03 05:46:11 +00002598 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002599 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002600 }
2601 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002602 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002603 GEN_ERROR("Invalid insertelement operands!");
Reid Spencera132e042006-12-03 05:46:11 +00002604 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002605 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002606 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00002607 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002608 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002609 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencera132e042006-12-03 05:46:11 +00002610 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002611 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00002612 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002613 | PHI_TOK PHIList {
2614 const Type *Ty = $2->front().first->getType();
2615 if (!Ty->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002616 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002617 $$ = new PHINode(Ty);
2618 ((PHINode*)$$)->reserveOperandSpace($2->size());
2619 while ($2->begin() != $2->end()) {
2620 if ($2->front().first->getType() != Ty)
Reid Spencer61c83e02006-08-18 08:43:06 +00002621 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002622 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2623 $2->pop_front();
2624 }
2625 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002626 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002627 }
2628 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer3da59db2006-11-27 01:05:10 +00002629 const PointerType *PFTy = 0;
2630 const FunctionType *Ty = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +00002631
Reid Spencera132e042006-12-03 05:46:11 +00002632 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002633 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2634 // Pull out the types of all of the arguments...
2635 std::vector<const Type*> ParamTypes;
2636 if ($6) {
Reid Spencera132e042006-12-03 05:46:11 +00002637 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner58af2a12006-02-15 07:22:58 +00002638 I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00002639 ParamTypes.push_back((*I)->getType());
Chris Lattner58af2a12006-02-15 07:22:58 +00002640 }
2641
2642 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2643 if (isVarArg) ParamTypes.pop_back();
2644
Reid Spencera132e042006-12-03 05:46:11 +00002645 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002646 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002647
Reid Spencera132e042006-12-03 05:46:11 +00002648 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattner58af2a12006-02-15 07:22:58 +00002649 PFTy = PointerType::get(Ty);
2650 }
2651
2652 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002653 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002654
2655 // Create the call node...
2656 if (!$6) { // Has no arguments?
2657 // Make sure no arguments is a good thing!
2658 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002659 GEN_ERROR("No arguments passed to a function that "
Chris Lattner58af2a12006-02-15 07:22:58 +00002660 "expects arguments!");
2661
2662 $$ = new CallInst(V, std::vector<Value*>());
2663 } else { // Has arguments?
2664 // Loop through FunctionType's arguments and ensure they are specified
2665 // correctly!
2666 //
2667 FunctionType::param_iterator I = Ty->param_begin();
2668 FunctionType::param_iterator E = Ty->param_end();
Reid Spencera132e042006-12-03 05:46:11 +00002669 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner58af2a12006-02-15 07:22:58 +00002670
Reid Spencera132e042006-12-03 05:46:11 +00002671 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
2672 if ((*ArgI)->getType() != *I)
2673 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2674 (*I)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002675
2676 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002677 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002678
Reid Spencera132e042006-12-03 05:46:11 +00002679 $$ = new CallInst(V, *$6);
Chris Lattner58af2a12006-02-15 07:22:58 +00002680 }
2681 cast<CallInst>($$)->setTailCall($1);
2682 cast<CallInst>($$)->setCallingConv($2);
Reid Spencera132e042006-12-03 05:46:11 +00002683 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002684 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002685 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002686 }
2687 | MemoryInst {
2688 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002689 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002690 };
2691
2692
2693// IndexList - List of indices for GEP based instructions...
2694IndexList : ',' ValueRefList {
2695 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002696 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002697 } | /* empty */ {
Reid Spencera132e042006-12-03 05:46:11 +00002698 $$ = new std::vector<Value*>();
Reid Spencer61c83e02006-08-18 08:43:06 +00002699 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002700 };
2701
2702OptVolatile : VOLATILE {
2703 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002704 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002705 }
2706 | /* empty */ {
2707 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002708 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002709 };
2710
2711
2712
2713MemoryInst : MALLOC Types OptCAlign {
Reid Spencera132e042006-12-03 05:46:11 +00002714 $$ = new MallocInst(*$2, 0, $3);
2715 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002716 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002717 }
2718 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencera132e042006-12-03 05:46:11 +00002719 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002720 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002721 $$ = new MallocInst(*$2, tmpVal, $6);
2722 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002723 }
2724 | ALLOCA Types OptCAlign {
Reid Spencera132e042006-12-03 05:46:11 +00002725 $$ = new AllocaInst(*$2, 0, $3);
2726 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002727 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002728 }
2729 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencera132e042006-12-03 05:46:11 +00002730 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002731 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002732 $$ = new AllocaInst(*$2, tmpVal, $6);
2733 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002734 }
2735 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002736 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002737 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencera132e042006-12-03 05:46:11 +00002738 $2->getType()->getDescription() + "!");
2739 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002740 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002741 }
2742
2743 | OptVolatile LOAD Types ValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00002744 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002745 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00002746 (*$3)->getDescription());
2747 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002748 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00002749 (*$3)->getDescription());
2750 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002751 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002752 $$ = new LoadInst(tmpVal, "", $1);
Reid Spencera132e042006-12-03 05:46:11 +00002753 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002754 }
2755 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00002756 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00002757 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00002758 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00002759 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002760 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00002761 if (ElTy != $3->getType())
2762 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Chris Lattner58af2a12006-02-15 07:22:58 +00002763 "' into space of type '" + ElTy->getDescription() + "'!");
2764
Reid Spencera132e042006-12-03 05:46:11 +00002765 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002766 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002767 $$ = new StoreInst($3, tmpVal, $1);
2768 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002769 }
2770 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencera132e042006-12-03 05:46:11 +00002771 if (!isa<PointerType>($2->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002772 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002773
Reid Spencera132e042006-12-03 05:46:11 +00002774 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer61c83e02006-08-18 08:43:06 +00002775 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002776 (*$2)->getDescription()+ "'!");
2777 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002778 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002779 $$ = new GetElementPtrInst(tmpVal, *$4);
2780 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002781 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002782 };
2783
2784
2785%%
Reid Spencer61c83e02006-08-18 08:43:06 +00002786
2787void llvm::GenerateError(const std::string &message, int LineNo) {
2788 if (LineNo == -1) LineNo = llvmAsmlineno;
2789 // TODO: column number in exception
2790 if (TheParseError)
2791 TheParseError->setError(CurFilename, message, LineNo);
2792 TriggerError = 1;
2793}
2794
Chris Lattner58af2a12006-02-15 07:22:58 +00002795int yyerror(const char *ErrorMsg) {
2796 std::string where
2797 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2798 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2799 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2800 if (yychar == YYEMPTY || yychar == 0)
2801 errMsg += "end-of-file.";
2802 else
2803 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00002804 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00002805 return 0;
2806}