blob: 4611262b0121260b65f55db3ca2d65e835766593 [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"
21#include "llvm/Assembly/AutoUpgrade.h"
22#include "llvm/Support/GetElementPtrTypeIterator.h"
23#include "llvm/ADT/STLExtras.h"
24#include "llvm/Support/MathExtras.h"
25#include <algorithm>
26#include <iostream>
27#include <list>
28#include <utility>
29
Reid Spencere4f47592006-08-18 17:32:55 +000030// The following is a gross hack. In order to rid the libAsmParser library of
31// exceptions, we have to have a way of getting the yyparse function to go into
32// an error situation. So, whenever we want an error to occur, the GenerateError
33// function (see bottom of file) sets TriggerError. Then, at the end of each
34// production in the grammer we use CHECK_FOR_ERROR which will invoke YYERROR
35// (a goto) to put YACC in error state. Furthermore, several calls to
36// GenerateError are made from inside productions and they must simulate the
37// previous exception behavior by exiting the production immediately. We have
38// replaced these with the GEN_ERROR macro which calls GeneratError and then
39// immediately invokes YYERROR. This would be so much cleaner if it was a
40// recursive descent parser.
Reid Spencer61c83e02006-08-18 08:43:06 +000041static bool TriggerError = false;
Reid Spencerf63697d2006-10-09 17:36:59 +000042#define CHECK_FOR_ERROR { if (TriggerError) { TriggerError = false; YYABORT; } }
Reid Spencer61c83e02006-08-18 08:43:06 +000043#define GEN_ERROR(msg) { GenerateError(msg); YYERROR; }
44
Chris Lattner58af2a12006-02-15 07:22:58 +000045int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
46int yylex(); // declaration" of xxx warnings.
47int yyparse();
48
49namespace llvm {
50 std::string CurFilename;
51}
52using namespace llvm;
53
54static Module *ParserResult;
55
56// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
57// relating to upreferences in the input stream.
58//
59//#define DEBUG_UPREFS 1
60#ifdef DEBUG_UPREFS
61#define UR_OUT(X) std::cerr << X
62#else
63#define UR_OUT(X)
64#endif
65
66#define YYERROR_VERBOSE 1
67
68static bool ObsoleteVarArgs;
69static bool NewVarArgs;
70static BasicBlock *CurBB;
71static GlobalVariable *CurGV;
72
73
74// This contains info used when building the body of a function. It is
75// destroyed when the function is completed.
76//
77typedef std::vector<Value *> ValueList; // Numbered defs
78static void
79ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
80 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
81
82static struct PerModuleInfo {
83 Module *CurrentModule;
84 std::map<const Type *, ValueList> Values; // Module level numbered definitions
85 std::map<const Type *,ValueList> LateResolveValues;
86 std::vector<PATypeHolder> Types;
87 std::map<ValID, PATypeHolder> LateResolveTypes;
88
89 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Chris Lattner0ad19702006-06-21 16:53:00 +000090 /// how they were referenced and on which line of the input they came from so
Chris Lattner58af2a12006-02-15 07:22:58 +000091 /// that we can resolve them later and print error messages as appropriate.
92 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
93
94 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
95 // references to global values. Global values may be referenced before they
96 // are defined, and if so, the temporary object that they represent is held
97 // here. This is used for forward references of GlobalValues.
98 //
99 typedef std::map<std::pair<const PointerType *,
100 ValID>, GlobalValue*> GlobalRefsType;
101 GlobalRefsType GlobalRefs;
102
103 void ModuleDone() {
104 // If we could not resolve some functions at function compilation time
105 // (calls to functions before they are defined), resolve them now... Types
106 // are resolved when the constant pool has been completely parsed.
107 //
108 ResolveDefinitions(LateResolveValues);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000109 if (TriggerError)
110 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000111
112 // Check to make sure that all global value forward references have been
113 // resolved!
114 //
115 if (!GlobalRefs.empty()) {
116 std::string UndefinedReferences = "Unresolved global references exist:\n";
117
118 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
119 I != E; ++I) {
120 UndefinedReferences += " " + I->first.first->getDescription() + " " +
121 I->first.second.getName() + "\n";
122 }
Reid Spencer61c83e02006-08-18 08:43:06 +0000123 GenerateError(UndefinedReferences);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000124 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000125 }
126
127 // Look for intrinsic functions and CallInst that need to be upgraded
Chris Lattnerd5efe842006-04-08 01:18:56 +0000128 for (Module::iterator FI = CurrentModule->begin(),
129 FE = CurrentModule->end(); FI != FE; )
Chris Lattnerc2c60382006-03-04 07:53:41 +0000130 UpgradeCallsToIntrinsic(FI++);
Chris Lattner58af2a12006-02-15 07:22:58 +0000131
132 Values.clear(); // Clear out function local definitions
133 Types.clear();
134 CurrentModule = 0;
135 }
136
137 // GetForwardRefForGlobal - Check to see if there is a forward reference
138 // for this global. If so, remove it from the GlobalRefs map and return it.
139 // If not, just return null.
140 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
141 // Check to see if there is a forward reference to this global variable...
142 // if there is, eliminate it and patch the reference to use the new def'n.
143 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
144 GlobalValue *Ret = 0;
145 if (I != GlobalRefs.end()) {
146 Ret = I->second;
147 GlobalRefs.erase(I);
148 }
149 return Ret;
150 }
151} CurModule;
152
153static struct PerFunctionInfo {
154 Function *CurrentFunction; // Pointer to current function being created
155
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000156 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
Chris Lattner58af2a12006-02-15 07:22:58 +0000157 std::map<const Type*, ValueList> LateResolveValues;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000158 bool isDeclare; // Is this function a forward declararation?
159 GlobalValue::LinkageTypes Linkage; // Linkage for forward declaration.
Chris Lattner58af2a12006-02-15 07:22:58 +0000160
161 /// BBForwardRefs - When we see forward references to basic blocks, keep
162 /// track of them here.
163 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
164 std::vector<BasicBlock*> NumberedBlocks;
165 unsigned NextBBNum;
166
167 inline PerFunctionInfo() {
168 CurrentFunction = 0;
169 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000170 Linkage = GlobalValue::ExternalLinkage;
Chris Lattner58af2a12006-02-15 07:22:58 +0000171 }
172
173 inline void FunctionStart(Function *M) {
174 CurrentFunction = M;
175 NextBBNum = 0;
176 }
177
178 void FunctionDone() {
179 NumberedBlocks.clear();
180
181 // Any forward referenced blocks left?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000182 if (!BBForwardRefs.empty()) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000183 GenerateError("Undefined reference to label " +
Chris Lattner58af2a12006-02-15 07:22:58 +0000184 BBForwardRefs.begin()->first->getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000185 return;
186 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000187
188 // Resolve all forward references now.
189 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
190
191 Values.clear(); // Clear out function local definitions
192 CurrentFunction = 0;
193 isDeclare = false;
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000194 Linkage = GlobalValue::ExternalLinkage;
Chris Lattner58af2a12006-02-15 07:22:58 +0000195 }
196} CurFun; // Info for the current function...
197
198static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
199
200
201//===----------------------------------------------------------------------===//
202// Code to handle definitions of all the types
203//===----------------------------------------------------------------------===//
204
205static int InsertValue(Value *V,
206 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
207 if (V->hasName()) return -1; // Is this a numbered definition?
208
209 // Yes, insert the value into the value table...
210 ValueList &List = ValueTab[V->getType()];
211 List.push_back(V);
212 return List.size()-1;
213}
214
215static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
216 switch (D.Type) {
217 case ValID::NumberVal: // Is it a numbered definition?
218 // Module constants occupy the lowest numbered slots...
219 if ((unsigned)D.Num < CurModule.Types.size())
220 return CurModule.Types[(unsigned)D.Num];
221 break;
222 case ValID::NameVal: // Is it a named definition?
223 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
224 D.destroy(); // Free old strdup'd memory...
225 return N;
226 }
227 break;
228 default:
Reid Spencer61c83e02006-08-18 08:43:06 +0000229 GenerateError("Internal parser error: Invalid symbol type reference!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000230 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000231 }
232
233 // If we reached here, we referenced either a symbol that we don't know about
234 // or an id number that hasn't been read yet. We may be referencing something
235 // forward, so just create an entry to be resolved later and get to it...
236 //
237 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
238
239
240 if (inFunctionScope()) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000241 if (D.Type == ValID::NameVal) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000242 GenerateError("Reference to an undefined type: '" + D.getName() + "'");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000243 return 0;
244 } else {
Reid Spencer61c83e02006-08-18 08:43:06 +0000245 GenerateError("Reference to an undefined type: #" + itostr(D.Num));
Reid Spencer5b7e7532006-09-28 19:28:24 +0000246 return 0;
247 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000248 }
249
250 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
251 if (I != CurModule.LateResolveTypes.end())
252 return I->second;
253
254 Type *Typ = OpaqueType::get();
255 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
256 return Typ;
257 }
258
259static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
260 SymbolTable &SymTab =
261 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
262 CurModule.CurrentModule->getSymbolTable();
263 return SymTab.lookup(Ty, Name);
264}
265
266// getValNonImprovising - Look up the value specified by the provided type and
267// the provided ValID. If the value exists and has already been defined, return
268// it. Otherwise return null.
269//
270static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000271 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000272 GenerateError("Functions are not values and "
Chris Lattner58af2a12006-02-15 07:22:58 +0000273 "must be referenced as pointers");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000274 return 0;
275 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000276
277 switch (D.Type) {
278 case ValID::NumberVal: { // Is it a numbered definition?
279 unsigned Num = (unsigned)D.Num;
280
281 // Module constants occupy the lowest numbered slots...
282 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
283 if (VI != CurModule.Values.end()) {
284 if (Num < VI->second.size())
285 return VI->second[Num];
286 Num -= VI->second.size();
287 }
288
289 // Make sure that our type is within bounds
290 VI = CurFun.Values.find(Ty);
291 if (VI == CurFun.Values.end()) return 0;
292
293 // Check that the number is within bounds...
294 if (VI->second.size() <= Num) return 0;
295
296 return VI->second[Num];
297 }
298
299 case ValID::NameVal: { // Is it a named definition?
300 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
301 if (N == 0) return 0;
302
303 D.destroy(); // Free old strdup'd memory...
304 return N;
305 }
306
307 // Check to make sure that "Ty" is an integral type, and that our
308 // value will fit into the specified type...
309 case ValID::ConstSIntVal: // Is it a constant pool reference??
Reid Spencerb83eb642006-10-20 07:07:24 +0000310 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000311 GenerateError("Signed integral constant '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000312 itostr(D.ConstPool64) + "' is invalid for type '" +
313 Ty->getDescription() + "'!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000314 return 0;
315 }
Reid Spencerb83eb642006-10-20 07:07:24 +0000316 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000317
318 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Reid Spencerb83eb642006-10-20 07:07:24 +0000319 if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) {
320 if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000321 GenerateError("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattner58af2a12006-02-15 07:22:58 +0000322 "' is invalid or out of range!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000323 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000324 } else { // This is really a signed reference. Transmogrify.
Reid Spencerb83eb642006-10-20 07:07:24 +0000325 return ConstantInt::get(Ty, D.ConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000326 }
327 } else {
Reid Spencerb83eb642006-10-20 07:07:24 +0000328 return ConstantInt::get(Ty, D.UConstPool64);
Chris Lattner58af2a12006-02-15 07:22:58 +0000329 }
330
331 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000332 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000333 GenerateError("FP constant invalid for type!!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000334 return 0;
335 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000336 return ConstantFP::get(Ty, D.ConstPoolFP);
337
338 case ValID::ConstNullVal: // Is it a null value?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000339 if (!isa<PointerType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000340 GenerateError("Cannot create a a non pointer null!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000341 return 0;
342 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000343 return ConstantPointerNull::get(cast<PointerType>(Ty));
344
345 case ValID::ConstUndefVal: // Is it an undef value?
346 return UndefValue::get(Ty);
347
348 case ValID::ConstZeroVal: // Is it a zero value?
349 return Constant::getNullValue(Ty);
350
351 case ValID::ConstantVal: // Fully resolved constant?
Reid Spencer5b7e7532006-09-28 19:28:24 +0000352 if (D.ConstantValue->getType() != Ty) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000353 GenerateError("Constant expression type different from required type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000354 return 0;
355 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000356 return D.ConstantValue;
357
358 case ValID::InlineAsmVal: { // Inline asm expression
359 const PointerType *PTy = dyn_cast<PointerType>(Ty);
360 const FunctionType *FTy =
361 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000362 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000363 GenerateError("Invalid type for asm constraint string!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000364 return 0;
365 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000366 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
367 D.IAD->HasSideEffects);
368 D.destroy(); // Free InlineAsmDescriptor.
369 return IA;
370 }
371 default:
372 assert(0 && "Unhandled case!");
373 return 0;
374 } // End of switch
375
376 assert(0 && "Unhandled case!");
377 return 0;
378}
379
380// getVal - This function is identical to getValNonImprovising, except that if a
381// value is not already defined, it "improvises" by creating a placeholder var
382// that looks and acts just like the requested variable. When the value is
383// defined later, all uses of the placeholder variable are replaced with the
384// real thing.
385//
386static Value *getVal(const Type *Ty, const ValID &ID) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000387 if (Ty == Type::LabelTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000388 GenerateError("Cannot use a basic block here");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000389 return 0;
390 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000391
392 // See if the value has already been defined.
393 Value *V = getValNonImprovising(Ty, ID);
394 if (V) return V;
Reid Spencer5b7e7532006-09-28 19:28:24 +0000395 if (TriggerError) return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000396
Reid Spencer5b7e7532006-09-28 19:28:24 +0000397 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000398 GenerateError("Invalid use of a composite type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000399 return 0;
400 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000401
402 // If we reached here, we referenced either a symbol that we don't know about
403 // or an id number that hasn't been read yet. We may be referencing something
404 // forward, so just create an entry to be resolved later and get to it...
405 //
406 V = new Argument(Ty);
407
408 // Remember where this forward reference came from. FIXME, shouldn't we try
409 // to recycle these things??
410 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
411 llvmAsmlineno)));
412
413 if (inFunctionScope())
414 InsertValue(V, CurFun.LateResolveValues);
415 else
416 InsertValue(V, CurModule.LateResolveValues);
417 return V;
418}
419
420/// getBBVal - This is used for two purposes:
421/// * If isDefinition is true, a new basic block with the specified ID is being
422/// defined.
423/// * If isDefinition is true, this is a reference to a basic block, which may
424/// or may not be a forward reference.
425///
426static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
427 assert(inFunctionScope() && "Can't get basic block at global scope!");
428
429 std::string Name;
430 BasicBlock *BB = 0;
431 switch (ID.Type) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000432 default:
433 GenerateError("Illegal label reference " + ID.getName());
434 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000435 case ValID::NumberVal: // Is it a numbered definition?
436 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
437 CurFun.NumberedBlocks.resize(ID.Num+1);
438 BB = CurFun.NumberedBlocks[ID.Num];
439 break;
440 case ValID::NameVal: // Is it a named definition?
441 Name = ID.Name;
442 if (Value *N = CurFun.CurrentFunction->
443 getSymbolTable().lookup(Type::LabelTy, Name))
444 BB = cast<BasicBlock>(N);
445 break;
446 }
447
448 // See if the block has already been defined.
449 if (BB) {
450 // If this is the definition of the block, make sure the existing value was
451 // just a forward reference. If it was a forward reference, there will be
452 // an entry for it in the PlaceHolderInfo map.
Reid Spencer5b7e7532006-09-28 19:28:24 +0000453 if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000454 // The existing value was a definition, not a forward reference.
Reid Spencer61c83e02006-08-18 08:43:06 +0000455 GenerateError("Redefinition of label " + ID.getName());
Reid Spencer5b7e7532006-09-28 19:28:24 +0000456 return 0;
457 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000458
459 ID.destroy(); // Free strdup'd memory.
460 return BB;
461 }
462
463 // Otherwise this block has not been seen before.
464 BB = new BasicBlock("", CurFun.CurrentFunction);
465 if (ID.Type == ValID::NameVal) {
466 BB->setName(ID.Name);
467 } else {
468 CurFun.NumberedBlocks[ID.Num] = BB;
469 }
470
471 // If this is not a definition, keep track of it so we can use it as a forward
472 // reference.
473 if (!isDefinition) {
474 // Remember where this forward reference came from.
475 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
476 } else {
477 // The forward declaration could have been inserted anywhere in the
478 // function: insert it into the correct place now.
479 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
480 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
481 }
482 ID.destroy();
483 return BB;
484}
485
486
487//===----------------------------------------------------------------------===//
488// Code to handle forward references in instructions
489//===----------------------------------------------------------------------===//
490//
491// This code handles the late binding needed with statements that reference
492// values not defined yet... for example, a forward branch, or the PHI node for
493// a loop body.
494//
495// This keeps a table (CurFun.LateResolveValues) of all such forward references
496// and back patchs after we are done.
497//
498
499// ResolveDefinitions - If we could not resolve some defs at parsing
500// time (forward branches, phi functions for loops, etc...) resolve the
501// defs now...
502//
503static void
504ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
505 std::map<const Type*,ValueList> *FutureLateResolvers) {
506 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
507 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
508 E = LateResolvers.end(); LRI != E; ++LRI) {
509 ValueList &List = LRI->second;
510 while (!List.empty()) {
511 Value *V = List.back();
512 List.pop_back();
513
514 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
515 CurModule.PlaceHolderInfo.find(V);
516 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
517
518 ValID &DID = PHI->second.first;
519
520 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000521 if (TriggerError)
522 return;
Chris Lattner58af2a12006-02-15 07:22:58 +0000523 if (TheRealValue) {
524 V->replaceAllUsesWith(TheRealValue);
525 delete V;
526 CurModule.PlaceHolderInfo.erase(PHI);
527 } else if (FutureLateResolvers) {
528 // Functions have their unresolved items forwarded to the module late
529 // resolver table
530 InsertValue(V, *FutureLateResolvers);
531 } else {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000532 if (DID.Type == ValID::NameVal) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000533 GenerateError("Reference to an invalid definition: '" +DID.getName()+
Chris Lattner58af2a12006-02-15 07:22:58 +0000534 "' of type '" + V->getType()->getDescription() + "'",
535 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000536 return;
537 } else {
Reid Spencer61c83e02006-08-18 08:43:06 +0000538 GenerateError("Reference to an invalid definition: #" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000539 itostr(DID.Num) + " of type '" +
540 V->getType()->getDescription() + "'",
541 PHI->second.second);
Reid Spencer5b7e7532006-09-28 19:28:24 +0000542 return;
543 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000544 }
545 }
546 }
547
548 LateResolvers.clear();
549}
550
551// ResolveTypeTo - A brand new type was just declared. This means that (if
552// name is not null) things referencing Name can be resolved. Otherwise, things
553// refering to the number can be resolved. Do this now.
554//
555static void ResolveTypeTo(char *Name, const Type *ToTy) {
556 ValID D;
557 if (Name) D = ValID::create(Name);
558 else D = ValID::create((int)CurModule.Types.size());
559
560 std::map<ValID, PATypeHolder>::iterator I =
561 CurModule.LateResolveTypes.find(D);
562 if (I != CurModule.LateResolveTypes.end()) {
563 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
564 CurModule.LateResolveTypes.erase(I);
565 }
566}
567
568// setValueName - Set the specified value to the name given. The name may be
569// null potentially, in which case this is a noop. The string passed in is
570// assumed to be a malloc'd string buffer, and is free'd by this function.
571//
572static void setValueName(Value *V, char *NameStr) {
573 if (NameStr) {
574 std::string Name(NameStr); // Copy string
575 free(NameStr); // Free old string
576
Reid Spencer5b7e7532006-09-28 19:28:24 +0000577 if (V->getType() == Type::VoidTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000578 GenerateError("Can't assign name '" + Name+"' to value with void type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000579 return;
580 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000581
582 assert(inFunctionScope() && "Must be in function scope!");
583 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
Reid Spencer5b7e7532006-09-28 19:28:24 +0000584 if (ST.lookup(V->getType(), Name)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000585 GenerateError("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000586 V->getType()->getDescription() + "' type plane!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000587 return;
588 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000589
590 // Set the name.
591 V->setName(Name);
592 }
593}
594
595/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
596/// this is a declaration, otherwise it is a definition.
597static GlobalVariable *
598ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
599 bool isConstantGlobal, const Type *Ty,
600 Constant *Initializer) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000601 if (isa<FunctionType>(Ty)) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000602 GenerateError("Cannot declare global vars of function type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000603 return 0;
604 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000605
606 const PointerType *PTy = PointerType::get(Ty);
607
608 std::string Name;
609 if (NameStr) {
610 Name = NameStr; // Copy string
611 free(NameStr); // Free old string
612 }
613
614 // See if this global value was forward referenced. If so, recycle the
615 // object.
616 ValID ID;
617 if (!Name.empty()) {
618 ID = ValID::create((char*)Name.c_str());
619 } else {
620 ID = ValID::create((int)CurModule.Values[PTy].size());
621 }
622
623 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
624 // Move the global to the end of the list, from whereever it was
625 // previously inserted.
626 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
627 CurModule.CurrentModule->getGlobalList().remove(GV);
628 CurModule.CurrentModule->getGlobalList().push_back(GV);
629 GV->setInitializer(Initializer);
630 GV->setLinkage(Linkage);
631 GV->setConstant(isConstantGlobal);
632 InsertValue(GV, CurModule.Values);
633 return GV;
634 }
635
636 // If this global has a name, check to see if there is already a definition
637 // of this global in the module. If so, merge as appropriate. Note that
638 // this is really just a hack around problems in the CFE. :(
639 if (!Name.empty()) {
640 // We are a simple redefinition of a value, check to see if it is defined
641 // the same as the old one.
642 if (GlobalVariable *EGV =
643 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
644 // We are allowed to redefine a global variable in two circumstances:
645 // 1. If at least one of the globals is uninitialized or
646 // 2. If both initializers have the same value.
647 //
648 if (!EGV->hasInitializer() || !Initializer ||
649 EGV->getInitializer() == Initializer) {
650
651 // Make sure the existing global version gets the initializer! Make
652 // sure that it also gets marked const if the new version is.
653 if (Initializer && !EGV->hasInitializer())
654 EGV->setInitializer(Initializer);
655 if (isConstantGlobal)
656 EGV->setConstant(true);
657 EGV->setLinkage(Linkage);
658 return EGV;
659 }
660
Reid Spencer61c83e02006-08-18 08:43:06 +0000661 GenerateError("Redefinition of global variable named '" + Name +
Chris Lattner58af2a12006-02-15 07:22:58 +0000662 "' in the '" + Ty->getDescription() + "' type plane!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000663 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000664 }
665 }
666
667 // Otherwise there is no existing GV to use, create one now.
668 GlobalVariable *GV =
669 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
670 CurModule.CurrentModule);
671 InsertValue(GV, CurModule.Values);
672 return GV;
673}
674
675// setTypeName - Set the specified type to the name given. The name may be
676// null potentially, in which case this is a noop. The string passed in is
677// assumed to be a malloc'd string buffer, and is freed by this function.
678//
679// This function returns true if the type has already been defined, but is
680// allowed to be redefined in the specified context. If the name is a new name
681// for the type plane, it is inserted and false is returned.
682static bool setTypeName(const Type *T, char *NameStr) {
683 assert(!inFunctionScope() && "Can't give types function-local names!");
684 if (NameStr == 0) return false;
685
686 std::string Name(NameStr); // Copy string
687 free(NameStr); // Free old string
688
689 // We don't allow assigning names to void type
Reid Spencer5b7e7532006-09-28 19:28:24 +0000690 if (T == Type::VoidTy) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000691 GenerateError("Can't assign name '" + Name + "' to the void type!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000692 return false;
693 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000694
695 // Set the type name, checking for conflicts as we do so.
696 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
697
698 if (AlreadyExists) { // Inserting a name that is already defined???
699 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
700 assert(Existing && "Conflict but no matching type?");
701
702 // There is only one case where this is allowed: when we are refining an
703 // opaque type. In this case, Existing will be an opaque type.
704 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
705 // We ARE replacing an opaque type!
706 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
707 return true;
708 }
709
710 // Otherwise, this is an attempt to redefine a type. That's okay if
711 // the redefinition is identical to the original. This will be so if
712 // Existing and T point to the same Type object. In this one case we
713 // allow the equivalent redefinition.
714 if (Existing == T) return true; // Yes, it's equal.
715
716 // Any other kind of (non-equivalent) redefinition is an error.
Reid Spencer61c83e02006-08-18 08:43:06 +0000717 GenerateError("Redefinition of type named '" + Name + "' in the '" +
Chris Lattner58af2a12006-02-15 07:22:58 +0000718 T->getDescription() + "' type plane!");
719 }
720
721 return false;
722}
723
724//===----------------------------------------------------------------------===//
725// Code for handling upreferences in type names...
726//
727
728// TypeContains - Returns true if Ty directly contains E in it.
729//
730static bool TypeContains(const Type *Ty, const Type *E) {
731 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
732 E) != Ty->subtype_end();
733}
734
735namespace {
736 struct UpRefRecord {
737 // NestingLevel - The number of nesting levels that need to be popped before
738 // this type is resolved.
739 unsigned NestingLevel;
740
741 // LastContainedTy - This is the type at the current binding level for the
742 // type. Every time we reduce the nesting level, this gets updated.
743 const Type *LastContainedTy;
744
745 // UpRefTy - This is the actual opaque type that the upreference is
746 // represented with.
747 OpaqueType *UpRefTy;
748
749 UpRefRecord(unsigned NL, OpaqueType *URTy)
750 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
751 };
752}
753
754// UpRefs - A list of the outstanding upreferences that need to be resolved.
755static std::vector<UpRefRecord> UpRefs;
756
757/// HandleUpRefs - Every time we finish a new layer of types, this function is
758/// called. It loops through the UpRefs vector, which is a list of the
759/// currently active types. For each type, if the up reference is contained in
760/// the newly completed type, we decrement the level count. When the level
761/// count reaches zero, the upreferenced type is the type that is passed in:
762/// thus we can complete the cycle.
763///
764static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner224f84f2006-08-18 17:34:45 +0000765 // If Ty isn't abstract, or if there are no up-references in it, then there is
766 // nothing to resolve here.
767 if (!ty->isAbstract() || UpRefs.empty()) return ty;
768
Chris Lattner58af2a12006-02-15 07:22:58 +0000769 PATypeHolder Ty(ty);
770 UR_OUT("Type '" << Ty->getDescription() <<
771 "' newly formed. Resolving upreferences.\n" <<
772 UpRefs.size() << " upreferences active!\n");
773
774 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
775 // to zero), we resolve them all together before we resolve them to Ty. At
776 // the end of the loop, if there is anything to resolve to Ty, it will be in
777 // this variable.
778 OpaqueType *TypeToResolve = 0;
779
780 for (unsigned i = 0; i != UpRefs.size(); ++i) {
781 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
782 << UpRefs[i].second->getDescription() << ") = "
783 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
784 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
785 // Decrement level of upreference
786 unsigned Level = --UpRefs[i].NestingLevel;
787 UpRefs[i].LastContainedTy = Ty;
788 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
789 if (Level == 0) { // Upreference should be resolved!
790 if (!TypeToResolve) {
791 TypeToResolve = UpRefs[i].UpRefTy;
792 } else {
793 UR_OUT(" * Resolving upreference for "
794 << UpRefs[i].second->getDescription() << "\n";
795 std::string OldName = UpRefs[i].UpRefTy->getDescription());
796 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
797 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
798 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
799 }
800 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
801 --i; // Do not skip the next element...
802 }
803 }
804 }
805
806 if (TypeToResolve) {
807 UR_OUT(" * Resolving upreference for "
808 << UpRefs[i].second->getDescription() << "\n";
809 std::string OldName = TypeToResolve->getDescription());
810 TypeToResolve->refineAbstractTypeTo(Ty);
811 }
812
813 return Ty;
814}
815
Reid Spencer1628cec2006-10-26 06:15:43 +0000816/// This function is used to obtain the correct opcode for an instruction when
817/// an obsolete opcode is encountered. The OI parameter (OpcodeInfo) has both
818/// an opcode and an "obsolete" flag. These are generated by the lexer and
819/// the "obsolete" member will be true when the lexer encounters the token for
820/// an obsolete opcode. For example, "div" was replaced by [usf]div but we need
821/// to maintain backwards compatibility for asm files that still have the "div"
822/// instruction. This function handles converting div -> [usf]div appropriately.
Reid Spencer3822ff52006-11-08 06:47:33 +0000823/// @brief Convert obsolete BinaryOps opcodes to new values
Reid Spencer1628cec2006-10-26 06:15:43 +0000824static void
825sanitizeOpCode(OpcodeInfo<Instruction::BinaryOps> &OI, const PATypeHolder& PATy)
826{
827 // If its not obsolete, don't do anything
828 if (!OI.obsolete)
829 return;
830
831 // If its a packed type we want to use the element type
832 const Type* Ty = PATy;
833 if (const PackedType* PTy = dyn_cast<PackedType>(Ty))
834 Ty = PTy->getElementType();
835
836 // Depending on the opcode ..
837 switch (OI.opcode) {
838 default:
Reid Spencer3ed469c2006-11-02 20:25:50 +0000839 GenerateError("Invalid obsolete opCode (check Lexer.l)");
Reid Spencer1628cec2006-10-26 06:15:43 +0000840 break;
841 case Instruction::UDiv:
842 // Handle cases where the opcode needs to change
843 if (Ty->isFloatingPoint())
844 OI.opcode = Instruction::FDiv;
845 else if (Ty->isSigned())
846 OI.opcode = Instruction::SDiv;
847 break;
Reid Spencer3ed469c2006-11-02 20:25:50 +0000848 case Instruction::URem:
849 if (Ty->isFloatingPoint())
850 OI.opcode = Instruction::FRem;
851 else if (Ty->isSigned())
852 OI.opcode = Instruction::SRem;
853 break;
Reid Spencer1628cec2006-10-26 06:15:43 +0000854 }
855 // Its not obsolete any more, we fixed it.
856 OI.obsolete = false;
857}
Reid Spencer3822ff52006-11-08 06:47:33 +0000858
859/// This function is similar to the previous overload of sanitizeOpCode but
860/// operates on Instruction::OtherOps instead of Instruction::BinaryOps.
861/// @brief Convert obsolete OtherOps opcodes to new values
862static void
863sanitizeOpCode(OpcodeInfo<Instruction::OtherOps> &OI, const PATypeHolder& PATy)
864{
865 // If its not obsolete, don't do anything
866 if (!OI.obsolete)
867 return;
868
869 const Type* Ty = PATy; // type conversion
870 switch (OI.opcode) {
871 default:
872 GenerateError("Invalid obsolete opcode (check Lexer.l)");
873 break;
874 case Instruction::LShr:
875 if (Ty->isSigned())
876 OI.opcode = Instruction::AShr;
877 break;
878 }
879 // Its not obsolete any more, we fixed it.
880 OI.obsolete = false;
881}
882
Chris Lattner58af2a12006-02-15 07:22:58 +0000883// common code from the two 'RunVMAsmParser' functions
Reid Spencer5b7e7532006-09-28 19:28:24 +0000884static Module* RunParser(Module * M) {
Chris Lattner58af2a12006-02-15 07:22:58 +0000885
886 llvmAsmlineno = 1; // Reset the current line number...
887 ObsoleteVarArgs = false;
888 NewVarArgs = false;
Chris Lattner58af2a12006-02-15 07:22:58 +0000889 CurModule.CurrentModule = M;
Reid Spencerf63697d2006-10-09 17:36:59 +0000890
891 // Check to make sure the parser succeeded
892 if (yyparse()) {
893 if (ParserResult)
894 delete ParserResult;
895 return 0;
896 }
897
898 // Check to make sure that parsing produced a result
Reid Spencer61c83e02006-08-18 08:43:06 +0000899 if (!ParserResult)
900 return 0;
Chris Lattner58af2a12006-02-15 07:22:58 +0000901
Reid Spencerf63697d2006-10-09 17:36:59 +0000902 // Reset ParserResult variable while saving its value for the result.
Chris Lattner58af2a12006-02-15 07:22:58 +0000903 Module *Result = ParserResult;
904 ParserResult = 0;
905
906 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
907 {
908 Function* F;
909 if ((F = Result->getNamedFunction("llvm.va_start"))
910 && F->getFunctionType()->getNumParams() == 0)
911 ObsoleteVarArgs = true;
912 if((F = Result->getNamedFunction("llvm.va_copy"))
913 && F->getFunctionType()->getNumParams() == 1)
914 ObsoleteVarArgs = true;
915 }
916
Reid Spencer5b7e7532006-09-28 19:28:24 +0000917 if (ObsoleteVarArgs && NewVarArgs) {
918 GenerateError(
919 "This file is corrupt: it uses both new and old style varargs");
920 return 0;
921 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000922
923 if(ObsoleteVarArgs) {
924 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000925 if (F->arg_size() != 0) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000926 GenerateError("Obsolete va_start takes 0 argument!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000927 return 0;
928 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000929
930 //foo = va_start()
931 // ->
932 //bar = alloca typeof(foo)
933 //va_start(bar)
934 //foo = load bar
935
936 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
937 const Type* ArgTy = F->getFunctionType()->getReturnType();
938 const Type* ArgTyPtr = PointerType::get(ArgTy);
939 Function* NF = Result->getOrInsertFunction("llvm.va_start",
940 RetTy, ArgTyPtr, (Type *)0);
941
942 while (!F->use_empty()) {
943 CallInst* CI = cast<CallInst>(F->use_back());
944 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
945 new CallInst(NF, bar, "", CI);
946 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
947 CI->replaceAllUsesWith(foo);
948 CI->getParent()->getInstList().erase(CI);
949 }
950 Result->getFunctionList().erase(F);
951 }
952
953 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000954 if(F->arg_size() != 1) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000955 GenerateError("Obsolete va_end takes 1 argument!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000956 return 0;
957 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000958
959 //vaend foo
960 // ->
961 //bar = alloca 1 of typeof(foo)
962 //vaend bar
963 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
964 const Type* ArgTy = F->getFunctionType()->getParamType(0);
965 const Type* ArgTyPtr = PointerType::get(ArgTy);
966 Function* NF = Result->getOrInsertFunction("llvm.va_end",
967 RetTy, ArgTyPtr, (Type *)0);
968
969 while (!F->use_empty()) {
970 CallInst* CI = cast<CallInst>(F->use_back());
971 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
972 new StoreInst(CI->getOperand(1), bar, CI);
973 new CallInst(NF, bar, "", CI);
974 CI->getParent()->getInstList().erase(CI);
975 }
976 Result->getFunctionList().erase(F);
977 }
978
979 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Reid Spencer5b7e7532006-09-28 19:28:24 +0000980 if(F->arg_size() != 1) {
Reid Spencer61c83e02006-08-18 08:43:06 +0000981 GenerateError("Obsolete va_copy takes 1 argument!");
Reid Spencer5b7e7532006-09-28 19:28:24 +0000982 return 0;
983 }
Chris Lattner58af2a12006-02-15 07:22:58 +0000984 //foo = vacopy(bar)
985 // ->
986 //a = alloca 1 of typeof(foo)
987 //b = alloca 1 of typeof(foo)
988 //store bar -> b
989 //vacopy(a, b)
990 //foo = load a
991
992 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
993 const Type* ArgTy = F->getFunctionType()->getReturnType();
994 const Type* ArgTyPtr = PointerType::get(ArgTy);
995 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
996 RetTy, ArgTyPtr, ArgTyPtr,
997 (Type *)0);
998
999 while (!F->use_empty()) {
1000 CallInst* CI = cast<CallInst>(F->use_back());
1001 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
1002 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
1003 new StoreInst(CI->getOperand(1), b, CI);
1004 new CallInst(NF, a, b, "", CI);
1005 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
1006 CI->replaceAllUsesWith(foo);
1007 CI->getParent()->getInstList().erase(CI);
1008 }
1009 Result->getFunctionList().erase(F);
1010 }
1011 }
1012
1013 return Result;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001014}
Chris Lattner58af2a12006-02-15 07:22:58 +00001015
1016//===----------------------------------------------------------------------===//
1017// RunVMAsmParser - Define an interface to this parser
1018//===----------------------------------------------------------------------===//
1019//
1020Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
1021 set_scan_file(F);
1022
1023 CurFilename = Filename;
1024 return RunParser(new Module(CurFilename));
1025}
1026
1027Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
1028 set_scan_string(AsmString);
1029
1030 CurFilename = "from_memory";
1031 if (M == NULL) {
1032 return RunParser(new Module (CurFilename));
1033 } else {
1034 return RunParser(M);
1035 }
1036}
1037
1038%}
1039
1040%union {
1041 llvm::Module *ModuleVal;
1042 llvm::Function *FunctionVal;
1043 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
1044 llvm::BasicBlock *BasicBlockVal;
1045 llvm::TerminatorInst *TermInstVal;
1046 llvm::Instruction *InstVal;
1047 llvm::Constant *ConstVal;
1048
1049 const llvm::Type *PrimType;
1050 llvm::PATypeHolder *TypeVal;
1051 llvm::Value *ValueVal;
1052
1053 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
1054 std::vector<llvm::Value*> *ValueList;
1055 std::list<llvm::PATypeHolder> *TypeList;
1056 // Represent the RHS of PHI node
1057 std::list<std::pair<llvm::Value*,
1058 llvm::BasicBlock*> > *PHIList;
1059 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
1060 std::vector<llvm::Constant*> *ConstVector;
1061
1062 llvm::GlobalValue::LinkageTypes Linkage;
1063 int64_t SInt64Val;
1064 uint64_t UInt64Val;
1065 int SIntVal;
1066 unsigned UIntVal;
1067 double FPVal;
1068 bool BoolVal;
1069
1070 char *StrVal; // This memory is strdup'd!
Reid Spencer1628cec2006-10-26 06:15:43 +00001071 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner58af2a12006-02-15 07:22:58 +00001072
Reid Spencer1628cec2006-10-26 06:15:43 +00001073 BinaryOpInfo BinaryOpVal;
1074 TermOpInfo TermOpVal;
1075 MemOpInfo MemOpVal;
1076 OtherOpInfo OtherOpVal;
1077 llvm::Module::Endianness Endianness;
Chris Lattner58af2a12006-02-15 07:22:58 +00001078}
1079
1080%type <ModuleVal> Module FunctionList
1081%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
1082%type <BasicBlockVal> BasicBlock InstructionList
1083%type <TermInstVal> BBTerminatorInst
1084%type <InstVal> Inst InstVal MemoryInst
1085%type <ConstVal> ConstVal ConstExpr
1086%type <ConstVector> ConstVector
1087%type <ArgList> ArgList ArgListH
1088%type <ArgVal> ArgVal
1089%type <PHIList> PHIList
1090%type <ValueList> ValueRefList ValueRefListE // For call param lists
1091%type <ValueList> IndexList // For GEP derived indices
1092%type <TypeList> TypeListI ArgTypeListI
1093%type <JumpTable> JumpTable
1094%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
1095%type <BoolVal> OptVolatile // 'volatile' or not
1096%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
1097%type <BoolVal> OptSideEffect // 'sideeffect' or not.
1098%type <Linkage> OptLinkage
1099%type <Endianness> BigOrLittle
1100
1101// ValueRef - Unresolved reference to a definition or BB
1102%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
1103%type <ValueVal> ResolvedVal // <type> <valref> pair
1104// Tokens and types for handling constant integer values
1105//
1106// ESINT64VAL - A negative number within long long range
1107%token <SInt64Val> ESINT64VAL
1108
1109// EUINT64VAL - A positive number within uns. long long range
1110%token <UInt64Val> EUINT64VAL
1111%type <SInt64Val> EINT64VAL
1112
1113%token <SIntVal> SINTVAL // Signed 32 bit ints...
1114%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
1115%type <SIntVal> INTVAL
1116%token <FPVal> FPVAL // Float or Double constant
1117
1118// Built in types...
1119%type <TypeVal> Types TypesV UpRTypes UpRTypesV
1120%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
1121%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
1122%token <PrimType> FLOAT DOUBLE TYPE LABEL
1123
1124%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
1125%type <StrVal> Name OptName OptAssign
1126%type <UIntVal> OptAlign OptCAlign
1127%type <StrVal> OptSection SectionString
1128
1129%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
1130%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001131%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
1132%token DLLIMPORT DLLEXPORT EXTERN_WEAK
Chris Lattner58af2a12006-02-15 07:22:58 +00001133%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
1134%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner75466192006-05-19 21:28:53 +00001135%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001136%token X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
Chris Lattner1ae022f2006-10-22 06:08:13 +00001137%token DATALAYOUT
Chris Lattner58af2a12006-02-15 07:22:58 +00001138%type <UIntVal> OptCallingConv
1139
1140// Basic Block Terminating Operators
1141%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
1142
1143// Binary Operators
1144%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Reid Spencer3ed469c2006-11-02 20:25:50 +00001145%token <BinaryOpVal> ADD SUB MUL UDIV SDIV FDIV UREM SREM FREM AND OR XOR
Reid Spencer1628cec2006-10-26 06:15:43 +00001146%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Chris Lattner58af2a12006-02-15 07:22:58 +00001147
1148// Memory Instructions
1149%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
1150
1151// Other Operators
1152%type <OtherOpVal> ShiftOps
Reid Spencer3822ff52006-11-08 06:47:33 +00001153%token <OtherOpVal> PHI_TOK CAST SELECT SHL LSHR ASHR VAARG
Chris Lattnerd5efe842006-04-08 01:18:56 +00001154%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Chris Lattner58af2a12006-02-15 07:22:58 +00001155%token VAARG_old VANEXT_old //OBSOLETE
1156
1157
1158%start Module
1159%%
1160
1161// Handle constant integer size restriction and conversion...
1162//
1163INTVAL : SINTVAL;
1164INTVAL : UINTVAL {
1165 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001166 GEN_ERROR("Value too large for type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001167 $$ = (int32_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001168 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001169};
1170
1171
1172EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
1173EINT64VAL : EUINT64VAL {
1174 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
Reid Spencer61c83e02006-08-18 08:43:06 +00001175 GEN_ERROR("Value too large for type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001176 $$ = (int64_t)$1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001177 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001178};
1179
1180// Operations that are notably excluded from this list include:
1181// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1182//
Reid Spencer3ed469c2006-11-02 20:25:50 +00001183ArithmeticOps: ADD | SUB | MUL | UDIV | SDIV | FDIV | UREM | SREM | FREM;
Chris Lattner58af2a12006-02-15 07:22:58 +00001184LogicalOps : AND | OR | XOR;
1185SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
1186
Reid Spencer3822ff52006-11-08 06:47:33 +00001187ShiftOps : SHL | LSHR | ASHR;
Chris Lattner58af2a12006-02-15 07:22:58 +00001188
1189// These are some types that allow classification if we only want a particular
1190// thing... for example, only a signed, unsigned, or integral type.
1191SIntType : LONG | INT | SHORT | SBYTE;
1192UIntType : ULONG | UINT | USHORT | UBYTE;
1193IntType : SIntType | UIntType;
1194FPType : FLOAT | DOUBLE;
1195
1196// OptAssign - Value producing statements have an optional assignment component
1197OptAssign : Name '=' {
1198 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001199 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001200 }
1201 | /*empty*/ {
1202 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001203 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001204 };
1205
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001206OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1207 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
1208 WEAK { $$ = GlobalValue::WeakLinkage; } |
1209 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1210 DLLIMPORT { $$ = GlobalValue::DLLImportLinkage; } |
1211 DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; } |
1212 EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } |
1213 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner58af2a12006-02-15 07:22:58 +00001214
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001215OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1216 CCC_TOK { $$ = CallingConv::C; } |
1217 CSRETCC_TOK { $$ = CallingConv::CSRet; } |
1218 FASTCC_TOK { $$ = CallingConv::Fast; } |
1219 COLDCC_TOK { $$ = CallingConv::Cold; } |
1220 X86_STDCALLCC_TOK { $$ = CallingConv::X86_StdCall; } |
1221 X86_FASTCALLCC_TOK { $$ = CallingConv::X86_FastCall; } |
1222 CC_TOK EUINT64VAL {
Chris Lattner58af2a12006-02-15 07:22:58 +00001223 if ((unsigned)$2 != $2)
Reid Spencer61c83e02006-08-18 08:43:06 +00001224 GEN_ERROR("Calling conv too large!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001225 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001226 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001227 };
1228
1229// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1230// a comma before it.
1231OptAlign : /*empty*/ { $$ = 0; } |
1232 ALIGN EUINT64VAL {
1233 $$ = $2;
1234 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001235 GEN_ERROR("Alignment must be a power of two!");
1236 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001237};
1238OptCAlign : /*empty*/ { $$ = 0; } |
1239 ',' ALIGN EUINT64VAL {
1240 $$ = $3;
1241 if ($$ != 0 && !isPowerOf2_32($$))
Reid Spencer61c83e02006-08-18 08:43:06 +00001242 GEN_ERROR("Alignment must be a power of two!");
1243 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001244};
1245
1246
1247SectionString : SECTION STRINGCONSTANT {
1248 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1249 if ($2[i] == '"' || $2[i] == '\\')
Reid Spencer61c83e02006-08-18 08:43:06 +00001250 GEN_ERROR("Invalid character in section name!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001251 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001252 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001253};
1254
1255OptSection : /*empty*/ { $$ = 0; } |
1256 SectionString { $$ = $1; };
1257
1258// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1259// is set to be the global we are processing.
1260//
1261GlobalVarAttributes : /* empty */ {} |
1262 ',' GlobalVarAttribute GlobalVarAttributes {};
1263GlobalVarAttribute : SectionString {
1264 CurGV->setSection($1);
1265 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001266 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001267 }
1268 | ALIGN EUINT64VAL {
1269 if ($2 != 0 && !isPowerOf2_32($2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001270 GEN_ERROR("Alignment must be a power of two!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001271 CurGV->setAlignment($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001272 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001273 };
1274
1275//===----------------------------------------------------------------------===//
1276// Types includes all predefined types... except void, because it can only be
1277// used in specific contexts (function returning void for example). To have
1278// access to it, a user must explicitly use TypesV.
1279//
1280
1281// TypesV includes all of 'Types', but it also includes the void type.
1282TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1283UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
1284
1285Types : UpRTypes {
1286 if (!UpRefs.empty())
Reid Spencer61c83e02006-08-18 08:43:06 +00001287 GEN_ERROR("Invalid upreference in type: " + (*$1)->getDescription());
Chris Lattner58af2a12006-02-15 07:22:58 +00001288 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001289 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001290 };
1291
1292
1293// Derived types are added later...
1294//
1295PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1296PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
1297UpRTypes : OPAQUE {
1298 $$ = new PATypeHolder(OpaqueType::get());
Reid Spencer61c83e02006-08-18 08:43:06 +00001299 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001300 }
1301 | PrimType {
1302 $$ = new PATypeHolder($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001303 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001304 };
1305UpRTypes : SymbolicValueRef { // Named types are also simple types...
Reid Spencer5b7e7532006-09-28 19:28:24 +00001306 const Type* tmp = getTypeVal($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001307 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00001308 $$ = new PATypeHolder(tmp);
Chris Lattner58af2a12006-02-15 07:22:58 +00001309};
1310
1311// Include derived types in the Types production.
1312//
1313UpRTypes : '\\' EUINT64VAL { // Type UpReference
Reid Spencer61c83e02006-08-18 08:43:06 +00001314 if ($2 > (uint64_t)~0U) GEN_ERROR("Value out of range!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001315 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
1316 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
1317 $$ = new PATypeHolder(OT);
1318 UR_OUT("New Upreference!\n");
Reid Spencer61c83e02006-08-18 08:43:06 +00001319 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001320 }
1321 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
1322 std::vector<const Type*> Params;
1323 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1324 E = $3->end(); I != E; ++I)
1325 Params.push_back(*I);
1326 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1327 if (isVarArg) Params.pop_back();
1328
1329 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
1330 delete $3; // Delete the argument list
1331 delete $1; // Delete the return type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001332 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001333 }
1334 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
1335 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
1336 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001337 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001338 }
1339 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1340 const llvm::Type* ElemTy = $4->get();
1341 if ((unsigned)$2 != $2)
Reid Spencer61c83e02006-08-18 08:43:06 +00001342 GEN_ERROR("Unsigned result not equal to signed result");
Chris Lattner58af2a12006-02-15 07:22:58 +00001343 if (!ElemTy->isPrimitiveType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001344 GEN_ERROR("Elemental type of a PackedType must be primitive");
Chris Lattner58af2a12006-02-15 07:22:58 +00001345 if (!isPowerOf2_32($2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001346 GEN_ERROR("Vector length should be a power of 2!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001347 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1348 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001349 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001350 }
1351 | '{' TypeListI '}' { // Structure type?
1352 std::vector<const Type*> Elements;
1353 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1354 E = $2->end(); I != E; ++I)
1355 Elements.push_back(*I);
1356
1357 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
1358 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00001359 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001360 }
1361 | '{' '}' { // Empty structure type?
1362 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Reid Spencer61c83e02006-08-18 08:43:06 +00001363 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001364 }
1365 | UpRTypes '*' { // Pointer type?
Chris Lattner59c85e92006-10-15 23:27:25 +00001366 if (*$1 == Type::LabelTy)
1367 GEN_ERROR("Cannot form a pointer to a basic block");
Chris Lattner58af2a12006-02-15 07:22:58 +00001368 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
1369 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001370 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001371 };
1372
1373// TypeList - Used for struct declarations and as a basis for function type
1374// declaration type lists
1375//
1376TypeListI : UpRTypes {
1377 $$ = new std::list<PATypeHolder>();
1378 $$->push_back(*$1); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001379 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001380 }
1381 | TypeListI ',' UpRTypes {
1382 ($$=$1)->push_back(*$3); delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001383 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001384 };
1385
1386// ArgTypeList - List of types for a function type declaration...
1387ArgTypeListI : TypeListI
1388 | TypeListI ',' DOTDOTDOT {
1389 ($$=$1)->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001390 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001391 }
1392 | DOTDOTDOT {
1393 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Reid Spencer61c83e02006-08-18 08:43:06 +00001394 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001395 }
1396 | /*empty*/ {
1397 $$ = new std::list<PATypeHolder>();
Reid Spencer61c83e02006-08-18 08:43:06 +00001398 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001399 };
1400
1401// ConstVal - The various declarations that go into the constant pool. This
1402// production is used ONLY to represent constants that show up AFTER a 'const',
1403// 'constant' or 'global' token at global scope. Constants that can be inlined
1404// into other expressions (such as integers and constexprs) are handled by the
1405// ResolvedVal, ValueRef and ConstValueRef productions.
1406//
1407ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1408 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1409 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001410 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001411 (*$1)->getDescription() + "'!");
1412 const Type *ETy = ATy->getElementType();
1413 int NumElements = ATy->getNumElements();
1414
1415 // Verify that we have the correct size...
1416 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001417 GEN_ERROR("Type mismatch: constant sized array initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001418 utostr($3->size()) + " arguments, but has size of " +
1419 itostr(NumElements) + "!");
1420
1421 // Verify all elements are correct type!
1422 for (unsigned i = 0; i < $3->size(); i++) {
1423 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001424 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001425 ETy->getDescription() +"' as required!\nIt is of type '"+
1426 (*$3)[i]->getType()->getDescription() + "'.");
1427 }
1428
1429 $$ = ConstantArray::get(ATy, *$3);
1430 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001431 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001432 }
1433 | Types '[' ']' {
1434 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1435 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001436 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001437 (*$1)->getDescription() + "'!");
1438
1439 int NumElements = ATy->getNumElements();
1440 if (NumElements != -1 && NumElements != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001441 GEN_ERROR("Type mismatch: constant sized array initialized with 0"
Chris Lattner58af2a12006-02-15 07:22:58 +00001442 " arguments, but has size of " + itostr(NumElements) +"!");
1443 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
1444 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001445 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001446 }
1447 | Types 'c' STRINGCONSTANT {
1448 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
1449 if (ATy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001450 GEN_ERROR("Cannot make array constant with type: '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001451 (*$1)->getDescription() + "'!");
1452
1453 int NumElements = ATy->getNumElements();
1454 const Type *ETy = ATy->getElementType();
1455 char *EndStr = UnEscapeLexed($3, true);
1456 if (NumElements != -1 && NumElements != (EndStr-$3))
Reid Spencer61c83e02006-08-18 08:43:06 +00001457 GEN_ERROR("Can't build string constant of size " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001458 itostr((int)(EndStr-$3)) +
1459 " when array has size " + itostr(NumElements) + "!");
1460 std::vector<Constant*> Vals;
1461 if (ETy == Type::SByteTy) {
1462 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
Reid Spencerb83eb642006-10-20 07:07:24 +00001463 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner58af2a12006-02-15 07:22:58 +00001464 } else if (ETy == Type::UByteTy) {
1465 for (unsigned char *C = (unsigned char *)$3;
1466 C != (unsigned char*)EndStr; ++C)
Reid Spencerb83eb642006-10-20 07:07:24 +00001467 Vals.push_back(ConstantInt::get(ETy, *C));
Chris Lattner58af2a12006-02-15 07:22:58 +00001468 } else {
1469 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001470 GEN_ERROR("Cannot build string arrays of non byte sized elements!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001471 }
1472 free($3);
1473 $$ = ConstantArray::get(ATy, Vals);
1474 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001475 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001476 }
1477 | Types '<' ConstVector '>' { // Nonempty unsized arr
1478 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1479 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001480 GEN_ERROR("Cannot make packed constant with type: '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001481 (*$1)->getDescription() + "'!");
1482 const Type *ETy = PTy->getElementType();
1483 int NumElements = PTy->getNumElements();
1484
1485 // Verify that we have the correct size...
1486 if (NumElements != -1 && NumElements != (int)$3->size())
Reid Spencer61c83e02006-08-18 08:43:06 +00001487 GEN_ERROR("Type mismatch: constant sized packed initialized with " +
Chris Lattner58af2a12006-02-15 07:22:58 +00001488 utostr($3->size()) + " arguments, but has size of " +
1489 itostr(NumElements) + "!");
1490
1491 // Verify all elements are correct type!
1492 for (unsigned i = 0; i < $3->size(); i++) {
1493 if (ETy != (*$3)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001494 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001495 ETy->getDescription() +"' as required!\nIt is of type '"+
1496 (*$3)[i]->getType()->getDescription() + "'.");
1497 }
1498
1499 $$ = ConstantPacked::get(PTy, *$3);
1500 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001501 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001502 }
1503 | Types '{' ConstVector '}' {
1504 const StructType *STy = dyn_cast<StructType>($1->get());
1505 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001506 GEN_ERROR("Cannot make struct constant with type: '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001507 (*$1)->getDescription() + "'!");
1508
1509 if ($3->size() != STy->getNumContainedTypes())
Reid Spencer61c83e02006-08-18 08:43:06 +00001510 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001511
1512 // Check to ensure that constants are compatible with the type initializer!
1513 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1514 if ((*$3)[i]->getType() != STy->getElementType(i))
Reid Spencer61c83e02006-08-18 08:43:06 +00001515 GEN_ERROR("Expected type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001516 STy->getElementType(i)->getDescription() +
1517 "' for element #" + utostr(i) +
1518 " of structure initializer!");
1519
1520 $$ = ConstantStruct::get(STy, *$3);
1521 delete $1; delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001522 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001523 }
1524 | Types '{' '}' {
1525 const StructType *STy = dyn_cast<StructType>($1->get());
1526 if (STy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001527 GEN_ERROR("Cannot make struct constant with type: '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001528 (*$1)->getDescription() + "'!");
1529
1530 if (STy->getNumContainedTypes() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001531 GEN_ERROR("Illegal number of initializers for structure type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001532
1533 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1534 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001535 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001536 }
1537 | Types NULL_TOK {
1538 const PointerType *PTy = dyn_cast<PointerType>($1->get());
1539 if (PTy == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001540 GEN_ERROR("Cannot make null pointer constant with type: '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001541 (*$1)->getDescription() + "'!");
1542
1543 $$ = ConstantPointerNull::get(PTy);
1544 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001545 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001546 }
1547 | Types UNDEF {
1548 $$ = UndefValue::get($1->get());
1549 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001550 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001551 }
1552 | Types SymbolicValueRef {
1553 const PointerType *Ty = dyn_cast<PointerType>($1->get());
1554 if (Ty == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00001555 GEN_ERROR("Global const reference must be a pointer type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001556
1557 // ConstExprs can exist in the body of a function, thus creating
1558 // GlobalValues whenever they refer to a variable. Because we are in
1559 // the context of a function, getValNonImprovising will search the functions
1560 // symbol table instead of the module symbol table for the global symbol,
1561 // which throws things all off. To get around this, we just tell
1562 // getValNonImprovising that we are at global scope here.
1563 //
1564 Function *SavedCurFn = CurFun.CurrentFunction;
1565 CurFun.CurrentFunction = 0;
1566
1567 Value *V = getValNonImprovising(Ty, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00001568 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001569
1570 CurFun.CurrentFunction = SavedCurFn;
1571
1572 // If this is an initializer for a constant pointer, which is referencing a
1573 // (currently) undefined variable, create a stub now that shall be replaced
1574 // in the future with the right type of variable.
1575 //
1576 if (V == 0) {
1577 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1578 const PointerType *PT = cast<PointerType>(Ty);
1579
1580 // First check to see if the forward references value is already created!
1581 PerModuleInfo::GlobalRefsType::iterator I =
1582 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
1583
1584 if (I != CurModule.GlobalRefs.end()) {
1585 V = I->second; // Placeholder already exists, use it...
1586 $2.destroy();
1587 } else {
1588 std::string Name;
1589 if ($2.Type == ValID::NameVal) Name = $2.Name;
1590
1591 // Create the forward referenced global.
1592 GlobalValue *GV;
1593 if (const FunctionType *FTy =
1594 dyn_cast<FunctionType>(PT->getElementType())) {
1595 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1596 CurModule.CurrentModule);
1597 } else {
1598 GV = new GlobalVariable(PT->getElementType(), false,
1599 GlobalValue::ExternalLinkage, 0,
1600 Name, CurModule.CurrentModule);
1601 }
1602
1603 // Keep track of the fact that we have a forward ref to recycle it
1604 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1605 V = GV;
1606 }
1607 }
1608
1609 $$ = cast<GlobalValue>(V);
1610 delete $1; // Free the type handle
Reid Spencer61c83e02006-08-18 08:43:06 +00001611 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001612 }
1613 | Types ConstExpr {
1614 if ($1->get() != $2->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001615 GEN_ERROR("Mismatched types for constant expression!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001616 $$ = $2;
1617 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001618 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001619 }
1620 | Types ZEROINITIALIZER {
1621 const Type *Ty = $1->get();
1622 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
Reid Spencer61c83e02006-08-18 08:43:06 +00001623 GEN_ERROR("Cannot create a null initialized value of this type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001624 $$ = Constant::getNullValue(Ty);
1625 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001626 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001627 };
1628
1629ConstVal : SIntType EINT64VAL { // integral constants
Reid Spencerb83eb642006-10-20 07:07:24 +00001630 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001631 GEN_ERROR("Constant value doesn't fit in type!");
Reid Spencerb83eb642006-10-20 07:07:24 +00001632 $$ = ConstantInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001633 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001634 }
1635 | UIntType EUINT64VAL { // integral constants
Reid Spencerb83eb642006-10-20 07:07:24 +00001636 if (!ConstantInt::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001637 GEN_ERROR("Constant value doesn't fit in type!");
Reid Spencerb83eb642006-10-20 07:07:24 +00001638 $$ = ConstantInt::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001639 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001640 }
1641 | BOOL TRUETOK { // Boolean constants
Chris Lattner47811b72006-09-28 23:35:22 +00001642 $$ = ConstantBool::getTrue();
Reid Spencer61c83e02006-08-18 08:43:06 +00001643 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001644 }
1645 | BOOL FALSETOK { // Boolean constants
Chris Lattner47811b72006-09-28 23:35:22 +00001646 $$ = ConstantBool::getFalse();
Reid Spencer61c83e02006-08-18 08:43:06 +00001647 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001648 }
1649 | FPType FPVAL { // Float & Double constants
1650 if (!ConstantFP::isValueValidForType($1, $2))
Reid Spencer61c83e02006-08-18 08:43:06 +00001651 GEN_ERROR("Floating point constant invalid for type!!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001652 $$ = ConstantFP::get($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001653 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001654 };
1655
1656
1657ConstExpr: CAST '(' ConstVal TO Types ')' {
1658 if (!$3->getType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001659 GEN_ERROR("cast constant expression from a non-primitive type: '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001660 $3->getType()->getDescription() + "'!");
1661 if (!$5->get()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001662 GEN_ERROR("cast constant expression to a non-primitive type: '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00001663 $5->get()->getDescription() + "'!");
1664 $$ = ConstantExpr::getCast($3, $5->get());
1665 delete $5;
Reid Spencer61c83e02006-08-18 08:43:06 +00001666 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001667 }
1668 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1669 if (!isa<PointerType>($3->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00001670 GEN_ERROR("GetElementPtr requires a pointer operand!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001671
1672 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1673 // indices to uint struct indices for compatibility.
1674 generic_gep_type_iterator<std::vector<Value*>::iterator>
1675 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1676 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1677 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1678 if (isa<StructType>(*GTI)) // Only change struct indices
Reid Spencerb83eb642006-10-20 07:07:24 +00001679 if (ConstantInt *CUI = dyn_cast<ConstantInt>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00001680 if (CUI->getType() == Type::UByteTy)
1681 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1682
1683 const Type *IdxTy =
1684 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
1685 if (!IdxTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001686 GEN_ERROR("Index list invalid for constant getelementptr!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001687
1688 std::vector<Constant*> IdxVec;
1689 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1690 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
1691 IdxVec.push_back(C);
1692 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001693 GEN_ERROR("Indices to constant getelementptr must be constants!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001694
1695 delete $4;
1696
1697 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Reid Spencer61c83e02006-08-18 08:43:06 +00001698 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001699 }
1700 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1701 if ($3->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001702 GEN_ERROR("Select condition must be of boolean type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001703 if ($5->getType() != $7->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001704 GEN_ERROR("Select operand types must match!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001705 $$ = ConstantExpr::getSelect($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001706 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001707 }
1708 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
1709 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001710 GEN_ERROR("Binary operator types must match!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001711 // First, make sure we're dealing with the right opcode by upgrading from
1712 // obsolete versions.
1713 sanitizeOpCode($1,$3->getType());
1714 CHECK_FOR_ERROR;
1715
Chris Lattner58af2a12006-02-15 07:22:58 +00001716 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1717 // To retain backward compatibility with these early compilers, we emit a
1718 // cast to the appropriate integer type automatically if we are in the
1719 // broken case. See PR424 for more information.
1720 if (!isa<PointerType>($3->getType())) {
Reid Spencer1628cec2006-10-26 06:15:43 +00001721 $$ = ConstantExpr::get($1.opcode, $3, $5);
Chris Lattner58af2a12006-02-15 07:22:58 +00001722 } else {
1723 const Type *IntPtrTy = 0;
1724 switch (CurModule.CurrentModule->getPointerSize()) {
1725 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1726 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
Reid Spencer61c83e02006-08-18 08:43:06 +00001727 default: GEN_ERROR("invalid pointer binary constant expr!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001728 }
Reid Spencer1628cec2006-10-26 06:15:43 +00001729 $$ = ConstantExpr::get($1.opcode, ConstantExpr::getCast($3, IntPtrTy),
Chris Lattner58af2a12006-02-15 07:22:58 +00001730 ConstantExpr::getCast($5, IntPtrTy));
1731 $$ = ConstantExpr::getCast($$, $3->getType());
1732 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001733 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001734 }
1735 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1736 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001737 GEN_ERROR("Logical operator types must match!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001738 if (!$3->getType()->isIntegral()) {
1739 if (!isa<PackedType>($3->getType()) ||
1740 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00001741 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001742 }
Reid Spencer1628cec2006-10-26 06:15:43 +00001743 $$ = ConstantExpr::get($1.opcode, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001744 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001745 }
1746 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1747 if ($3->getType() != $5->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00001748 GEN_ERROR("setcc operand types must match!");
Reid Spencer1628cec2006-10-26 06:15:43 +00001749 $$ = ConstantExpr::get($1.opcode, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001750 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001751 }
1752 | ShiftOps '(' ConstVal ',' ConstVal ')' {
1753 if ($5->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001754 GEN_ERROR("Shift count for shift constant must be unsigned byte!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001755 if (!$3->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00001756 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencer3822ff52006-11-08 06:47:33 +00001757 // Handle opcode upgrade situations
1758 sanitizeOpCode($1, $3->getType());
1759 CHECK_FOR_ERROR;
Reid Spencer1628cec2006-10-26 06:15:43 +00001760 $$ = ConstantExpr::get($1.opcode, $3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001761 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001762 }
1763 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Chris Lattnerf4bd7d82006-04-08 04:09:02 +00001764 if (!ExtractElementInst::isValidOperands($3, $5))
Reid Spencer61c83e02006-08-18 08:43:06 +00001765 GEN_ERROR("Invalid extractelement operands!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001766 $$ = ConstantExpr::getExtractElement($3, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001767 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001768 }
1769 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Chris Lattnerf4bd7d82006-04-08 04:09:02 +00001770 if (!InsertElementInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001771 GEN_ERROR("Invalid insertelement operands!");
Chris Lattnerd25db202006-04-08 03:55:17 +00001772 $$ = ConstantExpr::getInsertElement($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001773 CHECK_FOR_ERROR
Chris Lattnerd25db202006-04-08 03:55:17 +00001774 }
1775 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1776 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
Reid Spencer61c83e02006-08-18 08:43:06 +00001777 GEN_ERROR("Invalid shufflevector operands!");
Chris Lattnerd25db202006-04-08 03:55:17 +00001778 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Reid Spencer61c83e02006-08-18 08:43:06 +00001779 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001780 };
1781
Chris Lattnerd25db202006-04-08 03:55:17 +00001782
Chris Lattner58af2a12006-02-15 07:22:58 +00001783// ConstVector - A list of comma separated constants.
1784ConstVector : ConstVector ',' ConstVal {
1785 ($$ = $1)->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001786 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001787 }
1788 | ConstVal {
1789 $$ = new std::vector<Constant*>();
1790 $$->push_back($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001791 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001792 };
1793
1794
1795// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1796GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
1797
1798
1799//===----------------------------------------------------------------------===//
1800// Rules to match Modules
1801//===----------------------------------------------------------------------===//
1802
1803// Module rule: Capture the result of parsing the whole file into a result
1804// variable...
1805//
1806Module : FunctionList {
1807 $$ = ParserResult = $1;
1808 CurModule.ModuleDone();
Reid Spencerf63697d2006-10-09 17:36:59 +00001809 CHECK_FOR_ERROR;
Chris Lattner58af2a12006-02-15 07:22:58 +00001810};
1811
1812// FunctionList - A list of functions, preceeded by a constant pool.
1813//
1814FunctionList : FunctionList Function {
1815 $$ = $1;
1816 CurFun.FunctionDone();
Reid Spencer61c83e02006-08-18 08:43:06 +00001817 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001818 }
1819 | FunctionList FunctionProto {
1820 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001821 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001822 }
1823 | FunctionList MODULE ASM_TOK AsmBlock {
1824 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001825 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001826 }
1827 | FunctionList IMPLEMENTATION {
1828 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001829 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001830 }
1831 | ConstPool {
1832 $$ = CurModule.CurrentModule;
1833 // Emit an error if there are any unresolved types left.
1834 if (!CurModule.LateResolveTypes.empty()) {
1835 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
Reid Spencer61c83e02006-08-18 08:43:06 +00001836 if (DID.Type == ValID::NameVal) {
1837 GEN_ERROR("Reference to an undefined type: '"+DID.getName() + "'");
1838 } else {
1839 GEN_ERROR("Reference to an undefined type: #" + itostr(DID.Num));
1840 }
Chris Lattner58af2a12006-02-15 07:22:58 +00001841 }
Reid Spencer61c83e02006-08-18 08:43:06 +00001842 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001843 };
1844
1845// ConstPool - Constants with optional names assigned to them.
1846ConstPool : ConstPool OptAssign TYPE TypesV {
1847 // Eagerly resolve types. This is not an optimization, this is a
1848 // requirement that is due to the fact that we could have this:
1849 //
1850 // %list = type { %list * }
1851 // %list = type { %list * } ; repeated type decl
1852 //
1853 // If types are not resolved eagerly, then the two types will not be
1854 // determined to be the same type!
1855 //
1856 ResolveTypeTo($2, *$4);
1857
1858 if (!setTypeName(*$4, $2) && !$2) {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001859 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001860 // If this is a named type that is not a redefinition, add it to the slot
1861 // table.
1862 CurModule.Types.push_back(*$4);
1863 }
1864
1865 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00001866 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001867 }
1868 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001869 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001870 }
1871 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer61c83e02006-08-18 08:43:06 +00001872 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001873 }
1874 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001875 if ($5 == 0)
1876 GEN_ERROR("Global value initializer is not a constant!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001877 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00001878 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00001879 } GlobalVarAttributes {
1880 CurGV = 0;
Chris Lattner58af2a12006-02-15 07:22:58 +00001881 }
1882 | ConstPool OptAssign EXTERNAL GlobalType Types {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001883 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
1884 CHECK_FOR_ERROR
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001885 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001886 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001887 CurGV = 0;
1888 CHECK_FOR_ERROR
1889 }
1890 | ConstPool OptAssign DLLIMPORT GlobalType Types {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001891 CurGV = ParseGlobalVariable($2, GlobalValue::DLLImportLinkage, $4, *$5, 0);
1892 CHECK_FOR_ERROR
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001893 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001894 } GlobalVarAttributes {
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00001895 CurGV = 0;
1896 CHECK_FOR_ERROR
1897 }
1898 | ConstPool OptAssign EXTERN_WEAK GlobalType Types {
Reid Spencer5b7e7532006-09-28 19:28:24 +00001899 CurGV =
1900 ParseGlobalVariable($2, GlobalValue::ExternalWeakLinkage, $4, *$5, 0);
1901 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001902 delete $5;
Reid Spencer5b7e7532006-09-28 19:28:24 +00001903 } GlobalVarAttributes {
Chris Lattner58af2a12006-02-15 07:22:58 +00001904 CurGV = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00001905 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001906 }
1907 | ConstPool TARGET TargetDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001908 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001909 }
1910 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer61c83e02006-08-18 08:43:06 +00001911 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001912 }
1913 | /* empty: end of list */ {
1914 };
1915
1916
1917AsmBlock : STRINGCONSTANT {
1918 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
1919 char *EndStr = UnEscapeLexed($1, true);
1920 std::string NewAsm($1, EndStr);
1921 free($1);
1922
1923 if (AsmSoFar.empty())
1924 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
1925 else
1926 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Reid Spencer61c83e02006-08-18 08:43:06 +00001927 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001928};
1929
1930BigOrLittle : BIG { $$ = Module::BigEndian; };
1931BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1932
1933TargetDefinition : ENDIAN '=' BigOrLittle {
1934 CurModule.CurrentModule->setEndianness($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001935 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001936 }
1937 | POINTERSIZE '=' EUINT64VAL {
1938 if ($3 == 32)
1939 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1940 else if ($3 == 64)
1941 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1942 else
Reid Spencer61c83e02006-08-18 08:43:06 +00001943 GEN_ERROR("Invalid pointer size: '" + utostr($3) + "'!");
1944 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001945 }
1946 | TRIPLE '=' STRINGCONSTANT {
1947 CurModule.CurrentModule->setTargetTriple($3);
1948 free($3);
John Criswell2f6a8b12006-10-24 19:09:48 +00001949 }
Chris Lattner1ae022f2006-10-22 06:08:13 +00001950 | DATALAYOUT '=' STRINGCONSTANT {
Owen Anderson1dc69692006-10-18 02:21:48 +00001951 CurModule.CurrentModule->setDataLayout($3);
1952 free($3);
Owen Anderson1dc69692006-10-18 02:21:48 +00001953 };
Chris Lattner58af2a12006-02-15 07:22:58 +00001954
1955LibrariesDefinition : '[' LibList ']';
1956
1957LibList : LibList ',' STRINGCONSTANT {
1958 CurModule.CurrentModule->addLibrary($3);
1959 free($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00001960 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001961 }
1962 | STRINGCONSTANT {
1963 CurModule.CurrentModule->addLibrary($1);
1964 free($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00001965 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001966 }
1967 | /* empty: end of list */ {
Reid Spencer61c83e02006-08-18 08:43:06 +00001968 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001969 }
1970 ;
1971
1972//===----------------------------------------------------------------------===//
1973// Rules to match Function Headers
1974//===----------------------------------------------------------------------===//
1975
1976Name : VAR_ID | STRINGCONSTANT;
1977OptName : Name | /*empty*/ { $$ = 0; };
1978
1979ArgVal : Types OptName {
1980 if (*$1 == Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00001981 GEN_ERROR("void typed arguments are invalid!");
Chris Lattner58af2a12006-02-15 07:22:58 +00001982 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Reid Spencer61c83e02006-08-18 08:43:06 +00001983 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001984};
1985
1986ArgListH : ArgListH ',' ArgVal {
1987 $$ = $1;
1988 $1->push_back(*$3);
1989 delete $3;
Reid Spencer61c83e02006-08-18 08:43:06 +00001990 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001991 }
1992 | ArgVal {
1993 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1994 $$->push_back(*$1);
1995 delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00001996 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00001997 };
1998
1999ArgList : ArgListH {
2000 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002001 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002002 }
2003 | ArgListH ',' DOTDOTDOT {
2004 $$ = $1;
2005 $$->push_back(std::pair<PATypeHolder*,
2006 char*>(new PATypeHolder(Type::VoidTy), 0));
Reid Spencer61c83e02006-08-18 08:43:06 +00002007 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002008 }
2009 | DOTDOTDOT {
2010 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
2011 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Reid Spencer61c83e02006-08-18 08:43:06 +00002012 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002013 }
2014 | /* empty */ {
2015 $$ = 0;
Reid Spencer61c83e02006-08-18 08:43:06 +00002016 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002017 };
2018
2019FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
2020 OptSection OptAlign {
2021 UnEscapeLexed($3);
2022 std::string FunctionName($3);
2023 free($3); // Free strdup'd memory!
2024
2025 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002026 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002027
2028 std::vector<const Type*> ParamTypeList;
2029 if ($5) { // If there are arguments...
2030 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
2031 I != $5->end(); ++I)
2032 ParamTypeList.push_back(I->first->get());
2033 }
2034
2035 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
2036 if (isVarArg) ParamTypeList.pop_back();
2037
2038 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
2039 const PointerType *PFT = PointerType::get(FT);
2040 delete $2;
2041
2042 ValID ID;
2043 if (!FunctionName.empty()) {
2044 ID = ValID::create((char*)FunctionName.c_str());
2045 } else {
2046 ID = ValID::create((int)CurModule.Values[PFT].size());
2047 }
2048
2049 Function *Fn = 0;
2050 // See if this function was forward referenced. If so, recycle the object.
2051 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
2052 // Move the function to the end of the list, from whereever it was
2053 // previously inserted.
2054 Fn = cast<Function>(FWRef);
2055 CurModule.CurrentModule->getFunctionList().remove(Fn);
2056 CurModule.CurrentModule->getFunctionList().push_back(Fn);
2057 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
2058 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
2059 // If this is the case, either we need to be a forward decl, or it needs
2060 // to be.
2061 if (!CurFun.isDeclare && !Fn->isExternal())
Reid Spencer61c83e02006-08-18 08:43:06 +00002062 GEN_ERROR("Redefinition of function '" + FunctionName + "'!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002063
2064 // Make sure to strip off any argument names so we can't get conflicts.
2065 if (Fn->isExternal())
2066 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
2067 AI != AE; ++AI)
2068 AI->setName("");
Chris Lattner58af2a12006-02-15 07:22:58 +00002069 } else { // Not already defined?
2070 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
2071 CurModule.CurrentModule);
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002072
Chris Lattner58af2a12006-02-15 07:22:58 +00002073 InsertValue(Fn, CurModule.Values);
2074 }
2075
2076 CurFun.FunctionStart(Fn);
Anton Korobeynikov93c2b372006-09-17 13:06:18 +00002077
2078 if (CurFun.isDeclare) {
2079 // If we have declaration, always overwrite linkage. This will allow us to
2080 // correctly handle cases, when pointer to function is passed as argument to
2081 // another function.
2082 Fn->setLinkage(CurFun.Linkage);
2083 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002084 Fn->setCallingConv($1);
2085 Fn->setAlignment($8);
2086 if ($7) {
2087 Fn->setSection($7);
2088 free($7);
2089 }
2090
2091 // Add all of the arguments we parsed to the function...
2092 if ($5) { // Is null if empty...
2093 if (isVarArg) { // Nuke the last entry
2094 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
2095 "Not a varargs marker!");
2096 delete $5->back().first;
2097 $5->pop_back(); // Delete the last entry
2098 }
2099 Function::arg_iterator ArgIt = Fn->arg_begin();
2100 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
2101 I != $5->end(); ++I, ++ArgIt) {
2102 delete I->first; // Delete the typeholder...
2103
2104 setValueName(ArgIt, I->second); // Insert arg into symtab...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002105 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002106 InsertValue(ArgIt);
2107 }
2108
2109 delete $5; // We're now done with the argument list
2110 }
Reid Spencer61c83e02006-08-18 08:43:06 +00002111 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002112};
2113
2114BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
2115
2116FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
2117 $$ = CurFun.CurrentFunction;
2118
2119 // Make sure that we keep track of the linkage type even if there was a
2120 // previous "declare".
2121 $$->setLinkage($1);
2122};
2123
2124END : ENDTOK | '}'; // Allow end of '}' to end a function
2125
2126Function : BasicBlockList END {
2127 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002128 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002129};
2130
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002131FnDeclareLinkage: /*default*/ |
Chris Lattnerf49c1762006-11-08 05:58:47 +00002132 DLLIMPORT { CurFun.Linkage = GlobalValue::DLLImportLinkage; } |
2133 EXTERN_WEAK { CurFun.Linkage = GlobalValue::DLLImportLinkage; };
Anton Korobeynikovb74ed072006-09-14 18:23:27 +00002134
2135FunctionProto : DECLARE { CurFun.isDeclare = true; } FnDeclareLinkage FunctionHeaderH {
2136 $$ = CurFun.CurrentFunction;
2137 CurFun.FunctionDone();
2138 CHECK_FOR_ERROR
2139 };
Chris Lattner58af2a12006-02-15 07:22:58 +00002140
2141//===----------------------------------------------------------------------===//
2142// Rules to match Basic Blocks
2143//===----------------------------------------------------------------------===//
2144
2145OptSideEffect : /* empty */ {
2146 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002147 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002148 }
2149 | SIDEEFFECT {
2150 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002151 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002152 };
2153
2154ConstValueRef : ESINT64VAL { // A reference to a direct constant
2155 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002156 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002157 }
2158 | EUINT64VAL {
2159 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002160 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002161 }
2162 | FPVAL { // Perhaps it's an FP constant?
2163 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002164 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002165 }
2166 | TRUETOK {
Chris Lattner47811b72006-09-28 23:35:22 +00002167 $$ = ValID::create(ConstantBool::getTrue());
Reid Spencer61c83e02006-08-18 08:43:06 +00002168 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002169 }
2170 | FALSETOK {
Chris Lattner47811b72006-09-28 23:35:22 +00002171 $$ = ValID::create(ConstantBool::getFalse());
Reid Spencer61c83e02006-08-18 08:43:06 +00002172 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002173 }
2174 | NULL_TOK {
2175 $$ = ValID::createNull();
Reid Spencer61c83e02006-08-18 08:43:06 +00002176 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002177 }
2178 | UNDEF {
2179 $$ = ValID::createUndef();
Reid Spencer61c83e02006-08-18 08:43:06 +00002180 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002181 }
2182 | ZEROINITIALIZER { // A vector zero constant.
2183 $$ = ValID::createZeroInit();
Reid Spencer61c83e02006-08-18 08:43:06 +00002184 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002185 }
2186 | '<' ConstVector '>' { // Nonempty unsized packed vector
2187 const Type *ETy = (*$2)[0]->getType();
2188 int NumElements = $2->size();
2189
2190 PackedType* pt = PackedType::get(ETy, NumElements);
2191 PATypeHolder* PTy = new PATypeHolder(
2192 HandleUpRefs(
2193 PackedType::get(
2194 ETy,
2195 NumElements)
2196 )
2197 );
2198
2199 // Verify all elements are correct type!
2200 for (unsigned i = 0; i < $2->size(); i++) {
2201 if (ETy != (*$2)[i]->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002202 GEN_ERROR("Element #" + utostr(i) + " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002203 ETy->getDescription() +"' as required!\nIt is of type '" +
2204 (*$2)[i]->getType()->getDescription() + "'.");
2205 }
2206
2207 $$ = ValID::create(ConstantPacked::get(pt, *$2));
2208 delete PTy; delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002209 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002210 }
2211 | ConstExpr {
2212 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002213 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002214 }
2215 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
2216 char *End = UnEscapeLexed($3, true);
2217 std::string AsmStr = std::string($3, End);
2218 End = UnEscapeLexed($5, true);
2219 std::string Constraints = std::string($5, End);
2220 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
2221 free($3);
2222 free($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002223 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002224 };
2225
2226// SymbolicValueRef - Reference to one of two ways of symbolically refering to
2227// another value.
2228//
2229SymbolicValueRef : INTVAL { // Is it an integer reference...?
2230 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002231 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002232 }
2233 | Name { // Is it a named reference...?
2234 $$ = ValID::create($1);
Reid Spencer61c83e02006-08-18 08:43:06 +00002235 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002236 };
2237
2238// ValueRef - A reference to a definition... either constant or symbolic
2239ValueRef : SymbolicValueRef | ConstValueRef;
2240
2241
2242// ResolvedVal - a <type> <value> pair. This is used only in cases where the
2243// type immediately preceeds the value reference, and allows complex constant
2244// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
2245ResolvedVal : Types ValueRef {
2246 $$ = getVal(*$1, $2); delete $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002247 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002248 };
2249
2250BasicBlockList : BasicBlockList BasicBlock {
2251 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002252 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002253 }
2254 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
2255 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002256 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002257 };
2258
2259
2260// Basic blocks are terminated by branching instructions:
2261// br, br/cc, switch, ret
2262//
2263BasicBlock : InstructionList OptAssign BBTerminatorInst {
2264 setValueName($3, $2);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002265 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002266 InsertValue($3);
2267
2268 $1->getInstList().push_back($3);
2269 InsertValue($1);
2270 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002271 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002272 };
2273
2274InstructionList : InstructionList Inst {
2275 $1->getInstList().push_back($2);
2276 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002277 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002278 }
2279 | /* empty */ {
2280 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002281 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002282
2283 // Make sure to move the basic block to the correct location in the
2284 // function, instead of leaving it inserted wherever it was first
2285 // referenced.
2286 Function::BasicBlockListType &BBL =
2287 CurFun.CurrentFunction->getBasicBlockList();
2288 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002289 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002290 }
2291 | LABELSTR {
2292 $$ = CurBB = getBBVal(ValID::create($1), true);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002293 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002294
2295 // Make sure to move the basic block to the correct location in the
2296 // function, instead of leaving it inserted wherever it was first
2297 // referenced.
2298 Function::BasicBlockListType &BBL =
2299 CurFun.CurrentFunction->getBasicBlockList();
2300 BBL.splice(BBL.end(), BBL, $$);
Reid Spencer61c83e02006-08-18 08:43:06 +00002301 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002302 };
2303
2304BBTerminatorInst : RET ResolvedVal { // Return with a result...
2305 $$ = new ReturnInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002306 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002307 }
2308 | RET VOID { // Return with no result...
2309 $$ = new ReturnInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002310 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002311 }
2312 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002313 BasicBlock* tmpBB = getBBVal($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002314 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002315 $$ = new BranchInst(tmpBB);
Chris Lattner58af2a12006-02-15 07:22:58 +00002316 } // Conditional Branch...
2317 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002318 BasicBlock* tmpBBA = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002319 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002320 BasicBlock* tmpBBB = getBBVal($9);
2321 CHECK_FOR_ERROR
2322 Value* tmpVal = getVal(Type::BoolTy, $3);
2323 CHECK_FOR_ERROR
2324 $$ = new BranchInst(tmpBBA, tmpBBB, tmpVal);
Chris Lattner58af2a12006-02-15 07:22:58 +00002325 }
2326 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002327 Value* tmpVal = getVal($2, $3);
2328 CHECK_FOR_ERROR
2329 BasicBlock* tmpBB = getBBVal($6);
2330 CHECK_FOR_ERROR
2331 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, $8->size());
Chris Lattner58af2a12006-02-15 07:22:58 +00002332 $$ = S;
2333
2334 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
2335 E = $8->end();
2336 for (; I != E; ++I) {
2337 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2338 S->addCase(CI, I->second);
2339 else
Reid Spencer61c83e02006-08-18 08:43:06 +00002340 GEN_ERROR("Switch case is constant, but not a simple integer!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002341 }
2342 delete $8;
Reid Spencer61c83e02006-08-18 08:43:06 +00002343 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002344 }
2345 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002346 Value* tmpVal = getVal($2, $3);
2347 CHECK_FOR_ERROR
2348 BasicBlock* tmpBB = getBBVal($6);
2349 CHECK_FOR_ERROR
2350 SwitchInst *S = new SwitchInst(tmpVal, tmpBB, 0);
Chris Lattner58af2a12006-02-15 07:22:58 +00002351 $$ = S;
Reid Spencer61c83e02006-08-18 08:43:06 +00002352 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002353 }
2354 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2355 TO LABEL ValueRef UNWIND LABEL ValueRef {
2356 const PointerType *PFTy;
2357 const FunctionType *Ty;
2358
2359 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
2360 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2361 // Pull out the types of all of the arguments...
2362 std::vector<const Type*> ParamTypes;
2363 if ($6) {
2364 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
2365 I != E; ++I)
2366 ParamTypes.push_back((*I)->getType());
2367 }
2368
2369 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2370 if (isVarArg) ParamTypes.pop_back();
2371
2372 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
2373 PFTy = PointerType::get(Ty);
2374 }
2375
2376 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002377 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002378 BasicBlock *Normal = getBBVal($10);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002379 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002380 BasicBlock *Except = getBBVal($13);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002381 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002382
2383 // Create the call node...
2384 if (!$6) { // Has no arguments?
2385 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
2386 } else { // Has arguments?
2387 // Loop through FunctionType's arguments and ensure they are specified
2388 // correctly!
2389 //
2390 FunctionType::param_iterator I = Ty->param_begin();
2391 FunctionType::param_iterator E = Ty->param_end();
2392 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
2393
2394 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
2395 if ((*ArgI)->getType() != *I)
Reid Spencer61c83e02006-08-18 08:43:06 +00002396 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002397 (*I)->getDescription() + "'!");
2398
2399 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002400 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002401
2402 $$ = new InvokeInst(V, Normal, Except, *$6);
2403 }
2404 cast<InvokeInst>($$)->setCallingConv($2);
2405
2406 delete $3;
2407 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002408 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002409 }
2410 | UNWIND {
2411 $$ = new UnwindInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002412 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002413 }
2414 | UNREACHABLE {
2415 $$ = new UnreachableInst();
Reid Spencer61c83e02006-08-18 08:43:06 +00002416 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002417 };
2418
2419
2420
2421JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2422 $$ = $1;
2423 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002424 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002425 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002426 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002427
Reid Spencer5b7e7532006-09-28 19:28:24 +00002428 BasicBlock* tmpBB = getBBVal($6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002429 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002430 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002431 }
2432 | IntType ConstValueRef ',' LABEL ValueRef {
2433 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
2434 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Reid Spencer5b7e7532006-09-28 19:28:24 +00002435 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002436
2437 if (V == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002438 GEN_ERROR("May only switch on a constant pool value!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002439
Reid Spencer5b7e7532006-09-28 19:28:24 +00002440 BasicBlock* tmpBB = getBBVal($5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002441 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002442 $$->push_back(std::make_pair(V, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002443 };
2444
2445Inst : OptAssign InstVal {
2446 // Is this definition named?? if so, assign the name...
2447 setValueName($2, $1);
Reid Spencer5b7e7532006-09-28 19:28:24 +00002448 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002449 InsertValue($2);
2450 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002451 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002452};
2453
2454PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
2455 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Reid Spencer5b7e7532006-09-28 19:28:24 +00002456 Value* tmpVal = getVal(*$1, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002457 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002458 BasicBlock* tmpBB = getBBVal($5);
2459 CHECK_FOR_ERROR
2460 $$->push_back(std::make_pair(tmpVal, tmpBB));
2461 delete $1;
Chris Lattner58af2a12006-02-15 07:22:58 +00002462 }
2463 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2464 $$ = $1;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002465 Value* tmpVal = getVal($1->front().first->getType(), $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002466 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002467 BasicBlock* tmpBB = getBBVal($6);
2468 CHECK_FOR_ERROR
2469 $1->push_back(std::make_pair(tmpVal, tmpBB));
Chris Lattner58af2a12006-02-15 07:22:58 +00002470 };
2471
2472
2473ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
2474 $$ = new std::vector<Value*>();
2475 $$->push_back($1);
2476 }
2477 | ValueRefList ',' ResolvedVal {
2478 $$ = $1;
2479 $1->push_back($3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002480 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002481 };
2482
2483// ValueRefListE - Just like ValueRefList, except that it may also be empty!
2484ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
2485
2486OptTailCall : TAIL CALL {
2487 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002488 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002489 }
2490 | CALL {
2491 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002492 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002493 };
2494
Chris Lattner58af2a12006-02-15 07:22:58 +00002495InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
2496 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2497 !isa<PackedType>((*$2).get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002498 GEN_ERROR(
Chris Lattner58af2a12006-02-15 07:22:58 +00002499 "Arithmetic operator requires integer, FP, or packed operands!");
Reid Spencer3ed469c2006-11-02 20:25:50 +00002500 if (isa<PackedType>((*$2).get()) &&
2501 ($1.opcode == Instruction::URem ||
2502 $1.opcode == Instruction::SRem ||
2503 $1.opcode == Instruction::FRem))
2504 GEN_ERROR("U/S/FRem not supported on packed types!");
Reid Spencer1628cec2006-10-26 06:15:43 +00002505 // Upgrade the opcode from obsolete versions before we do anything with it.
2506 sanitizeOpCode($1,*$2);
2507 CHECK_FOR_ERROR;
Reid Spencer5b7e7532006-09-28 19:28:24 +00002508 Value* val1 = getVal(*$2, $3);
2509 CHECK_FOR_ERROR
2510 Value* val2 = getVal(*$2, $5);
2511 CHECK_FOR_ERROR
Reid Spencer1628cec2006-10-26 06:15:43 +00002512 $$ = BinaryOperator::create($1.opcode, val1, val2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002513 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002514 GEN_ERROR("binary operator returned null!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002515 delete $2;
2516 }
2517 | LogicalOps Types ValueRef ',' ValueRef {
2518 if (!(*$2)->isIntegral()) {
2519 if (!isa<PackedType>($2->get()) ||
2520 !cast<PackedType>($2->get())->getElementType()->isIntegral())
Reid Spencer61c83e02006-08-18 08:43:06 +00002521 GEN_ERROR("Logical operator requires integral operands!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002522 }
Reid Spencer5b7e7532006-09-28 19:28:24 +00002523 Value* tmpVal1 = getVal(*$2, $3);
2524 CHECK_FOR_ERROR
2525 Value* tmpVal2 = getVal(*$2, $5);
2526 CHECK_FOR_ERROR
Reid Spencer1628cec2006-10-26 06:15:43 +00002527 $$ = BinaryOperator::create($1.opcode, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002528 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002529 GEN_ERROR("binary operator returned null!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002530 delete $2;
2531 }
2532 | SetCondOps Types ValueRef ',' ValueRef {
2533 if(isa<PackedType>((*$2).get())) {
Reid Spencer61c83e02006-08-18 08:43:06 +00002534 GEN_ERROR(
Chris Lattner58af2a12006-02-15 07:22:58 +00002535 "PackedTypes currently not supported in setcc instructions!");
2536 }
Reid Spencer5b7e7532006-09-28 19:28:24 +00002537 Value* tmpVal1 = getVal(*$2, $3);
2538 CHECK_FOR_ERROR
2539 Value* tmpVal2 = getVal(*$2, $5);
2540 CHECK_FOR_ERROR
Reid Spencer1628cec2006-10-26 06:15:43 +00002541 $$ = new SetCondInst($1.opcode, tmpVal1, tmpVal2);
Chris Lattner58af2a12006-02-15 07:22:58 +00002542 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002543 GEN_ERROR("binary operator returned null!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002544 delete $2;
2545 }
2546 | NOT ResolvedVal {
2547 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2548 << " Replacing with 'xor'.\n";
2549
2550 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2551 if (Ones == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002552 GEN_ERROR("Expected integral type for not instruction!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002553
2554 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
2555 if ($$ == 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002556 GEN_ERROR("Could not create a xor instruction!");
2557 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002558 }
2559 | ShiftOps ResolvedVal ',' ResolvedVal {
2560 if ($4->getType() != Type::UByteTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002561 GEN_ERROR("Shift amount must be ubyte!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002562 if (!$2->getType()->isInteger())
Reid Spencer61c83e02006-08-18 08:43:06 +00002563 GEN_ERROR("Shift constant expression requires integer operand!");
Reid Spencer3822ff52006-11-08 06:47:33 +00002564 // Handle opcode upgrade situations
2565 sanitizeOpCode($1, $2->getType());
2566 CHECK_FOR_ERROR;
Reid Spencer1628cec2006-10-26 06:15:43 +00002567 $$ = new ShiftInst($1.opcode, $2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002568 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002569 }
2570 | CAST ResolvedVal TO Types {
2571 if (!$4->get()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002572 GEN_ERROR("cast instruction to a non-primitive type: '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002573 $4->get()->getDescription() + "'!");
2574 $$ = new CastInst($2, *$4);
2575 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002576 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002577 }
2578 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2579 if ($2->getType() != Type::BoolTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002580 GEN_ERROR("select condition must be boolean!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002581 if ($4->getType() != $6->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002582 GEN_ERROR("select value types should match!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002583 $$ = new SelectInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002584 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002585 }
2586 | VAARG ResolvedVal ',' Types {
2587 NewVarArgs = true;
2588 $$ = new VAArgInst($2, *$4);
2589 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002590 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002591 }
2592 | VAARG_old ResolvedVal ',' Types {
2593 ObsoleteVarArgs = true;
2594 const Type* ArgTy = $2->getType();
2595 Function* NF = CurModule.CurrentModule->
2596 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
2597
2598 //b = vaarg a, t ->
2599 //foo = alloca 1 of t
2600 //bar = vacopy a
2601 //store bar -> foo
2602 //b = vaarg foo, t
2603 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2604 CurBB->getInstList().push_back(foo);
2605 CallInst* bar = new CallInst(NF, $2);
2606 CurBB->getInstList().push_back(bar);
2607 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2608 $$ = new VAArgInst(foo, *$4);
2609 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002610 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002611 }
2612 | VANEXT_old ResolvedVal ',' Types {
2613 ObsoleteVarArgs = true;
2614 const Type* ArgTy = $2->getType();
2615 Function* NF = CurModule.CurrentModule->
2616 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
2617
2618 //b = vanext a, t ->
2619 //foo = alloca 1 of t
2620 //bar = vacopy a
2621 //store bar -> foo
2622 //tmp = vaarg foo, t
2623 //b = load foo
2624 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2625 CurBB->getInstList().push_back(foo);
2626 CallInst* bar = new CallInst(NF, $2);
2627 CurBB->getInstList().push_back(bar);
2628 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2629 Instruction* tmp = new VAArgInst(foo, *$4);
2630 CurBB->getInstList().push_back(tmp);
2631 $$ = new LoadInst(foo);
2632 delete $4;
Reid Spencer61c83e02006-08-18 08:43:06 +00002633 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002634 }
2635 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Chris Lattnerf4bd7d82006-04-08 04:09:02 +00002636 if (!ExtractElementInst::isValidOperands($2, $4))
Reid Spencer61c83e02006-08-18 08:43:06 +00002637 GEN_ERROR("Invalid extractelement operands!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002638 $$ = new ExtractElementInst($2, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002639 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002640 }
2641 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Chris Lattnerf4bd7d82006-04-08 04:09:02 +00002642 if (!InsertElementInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002643 GEN_ERROR("Invalid insertelement operands!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002644 $$ = new InsertElementInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002645 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002646 }
Chris Lattnerd5efe842006-04-08 01:18:56 +00002647 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Chris Lattnerd25db202006-04-08 03:55:17 +00002648 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
Reid Spencer61c83e02006-08-18 08:43:06 +00002649 GEN_ERROR("Invalid shufflevector operands!");
Chris Lattnerd5efe842006-04-08 01:18:56 +00002650 $$ = new ShuffleVectorInst($2, $4, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002651 CHECK_FOR_ERROR
Chris Lattnerd5efe842006-04-08 01:18:56 +00002652 }
Chris Lattner58af2a12006-02-15 07:22:58 +00002653 | PHI_TOK PHIList {
2654 const Type *Ty = $2->front().first->getType();
2655 if (!Ty->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002656 GEN_ERROR("PHI node operands must be of first class type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002657 $$ = new PHINode(Ty);
2658 ((PHINode*)$$)->reserveOperandSpace($2->size());
2659 while ($2->begin() != $2->end()) {
2660 if ($2->front().first->getType() != Ty)
Reid Spencer61c83e02006-08-18 08:43:06 +00002661 GEN_ERROR("All elements of a PHI node must be of the same type!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002662 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
2663 $2->pop_front();
2664 }
2665 delete $2; // Free the list...
Reid Spencer61c83e02006-08-18 08:43:06 +00002666 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002667 }
2668 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
2669 const PointerType *PFTy;
2670 const FunctionType *Ty;
2671
2672 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
2673 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
2674 // Pull out the types of all of the arguments...
2675 std::vector<const Type*> ParamTypes;
2676 if ($6) {
2677 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
2678 I != E; ++I)
2679 ParamTypes.push_back((*I)->getType());
2680 }
2681
2682 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2683 if (isVarArg) ParamTypes.pop_back();
2684
2685 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Reid Spencer61c83e02006-08-18 08:43:06 +00002686 GEN_ERROR("LLVM functions cannot return aggregate types!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002687
2688 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
2689 PFTy = PointerType::get(Ty);
2690 }
2691
2692 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Reid Spencer5b7e7532006-09-28 19:28:24 +00002693 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002694
2695 // Create the call node...
2696 if (!$6) { // Has no arguments?
2697 // Make sure no arguments is a good thing!
2698 if (Ty->getNumParams() != 0)
Reid Spencer61c83e02006-08-18 08:43:06 +00002699 GEN_ERROR("No arguments passed to a function that "
Chris Lattner58af2a12006-02-15 07:22:58 +00002700 "expects arguments!");
2701
2702 $$ = new CallInst(V, std::vector<Value*>());
2703 } else { // Has arguments?
2704 // Loop through FunctionType's arguments and ensure they are specified
2705 // correctly!
2706 //
2707 FunctionType::param_iterator I = Ty->param_begin();
2708 FunctionType::param_iterator E = Ty->param_end();
2709 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
2710
2711 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
2712 if ((*ArgI)->getType() != *I)
Reid Spencer61c83e02006-08-18 08:43:06 +00002713 GEN_ERROR("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002714 (*I)->getDescription() + "'!");
2715
2716 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002717 GEN_ERROR("Invalid number of parameters detected!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002718
2719 $$ = new CallInst(V, *$6);
2720 }
2721 cast<CallInst>($$)->setTailCall($1);
2722 cast<CallInst>($$)->setCallingConv($2);
2723 delete $3;
2724 delete $6;
Reid Spencer61c83e02006-08-18 08:43:06 +00002725 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002726 }
2727 | MemoryInst {
2728 $$ = $1;
Reid Spencer61c83e02006-08-18 08:43:06 +00002729 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002730 };
2731
2732
2733// IndexList - List of indices for GEP based instructions...
2734IndexList : ',' ValueRefList {
2735 $$ = $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002736 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002737 } | /* empty */ {
2738 $$ = new std::vector<Value*>();
Reid Spencer61c83e02006-08-18 08:43:06 +00002739 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002740 };
2741
2742OptVolatile : VOLATILE {
2743 $$ = true;
Reid Spencer61c83e02006-08-18 08:43:06 +00002744 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002745 }
2746 | /* empty */ {
2747 $$ = false;
Reid Spencer61c83e02006-08-18 08:43:06 +00002748 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002749 };
2750
2751
2752
2753MemoryInst : MALLOC Types OptCAlign {
2754 $$ = new MallocInst(*$2, 0, $3);
2755 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002756 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002757 }
2758 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002759 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002760 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002761 $$ = new MallocInst(*$2, tmpVal, $6);
2762 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002763 }
2764 | ALLOCA Types OptCAlign {
2765 $$ = new AllocaInst(*$2, 0, $3);
2766 delete $2;
Reid Spencer61c83e02006-08-18 08:43:06 +00002767 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002768 }
2769 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencer5b7e7532006-09-28 19:28:24 +00002770 Value* tmpVal = getVal($4, $5);
Reid Spencer61c83e02006-08-18 08:43:06 +00002771 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002772 $$ = new AllocaInst(*$2, tmpVal, $6);
2773 delete $2;
Chris Lattner58af2a12006-02-15 07:22:58 +00002774 }
2775 | FREE ResolvedVal {
2776 if (!isa<PointerType>($2->getType()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002777 GEN_ERROR("Trying to free nonpointer type " +
Chris Lattner58af2a12006-02-15 07:22:58 +00002778 $2->getType()->getDescription() + "!");
2779 $$ = new FreeInst($2);
Reid Spencer61c83e02006-08-18 08:43:06 +00002780 CHECK_FOR_ERROR
Chris Lattner58af2a12006-02-15 07:22:58 +00002781 }
2782
2783 | OptVolatile LOAD Types ValueRef {
2784 if (!isa<PointerType>($3->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002785 GEN_ERROR("Can't load from nonpointer type: " +
Chris Lattner58af2a12006-02-15 07:22:58 +00002786 (*$3)->getDescription());
2787 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002788 GEN_ERROR("Can't load from pointer of non-first-class type: " +
Chris Lattner58af2a12006-02-15 07:22:58 +00002789 (*$3)->getDescription());
Reid Spencer5b7e7532006-09-28 19:28:24 +00002790 Value* tmpVal = getVal(*$3, $4);
Reid Spencer61c83e02006-08-18 08:43:06 +00002791 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002792 $$ = new LoadInst(tmpVal, "", $1);
2793 delete $3;
Chris Lattner58af2a12006-02-15 07:22:58 +00002794 }
2795 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2796 const PointerType *PT = dyn_cast<PointerType>($5->get());
2797 if (!PT)
Reid Spencer61c83e02006-08-18 08:43:06 +00002798 GEN_ERROR("Can't store to a nonpointer type: " +
Chris Lattner58af2a12006-02-15 07:22:58 +00002799 (*$5)->getDescription());
2800 const Type *ElTy = PT->getElementType();
2801 if (ElTy != $3->getType())
Reid Spencer61c83e02006-08-18 08:43:06 +00002802 GEN_ERROR("Can't store '" + $3->getType()->getDescription() +
Chris Lattner58af2a12006-02-15 07:22:58 +00002803 "' into space of type '" + ElTy->getDescription() + "'!");
2804
Reid Spencer5b7e7532006-09-28 19:28:24 +00002805 Value* tmpVal = getVal(*$5, $6);
Reid Spencer61c83e02006-08-18 08:43:06 +00002806 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002807 $$ = new StoreInst($3, tmpVal, $1);
2808 delete $5;
Chris Lattner58af2a12006-02-15 07:22:58 +00002809 }
2810 | GETELEMENTPTR Types ValueRef IndexList {
2811 if (!isa<PointerType>($2->get()))
Reid Spencer61c83e02006-08-18 08:43:06 +00002812 GEN_ERROR("getelementptr insn requires pointer operand!");
Chris Lattner58af2a12006-02-15 07:22:58 +00002813
2814 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2815 // indices to uint struct indices for compatibility.
2816 generic_gep_type_iterator<std::vector<Value*>::iterator>
2817 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2818 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2819 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2820 if (isa<StructType>(*GTI)) // Only change struct indices
Reid Spencerb83eb642006-10-20 07:07:24 +00002821 if (ConstantInt *CUI = dyn_cast<ConstantInt>((*$4)[i]))
Chris Lattner58af2a12006-02-15 07:22:58 +00002822 if (CUI->getType() == Type::UByteTy)
2823 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2824
2825 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Reid Spencer61c83e02006-08-18 08:43:06 +00002826 GEN_ERROR("Invalid getelementptr indices for type '" +
Chris Lattner58af2a12006-02-15 07:22:58 +00002827 (*$2)->getDescription()+ "'!");
Reid Spencer5b7e7532006-09-28 19:28:24 +00002828 Value* tmpVal = getVal(*$2, $3);
Reid Spencer61c83e02006-08-18 08:43:06 +00002829 CHECK_FOR_ERROR
Reid Spencer5b7e7532006-09-28 19:28:24 +00002830 $$ = new GetElementPtrInst(tmpVal, *$4);
2831 delete $2;
2832 delete $4;
Chris Lattner58af2a12006-02-15 07:22:58 +00002833 };
2834
2835
2836%%
Reid Spencer61c83e02006-08-18 08:43:06 +00002837
2838void llvm::GenerateError(const std::string &message, int LineNo) {
2839 if (LineNo == -1) LineNo = llvmAsmlineno;
2840 // TODO: column number in exception
2841 if (TheParseError)
2842 TheParseError->setError(CurFilename, message, LineNo);
2843 TriggerError = 1;
2844}
2845
Chris Lattner58af2a12006-02-15 07:22:58 +00002846int yyerror(const char *ErrorMsg) {
2847 std::string where
2848 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2849 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
2850 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
2851 if (yychar == YYEMPTY || yychar == 0)
2852 errMsg += "end-of-file.";
2853 else
2854 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Reid Spencer61c83e02006-08-18 08:43:06 +00002855 GenerateError(errMsg);
Chris Lattner58af2a12006-02-15 07:22:58 +00002856 return 0;
2857}