blob: 51fe7c9cc5c707af04549bba38ae1003124a48d5 [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
Chris Lattner58af2a12006-02-15 07:22:58 +000067static bool NewVarArgs;
Chris Lattner58af2a12006-02-15 07:22:58 +000068static GlobalVariable *CurGV;
69
70
71// This contains info used when building the body of a function. It is
72// destroyed when the function is completed.
73//
74typedef std::vector<Value *> ValueList; // Numbered defs
75static void
76ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
77 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
78
79static struct PerModuleInfo {
80 Module *CurrentModule;
81 std::map<const Type *, ValueList> Values; // Module level numbered definitions
82 std::map<const Type *,ValueList> LateResolveValues;
Reid Spencer861d9d62006-11-28 07:29:44 +000083 std::vector<PATypeHolder> Types;
84 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner58af2a12006-02-15 07:22:58 +000085
86 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner0ad19702006-06-21 16:53:00 +000087 /// how they were referenced and on which line of the input they came from so
Chris Lattner58af2a12006-02-15 07:22:58 +000088 /// that we can resolve them later and print error messages as appropriate.
89 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
90
91 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
92 // references to global values. Global values may be referenced before they
93 // are defined, and if so, the temporary object that they represent is held
94 // here. This is used for forward references of GlobalValues.
95 //
96 typedef std::map<std::pair<const PointerType *,
97 ValID>, GlobalValue*> GlobalRefsType;
98 GlobalRefsType GlobalRefs;
99
100 void ModuleDone() {
101 // If we could not resolve some functions at function compilation time
102 // (calls to functions before they are defined), resolve them now... Types
103 // are resolved when the constant pool has been completely parsed.
104 //
105 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000106 if (TriggerError)
107 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000108
109 // Check to make sure that all global value forward references have been
110 // resolved!
111 //
112 if (!GlobalRefs.empty()) {
113 std::string UndefinedReferences = "Unresolved global references exist:\n";
114
115 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
116 I != E; ++I) {
117 UndefinedReferences += " " + I->first.first->getDescription() + " " +
118 I->first.second.getName() + "\n";
119 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000120 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000121 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000122 }
123
Chris Lattner58af2a12006-02-15 07:22:58 +0000124 Values.clear(); // Clear out function local definitions
125 Types.clear();
126 CurrentModule = 0;
127 }
128
129 // GetForwardRefForGlobal - Check to see if there is a forward reference
130 // for this global. If so, remove it from the GlobalRefs map and return it.
131 // If not, just return null.
132 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
133 // Check to see if there is a forward reference to this global variable...
134 // if there is, eliminate it and patch the reference to use the new def'n.
135 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
136 GlobalValue *Ret = 0;
137 if (I != GlobalRefs.end()) {
138 Ret = I->second;
139 GlobalRefs.erase(I);
140 }
141 return Ret;
142 }
143} CurModule;
144
145static struct PerFunctionInfo {
146 Function *CurrentFunction; // Pointer to current function being created
147
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000148 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattner58af2a12006-02-15 07:22:58 +0000149 std::map<const Type*, ValueList> LateResolveValues;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000150 bool isDeclare; // Is this function a forward declararation?
151 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Chris Lattner58af2a12006-02-15 07:22:58 +0000152
153 /// BBForwardRefs - When we see forward references to basic blocks, keep
154 /// track of them here.
155 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
156 std::vector<BasicBlock*> NumberedBlocks;
157 unsigned NextBBNum;
158
159 inline PerFunctionInfo() {
160 CurrentFunction = 0;
161 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000162 Linkage = GlobalValue::ExternalLinkage;
Chris Lattner58af2a12006-02-15 07:22:58 +0000163 }
164
165 inline void FunctionStart(Function *M) {
166 CurrentFunction = M;
167 NextBBNum = 0;
168 }
169
170 void FunctionDone() {
171 NumberedBlocks.clear();
172
173 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000174 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000175 GenerateError("Undefined reference to label " +
Chris Lattner58af2a12006-02-15 07:22:58 +0000176 BBForwardRefs.begin()->first->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000177 return;
178 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000179
180 // Resolve all forward references now.
181 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
182
183 Values.clear(); // Clear out function local definitions
184 CurrentFunction = 0;
185 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000186 Linkage = GlobalValue::ExternalLinkage;
Chris Lattner58af2a12006-02-15 07:22:58 +0000187 }
188} CurFun; // Info for the current function...
189
190static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
191
192
193//===----------------------------------------------------------------------===//
194// Code to handle definitions of all the types
195//===----------------------------------------------------------------------===//
196
197static int InsertValue(Value *V,
198 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
199 if (V->hasName()) return -1; // Is this a numbered definition?
200
201 // Yes, insert the value into the value table...
202 ValueList &List = ValueTab[V->getType()];
203 List.push_back(V);
204 return List.size()-1;
205}
206
207static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
208 switch (D.Type) {
209 case ValID::NumberVal: // Is it a numbered definition?
210 // Module constants occupy the lowest numbered slots...
211 if ((unsigned)D.Num < CurModule.Types.size())
Reid Spencer861d9d62006-11-28 07:29:44 +0000212 return CurModule.Types[(unsigned)D.Num];
Chris Lattner58af2a12006-02-15 07:22:58 +0000213 break;
214 case ValID::NameVal: // Is it a named definition?
215 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
216 D.destroy(); // Free old strdup'd memory...
217 return N;
218 }
219 break;
220 default:
Reid Spencer61c83e02006-08-18 08:43:06 +0000221 GenerateError("Internal parser error: Invalid symbol type reference!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000222 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000223 }
224
225 // If we reached here, we referenced either a symbol that we don't know about
226 // or an id number that hasn't been read yet. We may be referencing something
227 // forward, so just create an entry to be resolved later and get to it...
228 //
229 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
230
231
232 if (inFunctionScope()) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000233 if (D.Type == ValID::NameVal) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000234 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000235 return 0;
236 } else {
Reid Spencer61c83e02006-08-18 08:43:06 +0000237 GenerateError("Reference to an undefined type: #" + itostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000238 return 0;
239 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000240 }
241
Reid Spencer861d9d62006-11-28 07:29:44 +0000242 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
Chris Lattner58af2a12006-02-15 07:22:58 +0000243 if (I != CurModule.LateResolveTypes.end())
Reid Spencer861d9d62006-11-28 07:29:44 +0000244 return I->second;
Chris Lattner58af2a12006-02-15 07:22:58 +0000245
Reid Spencer861d9d62006-11-28 07:29:44 +0000246 Type *Typ = OpaqueType::get();
247 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
248 return Typ;
Reid Spencera132e042006-12-03 05:46:11 +0000249 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000250
251static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
252 SymbolTable &SymTab =
253 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
254 CurModule.CurrentModule->getSymbolTable();
255 return SymTab.lookup(Ty, Name);
256}
257
258// getValNonImprovising - Look up the value specified by the provided type and
259// the provided ValID. If the value exists and has already been defined, return
260// it. Otherwise return null.
261//
262static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000263 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000264 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000265 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000266 return 0;
267 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000268
269 switch (D.Type) {
270 case ValID::NumberVal: { // Is it a numbered definition?
271 unsigned Num = (unsigned)D.Num;
272
273 // Module constants occupy the lowest numbered slots...
274 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
275 if (VI != CurModule.Values.end()) {
276 if (Num < VI->second.size())
277 return VI->second[Num];
278 Num -= VI->second.size();
279 }
280
281 // Make sure that our type is within bounds
282 VI = CurFun.Values.find(Ty);
283 if (VI == CurFun.Values.end()) return 0;
284
285 // Check that the number is within bounds...
286 if (VI->second.size() <= Num) return 0;
287
288 return VI->second[Num];
289 }
290
291 case ValID::NameVal: { // Is it a named definition?
292 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
293 if (N == 0) return 0;
294
295 D.destroy(); // Free old strdup'd memory...
296 return N;
297 }
298
299 // Check to make sure that "Ty" is an integral type, and that our
300 // value will fit into the specified type...
301 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000302 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000303 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000304 itostr(D.ConstPool64) + "' is invalid for type '" +
305 Ty->getDescription() + "'!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000306 return 0;
307 }
Reid Spencerb83eb642006-10-20 07:07:24 +0000308 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000309
310 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000311 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
312 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000313 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattner58af2a12006-02-15 07:22:58 +0000314 "' is invalid or out of range!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000315 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000316 } else { // This is really a signed reference. Transmogrify.
Reid Spencerb83eb642006-10-20 07:07:24 +0000317 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000318 }
319 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000320 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000321 }
322
323 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000324 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000325 GenerateError("FP constant invalid for type!!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000326 return 0;
327 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000328 return ConstantFP::get(Ty, D.ConstPoolFP);
329
330 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000331 if (!isa<PointerType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000332 GenerateError("Cannot create a a non pointer null!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000333 return 0;
334 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000335 return ConstantPointerNull::get(cast<PointerType>(Ty));
336
337 case ValID::ConstUndefVal: // Is it an undef value?
338 return UndefValue::get(Ty);
339
340 case ValID::ConstZeroVal: // Is it a zero value?
341 return Constant::getNullValue(Ty);
342
343 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000344 if (D.ConstantValue->getType() != Ty) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000345 GenerateError("Constant expression type different from required type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000346 return 0;
347 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000348 return D.ConstantValue;
349
350 case ValID::InlineAsmVal: { // Inline asm expression
351 const PointerType *PTy = dyn_cast<PointerType>(Ty);
352 const FunctionType *FTy =
353 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000354 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000355 GenerateError("Invalid type for asm constraint string!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000356 return 0;
357 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000358 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
359 D.IAD->HasSideEffects);
360 D.destroy(); // Free InlineAsmDescriptor.
361 return IA;
362 }
363 default:
364 assert(0 && "Unhandled case!");
365 return 0;
366 } // End of switch
367
368 assert(0 && "Unhandled case!");
369 return 0;
370}
371
372// getVal - This function is identical to getValNonImprovising, except that if a
373// value is not already defined, it "improvises" by creating a placeholder var
374// that looks and acts just like the requested variable. When the value is
375// defined later, all uses of the placeholder variable are replaced with the
376// real thing.
377//
378static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000379 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000380 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000381 return 0;
382 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000383
384 // See if the value has already been defined.
385 Value *V = getValNonImprovising(Ty, ID);
386 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000387 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000388
Reid Spencer5b7e7532006-09-28 19:28:24 +0000389 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000390 GenerateError("Invalid use of a composite type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000391 return 0;
392 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000393
394 // If we reached here, we referenced either a symbol that we don't know about
395 // or an id number that hasn't been read yet. We may be referencing something
396 // forward, so just create an entry to be resolved later and get to it...
397 //
398 V = new Argument(Ty);
399
400 // Remember where this forward reference came from. FIXME, shouldn't we try
401 // to recycle these things??
402 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
403 llvmAsmlineno)));
404
405 if (inFunctionScope())
406 InsertValue(V, CurFun.LateResolveValues);
407 else
408 InsertValue(V, CurModule.LateResolveValues);
409 return V;
410}
411
412/// getBBVal - This is used for two purposes:
413/// * If isDefinition is true, a new basic block with the specified ID is being
414/// defined.
415/// * If isDefinition is true, this is a reference to a basic block, which may
416/// or may not be a forward reference.
417///
418static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
419 assert(inFunctionScope() && "Can't get basic block at global scope!");
420
421 std::string Name;
422 BasicBlock *BB = 0;
423 switch (ID.Type) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000424 default:
425 GenerateError("Illegal label reference " + ID.getName());
426 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000427 case ValID::NumberVal: // Is it a numbered definition?
428 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
429 CurFun.NumberedBlocks.resize(ID.Num+1);
430 BB = CurFun.NumberedBlocks[ID.Num];
431 break;
432 case ValID::NameVal: // Is it a named definition?
433 Name = ID.Name;
434 if (Value *N = CurFun.CurrentFunction->
435 getSymbolTable().lookup(Type::LabelTy, Name))
436 BB = cast<BasicBlock>(N);
437 break;
438 }
439
440 // See if the block has already been defined.
441 if (BB) {
442 // If this is the definition of the block, make sure the existing value was
443 // just a forward reference. If it was a forward reference, there will be
444 // an entry for it in the PlaceHolderInfo map.
Reid Spencer5b7e7532006-09-28 19:28:24 +0000445 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000446 // The existing value was a definition, not a forward reference.
Reid Spencer61c83e02006-08-18 08:43:06 +0000447 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000448 return 0;
449 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000450
451 ID.destroy(); // Free strdup'd memory.
452 return BB;
453 }
454
455 // Otherwise this block has not been seen before.
456 BB = new BasicBlock("", CurFun.CurrentFunction);
457 if (ID.Type == ValID::NameVal) {
458 BB->setName(ID.Name);
459 } else {
460 CurFun.NumberedBlocks[ID.Num] = BB;
461 }
462
463 // If this is not a definition, keep track of it so we can use it as a forward
464 // reference.
465 if (!isDefinition) {
466 // Remember where this forward reference came from.
467 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
468 } else {
469 // The forward declaration could have been inserted anywhere in the
470 // function: insert it into the correct place now.
471 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
472 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
473 }
474 ID.destroy();
475 return BB;
476}
477
478
479//===----------------------------------------------------------------------===//
480// Code to handle forward references in instructions
481//===----------------------------------------------------------------------===//
482//
483// This code handles the late binding needed with statements that reference
484// values not defined yet... for example, a forward branch, or the PHI node for
485// a loop body.
486//
487// This keeps a table (CurFun.LateResolveValues) of all such forward references
488// and back patchs after we are done.
489//
490
491// ResolveDefinitions - If we could not resolve some defs at parsing
492// time (forward branches, phi functions for loops, etc...) resolve the
493// defs now...
494//
495static void
496ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
497 std::map<const Type*,ValueList> *FutureLateResolvers) {
498 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
499 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
500 E = LateResolvers.end(); LRI != E; ++LRI) {
501 ValueList &List = LRI->second;
502 while (!List.empty()) {
503 Value *V = List.back();
504 List.pop_back();
505
506 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
507 CurModule.PlaceHolderInfo.find(V);
508 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
509
510 ValID &DID = PHI->second.first;
511
512 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000513 if (TriggerError)
514 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000515 if (TheRealValue) {
516 V->replaceAllUsesWith(TheRealValue);
517 delete V;
518 CurModule.PlaceHolderInfo.erase(PHI);
519 } else if (FutureLateResolvers) {
520 // Functions have their unresolved items forwarded to the module late
521 // resolver table
522 InsertValue(V, *FutureLateResolvers);
523 } else {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000524 if (DID.Type == ValID::NameVal) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000525 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Chris Lattner58af2a12006-02-15 07:22:58 +0000526 "' of type '" + V->getType()->getDescription() + "'",
527 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000528 return;
529 } else {
Reid Spencer61c83e02006-08-18 08:43:06 +0000530 GenerateError("Reference to an invalid definition: #" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000531 itostr(DID.Num) + " of type '" +
532 V->getType()->getDescription() + "'",
533 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000534 return;
535 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000536 }
537 }
538 }
539
540 LateResolvers.clear();
541}
542
543// ResolveTypeTo - A brand new type was just declared. This means that (if
544// name is not null) things referencing Name can be resolved. Otherwise, things
545// refering to the number can be resolved. Do this now.
546//
547static void ResolveTypeTo(char *Name, const Type *ToTy) {
548 ValID D;
549 if (Name) D = ValID::create(Name);
550 else D = ValID::create((int)CurModule.Types.size());
551
Reid Spencer861d9d62006-11-28 07:29:44 +0000552 std::map<ValID, PATypeHolder>::iterator I =
Chris Lattner58af2a12006-02-15 07:22:58 +0000553 CurModule.LateResolveTypes.find(D);
554 if (I != CurModule.LateResolveTypes.end()) {
Reid Spencer861d9d62006-11-28 07:29:44 +0000555 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner58af2a12006-02-15 07:22:58 +0000556 CurModule.LateResolveTypes.erase(I);
557 }
558}
559
560// setValueName - Set the specified value to the name given. The name may be
561// null potentially, in which case this is a noop. The string passed in is
562// assumed to be a malloc'd string buffer, and is free'd by this function.
563//
564static void setValueName(Value *V, char *NameStr) {
565 if (NameStr) {
566 std::string Name(NameStr); // Copy string
567 free(NameStr); // Free old string
568
Reid Spencer5b7e7532006-09-28 19:28:24 +0000569 if (V->getType() == Type::VoidTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000570 GenerateError("Can't assign name '" + Name+"' to value with void type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000571 return;
572 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000573
574 assert(inFunctionScope() && "Must be in function scope!");
575 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
Reid Spencer5b7e7532006-09-28 19:28:24 +0000576 if (ST.lookup(V->getType(), Name)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000577 GenerateError("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000578 V->getType()->getDescription() + "' type plane!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000579 return;
580 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000581
582 // Set the name.
583 V->setName(Name);
584 }
585}
586
587/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
588/// this is a declaration, otherwise it is a definition.
589static GlobalVariable *
590ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
591 bool isConstantGlobal, const Type *Ty,
592 Constant *Initializer) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000593 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000594 GenerateError("Cannot declare global vars of function type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000595 return 0;
596 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000597
598 const PointerType *PTy = PointerType::get(Ty);
599
600 std::string Name;
601 if (NameStr) {
602 Name = NameStr; // Copy string
603 free(NameStr); // Free old string
604 }
605
606 // See if this global value was forward referenced. If so, recycle the
607 // object.
608 ValID ID;
609 if (!Name.empty()) {
610 ID = ValID::create((char*)Name.c_str());
611 } else {
612 ID = ValID::create((int)CurModule.Values[PTy].size());
613 }
614
615 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
616 // Move the global to the end of the list, from whereever it was
617 // previously inserted.
618 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
619 CurModule.CurrentModule->getGlobalList().remove(GV);
620 CurModule.CurrentModule->getGlobalList().push_back(GV);
621 GV->setInitializer(Initializer);
622 GV->setLinkage(Linkage);
623 GV->setConstant(isConstantGlobal);
624 InsertValue(GV, CurModule.Values);
625 return GV;
626 }
627
628 // If this global has a name, check to see if there is already a definition
629 // of this global in the module. If so, merge as appropriate. Note that
630 // this is really just a hack around problems in the CFE. :(
631 if (!Name.empty()) {
632 // We are a simple redefinition of a value, check to see if it is defined
633 // the same as the old one.
634 if (GlobalVariable *EGV =
635 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
636 // We are allowed to redefine a global variable in two circumstances:
637 // 1. If at least one of the globals is uninitialized or
638 // 2. If both initializers have the same value.
639 //
640 if (!EGV->hasInitializer() || !Initializer ||
641 EGV->getInitializer() == Initializer) {
642
643 // Make sure the existing global version gets the initializer! Make
644 // sure that it also gets marked const if the new version is.
645 if (Initializer && !EGV->hasInitializer())
646 EGV->setInitializer(Initializer);
647 if (isConstantGlobal)
648 EGV->setConstant(true);
649 EGV->setLinkage(Linkage);
650 return EGV;
651 }
652
Reid Spencer61c83e02006-08-18 08:43:06 +0000653 GenerateError("Redefinition of global variable named '" + Name +
Chris Lattner58af2a12006-02-15 07:22:58 +0000654 "' in the '" + Ty->getDescription() + "' type plane!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000655 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000656 }
657 }
658
659 // Otherwise there is no existing GV to use, create one now.
660 GlobalVariable *GV =
661 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
662 CurModule.CurrentModule);
663 InsertValue(GV, CurModule.Values);
664 return GV;
665}
666
667// setTypeName - Set the specified type to the name given. The name may be
668// null potentially, in which case this is a noop. The string passed in is
669// assumed to be a malloc'd string buffer, and is freed by this function.
670//
671// This function returns true if the type has already been defined, but is
672// allowed to be redefined in the specified context. If the name is a new name
673// for the type plane, it is inserted and false is returned.
674static bool setTypeName(const Type *T, char *NameStr) {
675 assert(!inFunctionScope() && "Can't give types function-local names!");
676 if (NameStr == 0) return false;
677
678 std::string Name(NameStr); // Copy string
679 free(NameStr); // Free old string
680
681 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000682 if (T == Type::VoidTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000683 GenerateError("Can't assign name '" + Name + "' to the void type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000684 return false;
685 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000686
687 // Set the type name, checking for conflicts as we do so.
688 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
689
690 if (AlreadyExists) { // Inserting a name that is already defined???
691 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
692 assert(Existing && "Conflict but no matching type?");
693
694 // There is only one case where this is allowed: when we are refining an
695 // opaque type. In this case, Existing will be an opaque type.
696 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
697 // We ARE replacing an opaque type!
698 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
699 return true;
700 }
701
702 // Otherwise, this is an attempt to redefine a type. That's okay if
703 // the redefinition is identical to the original. This will be so if
704 // Existing and T point to the same Type object. In this one case we
705 // allow the equivalent redefinition.
706 if (Existing == T) return true; // Yes, it's equal.
707
708 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer61c83e02006-08-18 08:43:06 +0000709 GenerateError("Redefinition of type named '" + Name + "' in the '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000710 T->getDescription() + "' type plane!");
711 }
712
713 return false;
714}
715
716//===----------------------------------------------------------------------===//
717// Code for handling upreferences in type names...
718//
719
720// TypeContains - Returns true if Ty directly contains E in it.
721//
722static bool TypeContains(const Type *Ty, const Type *E) {
723 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
724 E) != Ty->subtype_end();
725}
726
727namespace {
728 struct UpRefRecord {
729 // NestingLevel - The number of nesting levels that need to be popped before
730 // this type is resolved.
731 unsigned NestingLevel;
732
733 // LastContainedTy - This is the type at the current binding level for the
734 // type. Every time we reduce the nesting level, this gets updated.
735 const Type *LastContainedTy;
736
737 // UpRefTy - This is the actual opaque type that the upreference is
738 // represented with.
739 OpaqueType *UpRefTy;
740
741 UpRefRecord(unsigned NL, OpaqueType *URTy)
742 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
743 };
744}
745
746// UpRefs - A list of the outstanding upreferences that need to be resolved.
747static std::vector<UpRefRecord> UpRefs;
748
749/// HandleUpRefs - Every time we finish a new layer of types, this function is
750/// called. It loops through the UpRefs vector, which is a list of the
751/// currently active types. For each type, if the up reference is contained in
752/// the newly completed type, we decrement the level count. When the level
753/// count reaches zero, the upreferenced type is the type that is passed in:
754/// thus we can complete the cycle.
755///
756static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000757 // If Ty isn't abstract, or if there are no up-references in it, then there is
758 // nothing to resolve here.
759 if (!ty->isAbstract() || UpRefs.empty()) return ty;
760
Chris Lattner58af2a12006-02-15 07:22:58 +0000761 PATypeHolder Ty(ty);
762 UR_OUT("Type '" << Ty->getDescription() <<
763 "' newly formed. Resolving upreferences.\n" <<
764 UpRefs.size() << " upreferences active!\n");
765
766 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
767 // to zero), we resolve them all together before we resolve them to Ty. At
768 // the end of the loop, if there is anything to resolve to Ty, it will be in
769 // this variable.
770 OpaqueType *TypeToResolve = 0;
771
772 for (unsigned i = 0; i != UpRefs.size(); ++i) {
773 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
774 << UpRefs[i].second->getDescription() << ") = "
775 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
776 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
777 // Decrement level of upreference
778 unsigned Level = --UpRefs[i].NestingLevel;
779 UpRefs[i].LastContainedTy = Ty;
780 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
781 if (Level == 0) { // Upreference should be resolved!
782 if (!TypeToResolve) {
783 TypeToResolve = UpRefs[i].UpRefTy;
784 } else {
785 UR_OUT(" * Resolving upreference for "
786 << UpRefs[i].second->getDescription() << "\n";
787 std::string OldName = UpRefs[i].UpRefTy->getDescription());
788 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
789 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
790 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
791 }
792 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
793 --i; // Do not skip the next element...
794 }
795 }
796 }
797
798 if (TypeToResolve) {
799 UR_OUT(" * Resolving upreference for "
800 << UpRefs[i].second->getDescription() << "\n";
801 std::string OldName = TypeToResolve->getDescription());
802 TypeToResolve->refineAbstractTypeTo(Ty);
803 }
804
805 return Ty;
806}
807
Chris Lattner58af2a12006-02-15 07:22:58 +0000808// common code from the two 'RunVMAsmParser' functions
Reid Spencer5b7e7532006-09-28 19:28:24 +0000809static Module* RunParser(Module * M) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000810
811 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner58af2a12006-02-15 07:22:58 +0000812 NewVarArgs = false;
Chris Lattner58af2a12006-02-15 07:22:58 +0000813 CurModule.CurrentModule = M;
Reid Spencerf63697d2006-10-09 17:36:59 +0000814
815 // Check to make sure the parser succeeded
816 if (yyparse()) {
817 if (ParserResult)
818 delete ParserResult;
819 return 0;
820 }
821
822 // Check to make sure that parsing produced a result
Reid Spencer61c83e02006-08-18 08:43:06 +0000823 if (!ParserResult)
824 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000825
Reid Spencerf63697d2006-10-09 17:36:59 +0000826 // Reset ParserResult variable while saving its value for the result.
Chris Lattner58af2a12006-02-15 07:22:58 +0000827 Module *Result = ParserResult;
828 ParserResult = 0;
829
Chris Lattner58af2a12006-02-15 07:22:58 +0000830 return Result;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000831}
Chris Lattner58af2a12006-02-15 07:22:58 +0000832
833//===----------------------------------------------------------------------===//
834// RunVMAsmParser - Define an interface to this parser
835//===----------------------------------------------------------------------===//
836//
837Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
838 set_scan_file(F);
839
840 CurFilename = Filename;
841 return RunParser(new Module(CurFilename));
842}
843
844Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
845 set_scan_string(AsmString);
846
847 CurFilename = "from_memory";
848 if (M == NULL) {
849 return RunParser(new Module (CurFilename));
850 } else {
851 return RunParser(M);
852 }
853}
854
855%}
856
857%union {
858 llvm::Module *ModuleVal;
859 llvm::Function *FunctionVal;
Reid Spencera132e042006-12-03 05:46:11 +0000860 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000861 llvm::BasicBlock *BasicBlockVal;
862 llvm::TerminatorInst *TermInstVal;
863 llvm::Instruction *InstVal;
Reid Spencera132e042006-12-03 05:46:11 +0000864 llvm::Constant *ConstVal;
Chris Lattner58af2a12006-02-15 07:22:58 +0000865
Reid Spencera132e042006-12-03 05:46:11 +0000866 const llvm::Type *PrimType;
867 llvm::PATypeHolder *TypeVal;
868 llvm::Value *ValueVal;
869
870 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
871 std::vector<llvm::Value*> *ValueList;
872 std::list<llvm::PATypeHolder> *TypeList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000873 // Represent the RHS of PHI node
Reid Spencera132e042006-12-03 05:46:11 +0000874 std::list<std::pair<llvm::Value*,
875 llvm::BasicBlock*> > *PHIList;
Chris Lattner58af2a12006-02-15 07:22:58 +0000876 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
Reid Spencera132e042006-12-03 05:46:11 +0000877 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner58af2a12006-02-15 07:22:58 +0000878
879 llvm::GlobalValue::LinkageTypes Linkage;
880 int64_t SInt64Val;
881 uint64_t UInt64Val;
882 int SIntVal;
883 unsigned UIntVal;
884 double FPVal;
885 bool BoolVal;
886
887 char *StrVal; // This memory is strdup'd!
Reid Spencer1628cec2006-10-26 06:15:43 +0000888 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner58af2a12006-02-15 07:22:58 +0000889
Reid Spencera132e042006-12-03 05:46:11 +0000890 llvm::Instruction::BinaryOps BinaryOpVal;
891 llvm::Instruction::TermOps TermOpVal;
892 llvm::Instruction::MemoryOps MemOpVal;
893 llvm::Instruction::CastOps CastOpVal;
894 llvm::Instruction::OtherOps OtherOpVal;
Reid Spencer1628cec2006-10-26 06:15:43 +0000895 llvm::Module::Endianness Endianness;
Reid Spencera132e042006-12-03 05:46:11 +0000896 llvm::ICmpInst::Predicate IPredicate;
897 llvm::FCmpInst::Predicate FPredicate;
Chris Lattner58af2a12006-02-15 07:22:58 +0000898}
899
900%type <ModuleVal> Module FunctionList
901%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
902%type <BasicBlockVal> BasicBlock InstructionList
903%type <TermInstVal> BBTerminatorInst
904%type <InstVal> Inst InstVal MemoryInst
905%type <ConstVal> ConstVal ConstExpr
906%type <ConstVector> ConstVector
907%type <ArgList> ArgList ArgListH
908%type <ArgVal> ArgVal
909%type <PHIList> PHIList
910%type <ValueList> ValueRefList ValueRefListE // For call param lists
911%type <ValueList> IndexList // For GEP derived indices
912%type <TypeList> TypeListI ArgTypeListI
913%type <JumpTable> JumpTable
914%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
915%type <BoolVal> OptVolatile // 'volatile' or not
916%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
917%type <BoolVal> OptSideEffect // 'sideeffect' or not.
918%type <Linkage> OptLinkage
919%type <Endianness> BigOrLittle
920
921// ValueRef - Unresolved reference to a definition or BB
922%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
923%type <ValueVal> ResolvedVal // <type> <valref> pair
924// Tokens and types for handling constant integer values
925//
926// ESINT64VAL - A negative number within long long range
927%token <SInt64Val> ESINT64VAL
928
929// EUINT64VAL - A positive number within uns. long long range
930%token <UInt64Val> EUINT64VAL
931%type <SInt64Val> EINT64VAL
932
933%token <SIntVal> SINTVAL // Signed 32 bit ints...
934%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
935%type <SIntVal> INTVAL
936%token <FPVal> FPVAL // Float or Double constant
937
938// Built in types...
939%type <TypeVal> Types TypesV UpRTypes UpRTypesV
Reid Spencera132e042006-12-03 05:46:11 +0000940%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
941%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
942%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner58af2a12006-02-15 07:22:58 +0000943
944%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
945%type <StrVal> Name OptName OptAssign
946%type <UIntVal> OptAlign OptCAlign
947%type <StrVal> OptSection SectionString
948
949%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
950%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000951%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
952%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Chris Lattner58af2a12006-02-15 07:22:58 +0000953%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
954%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner75466192006-05-19 21:28:53 +0000955%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000956%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner1ae022f2006-10-22 06:08:13 +0000957%token DATALAYOUT
Chris Lattner58af2a12006-02-15 07:22:58 +0000958%type <UIntVal> OptCallingConv
959
960// Basic Block Terminating Operators
961%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
962
963// Binary Operators
964%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +0000965%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer1628cec2006-10-26 06:15:43 +0000966%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencera132e042006-12-03 05:46:11 +0000967%token <OtherOpVal> ICMP FCMP
Reid Spencera132e042006-12-03 05:46:11 +0000968%type <IPredicate> IPredicates
Reid Spencera132e042006-12-03 05:46:11 +0000969%type <FPredicate> FPredicates
Reid Spencer6e18b7d2006-12-03 06:59:29 +0000970%token EQ NE SLT SGT SLE SGE ULT UGT ULE UGE
971%token OEQ ONE OLT OGT OLE OGE ORD UNO UEQ UNE
Chris Lattner58af2a12006-02-15 07:22:58 +0000972
973// Memory Instructions
974%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
975
Reid Spencer3da59db2006-11-27 01:05:10 +0000976// Cast Operators
977%type <CastOpVal> CastOps
978%token <CastOpVal> TRUNC ZEXT SEXT FPTRUNC FPEXT BITCAST
979%token <CastOpVal> UITOFP SITOFP FPTOUI FPTOSI INTTOPTR PTRTOINT
980
Chris Lattner58af2a12006-02-15 07:22:58 +0000981// Other Operators
982%type <OtherOpVal> ShiftOps
Reid Spencer3da59db2006-11-27 01:05:10 +0000983%token <OtherOpVal> PHI_TOK SELECT SHL LSHR ASHR VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +0000984%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattner58af2a12006-02-15 07:22:58 +0000985
986
987%start Module
988%%
989
990// Handle constant integer size restriction and conversion...
991//
992INTVAL : SINTVAL;
993INTVAL : UINTVAL {
994 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +0000995 GEN_ERROR("Value too large for type!");
Chris Lattner58af2a12006-02-15 07:22:58 +0000996 $$ = (int32_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +0000997 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +0000998};
999
1000
1001EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
1002EINT64VAL : EUINT64VAL {
1003 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001004 GEN_ERROR("Value too large for type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001005 $$ = (int64_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001006 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001007};
1008
1009// Operations that are notably excluded from this list include:
1010// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1011//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001012ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Chris Lattner58af2a12006-02-15 07:22:58 +00001013LogicalOps : AND | OR | XOR;
1014SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer3da59db2006-11-27 01:05:10 +00001015CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | BITCAST |
1016 UITOFP | SITOFP | FPTOUI | FPTOSI | INTTOPTR | PTRTOINT;
1017ShiftOps : SHL | LSHR | ASHR;
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001018IPredicates
Reid Spencer4012e832006-12-04 05:24:24 +00001019 : EQ { $$ = ICmpInst::ICMP_EQ; } | NE { $$ = ICmpInst::ICMP_NE; }
Reid Spencer6e18b7d2006-12-03 06:59:29 +00001020 | SLT { $$ = ICmpInst::ICMP_SLT; } | SGT { $$ = ICmpInst::ICMP_SGT; }
1021 | SLE { $$ = ICmpInst::ICMP_SLE; } | SGE { $$ = ICmpInst::ICMP_SGE; }
1022 | ULT { $$ = ICmpInst::ICMP_ULT; } | UGT { $$ = ICmpInst::ICMP_UGT; }
1023 | ULE { $$ = ICmpInst::ICMP_ULE; } | UGE { $$ = ICmpInst::ICMP_UGE; }
1024 ;
1025
1026FPredicates
1027 : OEQ { $$ = FCmpInst::FCMP_OEQ; } | ONE { $$ = FCmpInst::FCMP_ONE; }
1028 | OLT { $$ = FCmpInst::FCMP_OLT; } | OGT { $$ = FCmpInst::FCMP_OGT; }
1029 | OLE { $$ = FCmpInst::FCMP_OLE; } | OGE { $$ = FCmpInst::FCMP_OGE; }
1030 | ORD { $$ = FCmpInst::FCMP_ORD; } | UNO { $$ = FCmpInst::FCMP_UNO; }
1031 | UEQ { $$ = FCmpInst::FCMP_UEQ; } | UNE { $$ = FCmpInst::FCMP_UNE; }
1032 | ULT { $$ = FCmpInst::FCMP_ULT; } | UGT { $$ = FCmpInst::FCMP_UGT; }
1033 | ULE { $$ = FCmpInst::FCMP_ULE; } | UGE { $$ = FCmpInst::FCMP_UGE; }
1034 | TRUETOK { $$ = FCmpInst::FCMP_TRUE; }
1035 | FALSETOK { $$ = FCmpInst::FCMP_FALSE; }
1036 ;
Chris Lattner58af2a12006-02-15 07:22:58 +00001037
1038// These are some types that allow classification if we only want a particular
1039// thing... for example, only a signed, unsigned, or integral type.
1040SIntType : LONG | INT | SHORT | SBYTE;
1041UIntType : ULONG | UINT | USHORT | UBYTE;
1042IntType : SIntType | UIntType;
1043FPType : FLOAT | DOUBLE;
1044
1045// OptAssign - Value producing statements have an optional assignment component
1046OptAssign : Name '=' {
1047 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001048 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001049 }
1050 | /*empty*/ {
1051 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001052 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001053 };
1054
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001055OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1056 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
1057 WEAK { $$ = GlobalValue::WeakLinkage; } |
1058 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1059 DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; } |
1060 DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; } |
1061 EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } |
1062 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner58af2a12006-02-15 07:22:58 +00001063
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001064OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1065 CCC_TOK { $$ = CallingConv::C; } |
1066 CSRETCC_TOK { $$ = CallingConv::CSRet; } |
1067 FASTCC_TOK { $$ = CallingConv::Fast; } |
1068 COLDCC_TOK { $$ = CallingConv::Cold; } |
1069 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1070 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1071 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001072 if ((unsigned)$2 != $2)
Reid Spencer61c83e02006-08-18 08:43:06 +00001073 GEN_ERROR("Calling conv too large!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001074 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001075 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001076 };
1077
1078// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1079// a comma before it.
1080OptAlign : /*empty*/ { $$ = 0; } |
1081 ALIGN EUINT64VAL {
1082 $$ = $2;
1083 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001084 GEN_ERROR("Alignment must be a power of two!");
1085 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001086};
1087OptCAlign : /*empty*/ { $$ = 0; } |
1088 ',' ALIGN EUINT64VAL {
1089 $$ = $3;
1090 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001091 GEN_ERROR("Alignment must be a power of two!");
1092 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001093};
1094
1095
1096SectionString : SECTION STRINGCONSTANT {
1097 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1098 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer61c83e02006-08-18 08:43:06 +00001099 GEN_ERROR("Invalid character in section name!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001100 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001101 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001102};
1103
1104OptSection : /*empty*/ { $$ = 0; } |
1105 SectionString { $$ = $1; };
1106
1107// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1108// is set to be the global we are processing.
1109//
1110GlobalVarAttributes : /* empty */ {} |
1111 ',' GlobalVarAttribute GlobalVarAttributes {};
1112GlobalVarAttribute : SectionString {
1113 CurGV->setSection($1);
1114 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001115 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001116 }
1117 | ALIGN EUINT64VAL {
1118 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001119 GEN_ERROR("Alignment must be a power of two!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001120 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001121 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001122 };
1123
1124//===----------------------------------------------------------------------===//
1125// Types includes all predefined types... except void, because it can only be
1126// used in specific contexts (function returning void for example). To have
1127// access to it, a user must explicitly use TypesV.
1128//
1129
1130// TypesV includes all of 'Types', but it also includes the void type.
Reid Spencera132e042006-12-03 05:46:11 +00001131TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1132UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner58af2a12006-02-15 07:22:58 +00001133
1134Types : UpRTypes {
1135 if (!UpRefs.empty())
Reid Spencera132e042006-12-03 05:46:11 +00001136 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001137 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001138 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001139 };
Chris Lattner58af2a12006-02-15 07:22:58 +00001140
1141
1142// Derived types are added later...
1143//
1144PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1145PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
1146UpRTypes : OPAQUE {
Reid Spencera132e042006-12-03 05:46:11 +00001147 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001148 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001149 }
1150 | PrimType {
Reid Spencera132e042006-12-03 05:46:11 +00001151 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001152 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001153 };
1154UpRTypes : SymbolicValueRef { // Named types are also simple types...
Reid Spencer5b7e7532006-09-28 19:28:24 +00001155 const Type* tmp = getTypeVal($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001156 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001157 $$ = new PATypeHolder(tmp);
Chris Lattner58af2a12006-02-15 07:22:58 +00001158};
1159
1160// Include derived types in the Types production.
1161//
1162UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencer61c83e02006-08-18 08:43:06 +00001163 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001164 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1165 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Reid Spencera132e042006-12-03 05:46:11 +00001166 $$ = new PATypeHolder(OT);
Chris Lattner58af2a12006-02-15 07:22:58 +00001167 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001168 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001169 }
1170 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
1171 std::vector<const Type*> Params;
Reid Spencera132e042006-12-03 05:46:11 +00001172 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001173 E = $3->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001174 Params.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001175 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1176 if (isVarArg) Params.pop_back();
1177
Reid Spencera132e042006-12-03 05:46:11 +00001178 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001179 delete $3; // Delete the argument list
Reid Spencera132e042006-12-03 05:46:11 +00001180 delete $1; // Delete the return type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001181 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001182 }
1183 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencera132e042006-12-03 05:46:11 +00001184 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1185 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001186 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001187 }
1188 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencera132e042006-12-03 05:46:11 +00001189 const llvm::Type* ElemTy = $4->get();
1190 if ((unsigned)$2 != $2)
1191 GEN_ERROR("Unsigned result not equal to signed result");
1192 if (!ElemTy->isPrimitiveType())
1193 GEN_ERROR("Elemental type of a PackedType must be primitive");
1194 if (!isPowerOf2_32($2))
1195 GEN_ERROR("Vector length should be a power of 2!");
1196 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1197 delete $4;
1198 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001199 }
1200 | '{' TypeListI '}' { // Structure type?
1201 std::vector<const Type*> Elements;
Reid Spencera132e042006-12-03 05:46:11 +00001202 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
Chris Lattner58af2a12006-02-15 07:22:58 +00001203 E = $2->end(); I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001204 Elements.push_back(*I);
Chris Lattner58af2a12006-02-15 07:22:58 +00001205
Reid Spencera132e042006-12-03 05:46:11 +00001206 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattner58af2a12006-02-15 07:22:58 +00001207 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001208 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001209 }
1210 | '{' '}' { // Empty structure type?
Reid Spencera132e042006-12-03 05:46:11 +00001211 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001212 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001213 }
1214 | UpRTypes '*' { // Pointer type?
Reid Spencera132e042006-12-03 05:46:11 +00001215 if (*$1 == Type::LabelTy)
Chris Lattner59c85e92006-10-15 23:27:25 +00001216 GEN_ERROR("Cannot form a pointer to a basic block");
Reid Spencera132e042006-12-03 05:46:11 +00001217 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1218 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001219 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001220 };
1221
1222// TypeList - Used for struct declarations and as a basis for function type
1223// declaration type lists
1224//
1225TypeListI : UpRTypes {
Reid Spencera132e042006-12-03 05:46:11 +00001226 $$ = new std::list<PATypeHolder>();
1227 $$->push_back(*$1); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001228 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001229 }
1230 | TypeListI ',' UpRTypes {
Reid Spencera132e042006-12-03 05:46:11 +00001231 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001232 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001233 };
1234
1235// ArgTypeList - List of types for a function type declaration...
1236ArgTypeListI : TypeListI
1237 | TypeListI ',' DOTDOTDOT {
Reid Spencera132e042006-12-03 05:46:11 +00001238 ($$=$1)->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001239 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001240 }
1241 | DOTDOTDOT {
Reid Spencera132e042006-12-03 05:46:11 +00001242 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001243 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001244 }
1245 | /*empty*/ {
Reid Spencera132e042006-12-03 05:46:11 +00001246 $$ = new std::list<PATypeHolder>();
Reid Spencer61c83e02006-08-18 08:43:06 +00001247 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001248 };
1249
1250// ConstVal - The various declarations that go into the constant pool. This
1251// production is used ONLY to represent constants that show up AFTER a 'const',
1252// 'constant' or 'global' token at global scope. Constants that can be inlined
1253// into other expressions (such as integers and constexprs) are handled by the
1254// ResolvedVal, ValueRef and ConstValueRef productions.
1255//
1256ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencera132e042006-12-03 05:46:11 +00001257 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001258 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001259 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001260 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001261 const Type *ETy = ATy->getElementType();
1262 int NumElements = ATy->getNumElements();
1263
1264 // Verify that we have the correct size...
1265 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001266 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001267 utostr($3->size()) + " arguments, but has size of " +
1268 itostr(NumElements) + "!");
1269
1270 // Verify all elements are correct type!
1271 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001272 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001273 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001274 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001275 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001276 }
1277
Reid Spencera132e042006-12-03 05:46:11 +00001278 $$ = ConstantArray::get(ATy, *$3);
1279 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001280 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001281 }
1282 | Types '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00001283 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001284 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001285 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001286 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001287
1288 int NumElements = ATy->getNumElements();
1289 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001290 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Chris Lattner58af2a12006-02-15 07:22:58 +00001291 " arguments, but has size of " + itostr(NumElements) +"!");
Reid Spencera132e042006-12-03 05:46:11 +00001292 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1293 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001294 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001295 }
1296 | Types 'c' STRINGCONSTANT {
Reid Spencera132e042006-12-03 05:46:11 +00001297 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001298 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001299 GEN_ERROR("Cannot make array constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001300 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001301
1302 int NumElements = ATy->getNumElements();
1303 const Type *ETy = ATy->getElementType();
1304 char *EndStr = UnEscapeLexed($3, true);
1305 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001306 GEN_ERROR("Can't build string constant of size " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001307 itostr((int)(EndStr-$3)) +
1308 " when array has size " + itostr(NumElements) + "!");
1309 std::vector<Constant*> Vals;
1310 if (ETy == Type::SByteTy) {
1311 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
Reid Spencerb83eb642006-10-20 07:07:24 +00001312 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner58af2a12006-02-15 07:22:58 +00001313 } else if (ETy == Type::UByteTy) {
1314 for (unsigned char *C = (unsigned char *)$3;
1315 C != (unsigned char*)EndStr; ++C)
Reid Spencerb83eb642006-10-20 07:07:24 +00001316 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner58af2a12006-02-15 07:22:58 +00001317 } else {
1318 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001319 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001320 }
1321 free($3);
Reid Spencera132e042006-12-03 05:46:11 +00001322 $$ = ConstantArray::get(ATy, Vals);
1323 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001324 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001325 }
1326 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencera132e042006-12-03 05:46:11 +00001327 const PackedType *PTy = dyn_cast<PackedType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001328 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001329 GEN_ERROR("Cannot make packed constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001330 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001331 const Type *ETy = PTy->getElementType();
1332 int NumElements = PTy->getNumElements();
1333
1334 // Verify that we have the correct size...
1335 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001336 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001337 utostr($3->size()) + " arguments, but has size of " +
1338 itostr(NumElements) + "!");
1339
1340 // Verify all elements are correct type!
1341 for (unsigned i = 0; i < $3->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00001342 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001343 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001344 ETy->getDescription() +"' as required!\nIt is of type '"+
Reid Spencera132e042006-12-03 05:46:11 +00001345 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00001346 }
1347
Reid Spencera132e042006-12-03 05:46:11 +00001348 $$ = ConstantPacked::get(PTy, *$3);
1349 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001350 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001351 }
1352 | Types '{' ConstVector '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001353 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001354 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001355 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001356 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001357
1358 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer61c83e02006-08-18 08:43:06 +00001359 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001360
1361 // Check to ensure that constants are compatible with the type initializer!
1362 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Reid Spencera132e042006-12-03 05:46:11 +00001363 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001364 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001365 STy->getElementType(i)->getDescription() +
1366 "' for element #" + utostr(i) +
1367 " of structure initializer!");
1368
Reid Spencera132e042006-12-03 05:46:11 +00001369 $$ = ConstantStruct::get(STy, *$3);
1370 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001371 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001372 }
1373 | Types '{' '}' {
Reid Spencera132e042006-12-03 05:46:11 +00001374 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001375 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001376 GEN_ERROR("Cannot make struct constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001377 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001378
1379 if (STy->getNumContainedTypes() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001380 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001381
Reid Spencera132e042006-12-03 05:46:11 +00001382 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1383 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001384 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001385 }
1386 | Types NULL_TOK {
Reid Spencera132e042006-12-03 05:46:11 +00001387 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001388 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001389 GEN_ERROR("Cannot make null pointer constant with type: '" +
Reid Spencera132e042006-12-03 05:46:11 +00001390 (*$1)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001391
Reid Spencera132e042006-12-03 05:46:11 +00001392 $$ = ConstantPointerNull::get(PTy);
1393 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001394 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001395 }
1396 | Types UNDEF {
Reid Spencera132e042006-12-03 05:46:11 +00001397 $$ = UndefValue::get($1->get());
1398 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001399 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001400 }
1401 | Types SymbolicValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00001402 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001403 if (Ty == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001404 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001405
1406 // ConstExprs can exist in the body of a function, thus creating
1407 // GlobalValues whenever they refer to a variable. Because we are in
1408 // the context of a function, getValNonImprovising will search the functions
1409 // symbol table instead of the module symbol table for the global symbol,
1410 // which throws things all off. To get around this, we just tell
1411 // getValNonImprovising that we are at global scope here.
1412 //
1413 Function *SavedCurFn = CurFun.CurrentFunction;
1414 CurFun.CurrentFunction = 0;
1415
1416 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001417 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001418
1419 CurFun.CurrentFunction = SavedCurFn;
1420
1421 // If this is an initializer for a constant pointer, which is referencing a
1422 // (currently) undefined variable, create a stub now that shall be replaced
1423 // in the future with the right type of variable.
1424 //
1425 if (V == 0) {
1426 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1427 const PointerType *PT = cast<PointerType>(Ty);
1428
1429 // First check to see if the forward references value is already created!
1430 PerModuleInfo::GlobalRefsType::iterator I =
1431 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1432
1433 if (I != CurModule.GlobalRefs.end()) {
1434 V = I->second; // Placeholder already exists, use it...
1435 $2.destroy();
1436 } else {
1437 std::string Name;
1438 if ($2.Type == ValID::NameVal) Name = $2.Name;
1439
1440 // Create the forward referenced global.
1441 GlobalValue *GV;
1442 if (const FunctionType *FTy =
1443 dyn_cast<FunctionType>(PT->getElementType())) {
1444 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1445 CurModule.CurrentModule);
1446 } else {
1447 GV = new GlobalVariable(PT->getElementType(), false,
1448 GlobalValue::ExternalLinkage, 0,
1449 Name, CurModule.CurrentModule);
1450 }
1451
1452 // Keep track of the fact that we have a forward ref to recycle it
1453 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1454 V = GV;
1455 }
1456 }
1457
Reid Spencera132e042006-12-03 05:46:11 +00001458 $$ = cast<GlobalValue>(V);
1459 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001460 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001461 }
1462 | Types ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00001463 if ($1->get() != $2->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001464 GEN_ERROR("Mismatched types for constant expression!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001465 $$ = $2;
Reid Spencera132e042006-12-03 05:46:11 +00001466 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001467 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001468 }
1469 | Types ZEROINITIALIZER {
Reid Spencera132e042006-12-03 05:46:11 +00001470 const Type *Ty = $1->get();
Chris Lattner58af2a12006-02-15 07:22:58 +00001471 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +00001472 GEN_ERROR("Cannot create a null initialized value of this type!");
Reid Spencera132e042006-12-03 05:46:11 +00001473 $$ = Constant::getNullValue(Ty);
1474 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001475 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001476 }
1477 | SIntType EINT64VAL { // integral constants
1478 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001479 GEN_ERROR("Constant value doesn't fit in type!");
Reid Spencera132e042006-12-03 05:46:11 +00001480 $$ = ConstantInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001481 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001482 }
1483 | UIntType EUINT64VAL { // integral constants
Reid Spencera132e042006-12-03 05:46:11 +00001484 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001485 GEN_ERROR("Constant value doesn't fit in type!");
Reid Spencera132e042006-12-03 05:46:11 +00001486 $$ = ConstantInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001487 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001488 }
1489 | BOOL TRUETOK { // Boolean constants
Reid Spencera132e042006-12-03 05:46:11 +00001490 $$ = ConstantBool::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001491 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001492 }
1493 | BOOL FALSETOK { // Boolean constants
Reid Spencera132e042006-12-03 05:46:11 +00001494 $$ = ConstantBool::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001495 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001496 }
1497 | FPType FPVAL { // Float & Double constants
Reid Spencera132e042006-12-03 05:46:11 +00001498 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001499 GEN_ERROR("Floating point constant invalid for type!!");
Reid Spencera132e042006-12-03 05:46:11 +00001500 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001501 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001502 };
1503
1504
Reid Spencer3da59db2006-11-27 01:05:10 +00001505ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001506 Constant *Val = $3;
1507 const Type *Ty = $5->get();
Reid Spencer3da59db2006-11-27 01:05:10 +00001508 if (!Val->getType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001509 GEN_ERROR("cast constant expression from a non-primitive type: '" +
Reid Spencer3da59db2006-11-27 01:05:10 +00001510 Val->getType()->getDescription() + "'!");
1511 if (!Ty->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001512 GEN_ERROR("cast constant expression to a non-primitive type: '" +
Reid Spencer3da59db2006-11-27 01:05:10 +00001513 Ty->getDescription() + "'!");
Reid Spencera132e042006-12-03 05:46:11 +00001514 $$ = ConstantExpr::getCast($1, $3, $5->get());
1515 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00001516 }
1517 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001518 if (!isa<PointerType>($3->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001519 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001520
Reid Spencera132e042006-12-03 05:46:11 +00001521 const Type *IdxTy =
1522 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1523 if (!IdxTy)
1524 GEN_ERROR("Index list invalid for constant getelementptr!");
1525
1526 std::vector<Constant*> IdxVec;
1527 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1528 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001529 IdxVec.push_back(C);
1530 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001531 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001532
1533 delete $4;
1534
Reid Spencera132e042006-12-03 05:46:11 +00001535 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Reid Spencer61c83e02006-08-18 08:43:06 +00001536 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001537 }
1538 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001539 if ($3->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001540 GEN_ERROR("Select condition must be of boolean type!");
Reid Spencera132e042006-12-03 05:46:11 +00001541 if ($5->getType() != $7->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001542 GEN_ERROR("Select operand types must match!");
Reid Spencera132e042006-12-03 05:46:11 +00001543 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001544 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001545 }
1546 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001547 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001548 GEN_ERROR("Binary operator types must match!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001549 CHECK_FOR_ERROR;
Reid Spencer9eef56f2006-12-05 19:16:11 +00001550 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001551 }
1552 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001553 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001554 GEN_ERROR("Logical operator types must match!");
Reid Spencera132e042006-12-03 05:46:11 +00001555 if (!$3->getType()->isIntegral()) {
1556 if (!isa<PackedType>($3->getType()) ||
1557 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00001558 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001559 }
Reid Spencera132e042006-12-03 05:46:11 +00001560 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001561 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001562 }
1563 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001564 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001565 GEN_ERROR("setcc operand types must match!");
Reid Spencera132e042006-12-03 05:46:11 +00001566 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001567 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001568 }
Reid Spencer4012e832006-12-04 05:24:24 +00001569 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1570 if ($4->getType() != $6->getType())
Reid Spencera132e042006-12-03 05:46:11 +00001571 GEN_ERROR("icmp operand types must match!");
Reid Spencer4012e832006-12-04 05:24:24 +00001572 $$ = ConstantExpr::getICmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001573 }
Reid Spencer4012e832006-12-04 05:24:24 +00001574 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1575 if ($4->getType() != $6->getType())
Reid Spencera132e042006-12-03 05:46:11 +00001576 GEN_ERROR("fcmp operand types must match!");
Reid Spencer4012e832006-12-04 05:24:24 +00001577 $$ = ConstantExpr::getFCmp($2, $4, $6);
Reid Spencera132e042006-12-03 05:46:11 +00001578 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001579 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001580 if ($5->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001581 GEN_ERROR("Shift count for shift constant must be unsigned byte!");
Reid Spencera132e042006-12-03 05:46:11 +00001582 if (!$3->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00001583 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencer3822ff52006-11-08 06:47:33 +00001584 CHECK_FOR_ERROR;
Reid Spencera132e042006-12-03 05:46:11 +00001585 $$ = ConstantExpr::get($1, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001586 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001587 }
1588 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001589 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencer61c83e02006-08-18 08:43:06 +00001590 GEN_ERROR("Invalid extractelement operands!");
Reid Spencera132e042006-12-03 05:46:11 +00001591 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001592 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001593 }
1594 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001595 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001596 GEN_ERROR("Invalid insertelement operands!");
Reid Spencera132e042006-12-03 05:46:11 +00001597 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001598 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001599 }
1600 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencera132e042006-12-03 05:46:11 +00001601 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001602 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencera132e042006-12-03 05:46:11 +00001603 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001604 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001605 };
1606
Chris Lattnerd25db202006-04-08 03:55:17 +00001607
Chris Lattner58af2a12006-02-15 07:22:58 +00001608// ConstVector - A list of comma separated constants.
1609ConstVector : ConstVector ',' ConstVal {
1610 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001611 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001612 }
1613 | ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00001614 $$ = new std::vector<Constant*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00001615 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001616 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001617 };
1618
1619
1620// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1621GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1622
1623
1624//===----------------------------------------------------------------------===//
1625// Rules to match Modules
1626//===----------------------------------------------------------------------===//
1627
1628// Module rule: Capture the result of parsing the whole file into a result
1629// variable...
1630//
1631Module : FunctionList {
1632 $$ = ParserResult = $1;
1633 CurModule.ModuleDone();
Reid Spencerf63697d2006-10-09 17:36:59 +00001634 CHECK_FOR_ERROR;
Chris Lattner58af2a12006-02-15 07:22:58 +00001635};
1636
1637// FunctionList - A list of functions, preceeded by a constant pool.
1638//
1639FunctionList : FunctionList Function {
1640 $$ = $1;
1641 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001642 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001643 }
1644 | FunctionList FunctionProto {
1645 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001646 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001647 }
1648 | FunctionList MODULE ASM_TOK AsmBlock {
1649 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001650 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001651 }
1652 | FunctionList IMPLEMENTATION {
1653 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001654 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001655 }
1656 | ConstPool {
1657 $$ = CurModule.CurrentModule;
1658 // Emit an error if there are any unresolved types left.
1659 if (!CurModule.LateResolveTypes.empty()) {
1660 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer61c83e02006-08-18 08:43:06 +00001661 if (DID.Type == ValID::NameVal) {
1662 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1663 } else {
1664 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1665 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001666 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001667 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001668 };
1669
1670// ConstPool - Constants with optional names assigned to them.
1671ConstPool : ConstPool OptAssign TYPE TypesV {
1672 // Eagerly resolve types. This is not an optimization, this is a
1673 // requirement that is due to the fact that we could have this:
1674 //
1675 // %list = type { %list * }
1676 // %list = type { %list * } ; repeated type decl
1677 //
1678 // If types are not resolved eagerly, then the two types will not be
1679 // determined to be the same type!
1680 //
Reid Spencera132e042006-12-03 05:46:11 +00001681 ResolveTypeTo($2, *$4);
Chris Lattner58af2a12006-02-15 07:22:58 +00001682
Reid Spencera132e042006-12-03 05:46:11 +00001683 if (!setTypeName(*$4, $2) && !$2) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001684 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001685 // If this is a named type that is not a redefinition, add it to the slot
1686 // table.
Reid Spencera132e042006-12-03 05:46:11 +00001687 CurModule.Types.push_back(*$4);
Chris Lattner58af2a12006-02-15 07:22:58 +00001688 }
Reid Spencera132e042006-12-03 05:46:11 +00001689
1690 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001691 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001692 }
1693 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001694 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001695 }
1696 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001697 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001698 }
1699 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Reid Spencera132e042006-12-03 05:46:11 +00001700 if ($5 == 0)
Reid Spencer5b7e7532006-09-28 19:28:24 +00001701 GEN_ERROR("Global value initializer is not a constant!");
Reid Spencera132e042006-12-03 05:46:11 +00001702 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001703 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00001704 } GlobalVarAttributes {
1705 CurGV = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +00001706 }
1707 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencera132e042006-12-03 05:46:11 +00001708 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001709 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001710 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001711 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001712 CurGV = 0;
1713 CHECK_FOR_ERROR
1714 }
1715 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencera132e042006-12-03 05:46:11 +00001716 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, *$5, 0);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001717 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001718 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001719 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001720 CurGV = 0;
1721 CHECK_FOR_ERROR
1722 }
1723 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001724 CurGV =
Reid Spencera132e042006-12-03 05:46:11 +00001725 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, *$5, 0);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001726 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00001727 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001728 } GlobalVarAttributes {
Chris Lattner58af2a12006-02-15 07:22:58 +00001729 CurGV = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001730 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001731 }
1732 | ConstPool TARGET TargetDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001733 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001734 }
1735 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001736 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001737 }
1738 | /* empty: end of list */ {
1739 };
1740
1741
1742AsmBlock : STRINGCONSTANT {
1743 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
1744 char *EndStr = UnEscapeLexed($1, true);
1745 std::string NewAsm($1, EndStr);
1746 free($1);
1747
1748 if (AsmSoFar.empty())
1749 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
1750 else
1751 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00001752 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001753};
1754
1755BigOrLittle : BIG { $$ = Module::BigEndian; };
1756BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1757
1758TargetDefinition : ENDIAN '=' BigOrLittle {
1759 CurModule.CurrentModule->setEndianness($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001760 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001761 }
1762 | POINTERSIZE '=' EUINT64VAL {
1763 if ($3 == 32)
1764 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1765 else if ($3 == 64)
1766 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1767 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001768 GEN_ERROR("Invalid pointer size: '" + utostr($3) + "'!");
1769 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001770 }
1771 | TRIPLE '=' STRINGCONSTANT {
1772 CurModule.CurrentModule->setTargetTriple($3);
1773 free($3);
John Criswell2f6a8b12006-10-24 19:09:48 +00001774 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00001775 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson1dc69692006-10-18 02:21:48 +00001776 CurModule.CurrentModule->setDataLayout($3);
1777 free($3);
Owen Anderson1dc69692006-10-18 02:21:48 +00001778 };
Chris Lattner58af2a12006-02-15 07:22:58 +00001779
1780LibrariesDefinition : '[' LibList ']';
1781
1782LibList : LibList ',' STRINGCONSTANT {
1783 CurModule.CurrentModule->addLibrary($3);
1784 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001785 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001786 }
1787 | STRINGCONSTANT {
1788 CurModule.CurrentModule->addLibrary($1);
1789 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001790 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001791 }
1792 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00001793 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001794 }
1795 ;
1796
1797//===----------------------------------------------------------------------===//
1798// Rules to match Function Headers
1799//===----------------------------------------------------------------------===//
1800
1801Name : VAR_ID | STRINGCONSTANT;
1802OptName : Name | /*empty*/ { $$ = 0; };
1803
1804ArgVal : Types OptName {
Reid Spencera132e042006-12-03 05:46:11 +00001805 if (*$1 == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001806 GEN_ERROR("void typed arguments are invalid!");
Reid Spencera132e042006-12-03 05:46:11 +00001807 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001808 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001809};
1810
1811ArgListH : ArgListH ',' ArgVal {
1812 $$ = $1;
1813 $1->push_back(*$3);
1814 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001815 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001816 }
1817 | ArgVal {
Reid Spencera132e042006-12-03 05:46:11 +00001818 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner58af2a12006-02-15 07:22:58 +00001819 $$->push_back(*$1);
1820 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001821 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001822 };
1823
1824ArgList : ArgListH {
1825 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001826 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001827 }
1828 | ArgListH ',' DOTDOTDOT {
1829 $$ = $1;
Reid Spencera132e042006-12-03 05:46:11 +00001830 $$->push_back(std::pair<PATypeHolder*,
1831 char*>(new PATypeHolder(Type::VoidTy), 0));
Reid Spencer61c83e02006-08-18 08:43:06 +00001832 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001833 }
1834 | DOTDOTDOT {
Reid Spencera132e042006-12-03 05:46:11 +00001835 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1836 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Reid Spencer61c83e02006-08-18 08:43:06 +00001837 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001838 }
1839 | /* empty */ {
1840 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001841 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001842 };
1843
1844FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1845 OptSection OptAlign {
1846 UnEscapeLexed($3);
1847 std::string FunctionName($3);
1848 free($3); // Free strdup'd memory!
1849
Reid Spencera132e042006-12-03 05:46:11 +00001850 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001851 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001852
1853 std::vector<const Type*> ParamTypeList;
1854 if ($5) { // If there are arguments...
Reid Spencera132e042006-12-03 05:46:11 +00001855 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
Chris Lattner58af2a12006-02-15 07:22:58 +00001856 I != $5->end(); ++I)
Reid Spencera132e042006-12-03 05:46:11 +00001857 ParamTypeList.push_back(I->first->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00001858 }
1859
1860 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1861 if (isVarArg) ParamTypeList.pop_back();
1862
Reid Spencera132e042006-12-03 05:46:11 +00001863 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattner58af2a12006-02-15 07:22:58 +00001864 const PointerType *PFT = PointerType::get(FT);
Reid Spencera132e042006-12-03 05:46:11 +00001865 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00001866
1867 ValID ID;
1868 if (!FunctionName.empty()) {
1869 ID = ValID::create((char*)FunctionName.c_str());
1870 } else {
1871 ID = ValID::create((int)CurModule.Values[PFT].size());
1872 }
1873
1874 Function *Fn = 0;
1875 // See if this function was forward referenced. If so, recycle the object.
1876 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1877 // Move the function to the end of the list, from whereever it was
1878 // previously inserted.
1879 Fn = cast<Function>(FWRef);
1880 CurModule.CurrentModule->getFunctionList().remove(Fn);
1881 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1882 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1883 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1884 // If this is the case, either we need to be a forward decl, or it needs
1885 // to be.
1886 if (!CurFun.isDeclare && !Fn->isExternal())
Reid Spencer61c83e02006-08-18 08:43:06 +00001887 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001888
1889 // Make sure to strip off any argument names so we can't get conflicts.
1890 if (Fn->isExternal())
1891 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
1892 AI != AE; ++AI)
1893 AI->setName("");
Chris Lattner58af2a12006-02-15 07:22:58 +00001894 } else { // Not already defined?
1895 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1896 CurModule.CurrentModule);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001897
Chris Lattner58af2a12006-02-15 07:22:58 +00001898 InsertValue(Fn, CurModule.Values);
1899 }
1900
1901 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00001902
1903 if (CurFun.isDeclare) {
1904 // If we have declaration, always overwrite linkage. This will allow us to
1905 // correctly handle cases, when pointer to function is passed as argument to
1906 // another function.
1907 Fn->setLinkage(CurFun.Linkage);
1908 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001909 Fn->setCallingConv($1);
1910 Fn->setAlignment($8);
1911 if ($7) {
1912 Fn->setSection($7);
1913 free($7);
1914 }
1915
1916 // Add all of the arguments we parsed to the function...
1917 if ($5) { // Is null if empty...
1918 if (isVarArg) { // Nuke the last entry
Reid Spencera132e042006-12-03 05:46:11 +00001919 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
1920 "Not a varargs marker!");
1921 delete $5->back().first;
Chris Lattner58af2a12006-02-15 07:22:58 +00001922 $5->pop_back(); // Delete the last entry
1923 }
1924 Function::arg_iterator ArgIt = Fn->arg_begin();
Reid Spencera132e042006-12-03 05:46:11 +00001925 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
Chris Lattner58af2a12006-02-15 07:22:58 +00001926 I != $5->end(); ++I, ++ArgIt) {
Reid Spencera132e042006-12-03 05:46:11 +00001927 delete I->first; // Delete the typeholder...
1928
Chris Lattner58af2a12006-02-15 07:22:58 +00001929 setValueName(ArgIt, I->second); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00001930 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001931 InsertValue(ArgIt);
1932 }
Reid Spencera132e042006-12-03 05:46:11 +00001933
Chris Lattner58af2a12006-02-15 07:22:58 +00001934 delete $5; // We're now done with the argument list
1935 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001936 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001937};
1938
1939BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1940
1941FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
1942 $$ = CurFun.CurrentFunction;
1943
1944 // Make sure that we keep track of the linkage type even if there was a
1945 // previous "declare".
1946 $$->setLinkage($1);
1947};
1948
1949END : ENDTOK | '}'; // Allow end of '}' to end a function
1950
1951Function : BasicBlockList END {
1952 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001953 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001954};
1955
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001956FnDeclareLinkage: /*default*/ |
Chris Lattnerf49c1762006-11-08 05:58:47 +00001957 DLLIMPORT { CurFun.Linkage = GlobalValue::DLLImportLinkage; } |
Reid Spencer481169e2006-12-01 00:33:46 +00001958 EXTERN_WEAK { CurFun.Linkage = GlobalValue::ExternalWeakLinkage; };
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001959
1960FunctionProto : DECLARE { CurFun.isDeclare = true; } FnDeclareLinkage FunctionHeaderH {
1961 $$ = CurFun.CurrentFunction;
1962 CurFun.FunctionDone();
1963 CHECK_FOR_ERROR
1964 };
Chris Lattner58af2a12006-02-15 07:22:58 +00001965
1966//===----------------------------------------------------------------------===//
1967// Rules to match Basic Blocks
1968//===----------------------------------------------------------------------===//
1969
1970OptSideEffect : /* empty */ {
1971 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00001972 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001973 }
1974 | SIDEEFFECT {
1975 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00001976 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001977 };
1978
1979ConstValueRef : ESINT64VAL { // A reference to a direct constant
1980 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001981 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001982 }
1983 | EUINT64VAL {
1984 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001985 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001986 }
1987 | FPVAL { // Perhaps it's an FP constant?
1988 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001989 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001990 }
1991 | TRUETOK {
Chris Lattner47811b72006-09-28 23:35:22 +00001992 $$ = ValID::create(ConstantBool::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00001993 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001994 }
1995 | FALSETOK {
Chris Lattner47811b72006-09-28 23:35:22 +00001996 $$ = ValID::create(ConstantBool::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00001997 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001998 }
1999 | NULL_TOK {
2000 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002001 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002002 }
2003 | UNDEF {
2004 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002005 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002006 }
2007 | ZEROINITIALIZER { // A vector zero constant.
2008 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002009 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002010 }
2011 | '<' ConstVector '>' { // Nonempty unsized packed vector
Reid Spencera132e042006-12-03 05:46:11 +00002012 const Type *ETy = (*$2)[0]->getType();
Chris Lattner58af2a12006-02-15 07:22:58 +00002013 int NumElements = $2->size();
2014
2015 PackedType* pt = PackedType::get(ETy, NumElements);
2016 PATypeHolder* PTy = new PATypeHolder(
Reid Spencera132e042006-12-03 05:46:11 +00002017 HandleUpRefs(
2018 PackedType::get(
2019 ETy,
2020 NumElements)
2021 )
2022 );
Chris Lattner58af2a12006-02-15 07:22:58 +00002023
2024 // Verify all elements are correct type!
2025 for (unsigned i = 0; i < $2->size(); i++) {
Reid Spencera132e042006-12-03 05:46:11 +00002026 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002027 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002028 ETy->getDescription() +"' as required!\nIt is of type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002029 (*$2)[i]->getType()->getDescription() + "'.");
Chris Lattner58af2a12006-02-15 07:22:58 +00002030 }
2031
Reid Spencera132e042006-12-03 05:46:11 +00002032 $$ = ValID::create(ConstantPacked::get(pt, *$2));
Chris Lattner58af2a12006-02-15 07:22:58 +00002033 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002034 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002035 }
2036 | ConstExpr {
Reid Spencera132e042006-12-03 05:46:11 +00002037 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002038 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002039 }
2040 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2041 char *End = UnEscapeLexed($3, true);
2042 std::string AsmStr = std::string($3, End);
2043 End = UnEscapeLexed($5, true);
2044 std::string Constraints = std::string($5, End);
2045 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2046 free($3);
2047 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002048 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002049 };
2050
2051// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2052// another value.
2053//
2054SymbolicValueRef : INTVAL { // Is it an integer reference...?
2055 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002056 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002057 }
2058 | Name { // Is it a named reference...?
2059 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002060 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002061 };
2062
2063// ValueRef - A reference to a definition... either constant or symbolic
2064ValueRef : SymbolicValueRef | ConstValueRef;
2065
2066
2067// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2068// type immediately preceeds the value reference, and allows complex constant
2069// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2070ResolvedVal : Types ValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00002071 $$ = getVal(*$1, $2); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002072 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002073 };
2074
2075BasicBlockList : BasicBlockList BasicBlock {
2076 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002077 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002078 }
2079 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2080 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002081 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002082 };
2083
2084
2085// Basic blocks are terminated by branching instructions:
2086// br, br/cc, switch, ret
2087//
2088BasicBlock : InstructionList OptAssign BBTerminatorInst {
2089 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002090 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002091 InsertValue($3);
2092
2093 $1->getInstList().push_back($3);
2094 InsertValue($1);
2095 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002096 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002097 };
2098
2099InstructionList : InstructionList Inst {
Reid Spencer3da59db2006-11-27 01:05:10 +00002100 if (CastInst *CI1 = dyn_cast<CastInst>($2))
2101 if (CastInst *CI2 = dyn_cast<CastInst>(CI1->getOperand(0)))
2102 if (CI2->getParent() == 0)
2103 $1->getInstList().push_back(CI2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002104 $1->getInstList().push_back($2);
2105 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002106 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002107 }
2108 | /* empty */ {
Reid Spencercd42c582006-12-05 23:29:42 +00002109 $$ = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002110 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002111
2112 // Make sure to move the basic block to the correct location in the
2113 // function, instead of leaving it inserted wherever it was first
2114 // referenced.
2115 Function::BasicBlockListType &BBL =
2116 CurFun.CurrentFunction->getBasicBlockList();
2117 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002118 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002119 }
2120 | LABELSTR {
Reid Spencercd42c582006-12-05 23:29:42 +00002121 $$ = getBBVal(ValID::create($1), true);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002122 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002123
2124 // Make sure to move the basic block to the correct location in the
2125 // function, instead of leaving it inserted wherever it was first
2126 // referenced.
2127 Function::BasicBlockListType &BBL =
2128 CurFun.CurrentFunction->getBasicBlockList();
2129 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002130 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002131 };
2132
2133BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencera132e042006-12-03 05:46:11 +00002134 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002135 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002136 }
2137 | RET VOID { // Return with no result...
2138 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002139 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002140 }
2141 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002142 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002143 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002144 $$ = new BranchInst(tmpBB);
Chris Lattner58af2a12006-02-15 07:22:58 +00002145 } // Conditional Branch...
2146 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002147 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002148 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002149 BasicBlock* tmpBBB = getBBVal($9);
2150 CHECK_FOR_ERROR
2151 Value* tmpVal = getVal(Type::BoolTy, $3);
2152 CHECK_FOR_ERROR
2153 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002154 }
2155 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002156 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002157 CHECK_FOR_ERROR
2158 BasicBlock* tmpBB = getBBVal($6);
2159 CHECK_FOR_ERROR
2160 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002161 $$ = S;
2162
2163 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2164 E = $8->end();
2165 for (; I != E; ++I) {
2166 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2167 S->addCase(CI, I->second);
2168 else
Reid Spencer61c83e02006-08-18 08:43:06 +00002169 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002170 }
2171 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002172 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002173 }
2174 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencera132e042006-12-03 05:46:11 +00002175 Value* tmpVal = getVal($2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002176 CHECK_FOR_ERROR
2177 BasicBlock* tmpBB = getBBVal($6);
2178 CHECK_FOR_ERROR
2179 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002180 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002181 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002182 }
2183 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2184 TO LABEL ValueRef UNWIND LABEL ValueRef {
2185 const PointerType *PFTy;
2186 const FunctionType *Ty;
2187
Reid Spencera132e042006-12-03 05:46:11 +00002188 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002189 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2190 // Pull out the types of all of the arguments...
2191 std::vector<const Type*> ParamTypes;
2192 if ($6) {
Reid Spencera132e042006-12-03 05:46:11 +00002193 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner58af2a12006-02-15 07:22:58 +00002194 I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00002195 ParamTypes.push_back((*I)->getType());
Chris Lattner58af2a12006-02-15 07:22:58 +00002196 }
2197
2198 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2199 if (isVarArg) ParamTypes.pop_back();
2200
Reid Spencera132e042006-12-03 05:46:11 +00002201 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattner58af2a12006-02-15 07:22:58 +00002202 PFTy = PointerType::get(Ty);
2203 }
2204
2205 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002206 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002207 BasicBlock *Normal = getBBVal($10);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002208 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002209 BasicBlock *Except = getBBVal($13);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002210 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002211
2212 // Create the call node...
2213 if (!$6) { // Has no arguments?
2214 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
2215 } else { // Has arguments?
2216 // Loop through FunctionType's arguments and ensure they are specified
2217 // correctly!
2218 //
2219 FunctionType::param_iterator I = Ty->param_begin();
2220 FunctionType::param_iterator E = Ty->param_end();
Reid Spencera132e042006-12-03 05:46:11 +00002221 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner58af2a12006-02-15 07:22:58 +00002222
Reid Spencera132e042006-12-03 05:46:11 +00002223 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
2224 if ((*ArgI)->getType() != *I)
2225 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2226 (*I)->getDescription() + "'!");
2227
2228 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
2229 GEN_ERROR("Invalid number of parameters detected!");
2230
2231 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner58af2a12006-02-15 07:22:58 +00002232 }
2233 cast<InvokeInst>($$)->setCallingConv($2);
2234
Reid Spencera132e042006-12-03 05:46:11 +00002235 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002236 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002237 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002238 }
2239 | UNWIND {
2240 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002241 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002242 }
2243 | UNREACHABLE {
2244 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002245 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002246 };
2247
2248
2249
2250JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2251 $$ = $1;
Reid Spencera132e042006-12-03 05:46:11 +00002252 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002253 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002254 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002255 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002256
Reid Spencer5b7e7532006-09-28 19:28:24 +00002257 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002258 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002259 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002260 }
2261 | IntType ConstValueRef ',' LABEL ValueRef {
2262 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002263 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002264 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002265
2266 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002267 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002268
Reid Spencer5b7e7532006-09-28 19:28:24 +00002269 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002270 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002271 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002272 };
2273
2274Inst : OptAssign InstVal {
2275 // Is this definition named?? if so, assign the name...
2276 setValueName($2, $1);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002277 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002278 InsertValue($2);
2279 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002280 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002281};
2282
2283PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
2284 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencera132e042006-12-03 05:46:11 +00002285 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002286 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002287 BasicBlock* tmpBB = getBBVal($5);
2288 CHECK_FOR_ERROR
2289 $$->push_back(std::make_pair(tmpVal, tmpBB));
Reid Spencera132e042006-12-03 05:46:11 +00002290 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002291 }
2292 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2293 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002294 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002295 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002296 BasicBlock* tmpBB = getBBVal($6);
2297 CHECK_FOR_ERROR
2298 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002299 };
2300
2301
2302ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Reid Spencera132e042006-12-03 05:46:11 +00002303 $$ = new std::vector<Value*>();
Chris Lattner58af2a12006-02-15 07:22:58 +00002304 $$->push_back($1);
2305 }
2306 | ValueRefList ',' ResolvedVal {
2307 $$ = $1;
2308 $1->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002309 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002310 };
2311
2312// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Reid Spencera132e042006-12-03 05:46:11 +00002313ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner58af2a12006-02-15 07:22:58 +00002314
2315OptTailCall : TAIL CALL {
2316 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002317 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002318 }
2319 | CALL {
2320 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002321 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002322 };
2323
Chris Lattner58af2a12006-02-15 07:22:58 +00002324InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00002325 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2326 !isa<PackedType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002327 GEN_ERROR(
Chris Lattner58af2a12006-02-15 07:22:58 +00002328 "Arithmetic operator requires integer, FP, or packed operands!");
Reid Spencera132e042006-12-03 05:46:11 +00002329 if (isa<PackedType>((*$2).get()) &&
2330 ($1 == Instruction::URem ||
2331 $1 == Instruction::SRem ||
2332 $1 == Instruction::FRem))
Reid Spencer3ed469c2006-11-02 20:25:50 +00002333 GEN_ERROR("U/S/FRem not supported on packed types!");
Reid Spencera132e042006-12-03 05:46:11 +00002334 Value* val1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002335 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002336 Value* val2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002337 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002338 $$ = BinaryOperator::create($1, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002339 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002340 GEN_ERROR("binary operator returned null!");
Reid Spencera132e042006-12-03 05:46:11 +00002341 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002342 }
2343 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00002344 if (!(*$2)->isIntegral()) {
2345 if (!isa<PackedType>($2->get()) ||
2346 !cast<PackedType>($2->get())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00002347 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002348 }
Reid Spencera132e042006-12-03 05:46:11 +00002349 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002350 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002351 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002352 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002353 $$ = BinaryOperator::create($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002354 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002355 GEN_ERROR("binary operator returned null!");
Reid Spencera132e042006-12-03 05:46:11 +00002356 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002357 }
2358 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00002359 if(isa<PackedType>((*$2).get())) {
Reid Spencer61c83e02006-08-18 08:43:06 +00002360 GEN_ERROR(
Chris Lattner58af2a12006-02-15 07:22:58 +00002361 "PackedTypes currently not supported in setcc instructions!");
2362 }
Reid Spencera132e042006-12-03 05:46:11 +00002363 Value* tmpVal1 = getVal(*$2, $3);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002364 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002365 Value* tmpVal2 = getVal(*$2, $5);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002366 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002367 $$ = new SetCondInst($1, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002368 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002369 GEN_ERROR("binary operator returned null!");
Reid Spencera132e042006-12-03 05:46:11 +00002370 delete $2;
2371 }
2372 | ICMP IPredicates Types ValueRef ',' ValueRef {
2373 if (isa<PackedType>((*$3).get()))
2374 GEN_ERROR("Packed types not supported by icmp instruction");
2375 Value* tmpVal1 = getVal(*$3, $4);
2376 CHECK_FOR_ERROR
2377 Value* tmpVal2 = getVal(*$3, $6);
2378 CHECK_FOR_ERROR
2379 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2380 if ($$ == 0)
2381 GEN_ERROR("icmp operator returned null!");
2382 }
2383 | FCMP FPredicates Types ValueRef ',' ValueRef {
2384 if (isa<PackedType>((*$3).get()))
2385 GEN_ERROR("Packed types not supported by fcmp instruction");
2386 Value* tmpVal1 = getVal(*$3, $4);
2387 CHECK_FOR_ERROR
2388 Value* tmpVal2 = getVal(*$3, $6);
2389 CHECK_FOR_ERROR
2390 $$ = CmpInst::create($1, $2, tmpVal1, tmpVal2);
2391 if ($$ == 0)
2392 GEN_ERROR("fcmp operator returned null!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002393 }
2394 | NOT ResolvedVal {
Reid Spencer481169e2006-12-01 00:33:46 +00002395 llvm_cerr << "WARNING: Use of eliminated 'not' instruction:"
Chris Lattner58af2a12006-02-15 07:22:58 +00002396 << " Replacing with 'xor'.\n";
2397
Reid Spencera132e042006-12-03 05:46:11 +00002398 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
Chris Lattner58af2a12006-02-15 07:22:58 +00002399 if (Ones == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002400 GEN_ERROR("Expected integral type for not instruction!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002401
Reid Spencera132e042006-12-03 05:46:11 +00002402 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner58af2a12006-02-15 07:22:58 +00002403 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002404 GEN_ERROR("Could not create a xor instruction!");
2405 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002406 }
2407 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002408 if ($4->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002409 GEN_ERROR("Shift amount must be ubyte!");
Reid Spencera132e042006-12-03 05:46:11 +00002410 if (!$2->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00002411 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencer3822ff52006-11-08 06:47:33 +00002412 CHECK_FOR_ERROR;
Reid Spencera132e042006-12-03 05:46:11 +00002413 $$ = new ShiftInst($1, $2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002414 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002415 }
Reid Spencer3da59db2006-11-27 01:05:10 +00002416 | CastOps ResolvedVal TO Types {
Reid Spencera132e042006-12-03 05:46:11 +00002417 Value* Val = $2;
2418 const Type* Ty = $4->get();
Reid Spencer3da59db2006-11-27 01:05:10 +00002419 if (!Val->getType()->isFirstClassType())
2420 GEN_ERROR("cast from a non-primitive type: '" +
2421 Val->getType()->getDescription() + "'!");
2422 if (!Ty->isFirstClassType())
2423 GEN_ERROR("cast to a non-primitive type: '" + Ty->getDescription() +"'!");
Reid Spencera132e042006-12-03 05:46:11 +00002424 $$ = CastInst::create($1, $2, $4->get());
2425 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002426 }
2427 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002428 if ($2->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002429 GEN_ERROR("select condition must be boolean!");
Reid Spencera132e042006-12-03 05:46:11 +00002430 if ($4->getType() != $6->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002431 GEN_ERROR("select value types should match!");
Reid Spencera132e042006-12-03 05:46:11 +00002432 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002433 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002434 }
2435 | VAARG ResolvedVal ',' Types {
2436 NewVarArgs = true;
Reid Spencera132e042006-12-03 05:46:11 +00002437 $$ = new VAArgInst($2, *$4);
2438 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002439 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002440 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002441 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002442 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencer61c83e02006-08-18 08:43:06 +00002443 GEN_ERROR("Invalid extractelement operands!");
Reid Spencera132e042006-12-03 05:46:11 +00002444 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002445 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002446 }
2447 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002448 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002449 GEN_ERROR("Invalid insertelement operands!");
Reid Spencera132e042006-12-03 05:46:11 +00002450 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002451 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002452 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00002453 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002454 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002455 GEN_ERROR("Invalid shufflevector operands!");
Reid Spencera132e042006-12-03 05:46:11 +00002456 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002457 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00002458 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002459 | PHI_TOK PHIList {
2460 const Type *Ty = $2->front().first->getType();
2461 if (!Ty->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002462 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002463 $$ = new PHINode(Ty);
2464 ((PHINode*)$$)->reserveOperandSpace($2->size());
2465 while ($2->begin() != $2->end()) {
2466 if ($2->front().first->getType() != Ty)
Reid Spencer61c83e02006-08-18 08:43:06 +00002467 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002468 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2469 $2->pop_front();
2470 }
2471 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002472 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002473 }
2474 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Reid Spencer3da59db2006-11-27 01:05:10 +00002475 const PointerType *PFTy = 0;
2476 const FunctionType *Ty = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +00002477
Reid Spencera132e042006-12-03 05:46:11 +00002478 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattner58af2a12006-02-15 07:22:58 +00002479 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2480 // Pull out the types of all of the arguments...
2481 std::vector<const Type*> ParamTypes;
2482 if ($6) {
Reid Spencera132e042006-12-03 05:46:11 +00002483 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner58af2a12006-02-15 07:22:58 +00002484 I != E; ++I)
Reid Spencera132e042006-12-03 05:46:11 +00002485 ParamTypes.push_back((*I)->getType());
Chris Lattner58af2a12006-02-15 07:22:58 +00002486 }
2487
2488 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2489 if (isVarArg) ParamTypes.pop_back();
2490
Reid Spencera132e042006-12-03 05:46:11 +00002491 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002492 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002493
Reid Spencera132e042006-12-03 05:46:11 +00002494 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattner58af2a12006-02-15 07:22:58 +00002495 PFTy = PointerType::get(Ty);
2496 }
2497
2498 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002499 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002500
2501 // Create the call node...
2502 if (!$6) { // Has no arguments?
2503 // Make sure no arguments is a good thing!
2504 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002505 GEN_ERROR("No arguments passed to a function that "
Chris Lattner58af2a12006-02-15 07:22:58 +00002506 "expects arguments!");
2507
2508 $$ = new CallInst(V, std::vector<Value*>());
2509 } else { // Has arguments?
2510 // Loop through FunctionType's arguments and ensure they are specified
2511 // correctly!
2512 //
2513 FunctionType::param_iterator I = Ty->param_begin();
2514 FunctionType::param_iterator E = Ty->param_end();
Reid Spencera132e042006-12-03 05:46:11 +00002515 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner58af2a12006-02-15 07:22:58 +00002516
Reid Spencera132e042006-12-03 05:46:11 +00002517 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
2518 if ((*ArgI)->getType() != *I)
2519 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2520 (*I)->getDescription() + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002521
2522 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002523 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002524
Reid Spencera132e042006-12-03 05:46:11 +00002525 $$ = new CallInst(V, *$6);
Chris Lattner58af2a12006-02-15 07:22:58 +00002526 }
2527 cast<CallInst>($$)->setTailCall($1);
2528 cast<CallInst>($$)->setCallingConv($2);
Reid Spencera132e042006-12-03 05:46:11 +00002529 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002530 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002531 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002532 }
2533 | MemoryInst {
2534 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002535 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002536 };
2537
2538
2539// IndexList - List of indices for GEP based instructions...
2540IndexList : ',' ValueRefList {
2541 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002542 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002543 } | /* empty */ {
Reid Spencera132e042006-12-03 05:46:11 +00002544 $$ = new std::vector<Value*>();
Reid Spencer61c83e02006-08-18 08:43:06 +00002545 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002546 };
2547
2548OptVolatile : VOLATILE {
2549 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002550 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002551 }
2552 | /* empty */ {
2553 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002554 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002555 };
2556
2557
2558
2559MemoryInst : MALLOC Types OptCAlign {
Reid Spencera132e042006-12-03 05:46:11 +00002560 $$ = new MallocInst(*$2, 0, $3);
2561 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002562 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002563 }
2564 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencera132e042006-12-03 05:46:11 +00002565 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002566 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002567 $$ = new MallocInst(*$2, tmpVal, $6);
2568 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002569 }
2570 | ALLOCA Types OptCAlign {
Reid Spencera132e042006-12-03 05:46:11 +00002571 $$ = new AllocaInst(*$2, 0, $3);
2572 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002573 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002574 }
2575 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencera132e042006-12-03 05:46:11 +00002576 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002577 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002578 $$ = new AllocaInst(*$2, tmpVal, $6);
2579 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002580 }
2581 | FREE ResolvedVal {
Reid Spencera132e042006-12-03 05:46:11 +00002582 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002583 GEN_ERROR("Trying to free nonpointer type " +
Reid Spencera132e042006-12-03 05:46:11 +00002584 $2->getType()->getDescription() + "!");
2585 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002586 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002587 }
2588
2589 | OptVolatile LOAD Types ValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00002590 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002591 GEN_ERROR("Can't load from nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00002592 (*$3)->getDescription());
2593 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002594 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Reid Spencera132e042006-12-03 05:46:11 +00002595 (*$3)->getDescription());
2596 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002597 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002598 $$ = new LoadInst(tmpVal, "", $1);
Reid Spencera132e042006-12-03 05:46:11 +00002599 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002600 }
2601 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencera132e042006-12-03 05:46:11 +00002602 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner58af2a12006-02-15 07:22:58 +00002603 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00002604 GEN_ERROR("Can't store to a nonpointer type: " +
Reid Spencera132e042006-12-03 05:46:11 +00002605 (*$5)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00002606 const Type *ElTy = PT->getElementType();
Reid Spencera132e042006-12-03 05:46:11 +00002607 if (ElTy != $3->getType())
2608 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Chris Lattner58af2a12006-02-15 07:22:58 +00002609 "' into space of type '" + ElTy->getDescription() + "'!");
2610
Reid Spencera132e042006-12-03 05:46:11 +00002611 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002612 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002613 $$ = new StoreInst($3, tmpVal, $1);
2614 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002615 }
2616 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencera132e042006-12-03 05:46:11 +00002617 if (!isa<PointerType>($2->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002618 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002619
Reid Spencera132e042006-12-03 05:46:11 +00002620 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer61c83e02006-08-18 08:43:06 +00002621 GEN_ERROR("Invalid getelementptr indices for type '" +
Reid Spencera132e042006-12-03 05:46:11 +00002622 (*$2)->getDescription()+ "'!");
2623 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002624 CHECK_FOR_ERROR
Reid Spencera132e042006-12-03 05:46:11 +00002625 $$ = new GetElementPtrInst(tmpVal, *$4);
2626 delete $2;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002627 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002628 };
2629
2630
2631%%
Reid Spencer61c83e02006-08-18 08:43:06 +00002632
2633void llvm::GenerateError(const std::string &message, int LineNo) {
2634 if (LineNo == -1) LineNo = llvmAsmlineno;
2635 // TODO: column number in exception
2636 if (TheParseError)
2637 TheParseError->setError(CurFilename, message, LineNo);
2638 TriggerError = 1;
2639}
2640
Chris Lattner58af2a12006-02-15 07:22:58 +00002641int yyerror(const char *ErrorMsg) {
2642 std::string where
2643 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2644 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2645 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2646 if (yychar == YYEMPTY || yychar == 0)
2647 errMsg += "end-of-file.";
2648 else
2649 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00002650 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00002651 return 0;
2652}