blob: e48d6cfc9fca95fb38e9f77242ddf6700d2fb9e6 [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"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/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 " +
159 BBForwardRefs.begin()->second.first.getName());
160
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 Lattnerd78700d2002-08-16 21:14:40 +0000301 case ValID::ConstantVal: // Fully resolved constant?
302 if (D.ConstantValue->getType() != Ty)
303 ThrowException("Constant expression type different from required type!");
304 return D.ConstantValue;
305
Chris Lattner30c89792001-09-07 16:35:17 +0000306 default:
307 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000308 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000309 } // End of switch
310
Chris Lattner2079fde2001-10-13 06:41:08 +0000311 assert(0 && "Unhandled case!");
312 return 0;
313}
314
Chris Lattner2079fde2001-10-13 06:41:08 +0000315// getVal - This function is identical to getValNonImprovising, except that if a
316// value is not already defined, it "improvises" by creating a placeholder var
317// that looks and acts just like the requested variable. When the value is
318// defined later, all uses of the placeholder variable are replaced with the
319// real thing.
320//
Chris Lattner64116f92004-07-14 01:33:11 +0000321static Value *getVal(const Type *Ty, const ValID &ID) {
322 if (Ty == Type::LabelTy)
323 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000324
Chris Lattner64116f92004-07-14 01:33:11 +0000325 // See if the value has already been defined.
326 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000327 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000328
329 // If we reached here, we referenced either a symbol that we don't know about
330 // or an id number that hasn't been read yet. We may be referencing something
331 // forward, so just create an entry to be resolved later and get to it...
332 //
Chris Lattner64116f92004-07-14 01:33:11 +0000333 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000334
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000335 // Remember where this forward reference came from. FIXME, shouldn't we try
336 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000337 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000338 llvmAsmlineno)));
339
Chris Lattner79df7c02002-03-26 18:01:55 +0000340 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000341 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000342 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000343 InsertValue(V, CurModule.LateResolveValues);
344 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000345}
346
Chris Lattner2e2ed292004-07-14 06:28:35 +0000347/// getBBVal - This is used for two purposes:
348/// * If isDefinition is true, a new basic block with the specified ID is being
349/// defined.
350/// * If isDefinition is true, this is a reference to a basic block, which may
351/// or may not be a forward reference.
352///
353static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000354 assert(inFunctionScope() && "Can't get basic block at global scope!");
355
Chris Lattner2e2ed292004-07-14 06:28:35 +0000356 std::string Name;
357 BasicBlock *BB = 0;
358 switch (ID.Type) {
359 default: ThrowException("Illegal label reference " + ID.getName());
360 case ValID::NumberVal: // Is it a numbered definition?
361 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
362 CurFun.NumberedBlocks.resize(ID.Num+1);
363 BB = CurFun.NumberedBlocks[ID.Num];
364 break;
365 case ValID::NameVal: // Is it a named definition?
366 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000367 if (Value *N = CurFun.CurrentFunction->
368 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000369 BB = cast<BasicBlock>(N);
370 break;
371 }
Chris Lattner64116f92004-07-14 01:33:11 +0000372
Chris Lattner2e2ed292004-07-14 06:28:35 +0000373 // See if the block has already been defined.
374 if (BB) {
375 // If this is the definition of the block, make sure the existing value was
376 // just a forward reference. If it was a forward reference, there will be
377 // an entry for it in the PlaceHolderInfo map.
378 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
379 // The existing value was a definition, not a forward reference.
380 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000381
Chris Lattner2e2ed292004-07-14 06:28:35 +0000382 ID.destroy(); // Free strdup'd memory.
383 return BB;
384 }
385
386 // Otherwise this block has not been seen before.
387 BB = new BasicBlock("", CurFun.CurrentFunction);
388 if (ID.Type == ValID::NameVal) {
389 BB->setName(ID.Name);
390 } else {
391 CurFun.NumberedBlocks[ID.Num] = BB;
392 }
393
394 // If this is not a definition, keep track of it so we can use it as a forward
395 // reference.
396 if (!isDefinition) {
397 // Remember where this forward reference came from.
398 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
399 } else {
400 // The forward declaration could have been inserted anywhere in the
401 // function: insert it into the correct place now.
402 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
403 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
404 }
405
Chris Lattner64116f92004-07-14 01:33:11 +0000406 return BB;
407}
408
Chris Lattner00950542001-06-06 20:29:01 +0000409
410//===----------------------------------------------------------------------===//
411// Code to handle forward references in instructions
412//===----------------------------------------------------------------------===//
413//
414// This code handles the late binding needed with statements that reference
415// values not defined yet... for example, a forward branch, or the PHI node for
416// a loop body.
417//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000418// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000419// and back patchs after we are done.
420//
421
422// ResolveDefinitions - If we could not resolve some defs at parsing
423// time (forward branches, phi functions for loops, etc...) resolve the
424// defs now...
425//
Chris Lattner735f2702004-07-08 22:30:50 +0000426static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
427 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000428 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000429 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000430 E = LateResolvers.end(); LRI != E; ++LRI) {
431 ValueList &List = LRI->second;
432 while (!List.empty()) {
433 Value *V = List.back();
434 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000435
436 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
437 CurModule.PlaceHolderInfo.find(V);
438 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
439
440 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000441
Chris Lattner735f2702004-07-08 22:30:50 +0000442 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000443 if (TheRealValue) {
444 V->replaceAllUsesWith(TheRealValue);
445 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000446 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000447 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000448 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000449 // resolver table
450 InsertValue(V, *FutureLateResolvers);
451 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000452 if (DID.Type == ValID::NameVal)
453 ThrowException("Reference to an invalid definition: '" +DID.getName()+
454 "' of type '" + V->getType()->getDescription() + "'",
455 PHI->second.second);
456 else
457 ThrowException("Reference to an invalid definition: #" +
458 itostr(DID.Num) + " of type '" +
459 V->getType()->getDescription() + "'",
460 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000461 }
Chris Lattner00950542001-06-06 20:29:01 +0000462 }
463 }
464
465 LateResolvers.clear();
466}
467
Chris Lattner4a42e902001-10-22 05:56:09 +0000468// ResolveTypeTo - A brand new type was just declared. This means that (if
469// name is not null) things referencing Name can be resolved. Otherwise, things
470// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000471//
Chris Lattner4a42e902001-10-22 05:56:09 +0000472static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000473 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000474 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000475
Chris Lattner4a42e902001-10-22 05:56:09 +0000476 ValID D;
477 if (Name) D = ValID::create(Name);
478 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000479
Chris Lattner9232b992003-04-22 18:42:41 +0000480 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000481 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000482
Chris Lattner9232b992003-04-22 18:42:41 +0000483 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000484 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000485 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000486 LateResolver.erase(I);
487 }
488}
489
490// ResolveTypes - At this point, all types should be resolved. Any that aren't
491// are errors.
492//
Chris Lattner9232b992003-04-22 18:42:41 +0000493static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000494 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000495 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000496
497 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000498 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000499 else
Chris Lattner82269592001-10-22 06:01:08 +0000500 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000501 }
502}
503
Chris Lattner1781aca2001-09-18 04:00:54 +0000504// setValueName - Set the specified value to the name given. The name may be
505// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000506// assumed to be a malloc'd string buffer, and is free'd by this function.
507//
508static void setValueName(Value *V, char *NameStr) {
509 if (NameStr) {
510 std::string Name(NameStr); // Copy string
511 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000512
513 if (V->getType() == Type::VoidTy)
514 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000515
516 assert(inFunctionScope() && "Must be in function scope!");
517 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
518 if (ST.lookup(V->getType(), Name))
519 ThrowException("Redefinition of value named '" + Name + "' in the '" +
520 V->getType()->getDescription() + "' type plane!");
521
Chris Lattnerc9aea522004-07-13 08:12:39 +0000522 // Set the name.
523 V->setName(Name, &ST);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000524 }
525}
526
Chris Lattnerd88998d2004-07-14 08:23:52 +0000527/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
528/// this is a declaration, otherwise it is a definition.
529static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
530 bool isConstantGlobal, const Type *Ty,
531 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000532 if (isa<FunctionType>(Ty))
533 ThrowException("Cannot declare global vars of function type!");
534
Chris Lattnera09000d2004-07-14 23:03:46 +0000535 const PointerType *PTy = PointerType::get(Ty);
536
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000537 std::string Name;
538 if (NameStr) {
539 Name = NameStr; // Copy string
540 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000541 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000542
Chris Lattner8a327842004-07-14 21:44:00 +0000543 // See if this global value was forward referenced. If so, recycle the
544 // object.
545 ValID ID;
546 if (!Name.empty()) {
547 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000548 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000549 ID = ValID::create((int)CurModule.Values[PTy].size());
550 }
551
552 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
553 // Move the global to the end of the list, from whereever it was
554 // previously inserted.
555 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
556 CurModule.CurrentModule->getGlobalList().remove(GV);
557 CurModule.CurrentModule->getGlobalList().push_back(GV);
558 GV->setInitializer(Initializer);
559 GV->setLinkage(Linkage);
560 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000561 InsertValue(GV, CurModule.Values);
Chris Lattnera09000d2004-07-14 23:03:46 +0000562 return;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000563 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000564
565 // If this global has a name, check to see if there is already a definition
566 // of this global in the module. If so, merge as appropriate. Note that
567 // this is really just a hack around problems in the CFE. :(
568 if (!Name.empty()) {
569 // We are a simple redefinition of a value, check to see if it is defined
570 // the same as the old one.
571 if (GlobalVariable *EGV =
572 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
573 // We are allowed to redefine a global variable in two circumstances:
574 // 1. If at least one of the globals is uninitialized or
575 // 2. If both initializers have the same value.
576 //
577 if (!EGV->hasInitializer() || !Initializer ||
578 EGV->getInitializer() == Initializer) {
579
580 // Make sure the existing global version gets the initializer! Make
581 // sure that it also gets marked const if the new version is.
582 if (Initializer && !EGV->hasInitializer())
583 EGV->setInitializer(Initializer);
584 if (isConstantGlobal)
585 EGV->setConstant(true);
586 EGV->setLinkage(Linkage);
587 return;
588 }
589
590 ThrowException("Redefinition of global variable named '" + Name +
591 "' in the '" + Ty->getDescription() + "' type plane!");
592 }
593 }
594
595 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000596 GlobalVariable *GV =
597 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
598 CurModule.CurrentModule);
599 InsertValue(GV, CurModule.Values);
Chris Lattnerd88998d2004-07-14 08:23:52 +0000600}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000601
Reid Spencer75719bf2004-05-26 21:48:31 +0000602// setTypeName - Set the specified type to the name given. The name may be
603// null potentially, in which case this is a noop. The string passed in is
604// assumed to be a malloc'd string buffer, and is freed by this function.
605//
606// This function returns true if the type has already been defined, but is
607// allowed to be redefined in the specified context. If the name is a new name
608// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000609static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000610 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000611 if (NameStr == 0) return false;
612
613 std::string Name(NameStr); // Copy string
614 free(NameStr); // Free old string
615
616 // We don't allow assigning names to void type
617 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000618 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000619
Chris Lattnera09000d2004-07-14 23:03:46 +0000620 // Set the type name, checking for conflicts as we do so.
621 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000622
Chris Lattnera09000d2004-07-14 23:03:46 +0000623 if (AlreadyExists) { // Inserting a name that is already defined???
624 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
625 assert(Existing && "Conflict but no matching type?");
626
Reid Spencer75719bf2004-05-26 21:48:31 +0000627 // There is only one case where this is allowed: when we are refining an
628 // opaque type. In this case, Existing will be an opaque type.
629 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
630 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000631 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000632 return true;
633 }
634
635 // Otherwise, this is an attempt to redefine a type. That's okay if
636 // the redefinition is identical to the original. This will be so if
637 // Existing and T point to the same Type object. In this one case we
638 // allow the equivalent redefinition.
639 if (Existing == T) return true; // Yes, it's equal.
640
641 // Any other kind of (non-equivalent) redefinition is an error.
642 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000643 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000644 }
645
Reid Spencer75719bf2004-05-26 21:48:31 +0000646 return false;
647}
Chris Lattner8896eda2001-07-09 19:38:36 +0000648
Chris Lattner30c89792001-09-07 16:35:17 +0000649//===----------------------------------------------------------------------===//
650// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000651//
Chris Lattner8896eda2001-07-09 19:38:36 +0000652
Chris Lattner3b073862004-02-09 03:19:29 +0000653// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000654//
655static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000656 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
657}
658
659namespace {
660 struct UpRefRecord {
661 // NestingLevel - The number of nesting levels that need to be popped before
662 // this type is resolved.
663 unsigned NestingLevel;
664
665 // LastContainedTy - This is the type at the current binding level for the
666 // type. Every time we reduce the nesting level, this gets updated.
667 const Type *LastContainedTy;
668
669 // UpRefTy - This is the actual opaque type that the upreference is
670 // represented with.
671 OpaqueType *UpRefTy;
672
673 UpRefRecord(unsigned NL, OpaqueType *URTy)
674 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
675 };
Chris Lattner30c89792001-09-07 16:35:17 +0000676}
Chris Lattner698b56e2001-07-20 19:15:08 +0000677
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000678// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000679static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000680
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000681/// HandleUpRefs - Every time we finish a new layer of types, this function is
682/// called. It loops through the UpRefs vector, which is a list of the
683/// currently active types. For each type, if the up reference is contained in
684/// the newly completed type, we decrement the level count. When the level
685/// count reaches zero, the upreferenced type is the type that is passed in:
686/// thus we can complete the cycle.
687///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000688static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000689 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000690 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000691 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000692 "' newly formed. Resolving upreferences.\n" <<
693 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000694
695 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
696 // to zero), we resolve them all together before we resolve them to Ty. At
697 // the end of the loop, if there is anything to resolve to Ty, it will be in
698 // this variable.
699 OpaqueType *TypeToResolve = 0;
700
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000701 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000702 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencer3271ed52004-07-18 00:08:11 +0000703 << UpRefs[i].second->getDescription() << ") = "
704 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000705 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
706 // Decrement level of upreference
707 unsigned Level = --UpRefs[i].NestingLevel;
708 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000709 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000710 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000711 if (!TypeToResolve) {
712 TypeToResolve = UpRefs[i].UpRefTy;
713 } else {
714 UR_OUT(" * Resolving upreference for "
715 << UpRefs[i].second->getDescription() << "\n";
716 std::string OldName = UpRefs[i].UpRefTy->getDescription());
717 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
718 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
719 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
720 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000721 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000722 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000723 }
724 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000725 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000726
Chris Lattner026a8ce2004-02-09 18:53:54 +0000727 if (TypeToResolve) {
728 UR_OUT(" * Resolving upreference for "
729 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000730 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000731 TypeToResolve->refineAbstractTypeTo(Ty);
732 }
733
Chris Lattner8896eda2001-07-09 19:38:36 +0000734 return Ty;
735}
736
Chris Lattner30c89792001-09-07 16:35:17 +0000737
Chris Lattner00950542001-06-06 20:29:01 +0000738//===----------------------------------------------------------------------===//
739// RunVMAsmParser - Define an interface to this parser
740//===----------------------------------------------------------------------===//
741//
Chris Lattner86427632004-07-14 06:39:48 +0000742Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000743 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000744 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000745 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000746 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000747
Chris Lattner75f20532003-04-22 18:02:52 +0000748 // Allocate a new module to read
749 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000750
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000751 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000752
Chris Lattner00950542001-06-06 20:29:01 +0000753 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000754
755 // Check to see if they called va_start but not va_arg..
756 if (!ObsoleteVarArgs)
757 if (Function *F = Result->getNamedFunction("llvm.va_start"))
758 if (F->asize() == 1) {
759 std::cerr << "WARNING: this file uses obsolete features. "
760 << "Assemble and disassemble to update it.\n";
761 ObsoleteVarArgs = true;
762 }
763
Chris Lattner99e7ab72003-10-18 05:53:13 +0000764 if (ObsoleteVarArgs) {
765 // If the user is making use of obsolete varargs intrinsics, adjust them for
766 // the user.
767 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
768 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
769
770 const Type *RetTy = F->getFunctionType()->getParamType(0);
771 RetTy = cast<PointerType>(RetTy)->getElementType();
772 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
773
774 while (!F->use_empty()) {
775 CallInst *CI = cast<CallInst>(F->use_back());
776 Value *V = new CallInst(NF, "", CI);
777 new StoreInst(V, CI->getOperand(1), CI);
778 CI->getParent()->getInstList().erase(CI);
779 }
780 Result->getFunctionList().erase(F);
781 }
782
783 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
784 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
785 const Type *ArgTy = F->getFunctionType()->getParamType(0);
786 ArgTy = cast<PointerType>(ArgTy)->getElementType();
787 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
788 ArgTy, 0);
789
790 while (!F->use_empty()) {
791 CallInst *CI = cast<CallInst>(F->use_back());
792 Value *V = new LoadInst(CI->getOperand(1), "", CI);
793 new CallInst(NF, V, "", CI);
794 CI->getParent()->getInstList().erase(CI);
795 }
796 Result->getFunctionList().erase(F);
797 }
798
799 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
800 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
801 const Type *ArgTy = F->getFunctionType()->getParamType(0);
802 ArgTy = cast<PointerType>(ArgTy)->getElementType();
803 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
804 ArgTy, 0);
805
806 while (!F->use_empty()) {
807 CallInst *CI = cast<CallInst>(F->use_back());
808 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
809 new StoreInst(V, CI->getOperand(1), CI);
810 CI->getParent()->getInstList().erase(CI);
811 }
812 Result->getFunctionList().erase(F);
813 }
814 }
815
Chris Lattner00950542001-06-06 20:29:01 +0000816 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
817 ParserResult = 0;
818
819 return Result;
820}
821
822%}
823
824%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000825 llvm::Module *ModuleVal;
826 llvm::Function *FunctionVal;
827 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
828 llvm::BasicBlock *BasicBlockVal;
829 llvm::TerminatorInst *TermInstVal;
830 llvm::Instruction *InstVal;
831 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000832
Brian Gaeked0fde302003-11-11 22:41:34 +0000833 const llvm::Type *PrimType;
834 llvm::PATypeHolder *TypeVal;
835 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000836
Brian Gaeked0fde302003-11-11 22:41:34 +0000837 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
838 std::vector<llvm::Value*> *ValueList;
839 std::list<llvm::PATypeHolder> *TypeList;
840 std::list<std::pair<llvm::Value*,
841 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
842 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
843 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000844
Brian Gaeked0fde302003-11-11 22:41:34 +0000845 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000846 int64_t SInt64Val;
847 uint64_t UInt64Val;
848 int SIntVal;
849 unsigned UIntVal;
850 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000851 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000852
Chris Lattner30c89792001-09-07 16:35:17 +0000853 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000854 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000855
Brian Gaeked0fde302003-11-11 22:41:34 +0000856 llvm::Instruction::BinaryOps BinaryOpVal;
857 llvm::Instruction::TermOps TermOpVal;
858 llvm::Instruction::MemoryOps MemOpVal;
859 llvm::Instruction::OtherOps OtherOpVal;
860 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000861}
862
Chris Lattner79df7c02002-03-26 18:01:55 +0000863%type <ModuleVal> Module FunctionList
864%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000865%type <BasicBlockVal> BasicBlock InstructionList
866%type <TermInstVal> BBTerminatorInst
867%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000868%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000869%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000870%type <ArgList> ArgList ArgListH
871%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000872%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000873%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000874%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000875%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000876%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000877%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000878%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000879%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000880%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000881
Chris Lattner2079fde2001-10-13 06:41:08 +0000882// ValueRef - Unresolved reference to a definition or BB
883%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000884%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000885// Tokens and types for handling constant integer values
886//
887// ESINT64VAL - A negative number within long long range
888%token <SInt64Val> ESINT64VAL
889
890// EUINT64VAL - A positive number within uns. long long range
891%token <UInt64Val> EUINT64VAL
892%type <SInt64Val> EINT64VAL
893
894%token <SIntVal> SINTVAL // Signed 32 bit ints...
895%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
896%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000897%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000898
899// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000900%type <TypeVal> Types TypesV UpRTypes UpRTypesV
901%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000902%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
903%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000904
Chris Lattner6c23f572003-08-22 05:42:10 +0000905%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
906%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000907
908
Chris Lattner91ef4602004-03-31 03:49:47 +0000909%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000910%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000911%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Reid Spencer0be13e72004-07-25 17:58:28 +0000912%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
913%token DEPLIBS
Chris Lattner00950542001-06-06 20:29:01 +0000914
915// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000916%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000917
Chris Lattner00950542001-06-06 20:29:01 +0000918// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000919%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000920%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000921%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000922
923// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000924%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000925
Chris Lattner027dcc52001-07-08 21:10:27 +0000926// Other Operators
927%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000928%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000929%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000930
Chris Lattner00950542001-06-06 20:29:01 +0000931%start Module
932%%
933
934// Handle constant integer size restriction and conversion...
935//
Chris Lattner51727be2002-06-04 21:58:56 +0000936INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000937INTVAL : UINTVAL {
938 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
939 ThrowException("Value too large for type!");
940 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000941};
Chris Lattner00950542001-06-06 20:29:01 +0000942
943
Chris Lattner51727be2002-06-04 21:58:56 +0000944EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000945EINT64VAL : EUINT64VAL {
946 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
947 ThrowException("Value too large for type!");
948 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000949};
Chris Lattner00950542001-06-06 20:29:01 +0000950
Chris Lattner00950542001-06-06 20:29:01 +0000951// Operations that are notably excluded from this list include:
952// RET, BR, & SWITCH because they end basic blocks and are treated specially.
953//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000954ArithmeticOps: ADD | SUB | MUL | DIV | REM;
955LogicalOps : AND | OR | XOR;
956SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +0000957
Chris Lattner51727be2002-06-04 21:58:56 +0000958ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000959
Chris Lattnere98dda62001-07-14 06:10:16 +0000960// These are some types that allow classification if we only want a particular
961// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000962SIntType : LONG | INT | SHORT | SBYTE;
963UIntType : ULONG | UINT | USHORT | UBYTE;
964IntType : SIntType | UIntType;
965FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000966
Chris Lattnere98dda62001-07-14 06:10:16 +0000967// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000968OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000969 $$ = $1;
970 }
971 | /*empty*/ {
972 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000973 };
Chris Lattner00950542001-06-06 20:29:01 +0000974
Chris Lattner4ad02e72003-04-16 20:28:45 +0000975OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
976 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000977 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000978 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
979 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000980
981//===----------------------------------------------------------------------===//
982// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000983// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000984// access to it, a user must explicitly use TypesV.
985//
986
987// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000988TypesV : Types | VOID { $$ = new PATypeHolder($1); };
989UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000990
991Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000992 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000993 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
994 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000995 };
Chris Lattner30c89792001-09-07 16:35:17 +0000996
997
998// Derived types are added later...
999//
Chris Lattner51727be2002-06-04 21:58:56 +00001000PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1001PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001002UpRTypes : OPAQUE {
1003 $$ = new PATypeHolder(OpaqueType::get());
1004 }
1005 | PrimType {
1006 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001007 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001008UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001009 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001010};
Chris Lattner30c89792001-09-07 16:35:17 +00001011
Chris Lattner30c89792001-09-07 16:35:17 +00001012// Include derived types in the Types production.
1013//
1014UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001015 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001016 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001017 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001018 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001019 UR_OUT("New Upreference!\n");
1020 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001021 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001022 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001023 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Reid Spencer3271ed52004-07-18 00:08:11 +00001024 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001025 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1026 if (isVarArg) Params.pop_back();
1027
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001028 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001029 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001030 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001031 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001032 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001033 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001034 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001035 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001036 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001037 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001038 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Reid Spencer3271ed52004-07-18 00:08:11 +00001039 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001040
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001041 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001042 delete $2;
1043 }
1044 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001045 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001046 }
1047 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001048 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001049 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001050 };
Chris Lattner30c89792001-09-07 16:35:17 +00001051
Chris Lattner7e708292002-06-25 16:13:24 +00001052// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001053// declaration type lists
1054//
1055TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001056 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001057 $$->push_back(*$1); delete $1;
1058 }
1059 | TypeListI ',' UpRTypes {
1060 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001061 };
Chris Lattner30c89792001-09-07 16:35:17 +00001062
Chris Lattner7e708292002-06-25 16:13:24 +00001063// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001064ArgTypeListI : TypeListI
1065 | TypeListI ',' DOTDOTDOT {
1066 ($$=$1)->push_back(Type::VoidTy);
1067 }
1068 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001069 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001070 }
1071 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001072 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001073 };
Chris Lattner30c89792001-09-07 16:35:17 +00001074
Chris Lattnere98dda62001-07-14 06:10:16 +00001075// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001076// production is used ONLY to represent constants that show up AFTER a 'const',
1077// 'constant' or 'global' token at global scope. Constants that can be inlined
1078// into other expressions (such as integers and constexprs) are handled by the
1079// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001080//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001081ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001082 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001083 if (ATy == 0)
1084 ThrowException("Cannot make array constant with type: '" +
1085 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001086 const Type *ETy = ATy->getElementType();
1087 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001088
Chris Lattner30c89792001-09-07 16:35:17 +00001089 // Verify that we have the correct size...
1090 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001091 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001092 utostr($3->size()) + " arguments, but has size of " +
1093 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001094
Chris Lattner30c89792001-09-07 16:35:17 +00001095 // Verify all elements are correct type!
1096 for (unsigned i = 0; i < $3->size(); i++) {
1097 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001098 ThrowException("Element #" + utostr(i) + " is not of type '" +
1099 ETy->getDescription() +"' as required!\nIt is of type '"+
1100 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001101 }
1102
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001103 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001104 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001105 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001106 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001107 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001108 if (ATy == 0)
1109 ThrowException("Cannot make array constant with type: '" +
1110 (*$1)->getDescription() + "'!");
1111
1112 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001113 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001114 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001115 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001116 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001117 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001118 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001119 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001120 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001121 if (ATy == 0)
1122 ThrowException("Cannot make array constant with type: '" +
1123 (*$1)->getDescription() + "'!");
1124
Chris Lattner30c89792001-09-07 16:35:17 +00001125 int NumElements = ATy->getNumElements();
1126 const Type *ETy = ATy->getElementType();
1127 char *EndStr = UnEscapeLexed($3, true);
1128 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001129 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001130 itostr((int)(EndStr-$3)) +
1131 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001132 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001133 if (ETy == Type::SByteTy) {
1134 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001135 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001136 } else if (ETy == Type::UByteTy) {
1137 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001138 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001139 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001140 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001141 ThrowException("Cannot build string arrays of non byte sized elements!");
1142 }
Chris Lattner30c89792001-09-07 16:35:17 +00001143 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001144 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001145 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001146 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001147 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001148 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001149 if (STy == 0)
1150 ThrowException("Cannot make struct constant with type: '" +
1151 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001152
Chris Lattnerc6212f12003-05-21 16:06:56 +00001153 if ($3->size() != STy->getNumContainedTypes())
1154 ThrowException("Illegal number of initializers for structure type!");
1155
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001156 // Check to ensure that constants are compatible with the type initializer!
1157 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001158 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001159 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001160 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001161 "' for element #" + utostr(i) +
1162 " of structure initializer!");
1163
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001164 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001165 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001166 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001167 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001168 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001169 if (STy == 0)
1170 ThrowException("Cannot make struct constant with type: '" +
1171 (*$1)->getDescription() + "'!");
1172
1173 if (STy->getNumContainedTypes() != 0)
1174 ThrowException("Illegal number of initializers for structure type!");
1175
1176 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1177 delete $1;
1178 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001179 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001180 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001181 if (PTy == 0)
1182 ThrowException("Cannot make null pointer constant with type: '" +
1183 (*$1)->getDescription() + "'!");
1184
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001185 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001186 delete $1;
1187 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001188 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001189 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001190 if (Ty == 0)
1191 ThrowException("Global const reference must be a pointer type!");
1192
Chris Lattner3101c252002-08-15 17:58:33 +00001193 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001194 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001195 // the context of a function, getValNonImprovising will search the functions
1196 // symbol table instead of the module symbol table for the global symbol,
1197 // which throws things all off. To get around this, we just tell
1198 // getValNonImprovising that we are at global scope here.
1199 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001200 Function *SavedCurFn = CurFun.CurrentFunction;
1201 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001202
Chris Lattner2079fde2001-10-13 06:41:08 +00001203 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001204
Chris Lattner394f2eb2003-10-16 20:12:13 +00001205 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001206
Chris Lattner2079fde2001-10-13 06:41:08 +00001207 // If this is an initializer for a constant pointer, which is referencing a
1208 // (currently) undefined variable, create a stub now that shall be replaced
1209 // in the future with the right type of variable.
1210 //
1211 if (V == 0) {
1212 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1213 const PointerType *PT = cast<PointerType>(Ty);
1214
1215 // First check to see if the forward references value is already created!
1216 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001217 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001218
1219 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001220 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001221 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001222 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001223 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001224 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001225
Reid Spencer3271ed52004-07-18 00:08:11 +00001226 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001227 GlobalValue *GV;
1228 if (const FunctionType *FTy =
1229 dyn_cast<FunctionType>(PT->getElementType())) {
1230 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1231 CurModule.CurrentModule);
1232 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001233 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001234 GlobalValue::ExternalLinkage, 0,
1235 Name, CurModule.CurrentModule);
1236 }
Chris Lattner8a327842004-07-14 21:44:00 +00001237
Reid Spencer3271ed52004-07-18 00:08:11 +00001238 // Keep track of the fact that we have a forward ref to recycle it
1239 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1240 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001241 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001242 }
1243
Reid Spencer3271ed52004-07-18 00:08:11 +00001244 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001245 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001246 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001247 | Types ConstExpr {
1248 if ($1->get() != $2->getType())
1249 ThrowException("Mismatched types for constant expression!");
1250 $$ = $2;
1251 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001252 }
1253 | Types ZEROINITIALIZER {
1254 $$ = Constant::getNullValue($1->get());
1255 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001256 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001257
Chris Lattnere43f40b2002-10-09 00:25:32 +00001258ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001259 if (!ConstantSInt::isValueValidForType($1, $2))
1260 ThrowException("Constant value doesn't fit in type!");
1261 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001262 }
1263 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001264 if (!ConstantUInt::isValueValidForType($1, $2))
1265 ThrowException("Constant value doesn't fit in type!");
1266 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001267 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001268 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001269 $$ = ConstantBool::True;
1270 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001271 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001272 $$ = ConstantBool::False;
1273 }
1274 | FPType FPVAL { // Float & Double constants
1275 $$ = ConstantFP::get($1, $2);
1276 };
1277
Chris Lattner00950542001-06-06 20:29:01 +00001278
Chris Lattnerd78700d2002-08-16 21:14:40 +00001279ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001280 if (!$3->getType()->isFirstClassType())
1281 ThrowException("cast constant expression from a non-primitive type: '" +
1282 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001283 if (!$5->get()->isFirstClassType())
1284 ThrowException("cast constant expression to a non-primitive type: '" +
1285 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001286 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001287 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001288 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001289 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1290 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001291 ThrowException("GetElementPtr requires a pointer operand!");
1292
Chris Lattner830b6f92004-04-05 01:30:04 +00001293 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1294 // indices to uint struct indices for compatibility.
1295 generic_gep_type_iterator<std::vector<Value*>::iterator>
1296 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1297 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1298 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1299 if (isa<StructType>(*GTI)) // Only change struct indices
1300 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1301 if (CUI->getType() == Type::UByteTy)
1302 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1303
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001304 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001305 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001306 if (!IdxTy)
1307 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001308
Chris Lattner9232b992003-04-22 18:42:41 +00001309 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001310 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1311 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001312 IdxVec.push_back(C);
1313 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001314 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001315
Chris Lattnerd78700d2002-08-16 21:14:40 +00001316 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001317
Chris Lattnerd78700d2002-08-16 21:14:40 +00001318 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001319 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001320 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1321 if ($3->getType() != Type::BoolTy)
1322 ThrowException("Select condition must be of boolean type!");
1323 if ($5->getType() != $7->getType())
1324 ThrowException("Select operand types must match!");
1325 $$ = ConstantExpr::getSelect($3, $5, $7);
1326 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001327 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001328 if ($3->getType() != $5->getType())
1329 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001330 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1331 // To retain backward compatibility with these early compilers, we emit a
1332 // cast to the appropriate integer type automatically if we are in the
1333 // broken case. See PR424 for more information.
1334 if (!isa<PointerType>($3->getType())) {
1335 $$ = ConstantExpr::get($1, $3, $5);
1336 } else {
1337 const Type *IntPtrTy;
1338 switch (CurModule.CurrentModule->getPointerSize()) {
1339 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1340 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1341 default: ThrowException("invalid pointer binary constant expr!");
1342 }
1343 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1344 ConstantExpr::getCast($5, IntPtrTy));
1345 $$ = ConstantExpr::getCast($$, $3->getType());
1346 }
1347 }
1348 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1349 if ($3->getType() != $5->getType())
1350 ThrowException("Logical operator types must match!");
1351 if (!$3->getType()->isIntegral())
1352 ThrowException("Logical operands must have integral types!");
1353 $$ = ConstantExpr::get($1, $3, $5);
1354 }
1355 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1356 if ($3->getType() != $5->getType())
1357 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001358 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001359 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001360 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001361 if ($5->getType() != Type::UByteTy)
1362 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001363 if (!$3->getType()->isInteger())
1364 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001365 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001366 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001367
1368
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001369// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001370ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001371 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001372 }
1373 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001374 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001375 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001376 };
Chris Lattner00950542001-06-06 20:29:01 +00001377
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001378
Chris Lattner1781aca2001-09-18 04:00:54 +00001379// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001380GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001381
Chris Lattner00950542001-06-06 20:29:01 +00001382
Chris Lattner0e73ce62002-05-02 19:11:13 +00001383//===----------------------------------------------------------------------===//
1384// Rules to match Modules
1385//===----------------------------------------------------------------------===//
1386
1387// Module rule: Capture the result of parsing the whole file into a result
1388// variable...
1389//
1390Module : FunctionList {
1391 $$ = ParserResult = $1;
1392 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001393};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001394
Chris Lattner7e708292002-06-25 16:13:24 +00001395// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001396//
1397FunctionList : FunctionList Function {
1398 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001399 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001400 }
1401 | FunctionList FunctionProto {
1402 $$ = $1;
1403 }
1404 | FunctionList IMPLEMENTATION {
1405 $$ = $1;
1406 }
1407 | ConstPool {
1408 $$ = CurModule.CurrentModule;
1409 // Resolve circular types before we parse the body of the module
1410 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001411 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001412
Chris Lattnere98dda62001-07-14 06:10:16 +00001413// ConstPool - Constants with optional names assigned to them.
Chris Lattnere0026942004-07-14 06:44:56 +00001414ConstPool : ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001415 // Eagerly resolve types. This is not an optimization, this is a
1416 // requirement that is due to the fact that we could have this:
1417 //
1418 // %list = type { %list * }
1419 // %list = type { %list * } ; repeated type decl
1420 //
1421 // If types are not resolved eagerly, then the two types will not be
1422 // determined to be the same type!
1423 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001424 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001425
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001426 if (!setTypeName(*$4, $2) && !$2) {
1427 // If this is a named type that is not a redefinition, add it to the slot
1428 // table.
Chris Lattner64116f92004-07-14 01:33:11 +00001429 if (inFunctionScope())
1430 CurFun.Types.push_back(*$4);
1431 else
1432 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001433 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001434
1435 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001436 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001437 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001438 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001439 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001440 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1441 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Chris Lattner1781aca2001-09-18 04:00:54 +00001442 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001443 | ConstPool OptAssign EXTERNAL GlobalType Types {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001444 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001445 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001446 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001447 | ConstPool TARGET TargetDefinition {
1448 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001449 | ConstPool DEPLIBS '=' LibrariesDefinition {
1450 }
Chris Lattner00950542001-06-06 20:29:01 +00001451 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001452 };
Chris Lattner00950542001-06-06 20:29:01 +00001453
1454
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001455
1456BigOrLittle : BIG { $$ = Module::BigEndian; };
1457BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1458
1459TargetDefinition : ENDIAN '=' BigOrLittle {
1460 CurModule.CurrentModule->setEndianness($3);
1461 }
1462 | POINTERSIZE '=' EUINT64VAL {
1463 if ($3 == 32)
1464 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1465 else if ($3 == 64)
1466 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1467 else
1468 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001469 }
1470 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001471 CurModule.CurrentModule->setTargetTriple($3);
1472 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001473 };
1474
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001475LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001476
1477LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001478 CurModule.CurrentModule->addLibrary($3);
1479 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001480 }
1481 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001482 CurModule.CurrentModule->addLibrary($1);
1483 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001484 }
1485 | /* empty: end of list */ {
1486 }
1487 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001488
Chris Lattner00950542001-06-06 20:29:01 +00001489//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001490// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001491//===----------------------------------------------------------------------===//
1492
Chris Lattner6c23f572003-08-22 05:42:10 +00001493Name : VAR_ID | STRINGCONSTANT;
1494OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001495
Chris Lattner6c23f572003-08-22 05:42:10 +00001496ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001497 if (*$1 == Type::VoidTy)
1498 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001499 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001500};
Chris Lattner00950542001-06-06 20:29:01 +00001501
Chris Lattner69da5cf2002-10-13 20:57:00 +00001502ArgListH : ArgListH ',' ArgVal {
1503 $$ = $1;
1504 $1->push_back(*$3);
1505 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001506 }
1507 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001508 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001509 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001510 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001511 };
Chris Lattner00950542001-06-06 20:29:01 +00001512
1513ArgList : ArgListH {
1514 $$ = $1;
1515 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001516 | ArgListH ',' DOTDOTDOT {
1517 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001518 $$->push_back(std::pair<PATypeHolder*,
1519 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001520 }
1521 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001522 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1523 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001524 }
Chris Lattner00950542001-06-06 20:29:01 +00001525 | /* empty */ {
1526 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001527 };
Chris Lattner00950542001-06-06 20:29:01 +00001528
Chris Lattner6c23f572003-08-22 05:42:10 +00001529FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001530 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001531 std::string FunctionName($2);
Chris Lattnera09000d2004-07-14 23:03:46 +00001532 free($2); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001533
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001534 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1535 ThrowException("LLVM functions cannot return aggregate types!");
1536
Chris Lattner9232b992003-04-22 18:42:41 +00001537 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001538 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001539 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001540 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001541 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001542 }
Chris Lattner00950542001-06-06 20:29:01 +00001543
Chris Lattner2079fde2001-10-13 06:41:08 +00001544 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1545 if (isVarArg) ParamTypeList.pop_back();
1546
Chris Lattner1f862af2003-04-16 18:13:57 +00001547 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001548 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001549 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001550
Chris Lattnera09000d2004-07-14 23:03:46 +00001551 ValID ID;
1552 if (!FunctionName.empty()) {
1553 ID = ValID::create((char*)FunctionName.c_str());
1554 } else {
1555 ID = ValID::create((int)CurModule.Values[PFT].size());
1556 }
1557
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001558 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001559 // See if this function was forward referenced. If so, recycle the object.
1560 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1561 // Move the function to the end of the list, from whereever it was
1562 // previously inserted.
1563 Fn = cast<Function>(FWRef);
1564 CurModule.CurrentModule->getFunctionList().remove(Fn);
1565 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1566 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1567 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1568 // If this is the case, either we need to be a forward decl, or it needs
1569 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001570 if (!CurFun.isDeclare && !Fn->isExternal())
1571 ThrowException("Redefinition of function '" + FunctionName + "'!");
1572
Chris Lattnera09000d2004-07-14 23:03:46 +00001573 // Make sure to strip off any argument names so we can't get conflicts.
1574 if (Fn->isExternal())
1575 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend();
1576 AI != AE; ++AI)
1577 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001578
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001579 } else { // Not already defined?
1580 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1581 CurModule.CurrentModule);
1582 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001583 }
Chris Lattner00950542001-06-06 20:29:01 +00001584
Chris Lattner394f2eb2003-10-16 20:12:13 +00001585 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001586
Chris Lattner7e708292002-06-25 16:13:24 +00001587 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001588 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001589 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001590 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001591 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001592 delete $4->back().first;
1593 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001594 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001595 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001596 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001597 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001598 delete I->first; // Delete the typeholder...
1599
Chris Lattner8ab406d2004-07-13 07:59:27 +00001600 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001601 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001602 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001603
Chris Lattner1f862af2003-04-16 18:13:57 +00001604 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001605 }
Chris Lattner51727be2002-06-04 21:58:56 +00001606};
Chris Lattner00950542001-06-06 20:29:01 +00001607
Chris Lattner9b02cc32002-05-03 18:23:48 +00001608BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1609
Chris Lattner4ad02e72003-04-16 20:28:45 +00001610FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001611 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001612
Chris Lattner4ad02e72003-04-16 20:28:45 +00001613 // Make sure that we keep track of the linkage type even if there was a
1614 // previous "declare".
1615 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001616
Chris Lattner7e708292002-06-25 16:13:24 +00001617 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001618 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001619};
Chris Lattner00950542001-06-06 20:29:01 +00001620
Chris Lattner9b02cc32002-05-03 18:23:48 +00001621END : ENDTOK | '}'; // Allow end of '}' to end a function
1622
Chris Lattner79df7c02002-03-26 18:01:55 +00001623Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001624 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001625};
Chris Lattner00950542001-06-06 20:29:01 +00001626
Chris Lattner394f2eb2003-10-16 20:12:13 +00001627FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1628 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001629 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001630};
Chris Lattner00950542001-06-06 20:29:01 +00001631
1632//===----------------------------------------------------------------------===//
1633// Rules to match Basic Blocks
1634//===----------------------------------------------------------------------===//
1635
1636ConstValueRef : ESINT64VAL { // A reference to a direct constant
1637 $$ = ValID::create($1);
1638 }
1639 | EUINT64VAL {
1640 $$ = ValID::create($1);
1641 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001642 | FPVAL { // Perhaps it's an FP constant?
1643 $$ = ValID::create($1);
1644 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001645 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001646 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001647 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001648 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001649 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001650 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001651 | NULL_TOK {
1652 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001653 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001654 | ConstExpr {
1655 $$ = ValID::create($1);
1656 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001657
Chris Lattner2079fde2001-10-13 06:41:08 +00001658// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1659// another value.
1660//
1661SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001662 $$ = ValID::create($1);
1663 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001664 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001665 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001666 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001667
1668// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001669ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001670
Chris Lattner00950542001-06-06 20:29:01 +00001671
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001672// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1673// type immediately preceeds the value reference, and allows complex constant
1674// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001675ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001676 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001677 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001678
Chris Lattner00950542001-06-06 20:29:01 +00001679BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001680 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001681 }
Chris Lattner7e708292002-06-25 16:13:24 +00001682 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001683 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001684 };
Chris Lattner00950542001-06-06 20:29:01 +00001685
1686
1687// Basic blocks are terminated by branching instructions:
1688// br, br/cc, switch, ret
1689//
Chris Lattner2079fde2001-10-13 06:41:08 +00001690BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001691 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001692 InsertValue($3);
1693
1694 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001695 InsertValue($1);
1696 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001697 };
Chris Lattner00950542001-06-06 20:29:01 +00001698
1699InstructionList : InstructionList Inst {
1700 $1->getInstList().push_back($2);
1701 $$ = $1;
1702 }
1703 | /* empty */ {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001704 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001705
1706 // Make sure to move the basic block to the correct location in the
1707 // function, instead of leaving it inserted wherever it was first
1708 // referenced.
1709 CurFun.CurrentFunction->getBasicBlockList().remove(CurBB);
1710 CurFun.CurrentFunction->getBasicBlockList().push_back(CurBB);
Chris Lattner22ae2322004-07-13 08:39:15 +00001711 }
1712 | LABELSTR {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001713 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001714
1715 // Make sure to move the basic block to the correct location in the
1716 // function, instead of leaving it inserted wherever it was first
1717 // referenced.
1718 CurFun.CurrentFunction->getBasicBlockList().remove(CurBB);
1719 CurFun.CurrentFunction->getBasicBlockList().push_back(CurBB);
Chris Lattner51727be2002-06-04 21:58:56 +00001720 };
Chris Lattner00950542001-06-06 20:29:01 +00001721
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001722BBTerminatorInst : RET ResolvedVal { // Return with a result...
1723 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001724 }
1725 | RET VOID { // Return with no result...
1726 $$ = new ReturnInst();
1727 }
1728 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001729 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001730 } // Conditional Branch...
1731 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001732 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001733 }
1734 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001735 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner00950542001-06-06 20:29:01 +00001736 $$ = S;
1737
Chris Lattner9232b992003-04-22 18:42:41 +00001738 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001739 E = $8->end();
1740 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001741 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001742 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001743 }
Chris Lattner196850c2003-05-15 21:30:00 +00001744 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001745 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner196850c2003-05-15 21:30:00 +00001746 $$ = S;
1747 }
Chris Lattner64116f92004-07-14 01:33:11 +00001748 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1749 UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001750 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001751 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001752
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001753 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1754 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001755 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001756 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001757 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001758 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1759 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001760 ParamTypes.push_back((*I)->getType());
1761 }
1762
1763 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1764 if (isVarArg) ParamTypes.pop_back();
1765
Chris Lattner79df7c02002-03-26 18:01:55 +00001766 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001767 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001768 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001769
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001770 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001771
Chris Lattner64116f92004-07-14 01:33:11 +00001772 BasicBlock *Normal = getBBVal($9);
1773 BasicBlock *Except = getBBVal($12);
Chris Lattner2079fde2001-10-13 06:41:08 +00001774
1775 // Create the call node...
1776 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001777 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001778 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001779 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001780 // correctly!
1781 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001782 FunctionType::param_iterator I = Ty->param_begin();
1783 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001784 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001785
1786 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001787 if ((*ArgI)->getType() != *I)
1788 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1789 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001790
1791 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001792 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001793
Chris Lattner6cdb0112001-11-26 16:54:11 +00001794 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001795 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001796 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001797 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001798 }
1799 | UNWIND {
1800 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001801 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001802
1803
Chris Lattner00950542001-06-06 20:29:01 +00001804
1805JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1806 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001807 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001808 if (V == 0)
1809 ThrowException("May only switch on a constant pool value!");
1810
Chris Lattner64116f92004-07-14 01:33:11 +00001811 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001812 }
1813 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001814 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001815 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001816
1817 if (V == 0)
1818 ThrowException("May only switch on a constant pool value!");
1819
Chris Lattner64116f92004-07-14 01:33:11 +00001820 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001821 };
Chris Lattner00950542001-06-06 20:29:01 +00001822
1823Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001824 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001825 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001826 InsertValue($2);
1827 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001828};
Chris Lattner00950542001-06-06 20:29:01 +00001829
Chris Lattnerc24d2082001-06-11 15:04:20 +00001830PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001831 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001832 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001833 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001834 }
1835 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1836 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001837 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001838 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001839 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001840
1841
Chris Lattner30c89792001-09-07 16:35:17 +00001842ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001843 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001844 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001845 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001846 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001847 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001848 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001849 };
Chris Lattner00950542001-06-06 20:29:01 +00001850
1851// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001852ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001853
Chris Lattner4a6482b2002-09-10 19:57:26 +00001854InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1855 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1856 ThrowException("Arithmetic operator requires integer or FP operands!");
1857 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1858 if ($$ == 0)
1859 ThrowException("binary operator returned null!");
1860 delete $2;
1861 }
1862 | LogicalOps Types ValueRef ',' ValueRef {
1863 if (!(*$2)->isIntegral())
1864 ThrowException("Logical operator requires integral operands!");
1865 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1866 if ($$ == 0)
1867 ThrowException("binary operator returned null!");
1868 delete $2;
1869 }
1870 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001871 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001872 if ($$ == 0)
1873 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001874 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001875 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001876 | NOT ResolvedVal {
1877 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1878 << " Replacing with 'xor'.\n";
1879
1880 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1881 if (Ones == 0)
1882 ThrowException("Expected integral type for not instruction!");
1883
1884 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001885 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001886 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001887 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001888 | ShiftOps ResolvedVal ',' ResolvedVal {
1889 if ($4->getType() != Type::UByteTy)
1890 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001891 if (!$2->getType()->isInteger())
1892 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001893 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001894 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001895 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001896 if (!$4->get()->isFirstClassType())
1897 ThrowException("cast instruction to a non-primitive type: '" +
1898 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001899 $$ = new CastInst($2, *$4);
1900 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001901 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001902 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1903 if ($2->getType() != Type::BoolTy)
1904 ThrowException("select condition must be boolean!");
1905 if ($4->getType() != $6->getType())
1906 ThrowException("select value types should match!");
1907 $$ = new SelectInst($2, $4, $6);
1908 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001909 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001910 // FIXME: This is emulation code for an obsolete syntax. This should be
1911 // removed at some point.
1912 if (!ObsoleteVarArgs) {
1913 std::cerr << "WARNING: this file uses obsolete features. "
1914 << "Assemble and disassemble to update it.\n";
1915 ObsoleteVarArgs = true;
1916 }
1917
1918 // First, load the valist...
1919 Instruction *CurVAList = new LoadInst($2, "");
1920 CurBB->getInstList().push_back(CurVAList);
1921
1922 // Emit the vaarg instruction.
1923 $$ = new VAArgInst(CurVAList, *$4);
1924
1925 // Now we must advance the pointer and update it in memory.
1926 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1927 CurBB->getInstList().push_back(TheVANext);
1928
1929 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1930 delete $4;
1931 }
1932 | VAARG ResolvedVal ',' Types {
1933 $$ = new VAArgInst($2, *$4);
1934 delete $4;
1935 }
1936 | VANEXT ResolvedVal ',' Types {
1937 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001938 delete $4;
1939 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001940 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001941 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001942 if (!Ty->isFirstClassType())
1943 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001944 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001945 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001946 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001947 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00001948 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001949 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001950 $2->pop_front();
1951 }
1952 delete $2; // Free the list...
1953 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001954 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001955 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001956 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001957
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001958 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1959 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001960 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001961 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001962 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001963 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1964 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001965 ParamTypes.push_back((*I)->getType());
1966 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001967
1968 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1969 if (isVarArg) ParamTypes.pop_back();
1970
Chris Lattner79df7c02002-03-26 18:01:55 +00001971 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001972 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001973 }
Chris Lattner00950542001-06-06 20:29:01 +00001974
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001975 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001976
Chris Lattner8b81bf52001-07-25 22:47:46 +00001977 // Create the call node...
1978 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001979 // Make sure no arguments is a good thing!
1980 if (Ty->getNumParams() != 0)
1981 ThrowException("No arguments passed to a function that "
1982 "expects arguments!");
1983
Chris Lattner9232b992003-04-22 18:42:41 +00001984 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001985 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001986 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001987 // correctly!
1988 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001989 FunctionType::param_iterator I = Ty->param_begin();
1990 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001991 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001992
1993 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001994 if ((*ArgI)->getType() != *I)
1995 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1996 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001997
Chris Lattner8b81bf52001-07-25 22:47:46 +00001998 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001999 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002000
Chris Lattner6cdb0112001-11-26 16:54:11 +00002001 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002002 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00002003 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002004 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00002005 }
2006 | MemoryInst {
2007 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002008 };
Chris Lattner00950542001-06-06 20:29:01 +00002009
Chris Lattner6cdb0112001-11-26 16:54:11 +00002010
2011// IndexList - List of indices for GEP based instructions...
2012IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002013 $$ = $2;
2014 } | /* empty */ {
2015 $$ = new std::vector<Value*>();
2016 };
2017
2018OptVolatile : VOLATILE {
2019 $$ = true;
2020 }
2021 | /* empty */ {
2022 $$ = false;
2023 };
2024
Chris Lattner027dcc52001-07-08 21:10:27 +00002025
Chris Lattner00950542001-06-06 20:29:01 +00002026MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002027 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002028 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002029 }
2030 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002031 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002032 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002033 }
2034 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002035 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002036 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002037 }
2038 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002039 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002040 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002041 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002042 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002043 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002044 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002045 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002046 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002047 }
2048
Chris Lattner15c9c032003-09-08 18:20:29 +00002049 | OptVolatile LOAD Types ValueRef {
2050 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002051 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002052 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002053 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002054 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002055 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002056 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2057 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002058 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002059 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002060 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002061 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002062 if (ElTy != $3->getType())
2063 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002064 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002065
Chris Lattner79ad13802003-09-08 20:29:46 +00002066 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002067 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002068 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002069 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002070 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002071 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002072
2073 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2074 // indices to uint struct indices for compatibility.
2075 generic_gep_type_iterator<std::vector<Value*>::iterator>
2076 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2077 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2078 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2079 if (isa<StructType>(*GTI)) // Only change struct indices
2080 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2081 if (CUI->getType() == Type::UByteTy)
2082 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2083
Chris Lattner30c89792001-09-07 16:35:17 +00002084 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002085 ThrowException("Invalid getelementptr indices for type '" +
2086 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002087 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2088 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002089 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002090
Brian Gaeked0fde302003-11-11 22:41:34 +00002091
Chris Lattner00950542001-06-06 20:29:01 +00002092%%
Chris Lattner09083092001-07-08 04:57:15 +00002093int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002094 std::string where
2095 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002096 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002097 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002098 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002099 errMsg += "end-of-file.";
2100 else
Chris Lattner9232b992003-04-22 18:42:41 +00002101 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002102 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002103 return 0;
2104}