blob: 9da63641b212fc8e715c3978f3513761757782ed [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000016#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000018#include "llvm/SymbolTable.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000019#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/ADT/STLExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000021#include <algorithm>
22#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000023#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000024#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000025
Chris Lattner386a3b72001-10-16 19:54:17 +000026int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000027int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000028int yyparse();
29
Brian Gaeked0fde302003-11-11 22:41:34 +000030namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000031 std::string CurFilename;
32}
33using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner00950542001-06-06 20:29:01 +000035static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattner30c89792001-09-07 16:35:17 +000037// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
38// relating to upreferences in the input stream.
39//
40//#define DEBUG_UPREFS 1
41#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000042#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000043#else
44#define UR_OUT(X)
45#endif
46
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000047#define YYERROR_VERBOSE 1
48
Chris Lattner99e7ab72003-10-18 05:53:13 +000049// HACK ALERT: This variable is used to implement the automatic conversion of
50// variable argument instructions from their old to new forms. When this
51// compatiblity "Feature" is removed, this should be too.
52//
53static BasicBlock *CurBB;
54static bool ObsoleteVarArgs;
55
56
Chris Lattner7e708292002-06-25 16:13:24 +000057// This contains info used when building the body of a function. It is
58// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000059//
Chris Lattner9232b992003-04-22 18:42:41 +000060typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner735f2702004-07-08 22:30:50 +000061static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
62 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000063
64static struct PerModuleInfo {
65 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000066 std::map<const Type *, ValueList> Values; // Module level numbered definitions
67 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000068 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000069 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000070
Chris Lattnerbc721ed2004-07-13 08:28:21 +000071 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
72 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000073 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000074 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
75
Chris Lattner2079fde2001-10-13 06:41:08 +000076 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
77 // references to global values. Global values may be referenced before they
78 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000079 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000080 //
Chris Lattner9232b992003-04-22 18:42:41 +000081 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000082 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000083 GlobalRefsType GlobalRefs;
84
Chris Lattner00950542001-06-06 20:29:01 +000085 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000086 // If we could not resolve some functions at function compilation time
87 // (calls to functions before they are defined), resolve them now... Types
88 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000089 //
Chris Lattner00950542001-06-06 20:29:01 +000090 ResolveDefinitions(LateResolveValues);
91
Chris Lattner2079fde2001-10-13 06:41:08 +000092 // Check to make sure that all global value forward references have been
93 // resolved!
94 //
95 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000096 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000097
98 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
99 I != E; ++I) {
100 UndefinedReferences += " " + I->first.first->getDescription() + " " +
101 I->first.second.getName() + "\n";
102 }
103 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000104 }
105
Chris Lattner7e708292002-06-25 16:13:24 +0000106 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000107 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000108 CurrentModule = 0;
109 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000110
111
Chris Lattner8a327842004-07-14 21:44:00 +0000112 // GetForwardRefForGlobal - Check to see if there is a forward reference
113 // for this global. If so, remove it from the GlobalRefs map and return it.
114 // If not, just return null.
115 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
116 // Check to see if there is a forward reference to this global variable...
117 // if there is, eliminate it and patch the reference to use the new def'n.
118 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
119 GlobalValue *Ret = 0;
120 if (I != GlobalRefs.end()) {
121 Ret = I->second;
122 GlobalRefs.erase(I);
123 }
124 return Ret;
125 }
Chris Lattner00950542001-06-06 20:29:01 +0000126} CurModule;
127
Chris Lattner79df7c02002-03-26 18:01:55 +0000128static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000129 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000130
Chris Lattner735f2702004-07-08 22:30:50 +0000131 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
132 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000133 std::vector<PATypeHolder> Types;
134 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000135 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000136
Chris Lattner2e2ed292004-07-14 06:28:35 +0000137 /// BBForwardRefs - When we see forward references to basic blocks, keep
138 /// track of them here.
139 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
140 std::vector<BasicBlock*> NumberedBlocks;
141 unsigned NextBBNum;
142
Chris Lattner79df7c02002-03-26 18:01:55 +0000143 inline PerFunctionInfo() {
144 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000145 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000146 }
147
Chris Lattner79df7c02002-03-26 18:01:55 +0000148 inline void FunctionStart(Function *M) {
149 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000150 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000151 }
152
Chris Lattner79df7c02002-03-26 18:01:55 +0000153 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000154 NumberedBlocks.clear();
155
156 // Any forward referenced blocks left?
157 if (!BBForwardRefs.empty())
158 ThrowException("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000159 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000160
161 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000162 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000163
Chris Lattner7e708292002-06-25 16:13:24 +0000164 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000165 Types.clear(); // Clear out function local types
Chris Lattner79df7c02002-03-26 18:01:55 +0000166 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000167 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000168 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000169} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000170
Chris Lattner394f2eb2003-10-16 20:12:13 +0000171static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000172
Chris Lattner00950542001-06-06 20:29:01 +0000173
174//===----------------------------------------------------------------------===//
175// Code to handle definitions of all the types
176//===----------------------------------------------------------------------===//
177
Chris Lattner735f2702004-07-08 22:30:50 +0000178static int InsertValue(Value *V,
179 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
180 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000181
182 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000183 ValueList &List = ValueTab[V->getType()];
184 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000185 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000186}
187
Chris Lattner30c89792001-09-07 16:35:17 +0000188static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000189 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000190 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000191 // Module constants occupy the lowest numbered slots...
Chris Lattnera09000d2004-07-14 23:03:46 +0000192 if ((unsigned)D.Num < CurModule.Types.size())
193 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000194 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000195 case ValID::NameVal: // Is it a named definition?
196 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
197 D.destroy(); // Free old strdup'd memory...
198 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000199 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000200 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000201 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000202 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000203 }
204
205 // If we reached here, we referenced either a symbol that we don't know about
206 // or an id number that hasn't been read yet. We may be referencing something
207 // forward, so just create an entry to be resolved later and get to it...
208 //
209 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
210
Chris Lattner9232b992003-04-22 18:42:41 +0000211 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000212 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000213
Chris Lattner9232b992003-04-22 18:42:41 +0000214 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000215 if (I != LateResolver.end()) {
216 return I->second;
217 }
Chris Lattner30c89792001-09-07 16:35:17 +0000218
Chris Lattner82269592001-10-22 06:01:08 +0000219 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000220 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000221 return Typ;
222}
223
Chris Lattner9232b992003-04-22 18:42:41 +0000224static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000225 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000226 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000227 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000228 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000229}
230
Chris Lattner2079fde2001-10-13 06:41:08 +0000231// getValNonImprovising - Look up the value specified by the provided type and
232// the provided ValID. If the value exists and has already been defined, return
233// it. Otherwise return null.
234//
235static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000236 if (isa<FunctionType>(Ty))
237 ThrowException("Functions are not values and "
238 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000239
Chris Lattner30c89792001-09-07 16:35:17 +0000240 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000241 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000242 unsigned Num = (unsigned)D.Num;
243
244 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000245 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000246 if (VI != CurModule.Values.end()) {
247 if (Num < VI->second.size())
248 return VI->second[Num];
249 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000250 }
251
252 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000253 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000254 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000255
256 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000257 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000258
Chris Lattner16af11d2004-02-09 21:03:38 +0000259 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000260 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000261
Chris Lattner1a1cb112001-09-30 22:46:54 +0000262 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000263 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000264 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000265
266 D.destroy(); // Free old strdup'd memory...
267 return N;
268 }
269
Chris Lattner2079fde2001-10-13 06:41:08 +0000270 // Check to make sure that "Ty" is an integral type, and that our
271 // value will fit into the specified type...
272 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000273 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
274 ThrowException("Signed integral constant '" +
275 itostr(D.ConstPool64) + "' is invalid for type '" +
276 Ty->getDescription() + "'!");
277 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000278
279 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000280 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
281 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000282 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000283 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000284 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000285 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000286 }
287 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000288 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000289 }
290
Chris Lattner2079fde2001-10-13 06:41:08 +0000291 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000292 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000293 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000294 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000295
296 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000297 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000298 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000299 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000300
Chris Lattner16710e92004-10-16 18:17:13 +0000301 case ValID::ConstUndefVal: // Is it an undef value?
302 return UndefValue::get(Ty);
303
Chris Lattnerd78700d2002-08-16 21:14:40 +0000304 case ValID::ConstantVal: // Fully resolved constant?
305 if (D.ConstantValue->getType() != Ty)
306 ThrowException("Constant expression type different from required type!");
307 return D.ConstantValue;
308
Chris Lattner30c89792001-09-07 16:35:17 +0000309 default:
310 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000311 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000312 } // End of switch
313
Chris Lattner2079fde2001-10-13 06:41:08 +0000314 assert(0 && "Unhandled case!");
315 return 0;
316}
317
Chris Lattner2079fde2001-10-13 06:41:08 +0000318// getVal - This function is identical to getValNonImprovising, except that if a
319// value is not already defined, it "improvises" by creating a placeholder var
320// that looks and acts just like the requested variable. When the value is
321// defined later, all uses of the placeholder variable are replaced with the
322// real thing.
323//
Chris Lattner64116f92004-07-14 01:33:11 +0000324static Value *getVal(const Type *Ty, const ValID &ID) {
325 if (Ty == Type::LabelTy)
326 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000327
Chris Lattner64116f92004-07-14 01:33:11 +0000328 // See if the value has already been defined.
329 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000330 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000331
332 // If we reached here, we referenced either a symbol that we don't know about
333 // or an id number that hasn't been read yet. We may be referencing something
334 // forward, so just create an entry to be resolved later and get to it...
335 //
Chris Lattner64116f92004-07-14 01:33:11 +0000336 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000337
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000338 // Remember where this forward reference came from. FIXME, shouldn't we try
339 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000340 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000341 llvmAsmlineno)));
342
Chris Lattner79df7c02002-03-26 18:01:55 +0000343 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000344 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000345 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000346 InsertValue(V, CurModule.LateResolveValues);
347 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000348}
349
Chris Lattner2e2ed292004-07-14 06:28:35 +0000350/// getBBVal - This is used for two purposes:
351/// * If isDefinition is true, a new basic block with the specified ID is being
352/// defined.
353/// * If isDefinition is true, this is a reference to a basic block, which may
354/// or may not be a forward reference.
355///
356static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000357 assert(inFunctionScope() && "Can't get basic block at global scope!");
358
Chris Lattner2e2ed292004-07-14 06:28:35 +0000359 std::string Name;
360 BasicBlock *BB = 0;
361 switch (ID.Type) {
362 default: ThrowException("Illegal label reference " + ID.getName());
363 case ValID::NumberVal: // Is it a numbered definition?
364 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
365 CurFun.NumberedBlocks.resize(ID.Num+1);
366 BB = CurFun.NumberedBlocks[ID.Num];
367 break;
368 case ValID::NameVal: // Is it a named definition?
369 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000370 if (Value *N = CurFun.CurrentFunction->
371 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000372 BB = cast<BasicBlock>(N);
373 break;
374 }
Chris Lattner64116f92004-07-14 01:33:11 +0000375
Chris Lattner2e2ed292004-07-14 06:28:35 +0000376 // See if the block has already been defined.
377 if (BB) {
378 // If this is the definition of the block, make sure the existing value was
379 // just a forward reference. If it was a forward reference, there will be
380 // an entry for it in the PlaceHolderInfo map.
381 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
382 // The existing value was a definition, not a forward reference.
383 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000384
Chris Lattner2e2ed292004-07-14 06:28:35 +0000385 ID.destroy(); // Free strdup'd memory.
386 return BB;
387 }
388
389 // Otherwise this block has not been seen before.
390 BB = new BasicBlock("", CurFun.CurrentFunction);
391 if (ID.Type == ValID::NameVal) {
392 BB->setName(ID.Name);
393 } else {
394 CurFun.NumberedBlocks[ID.Num] = BB;
395 }
396
397 // If this is not a definition, keep track of it so we can use it as a forward
398 // reference.
399 if (!isDefinition) {
400 // Remember where this forward reference came from.
401 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
402 } else {
403 // The forward declaration could have been inserted anywhere in the
404 // function: insert it into the correct place now.
405 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
406 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
407 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000408 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000409 return BB;
410}
411
Chris Lattner00950542001-06-06 20:29:01 +0000412
413//===----------------------------------------------------------------------===//
414// Code to handle forward references in instructions
415//===----------------------------------------------------------------------===//
416//
417// This code handles the late binding needed with statements that reference
418// values not defined yet... for example, a forward branch, or the PHI node for
419// a loop body.
420//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000421// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000422// and back patchs after we are done.
423//
424
425// ResolveDefinitions - If we could not resolve some defs at parsing
426// time (forward branches, phi functions for loops, etc...) resolve the
427// defs now...
428//
Chris Lattner735f2702004-07-08 22:30:50 +0000429static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
430 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000431 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000432 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000433 E = LateResolvers.end(); LRI != E; ++LRI) {
434 ValueList &List = LRI->second;
435 while (!List.empty()) {
436 Value *V = List.back();
437 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000438
439 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
440 CurModule.PlaceHolderInfo.find(V);
441 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
442
443 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000444
Chris Lattner735f2702004-07-08 22:30:50 +0000445 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000446 if (TheRealValue) {
447 V->replaceAllUsesWith(TheRealValue);
448 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000449 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000450 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000451 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000452 // resolver table
453 InsertValue(V, *FutureLateResolvers);
454 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000455 if (DID.Type == ValID::NameVal)
456 ThrowException("Reference to an invalid definition: '" +DID.getName()+
457 "' of type '" + V->getType()->getDescription() + "'",
458 PHI->second.second);
459 else
460 ThrowException("Reference to an invalid definition: #" +
461 itostr(DID.Num) + " of type '" +
462 V->getType()->getDescription() + "'",
463 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000464 }
Chris Lattner00950542001-06-06 20:29:01 +0000465 }
466 }
467
468 LateResolvers.clear();
469}
470
Chris Lattner4a42e902001-10-22 05:56:09 +0000471// ResolveTypeTo - A brand new type was just declared. This means that (if
472// name is not null) things referencing Name can be resolved. Otherwise, things
473// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000474//
Chris Lattner4a42e902001-10-22 05:56:09 +0000475static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000476 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000477 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000478
Chris Lattner4a42e902001-10-22 05:56:09 +0000479 ValID D;
480 if (Name) D = ValID::create(Name);
481 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000482
Chris Lattner9232b992003-04-22 18:42:41 +0000483 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000484 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000485
Chris Lattner9232b992003-04-22 18:42:41 +0000486 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000487 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000488 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000489 LateResolver.erase(I);
490 }
491}
492
493// ResolveTypes - At this point, all types should be resolved. Any that aren't
494// are errors.
495//
Chris Lattner9232b992003-04-22 18:42:41 +0000496static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000497 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000498 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000499
500 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000501 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000502 else
Chris Lattner82269592001-10-22 06:01:08 +0000503 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000504 }
505}
506
Chris Lattner1781aca2001-09-18 04:00:54 +0000507// setValueName - Set the specified value to the name given. The name may be
508// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000509// assumed to be a malloc'd string buffer, and is free'd by this function.
510//
511static void setValueName(Value *V, char *NameStr) {
512 if (NameStr) {
513 std::string Name(NameStr); // Copy string
514 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000515
516 if (V->getType() == Type::VoidTy)
517 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000518
519 assert(inFunctionScope() && "Must be in function scope!");
520 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
521 if (ST.lookup(V->getType(), Name))
522 ThrowException("Redefinition of value named '" + Name + "' in the '" +
523 V->getType()->getDescription() + "' type plane!");
524
Chris Lattnerc9aea522004-07-13 08:12:39 +0000525 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000526 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000527 }
528}
529
Chris Lattnerd88998d2004-07-14 08:23:52 +0000530/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
531/// this is a declaration, otherwise it is a definition.
532static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
533 bool isConstantGlobal, const Type *Ty,
534 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000535 if (isa<FunctionType>(Ty))
536 ThrowException("Cannot declare global vars of function type!");
537
Chris Lattnera09000d2004-07-14 23:03:46 +0000538 const PointerType *PTy = PointerType::get(Ty);
539
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000540 std::string Name;
541 if (NameStr) {
542 Name = NameStr; // Copy string
543 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000544 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000545
Chris Lattner8a327842004-07-14 21:44:00 +0000546 // See if this global value was forward referenced. If so, recycle the
547 // object.
548 ValID ID;
549 if (!Name.empty()) {
550 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000551 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000552 ID = ValID::create((int)CurModule.Values[PTy].size());
553 }
554
555 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
556 // Move the global to the end of the list, from whereever it was
557 // previously inserted.
558 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
559 CurModule.CurrentModule->getGlobalList().remove(GV);
560 CurModule.CurrentModule->getGlobalList().push_back(GV);
561 GV->setInitializer(Initializer);
562 GV->setLinkage(Linkage);
563 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000564 InsertValue(GV, CurModule.Values);
Chris Lattnera09000d2004-07-14 23:03:46 +0000565 return;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000566 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000567
568 // If this global has a name, check to see if there is already a definition
569 // of this global in the module. If so, merge as appropriate. Note that
570 // this is really just a hack around problems in the CFE. :(
571 if (!Name.empty()) {
572 // We are a simple redefinition of a value, check to see if it is defined
573 // the same as the old one.
574 if (GlobalVariable *EGV =
575 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
576 // We are allowed to redefine a global variable in two circumstances:
577 // 1. If at least one of the globals is uninitialized or
578 // 2. If both initializers have the same value.
579 //
580 if (!EGV->hasInitializer() || !Initializer ||
581 EGV->getInitializer() == Initializer) {
582
583 // Make sure the existing global version gets the initializer! Make
584 // sure that it also gets marked const if the new version is.
585 if (Initializer && !EGV->hasInitializer())
586 EGV->setInitializer(Initializer);
587 if (isConstantGlobal)
588 EGV->setConstant(true);
589 EGV->setLinkage(Linkage);
590 return;
591 }
592
593 ThrowException("Redefinition of global variable named '" + Name +
594 "' in the '" + Ty->getDescription() + "' type plane!");
595 }
596 }
597
598 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000599 GlobalVariable *GV =
600 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
601 CurModule.CurrentModule);
602 InsertValue(GV, CurModule.Values);
Chris Lattnerd88998d2004-07-14 08:23:52 +0000603}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000604
Reid Spencer75719bf2004-05-26 21:48:31 +0000605// setTypeName - Set the specified type to the name given. The name may be
606// null potentially, in which case this is a noop. The string passed in is
607// assumed to be a malloc'd string buffer, and is freed by this function.
608//
609// This function returns true if the type has already been defined, but is
610// allowed to be redefined in the specified context. If the name is a new name
611// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000612static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000613 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000614 if (NameStr == 0) return false;
615
616 std::string Name(NameStr); // Copy string
617 free(NameStr); // Free old string
618
619 // We don't allow assigning names to void type
620 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000621 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000622
Chris Lattnera09000d2004-07-14 23:03:46 +0000623 // Set the type name, checking for conflicts as we do so.
624 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000625
Chris Lattnera09000d2004-07-14 23:03:46 +0000626 if (AlreadyExists) { // Inserting a name that is already defined???
627 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
628 assert(Existing && "Conflict but no matching type?");
629
Reid Spencer75719bf2004-05-26 21:48:31 +0000630 // There is only one case where this is allowed: when we are refining an
631 // opaque type. In this case, Existing will be an opaque type.
632 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
633 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000634 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000635 return true;
636 }
637
638 // Otherwise, this is an attempt to redefine a type. That's okay if
639 // the redefinition is identical to the original. This will be so if
640 // Existing and T point to the same Type object. In this one case we
641 // allow the equivalent redefinition.
642 if (Existing == T) return true; // Yes, it's equal.
643
644 // Any other kind of (non-equivalent) redefinition is an error.
645 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000646 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000647 }
648
Reid Spencer75719bf2004-05-26 21:48:31 +0000649 return false;
650}
Chris Lattner8896eda2001-07-09 19:38:36 +0000651
Chris Lattner30c89792001-09-07 16:35:17 +0000652//===----------------------------------------------------------------------===//
653// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000654//
Chris Lattner8896eda2001-07-09 19:38:36 +0000655
Chris Lattner3b073862004-02-09 03:19:29 +0000656// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000657//
658static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000659 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
660 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000661}
662
663namespace {
664 struct UpRefRecord {
665 // NestingLevel - The number of nesting levels that need to be popped before
666 // this type is resolved.
667 unsigned NestingLevel;
668
669 // LastContainedTy - This is the type at the current binding level for the
670 // type. Every time we reduce the nesting level, this gets updated.
671 const Type *LastContainedTy;
672
673 // UpRefTy - This is the actual opaque type that the upreference is
674 // represented with.
675 OpaqueType *UpRefTy;
676
677 UpRefRecord(unsigned NL, OpaqueType *URTy)
678 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
679 };
Chris Lattner30c89792001-09-07 16:35:17 +0000680}
Chris Lattner698b56e2001-07-20 19:15:08 +0000681
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000682// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000683static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000684
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000685/// HandleUpRefs - Every time we finish a new layer of types, this function is
686/// called. It loops through the UpRefs vector, which is a list of the
687/// currently active types. For each type, if the up reference is contained in
688/// the newly completed type, we decrement the level count. When the level
689/// count reaches zero, the upreferenced type is the type that is passed in:
690/// thus we can complete the cycle.
691///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000692static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000693 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000694 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000695 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000696 "' newly formed. Resolving upreferences.\n" <<
697 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000698
699 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
700 // to zero), we resolve them all together before we resolve them to Ty. At
701 // the end of the loop, if there is anything to resolve to Ty, it will be in
702 // this variable.
703 OpaqueType *TypeToResolve = 0;
704
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000705 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000706 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencer3271ed52004-07-18 00:08:11 +0000707 << UpRefs[i].second->getDescription() << ") = "
708 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000709 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
710 // Decrement level of upreference
711 unsigned Level = --UpRefs[i].NestingLevel;
712 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000713 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000714 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000715 if (!TypeToResolve) {
716 TypeToResolve = UpRefs[i].UpRefTy;
717 } else {
718 UR_OUT(" * Resolving upreference for "
719 << UpRefs[i].second->getDescription() << "\n";
720 std::string OldName = UpRefs[i].UpRefTy->getDescription());
721 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
722 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
723 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
724 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000725 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000726 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000727 }
728 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000729 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000730
Chris Lattner026a8ce2004-02-09 18:53:54 +0000731 if (TypeToResolve) {
732 UR_OUT(" * Resolving upreference for "
733 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000734 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000735 TypeToResolve->refineAbstractTypeTo(Ty);
736 }
737
Chris Lattner8896eda2001-07-09 19:38:36 +0000738 return Ty;
739}
740
Chris Lattner30c89792001-09-07 16:35:17 +0000741
Chris Lattner00950542001-06-06 20:29:01 +0000742//===----------------------------------------------------------------------===//
743// RunVMAsmParser - Define an interface to this parser
744//===----------------------------------------------------------------------===//
745//
Chris Lattner86427632004-07-14 06:39:48 +0000746Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000747 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000748 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000749 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000750 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000751
Chris Lattner75f20532003-04-22 18:02:52 +0000752 // Allocate a new module to read
753 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000754
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000755 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000756
Chris Lattner00950542001-06-06 20:29:01 +0000757 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000758
759 // Check to see if they called va_start but not va_arg..
760 if (!ObsoleteVarArgs)
761 if (Function *F = Result->getNamedFunction("llvm.va_start"))
762 if (F->asize() == 1) {
763 std::cerr << "WARNING: this file uses obsolete features. "
764 << "Assemble and disassemble to update it.\n";
765 ObsoleteVarArgs = true;
766 }
767
Chris Lattner99e7ab72003-10-18 05:53:13 +0000768 if (ObsoleteVarArgs) {
769 // If the user is making use of obsolete varargs intrinsics, adjust them for
770 // the user.
771 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
772 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
773
774 const Type *RetTy = F->getFunctionType()->getParamType(0);
775 RetTy = cast<PointerType>(RetTy)->getElementType();
776 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
777
778 while (!F->use_empty()) {
779 CallInst *CI = cast<CallInst>(F->use_back());
780 Value *V = new CallInst(NF, "", CI);
781 new StoreInst(V, CI->getOperand(1), CI);
782 CI->getParent()->getInstList().erase(CI);
783 }
784 Result->getFunctionList().erase(F);
785 }
786
787 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
788 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
789 const Type *ArgTy = F->getFunctionType()->getParamType(0);
790 ArgTy = cast<PointerType>(ArgTy)->getElementType();
791 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
792 ArgTy, 0);
793
794 while (!F->use_empty()) {
795 CallInst *CI = cast<CallInst>(F->use_back());
796 Value *V = new LoadInst(CI->getOperand(1), "", CI);
797 new CallInst(NF, V, "", CI);
798 CI->getParent()->getInstList().erase(CI);
799 }
800 Result->getFunctionList().erase(F);
801 }
802
803 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
804 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
805 const Type *ArgTy = F->getFunctionType()->getParamType(0);
806 ArgTy = cast<PointerType>(ArgTy)->getElementType();
807 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
808 ArgTy, 0);
809
810 while (!F->use_empty()) {
811 CallInst *CI = cast<CallInst>(F->use_back());
812 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
813 new StoreInst(V, CI->getOperand(1), CI);
814 CI->getParent()->getInstList().erase(CI);
815 }
816 Result->getFunctionList().erase(F);
817 }
818 }
819
Chris Lattner00950542001-06-06 20:29:01 +0000820 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
821 ParserResult = 0;
822
823 return Result;
824}
825
826%}
827
828%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000829 llvm::Module *ModuleVal;
830 llvm::Function *FunctionVal;
831 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
832 llvm::BasicBlock *BasicBlockVal;
833 llvm::TerminatorInst *TermInstVal;
834 llvm::Instruction *InstVal;
835 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000836
Brian Gaeked0fde302003-11-11 22:41:34 +0000837 const llvm::Type *PrimType;
838 llvm::PATypeHolder *TypeVal;
839 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000840
Brian Gaeked0fde302003-11-11 22:41:34 +0000841 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
842 std::vector<llvm::Value*> *ValueList;
843 std::list<llvm::PATypeHolder> *TypeList;
844 std::list<std::pair<llvm::Value*,
845 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
846 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
847 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000848
Brian Gaeked0fde302003-11-11 22:41:34 +0000849 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000850 int64_t SInt64Val;
851 uint64_t UInt64Val;
852 int SIntVal;
853 unsigned UIntVal;
854 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000855 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000856
Chris Lattner30c89792001-09-07 16:35:17 +0000857 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000858 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000859
Brian Gaeked0fde302003-11-11 22:41:34 +0000860 llvm::Instruction::BinaryOps BinaryOpVal;
861 llvm::Instruction::TermOps TermOpVal;
862 llvm::Instruction::MemoryOps MemOpVal;
863 llvm::Instruction::OtherOps OtherOpVal;
864 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000865}
866
Chris Lattner79df7c02002-03-26 18:01:55 +0000867%type <ModuleVal> Module FunctionList
868%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000869%type <BasicBlockVal> BasicBlock InstructionList
870%type <TermInstVal> BBTerminatorInst
871%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000872%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000873%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000874%type <ArgList> ArgList ArgListH
875%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000876%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000877%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000878%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000879%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000880%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000881%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000882%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000883%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000884%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000885
Chris Lattner2079fde2001-10-13 06:41:08 +0000886// ValueRef - Unresolved reference to a definition or BB
887%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000888%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000889// Tokens and types for handling constant integer values
890//
891// ESINT64VAL - A negative number within long long range
892%token <SInt64Val> ESINT64VAL
893
894// EUINT64VAL - A positive number within uns. long long range
895%token <UInt64Val> EUINT64VAL
896%type <SInt64Val> EINT64VAL
897
898%token <SIntVal> SINTVAL // Signed 32 bit ints...
899%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
900%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000901%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000902
903// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000904%type <TypeVal> Types TypesV UpRTypes UpRTypesV
905%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000906%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
907%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000908
Chris Lattner6c23f572003-08-22 05:42:10 +0000909%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
910%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000911
912
Chris Lattner91ef4602004-03-31 03:49:47 +0000913%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000914%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000915%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Reid Spencer0be13e72004-07-25 17:58:28 +0000916%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer1c7b9072004-09-14 05:43:23 +0000917%token DEPLIBS
Chris Lattner00950542001-06-06 20:29:01 +0000918
919// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000920%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000921
Chris Lattner00950542001-06-06 20:29:01 +0000922// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000923%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000924%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000925%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000926
927// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000928%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000929
Chris Lattner027dcc52001-07-08 21:10:27 +0000930// Other Operators
931%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000932%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000933%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000934
Chris Lattner00950542001-06-06 20:29:01 +0000935%start Module
936%%
937
938// Handle constant integer size restriction and conversion...
939//
Chris Lattner51727be2002-06-04 21:58:56 +0000940INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000941INTVAL : UINTVAL {
942 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
943 ThrowException("Value too large for type!");
944 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000945};
Chris Lattner00950542001-06-06 20:29:01 +0000946
947
Chris Lattner51727be2002-06-04 21:58:56 +0000948EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000949EINT64VAL : EUINT64VAL {
950 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
951 ThrowException("Value too large for type!");
952 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000953};
Chris Lattner00950542001-06-06 20:29:01 +0000954
Chris Lattner00950542001-06-06 20:29:01 +0000955// Operations that are notably excluded from this list include:
956// RET, BR, & SWITCH because they end basic blocks and are treated specially.
957//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000958ArithmeticOps: ADD | SUB | MUL | DIV | REM;
959LogicalOps : AND | OR | XOR;
960SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +0000961
Chris Lattner51727be2002-06-04 21:58:56 +0000962ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000963
Chris Lattnere98dda62001-07-14 06:10:16 +0000964// These are some types that allow classification if we only want a particular
965// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000966SIntType : LONG | INT | SHORT | SBYTE;
967UIntType : ULONG | UINT | USHORT | UBYTE;
968IntType : SIntType | UIntType;
969FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000970
Chris Lattnere98dda62001-07-14 06:10:16 +0000971// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000972OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000973 $$ = $1;
974 }
975 | /*empty*/ {
976 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000977 };
Chris Lattner00950542001-06-06 20:29:01 +0000978
Chris Lattner4ad02e72003-04-16 20:28:45 +0000979OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
980 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000981 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000982 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
983 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000984
985//===----------------------------------------------------------------------===//
986// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000987// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000988// access to it, a user must explicitly use TypesV.
989//
990
991// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000992TypesV : Types | VOID { $$ = new PATypeHolder($1); };
993UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000994
995Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000996 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000997 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
998 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000999 };
Chris Lattner30c89792001-09-07 16:35:17 +00001000
1001
1002// Derived types are added later...
1003//
Chris Lattner51727be2002-06-04 21:58:56 +00001004PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1005PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001006UpRTypes : OPAQUE {
1007 $$ = new PATypeHolder(OpaqueType::get());
1008 }
1009 | PrimType {
1010 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001011 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001012UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001013 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001014};
Chris Lattner30c89792001-09-07 16:35:17 +00001015
Chris Lattner30c89792001-09-07 16:35:17 +00001016// Include derived types in the Types production.
1017//
1018UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001019 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001020 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001021 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001022 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001023 UR_OUT("New Upreference!\n");
1024 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001025 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001026 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001027 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1028 E = $3->end(); I != E; ++I)
1029 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001030 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1031 if (isVarArg) Params.pop_back();
1032
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001033 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001034 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001035 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001036 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001037 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001038 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001039 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001040 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001041 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1042 const llvm::Type* ElemTy = $4->get();
1043 if ((unsigned)$2 != $2) {
1044 ThrowException("Unsigned result not equal to signed result");
1045 }
1046 if(!ElemTy->isPrimitiveType()) {
1047 ThrowException("Elemental type of a PackedType must be primitive");
1048 }
1049 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1050 delete $4;
1051 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001052 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001053 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001054 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1055 E = $2->end(); I != E; ++I)
1056 Elements.push_back(*I);
1057
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001058 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001059 delete $2;
1060 }
1061 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001062 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001063 }
1064 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001065 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001066 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001067 };
Chris Lattner30c89792001-09-07 16:35:17 +00001068
Chris Lattner7e708292002-06-25 16:13:24 +00001069// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001070// declaration type lists
1071//
1072TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001073 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001074 $$->push_back(*$1); delete $1;
1075 }
1076 | TypeListI ',' UpRTypes {
1077 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001078 };
Chris Lattner30c89792001-09-07 16:35:17 +00001079
Chris Lattner7e708292002-06-25 16:13:24 +00001080// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001081ArgTypeListI : TypeListI
1082 | TypeListI ',' DOTDOTDOT {
1083 ($$=$1)->push_back(Type::VoidTy);
1084 }
1085 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001086 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001087 }
1088 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001089 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001090 };
Chris Lattner30c89792001-09-07 16:35:17 +00001091
Chris Lattnere98dda62001-07-14 06:10:16 +00001092// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001093// production is used ONLY to represent constants that show up AFTER a 'const',
1094// 'constant' or 'global' token at global scope. Constants that can be inlined
1095// into other expressions (such as integers and constexprs) are handled by the
1096// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001097//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001098ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001099 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001100 if (ATy == 0)
1101 ThrowException("Cannot make array constant with type: '" +
1102 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001103 const Type *ETy = ATy->getElementType();
1104 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001105
Chris Lattner30c89792001-09-07 16:35:17 +00001106 // Verify that we have the correct size...
1107 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001108 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001109 utostr($3->size()) + " arguments, but has size of " +
1110 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001111
Chris Lattner30c89792001-09-07 16:35:17 +00001112 // Verify all elements are correct type!
1113 for (unsigned i = 0; i < $3->size(); i++) {
1114 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001115 ThrowException("Element #" + utostr(i) + " is not of type '" +
1116 ETy->getDescription() +"' as required!\nIt is of type '"+
1117 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001118 }
1119
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001120 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001121 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001122 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001123 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001124 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001125 if (ATy == 0)
1126 ThrowException("Cannot make array constant with type: '" +
1127 (*$1)->getDescription() + "'!");
1128
1129 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001130 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001131 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001132 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001133 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001134 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001135 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001136 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001137 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001138 if (ATy == 0)
1139 ThrowException("Cannot make array constant with type: '" +
1140 (*$1)->getDescription() + "'!");
1141
Chris Lattner30c89792001-09-07 16:35:17 +00001142 int NumElements = ATy->getNumElements();
1143 const Type *ETy = ATy->getElementType();
1144 char *EndStr = UnEscapeLexed($3, true);
1145 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001146 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001147 itostr((int)(EndStr-$3)) +
1148 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001149 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001150 if (ETy == Type::SByteTy) {
1151 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001152 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001153 } else if (ETy == Type::UByteTy) {
1154 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001155 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001156 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001157 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001158 ThrowException("Cannot build string arrays of non byte sized elements!");
1159 }
Chris Lattner30c89792001-09-07 16:35:17 +00001160 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001161 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001162 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001163 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001164 | Types '<' ConstVector '>' { // Nonempty unsized arr
1165 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1166 if (PTy == 0)
1167 ThrowException("Cannot make packed constant with type: '" +
1168 (*$1)->getDescription() + "'!");
1169 const Type *ETy = PTy->getElementType();
1170 int NumElements = PTy->getNumElements();
1171
1172 // Verify that we have the correct size...
1173 if (NumElements != -1 && NumElements != (int)$3->size())
1174 ThrowException("Type mismatch: constant sized packed initialized with " +
1175 utostr($3->size()) + " arguments, but has size of " +
1176 itostr(NumElements) + "!");
1177
1178 // Verify all elements are correct type!
1179 for (unsigned i = 0; i < $3->size(); i++) {
1180 if (ETy != (*$3)[i]->getType())
1181 ThrowException("Element #" + utostr(i) + " is not of type '" +
1182 ETy->getDescription() +"' as required!\nIt is of type '"+
1183 (*$3)[i]->getType()->getDescription() + "'.");
1184 }
1185
1186 $$ = ConstantPacked::get(PTy, *$3);
1187 delete $1; delete $3;
1188 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001189 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001190 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001191 if (STy == 0)
1192 ThrowException("Cannot make struct constant with type: '" +
1193 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001194
Chris Lattnerc6212f12003-05-21 16:06:56 +00001195 if ($3->size() != STy->getNumContainedTypes())
1196 ThrowException("Illegal number of initializers for structure type!");
1197
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001198 // Check to ensure that constants are compatible with the type initializer!
1199 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001200 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001201 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001202 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001203 "' for element #" + utostr(i) +
1204 " of structure initializer!");
1205
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001206 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001207 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001208 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001209 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001210 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001211 if (STy == 0)
1212 ThrowException("Cannot make struct constant with type: '" +
1213 (*$1)->getDescription() + "'!");
1214
1215 if (STy->getNumContainedTypes() != 0)
1216 ThrowException("Illegal number of initializers for structure type!");
1217
1218 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1219 delete $1;
1220 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001221 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001222 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001223 if (PTy == 0)
1224 ThrowException("Cannot make null pointer constant with type: '" +
1225 (*$1)->getDescription() + "'!");
1226
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001227 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001228 delete $1;
1229 }
Chris Lattner16710e92004-10-16 18:17:13 +00001230 | Types UNDEF {
1231 $$ = UndefValue::get($1->get());
1232 delete $1;
1233 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001234 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001235 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001236 if (Ty == 0)
1237 ThrowException("Global const reference must be a pointer type!");
1238
Chris Lattner3101c252002-08-15 17:58:33 +00001239 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001240 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001241 // the context of a function, getValNonImprovising will search the functions
1242 // symbol table instead of the module symbol table for the global symbol,
1243 // which throws things all off. To get around this, we just tell
1244 // getValNonImprovising that we are at global scope here.
1245 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001246 Function *SavedCurFn = CurFun.CurrentFunction;
1247 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001248
Chris Lattner2079fde2001-10-13 06:41:08 +00001249 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001250
Chris Lattner394f2eb2003-10-16 20:12:13 +00001251 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001252
Chris Lattner2079fde2001-10-13 06:41:08 +00001253 // If this is an initializer for a constant pointer, which is referencing a
1254 // (currently) undefined variable, create a stub now that shall be replaced
1255 // in the future with the right type of variable.
1256 //
1257 if (V == 0) {
1258 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1259 const PointerType *PT = cast<PointerType>(Ty);
1260
1261 // First check to see if the forward references value is already created!
1262 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001263 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001264
1265 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001266 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001267 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001268 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001269 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001270 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001271
Reid Spencer3271ed52004-07-18 00:08:11 +00001272 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001273 GlobalValue *GV;
1274 if (const FunctionType *FTy =
1275 dyn_cast<FunctionType>(PT->getElementType())) {
1276 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1277 CurModule.CurrentModule);
1278 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001279 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001280 GlobalValue::ExternalLinkage, 0,
1281 Name, CurModule.CurrentModule);
1282 }
Chris Lattner8a327842004-07-14 21:44:00 +00001283
Reid Spencer3271ed52004-07-18 00:08:11 +00001284 // Keep track of the fact that we have a forward ref to recycle it
1285 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1286 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001287 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001288 }
1289
Reid Spencer3271ed52004-07-18 00:08:11 +00001290 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001291 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001292 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001293 | Types ConstExpr {
1294 if ($1->get() != $2->getType())
1295 ThrowException("Mismatched types for constant expression!");
1296 $$ = $2;
1297 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001298 }
1299 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001300 const Type *Ty = $1->get();
1301 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1302 ThrowException("Cannot create a null initialized value of this type!");
1303 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001304 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001305 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001306
Chris Lattnere43f40b2002-10-09 00:25:32 +00001307ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001308 if (!ConstantSInt::isValueValidForType($1, $2))
1309 ThrowException("Constant value doesn't fit in type!");
1310 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001311 }
1312 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001313 if (!ConstantUInt::isValueValidForType($1, $2))
1314 ThrowException("Constant value doesn't fit in type!");
1315 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001316 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001317 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001318 $$ = ConstantBool::True;
1319 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001320 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001321 $$ = ConstantBool::False;
1322 }
1323 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001324 if (!ConstantFP::isValueValidForType($1, $2))
1325 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001326 $$ = ConstantFP::get($1, $2);
1327 };
1328
Chris Lattner00950542001-06-06 20:29:01 +00001329
Chris Lattnerd78700d2002-08-16 21:14:40 +00001330ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001331 if (!$3->getType()->isFirstClassType())
1332 ThrowException("cast constant expression from a non-primitive type: '" +
1333 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001334 if (!$5->get()->isFirstClassType())
1335 ThrowException("cast constant expression to a non-primitive type: '" +
1336 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001337 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001338 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001339 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001340 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1341 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001342 ThrowException("GetElementPtr requires a pointer operand!");
1343
Chris Lattner830b6f92004-04-05 01:30:04 +00001344 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1345 // indices to uint struct indices for compatibility.
1346 generic_gep_type_iterator<std::vector<Value*>::iterator>
1347 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1348 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1349 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1350 if (isa<StructType>(*GTI)) // Only change struct indices
1351 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1352 if (CUI->getType() == Type::UByteTy)
1353 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1354
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001355 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001356 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001357 if (!IdxTy)
1358 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001359
Chris Lattner9232b992003-04-22 18:42:41 +00001360 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001361 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1362 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001363 IdxVec.push_back(C);
1364 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001365 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001366
Chris Lattnerd78700d2002-08-16 21:14:40 +00001367 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001368
Chris Lattnerd78700d2002-08-16 21:14:40 +00001369 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001370 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001371 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1372 if ($3->getType() != Type::BoolTy)
1373 ThrowException("Select condition must be of boolean type!");
1374 if ($5->getType() != $7->getType())
1375 ThrowException("Select operand types must match!");
1376 $$ = ConstantExpr::getSelect($3, $5, $7);
1377 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001378 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001379 if ($3->getType() != $5->getType())
1380 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001381 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1382 // To retain backward compatibility with these early compilers, we emit a
1383 // cast to the appropriate integer type automatically if we are in the
1384 // broken case. See PR424 for more information.
1385 if (!isa<PointerType>($3->getType())) {
1386 $$ = ConstantExpr::get($1, $3, $5);
1387 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001388 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001389 switch (CurModule.CurrentModule->getPointerSize()) {
1390 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1391 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1392 default: ThrowException("invalid pointer binary constant expr!");
1393 }
1394 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1395 ConstantExpr::getCast($5, IntPtrTy));
1396 $$ = ConstantExpr::getCast($$, $3->getType());
1397 }
1398 }
1399 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1400 if ($3->getType() != $5->getType())
1401 ThrowException("Logical operator types must match!");
1402 if (!$3->getType()->isIntegral())
1403 ThrowException("Logical operands must have integral types!");
1404 $$ = ConstantExpr::get($1, $3, $5);
1405 }
1406 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1407 if ($3->getType() != $5->getType())
1408 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001409 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001410 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001411 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001412 if ($5->getType() != Type::UByteTy)
1413 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001414 if (!$3->getType()->isInteger())
1415 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001416 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001417 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001418
1419
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001420// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001421ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001422 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001423 }
1424 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001425 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001426 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001427 };
Chris Lattner00950542001-06-06 20:29:01 +00001428
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001429
Chris Lattner1781aca2001-09-18 04:00:54 +00001430// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001431GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001432
Chris Lattner00950542001-06-06 20:29:01 +00001433
Chris Lattner0e73ce62002-05-02 19:11:13 +00001434//===----------------------------------------------------------------------===//
1435// Rules to match Modules
1436//===----------------------------------------------------------------------===//
1437
1438// Module rule: Capture the result of parsing the whole file into a result
1439// variable...
1440//
1441Module : FunctionList {
1442 $$ = ParserResult = $1;
1443 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001444};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001445
Chris Lattner7e708292002-06-25 16:13:24 +00001446// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001447//
1448FunctionList : FunctionList Function {
1449 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001450 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001451 }
1452 | FunctionList FunctionProto {
1453 $$ = $1;
1454 }
1455 | FunctionList IMPLEMENTATION {
1456 $$ = $1;
1457 }
1458 | ConstPool {
1459 $$ = CurModule.CurrentModule;
1460 // Resolve circular types before we parse the body of the module
1461 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001462 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001463
Chris Lattnere98dda62001-07-14 06:10:16 +00001464// ConstPool - Constants with optional names assigned to them.
Chris Lattnere0026942004-07-14 06:44:56 +00001465ConstPool : ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001466 // Eagerly resolve types. This is not an optimization, this is a
1467 // requirement that is due to the fact that we could have this:
1468 //
1469 // %list = type { %list * }
1470 // %list = type { %list * } ; repeated type decl
1471 //
1472 // If types are not resolved eagerly, then the two types will not be
1473 // determined to be the same type!
1474 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001475 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001476
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001477 if (!setTypeName(*$4, $2) && !$2) {
1478 // If this is a named type that is not a redefinition, add it to the slot
1479 // table.
Chris Lattner64116f92004-07-14 01:33:11 +00001480 if (inFunctionScope())
1481 CurFun.Types.push_back(*$4);
1482 else
1483 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001484 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001485
1486 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001487 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001488 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001489 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001490 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001491 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1492 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Chris Lattner1781aca2001-09-18 04:00:54 +00001493 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001494 | ConstPool OptAssign EXTERNAL GlobalType Types {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001495 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001496 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001497 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001498 | ConstPool TARGET TargetDefinition {
1499 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001500 | ConstPool DEPLIBS '=' LibrariesDefinition {
1501 }
Chris Lattner00950542001-06-06 20:29:01 +00001502 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001503 };
Chris Lattner00950542001-06-06 20:29:01 +00001504
1505
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001506
1507BigOrLittle : BIG { $$ = Module::BigEndian; };
1508BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1509
1510TargetDefinition : ENDIAN '=' BigOrLittle {
1511 CurModule.CurrentModule->setEndianness($3);
1512 }
1513 | POINTERSIZE '=' EUINT64VAL {
1514 if ($3 == 32)
1515 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1516 else if ($3 == 64)
1517 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1518 else
1519 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001520 }
1521 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001522 CurModule.CurrentModule->setTargetTriple($3);
1523 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001524 };
1525
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001526LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001527
1528LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001529 CurModule.CurrentModule->addLibrary($3);
1530 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001531 }
1532 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001533 CurModule.CurrentModule->addLibrary($1);
1534 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001535 }
1536 | /* empty: end of list */ {
1537 }
1538 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001539
Chris Lattner00950542001-06-06 20:29:01 +00001540//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001541// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001542//===----------------------------------------------------------------------===//
1543
Chris Lattner6c23f572003-08-22 05:42:10 +00001544Name : VAR_ID | STRINGCONSTANT;
1545OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001546
Chris Lattner6c23f572003-08-22 05:42:10 +00001547ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001548 if (*$1 == Type::VoidTy)
1549 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001550 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001551};
Chris Lattner00950542001-06-06 20:29:01 +00001552
Chris Lattner69da5cf2002-10-13 20:57:00 +00001553ArgListH : ArgListH ',' ArgVal {
1554 $$ = $1;
1555 $1->push_back(*$3);
1556 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001557 }
1558 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001559 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001560 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001561 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001562 };
Chris Lattner00950542001-06-06 20:29:01 +00001563
1564ArgList : ArgListH {
1565 $$ = $1;
1566 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001567 | ArgListH ',' DOTDOTDOT {
1568 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001569 $$->push_back(std::pair<PATypeHolder*,
1570 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001571 }
1572 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001573 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1574 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001575 }
Chris Lattner00950542001-06-06 20:29:01 +00001576 | /* empty */ {
1577 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001578 };
Chris Lattner00950542001-06-06 20:29:01 +00001579
Chris Lattner6c23f572003-08-22 05:42:10 +00001580FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001581 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001582 std::string FunctionName($2);
Chris Lattnera09000d2004-07-14 23:03:46 +00001583 free($2); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001584
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001585 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1586 ThrowException("LLVM functions cannot return aggregate types!");
1587
Chris Lattner9232b992003-04-22 18:42:41 +00001588 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001589 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001590 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001591 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001592 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001593 }
Chris Lattner00950542001-06-06 20:29:01 +00001594
Chris Lattner2079fde2001-10-13 06:41:08 +00001595 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1596 if (isVarArg) ParamTypeList.pop_back();
1597
Chris Lattner1f862af2003-04-16 18:13:57 +00001598 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001599 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001600 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001601
Chris Lattnera09000d2004-07-14 23:03:46 +00001602 ValID ID;
1603 if (!FunctionName.empty()) {
1604 ID = ValID::create((char*)FunctionName.c_str());
1605 } else {
1606 ID = ValID::create((int)CurModule.Values[PFT].size());
1607 }
1608
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001609 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001610 // See if this function was forward referenced. If so, recycle the object.
1611 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1612 // Move the function to the end of the list, from whereever it was
1613 // previously inserted.
1614 Fn = cast<Function>(FWRef);
1615 CurModule.CurrentModule->getFunctionList().remove(Fn);
1616 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1617 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1618 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1619 // If this is the case, either we need to be a forward decl, or it needs
1620 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001621 if (!CurFun.isDeclare && !Fn->isExternal())
1622 ThrowException("Redefinition of function '" + FunctionName + "'!");
1623
Chris Lattnera09000d2004-07-14 23:03:46 +00001624 // Make sure to strip off any argument names so we can't get conflicts.
1625 if (Fn->isExternal())
1626 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend();
1627 AI != AE; ++AI)
1628 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001629
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001630 } else { // Not already defined?
1631 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1632 CurModule.CurrentModule);
1633 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001634 }
Chris Lattner00950542001-06-06 20:29:01 +00001635
Chris Lattner394f2eb2003-10-16 20:12:13 +00001636 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001637
Chris Lattner7e708292002-06-25 16:13:24 +00001638 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001639 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001640 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001641 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001642 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001643 delete $4->back().first;
1644 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001645 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001646 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001647 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001648 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001649 delete I->first; // Delete the typeholder...
1650
Chris Lattner8ab406d2004-07-13 07:59:27 +00001651 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001652 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001653 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001654
Chris Lattner1f862af2003-04-16 18:13:57 +00001655 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001656 }
Chris Lattner51727be2002-06-04 21:58:56 +00001657};
Chris Lattner00950542001-06-06 20:29:01 +00001658
Chris Lattner9b02cc32002-05-03 18:23:48 +00001659BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1660
Chris Lattner4ad02e72003-04-16 20:28:45 +00001661FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001662 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001663
Chris Lattner4ad02e72003-04-16 20:28:45 +00001664 // Make sure that we keep track of the linkage type even if there was a
1665 // previous "declare".
1666 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001667
Chris Lattner7e708292002-06-25 16:13:24 +00001668 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001669 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001670};
Chris Lattner00950542001-06-06 20:29:01 +00001671
Chris Lattner9b02cc32002-05-03 18:23:48 +00001672END : ENDTOK | '}'; // Allow end of '}' to end a function
1673
Chris Lattner79df7c02002-03-26 18:01:55 +00001674Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001675 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001676};
Chris Lattner00950542001-06-06 20:29:01 +00001677
Chris Lattner394f2eb2003-10-16 20:12:13 +00001678FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1679 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001680 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001681};
Chris Lattner00950542001-06-06 20:29:01 +00001682
1683//===----------------------------------------------------------------------===//
1684// Rules to match Basic Blocks
1685//===----------------------------------------------------------------------===//
1686
1687ConstValueRef : ESINT64VAL { // A reference to a direct constant
1688 $$ = ValID::create($1);
1689 }
1690 | EUINT64VAL {
1691 $$ = ValID::create($1);
1692 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001693 | FPVAL { // Perhaps it's an FP constant?
1694 $$ = ValID::create($1);
1695 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001696 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001697 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001698 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001699 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001700 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001701 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001702 | NULL_TOK {
1703 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001704 }
Chris Lattner16710e92004-10-16 18:17:13 +00001705 | UNDEF {
1706 $$ = ValID::createUndef();
1707 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001708 | '<' ConstVector '>' { // Nonempty unsized packed vector
1709 const Type *ETy = (*$2)[0]->getType();
1710 int NumElements = $2->size();
1711
1712 PackedType* pt = PackedType::get(ETy, NumElements);
1713 PATypeHolder* PTy = new PATypeHolder(
1714 HandleUpRefs(
1715 PackedType::get(
1716 ETy,
1717 NumElements)
1718 )
1719 );
1720
1721 // Verify all elements are correct type!
1722 for (unsigned i = 0; i < $2->size(); i++) {
1723 if (ETy != (*$2)[i]->getType())
1724 ThrowException("Element #" + utostr(i) + " is not of type '" +
1725 ETy->getDescription() +"' as required!\nIt is of type '" +
1726 (*$2)[i]->getType()->getDescription() + "'.");
1727 }
1728
1729 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1730 delete PTy; delete $2;
1731 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001732 | ConstExpr {
1733 $$ = ValID::create($1);
1734 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001735
Chris Lattner2079fde2001-10-13 06:41:08 +00001736// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1737// another value.
1738//
1739SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001740 $$ = ValID::create($1);
1741 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001742 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001743 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001744 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001745
1746// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001747ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001748
Chris Lattner00950542001-06-06 20:29:01 +00001749
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001750// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1751// type immediately preceeds the value reference, and allows complex constant
1752// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001753ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001754 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001755 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001756
Chris Lattner00950542001-06-06 20:29:01 +00001757BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001758 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001759 }
Chris Lattner7e708292002-06-25 16:13:24 +00001760 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001761 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001762 };
Chris Lattner00950542001-06-06 20:29:01 +00001763
1764
1765// Basic blocks are terminated by branching instructions:
1766// br, br/cc, switch, ret
1767//
Chris Lattner2079fde2001-10-13 06:41:08 +00001768BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001769 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001770 InsertValue($3);
1771
1772 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001773 InsertValue($1);
1774 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001775 };
Chris Lattner00950542001-06-06 20:29:01 +00001776
1777InstructionList : InstructionList Inst {
1778 $1->getInstList().push_back($2);
1779 $$ = $1;
1780 }
1781 | /* empty */ {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001782 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001783
1784 // Make sure to move the basic block to the correct location in the
1785 // function, instead of leaving it inserted wherever it was first
1786 // referenced.
1787 CurFun.CurrentFunction->getBasicBlockList().remove(CurBB);
1788 CurFun.CurrentFunction->getBasicBlockList().push_back(CurBB);
Chris Lattner22ae2322004-07-13 08:39:15 +00001789 }
1790 | LABELSTR {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001791 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001792
1793 // Make sure to move the basic block to the correct location in the
1794 // function, instead of leaving it inserted wherever it was first
1795 // referenced.
1796 CurFun.CurrentFunction->getBasicBlockList().remove(CurBB);
1797 CurFun.CurrentFunction->getBasicBlockList().push_back(CurBB);
Chris Lattner51727be2002-06-04 21:58:56 +00001798 };
Chris Lattner00950542001-06-06 20:29:01 +00001799
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001800BBTerminatorInst : RET ResolvedVal { // Return with a result...
1801 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001802 }
1803 | RET VOID { // Return with no result...
1804 $$ = new ReturnInst();
1805 }
1806 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001807 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001808 } // Conditional Branch...
1809 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001810 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001811 }
1812 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001813 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001814 $$ = S;
1815
Chris Lattner9232b992003-04-22 18:42:41 +00001816 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001817 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001818 for (; I != E; ++I) {
1819 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
1820 S->addCase(CI, I->second);
1821 else
1822 ThrowException("Switch case is constant, but not a simple integer!");
1823 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001824 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001825 }
Chris Lattner196850c2003-05-15 21:30:00 +00001826 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001827 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00001828 $$ = S;
1829 }
Chris Lattner64116f92004-07-14 01:33:11 +00001830 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1831 UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001832 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001833 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001834
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001835 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1836 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001837 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001838 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001839 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001840 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1841 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001842 ParamTypes.push_back((*I)->getType());
1843 }
1844
1845 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1846 if (isVarArg) ParamTypes.pop_back();
1847
Chris Lattner79df7c02002-03-26 18:01:55 +00001848 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001849 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001850 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001851
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001852 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001853
Chris Lattner64116f92004-07-14 01:33:11 +00001854 BasicBlock *Normal = getBBVal($9);
1855 BasicBlock *Except = getBBVal($12);
Chris Lattner2079fde2001-10-13 06:41:08 +00001856
1857 // Create the call node...
1858 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001859 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001860 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001861 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001862 // correctly!
1863 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001864 FunctionType::param_iterator I = Ty->param_begin();
1865 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001866 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001867
1868 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001869 if ((*ArgI)->getType() != *I)
1870 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1871 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001872
1873 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001874 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001875
Chris Lattner6cdb0112001-11-26 16:54:11 +00001876 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001877 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001878 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001879 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001880 }
1881 | UNWIND {
1882 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00001883 }
1884 | UNREACHABLE {
1885 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001886 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001887
1888
Chris Lattner00950542001-06-06 20:29:01 +00001889
1890JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1891 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001892 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001893 if (V == 0)
1894 ThrowException("May only switch on a constant pool value!");
1895
Chris Lattner64116f92004-07-14 01:33:11 +00001896 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001897 }
1898 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001899 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001900 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001901
1902 if (V == 0)
1903 ThrowException("May only switch on a constant pool value!");
1904
Chris Lattner64116f92004-07-14 01:33:11 +00001905 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001906 };
Chris Lattner00950542001-06-06 20:29:01 +00001907
1908Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001909 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001910 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001911 InsertValue($2);
1912 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001913};
Chris Lattner00950542001-06-06 20:29:01 +00001914
Chris Lattnerc24d2082001-06-11 15:04:20 +00001915PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001916 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001917 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001918 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001919 }
1920 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1921 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001922 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001923 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001924 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001925
1926
Chris Lattner30c89792001-09-07 16:35:17 +00001927ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001928 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001929 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001930 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001931 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001932 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001933 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001934 };
Chris Lattner00950542001-06-06 20:29:01 +00001935
1936// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001937ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001938
Chris Lattner4a6482b2002-09-10 19:57:26 +00001939InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00001940 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
1941 !isa<PackedType>((*$2).get()))
1942 ThrowException(
1943 "Arithmetic operator requires integer, FP, or packed operands!");
1944 if(isa<PackedType>((*$2).get()) && $1 == Instruction::Rem) {
1945 ThrowException(
1946 "Rem not supported on packed types!");
1947 }
Chris Lattner4a6482b2002-09-10 19:57:26 +00001948 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1949 if ($$ == 0)
1950 ThrowException("binary operator returned null!");
1951 delete $2;
1952 }
1953 | LogicalOps Types ValueRef ',' ValueRef {
1954 if (!(*$2)->isIntegral())
1955 ThrowException("Logical operator requires integral operands!");
1956 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1957 if ($$ == 0)
1958 ThrowException("binary operator returned null!");
1959 delete $2;
1960 }
1961 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00001962 if(isa<PackedType>((*$2).get())) {
1963 ThrowException(
1964 "PackedTypes currently not supported in setcc instructions!");
1965 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00001966 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001967 if ($$ == 0)
1968 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001969 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001970 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001971 | NOT ResolvedVal {
1972 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1973 << " Replacing with 'xor'.\n";
1974
1975 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1976 if (Ones == 0)
1977 ThrowException("Expected integral type for not instruction!");
1978
1979 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001980 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001981 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001982 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001983 | ShiftOps ResolvedVal ',' ResolvedVal {
1984 if ($4->getType() != Type::UByteTy)
1985 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001986 if (!$2->getType()->isInteger())
1987 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001988 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001989 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001990 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001991 if (!$4->get()->isFirstClassType())
1992 ThrowException("cast instruction to a non-primitive type: '" +
1993 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001994 $$ = new CastInst($2, *$4);
1995 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001996 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001997 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1998 if ($2->getType() != Type::BoolTy)
1999 ThrowException("select condition must be boolean!");
2000 if ($4->getType() != $6->getType())
2001 ThrowException("select value types should match!");
2002 $$ = new SelectInst($2, $4, $6);
2003 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00002004 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00002005 // FIXME: This is emulation code for an obsolete syntax. This should be
2006 // removed at some point.
2007 if (!ObsoleteVarArgs) {
2008 std::cerr << "WARNING: this file uses obsolete features. "
2009 << "Assemble and disassemble to update it.\n";
2010 ObsoleteVarArgs = true;
2011 }
2012
2013 // First, load the valist...
2014 Instruction *CurVAList = new LoadInst($2, "");
2015 CurBB->getInstList().push_back(CurVAList);
2016
2017 // Emit the vaarg instruction.
2018 $$ = new VAArgInst(CurVAList, *$4);
2019
2020 // Now we must advance the pointer and update it in memory.
2021 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
2022 CurBB->getInstList().push_back(TheVANext);
2023
2024 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
2025 delete $4;
2026 }
2027 | VAARG ResolvedVal ',' Types {
2028 $$ = new VAArgInst($2, *$4);
2029 delete $4;
2030 }
2031 | VANEXT ResolvedVal ',' Types {
2032 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002033 delete $4;
2034 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002035 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002036 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002037 if (!Ty->isFirstClassType())
2038 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002039 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002040 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002041 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002042 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00002043 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002044 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002045 $2->pop_front();
2046 }
2047 delete $2; // Free the list...
2048 }
Chris Lattner93750fa2001-07-28 17:48:55 +00002049 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002050 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002051 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002052
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002053 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
2054 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002055 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002056 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00002057 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00002058 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
2059 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002060 ParamTypes.push_back((*I)->getType());
2061 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002062
2063 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2064 if (isVarArg) ParamTypes.pop_back();
2065
Chris Lattner595f06c2005-02-01 01:47:42 +00002066 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
2067 ThrowException("LLVM functions cannot return aggregate types!");
2068
Chris Lattner79df7c02002-03-26 18:01:55 +00002069 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002070 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002071 }
Chris Lattner00950542001-06-06 20:29:01 +00002072
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002073 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002074
Chris Lattner8b81bf52001-07-25 22:47:46 +00002075 // Create the call node...
2076 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002077 // Make sure no arguments is a good thing!
2078 if (Ty->getNumParams() != 0)
2079 ThrowException("No arguments passed to a function that "
2080 "expects arguments!");
2081
Chris Lattner9232b992003-04-22 18:42:41 +00002082 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002083 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002084 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002085 // correctly!
2086 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002087 FunctionType::param_iterator I = Ty->param_begin();
2088 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00002089 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002090
2091 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002092 if ((*ArgI)->getType() != *I)
2093 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2094 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002095
Chris Lattner8b81bf52001-07-25 22:47:46 +00002096 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002097 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002098
Chris Lattner6cdb0112001-11-26 16:54:11 +00002099 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002100 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00002101 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002102 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00002103 }
2104 | MemoryInst {
2105 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002106 };
Chris Lattner00950542001-06-06 20:29:01 +00002107
Chris Lattner6cdb0112001-11-26 16:54:11 +00002108
2109// IndexList - List of indices for GEP based instructions...
2110IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002111 $$ = $2;
2112 } | /* empty */ {
2113 $$ = new std::vector<Value*>();
2114 };
2115
2116OptVolatile : VOLATILE {
2117 $$ = true;
2118 }
2119 | /* empty */ {
2120 $$ = false;
2121 };
2122
Chris Lattner027dcc52001-07-08 21:10:27 +00002123
Chris Lattner00950542001-06-06 20:29:01 +00002124MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002125 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002126 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002127 }
2128 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002129 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002130 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002131 }
2132 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002133 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002134 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002135 }
2136 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002137 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002138 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002139 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002140 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002141 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002142 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002143 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002144 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002145 }
2146
Chris Lattner15c9c032003-09-08 18:20:29 +00002147 | OptVolatile LOAD Types ValueRef {
2148 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002149 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002150 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002151 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2152 ThrowException("Can't load from pointer of non-first-class type: " +
2153 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002154 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002155 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002156 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002157 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2158 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002159 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002160 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002161 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002162 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002163 if (ElTy != $3->getType())
2164 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002165 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002166
Chris Lattner79ad13802003-09-08 20:29:46 +00002167 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002168 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002169 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002170 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002171 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002172 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002173
2174 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2175 // indices to uint struct indices for compatibility.
2176 generic_gep_type_iterator<std::vector<Value*>::iterator>
2177 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2178 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2179 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2180 if (isa<StructType>(*GTI)) // Only change struct indices
2181 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2182 if (CUI->getType() == Type::UByteTy)
2183 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2184
Chris Lattner30c89792001-09-07 16:35:17 +00002185 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002186 ThrowException("Invalid getelementptr indices for type '" +
2187 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002188 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2189 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002190 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002191
Brian Gaeked0fde302003-11-11 22:41:34 +00002192
Chris Lattner00950542001-06-06 20:29:01 +00002193%%
Chris Lattner09083092001-07-08 04:57:15 +00002194int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002195 std::string where
2196 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002197 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002198 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002199 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002200 errMsg += "end-of-file.";
2201 else
Chris Lattner9232b992003-04-22 18:42:41 +00002202 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002203 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002204 return 0;
2205}