blob: b2ac21eed9be65d3ae9a634389a3900211acf0d1 [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"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000020#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/STLExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000024#include <algorithm>
25#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000026#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000027#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000028
Chris Lattner386a3b72001-10-16 19:54:17 +000029int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000030int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000031int yyparse();
32
Brian Gaeked0fde302003-11-11 22:41:34 +000033namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000034 std::string CurFilename;
35}
36using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner00950542001-06-06 20:29:01 +000038static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000039
Chris Lattner30c89792001-09-07 16:35:17 +000040// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
41// relating to upreferences in the input stream.
42//
43//#define DEBUG_UPREFS 1
44#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000045#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000046#else
47#define UR_OUT(X)
48#endif
49
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000050#define YYERROR_VERBOSE 1
51
Chris Lattner99e7ab72003-10-18 05:53:13 +000052// HACK ALERT: This variable is used to implement the automatic conversion of
53// variable argument instructions from their old to new forms. When this
54// compatiblity "Feature" is removed, this should be too.
55//
56static BasicBlock *CurBB;
57static bool ObsoleteVarArgs;
58
59
Chris Lattner7e708292002-06-25 16:13:24 +000060// This contains info used when building the body of a function. It is
61// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000062//
Chris Lattner9232b992003-04-22 18:42:41 +000063typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner735f2702004-07-08 22:30:50 +000064static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
65 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000066
67static struct PerModuleInfo {
68 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000069 std::map<const Type *, ValueList> Values; // Module level numbered definitions
70 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000071 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000072 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000073
Chris Lattnerbc721ed2004-07-13 08:28:21 +000074 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
75 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000076 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000077 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
78
Chris Lattner2079fde2001-10-13 06:41:08 +000079 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
80 // references to global values. Global values may be referenced before they
81 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000082 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000083 //
Chris Lattner9232b992003-04-22 18:42:41 +000084 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000085 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000086 GlobalRefsType GlobalRefs;
87
Chris Lattner00950542001-06-06 20:29:01 +000088 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000089 // If we could not resolve some functions at function compilation time
90 // (calls to functions before they are defined), resolve them now... Types
91 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000092 //
Chris Lattner00950542001-06-06 20:29:01 +000093 ResolveDefinitions(LateResolveValues);
94
Chris Lattner2079fde2001-10-13 06:41:08 +000095 // Check to make sure that all global value forward references have been
96 // resolved!
97 //
98 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000099 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +0000100
101 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
102 I != E; ++I) {
103 UndefinedReferences += " " + I->first.first->getDescription() + " " +
104 I->first.second.getName() + "\n";
105 }
106 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000107 }
108
Chris Lattner7e708292002-06-25 16:13:24 +0000109 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000110 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000111 CurrentModule = 0;
112 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000113
114
Chris Lattner8a327842004-07-14 21:44:00 +0000115 // GetForwardRefForGlobal - Check to see if there is a forward reference
116 // for this global. If so, remove it from the GlobalRefs map and return it.
117 // If not, just return null.
118 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
119 // Check to see if there is a forward reference to this global variable...
120 // if there is, eliminate it and patch the reference to use the new def'n.
121 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
122 GlobalValue *Ret = 0;
123 if (I != GlobalRefs.end()) {
124 Ret = I->second;
125 GlobalRefs.erase(I);
126 }
127 return Ret;
128 }
129
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000130 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000131 // is used to remove things from the forward declaration map, resolving them
132 // to the correct thing as needed.
133 //
134 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
135 // Check to see if there is a forward reference to this global variable...
136 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000137 GlobalRefsType::iterator I =
138 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000139
140 if (I != GlobalRefs.end()) {
Chris Lattnerbc207592004-03-08 06:09:57 +0000141 GlobalValue *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000142 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner38ab6bf2004-07-13 06:58:07 +0000143
144 // Replace all uses of the placeholder with the new GV
145 OldGV->replaceAllUsesWith(GV);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000146
147 // Remove OldGV from the module...
Chris Lattnerbc207592004-03-08 06:09:57 +0000148 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
149 CurrentModule->getGlobalList().erase(GVar);
150 else
151 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000152
Chris Lattner2079fde2001-10-13 06:41:08 +0000153 // Remove the map entry for the global now that it has been created...
154 GlobalRefs.erase(I);
155 }
156 }
157
Chris Lattner00950542001-06-06 20:29:01 +0000158} CurModule;
159
Chris Lattner79df7c02002-03-26 18:01:55 +0000160static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000161 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000162
Chris Lattner735f2702004-07-08 22:30:50 +0000163 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
164 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000165 std::vector<PATypeHolder> Types;
166 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000167 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000168
Chris Lattner2e2ed292004-07-14 06:28:35 +0000169 /// BBForwardRefs - When we see forward references to basic blocks, keep
170 /// track of them here.
171 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
172 std::vector<BasicBlock*> NumberedBlocks;
173 unsigned NextBBNum;
174
Chris Lattner79df7c02002-03-26 18:01:55 +0000175 inline PerFunctionInfo() {
176 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000177 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000178 }
179
Chris Lattner79df7c02002-03-26 18:01:55 +0000180 inline void FunctionStart(Function *M) {
181 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000182 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000183 }
184
Chris Lattner79df7c02002-03-26 18:01:55 +0000185 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000186 NumberedBlocks.clear();
187
188 // Any forward referenced blocks left?
189 if (!BBForwardRefs.empty())
190 ThrowException("Undefined reference to label " +
191 BBForwardRefs.begin()->second.first.getName());
192
193 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000194 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000195
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000196 // Make sure to resolve any constant expr references that might exist within
197 // the function we just declared itself.
198 ValID FID;
199 if (CurrentFunction->hasName()) {
200 FID = ValID::create((char*)CurrentFunction->getName().c_str());
201 } else {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000202 // Figure out which slot number if is...
Chris Lattner735f2702004-07-08 22:30:50 +0000203 ValueList &List = CurModule.Values[CurrentFunction->getType()];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000204 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000205 assert(i < List.size() && "Function not found!");
206 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000207 FID = ValID::create((int)i);
208 break;
209 }
210 }
211 }
212 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
213
Chris Lattner7e708292002-06-25 16:13:24 +0000214 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000215 Types.clear(); // Clear out function local types
Chris Lattner79df7c02002-03-26 18:01:55 +0000216 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000217 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000218 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000219} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000220
Chris Lattner394f2eb2003-10-16 20:12:13 +0000221static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000222
Chris Lattner00950542001-06-06 20:29:01 +0000223
224//===----------------------------------------------------------------------===//
225// Code to handle definitions of all the types
226//===----------------------------------------------------------------------===//
227
Chris Lattner735f2702004-07-08 22:30:50 +0000228static int InsertValue(Value *V,
229 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
230 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000231
232 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000233 ValueList &List = ValueTab[V->getType()];
234 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000235 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000236}
237
Chris Lattner30c89792001-09-07 16:35:17 +0000238static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000239 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000240 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000241 unsigned Num = (unsigned)D.Num;
242
243 // Module constants occupy the lowest numbered slots...
244 if (Num < CurModule.Types.size())
245 return CurModule.Types[Num];
246
247 Num -= CurModule.Types.size();
248
249 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000250 if (Num <= CurFun.Types.size())
251 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000252 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000253 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000254 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000255 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000256 SymbolTable *SymTab = 0;
Reid Spencer8ce1da72004-07-04 12:19:05 +0000257 Type *N = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000258 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000259 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000260 N = SymTab->lookupType(Name);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000261 }
Chris Lattner30c89792001-09-07 16:35:17 +0000262
263 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000264 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000265 // hasn't been added to the module...
266 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000267 SymTab = &CurModule.CurrentModule->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000268 N = SymTab->lookupType(Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000269 if (N == 0) break;
270 }
271
272 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000273 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000274 }
275 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000276 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000277 }
278
279 // If we reached here, we referenced either a symbol that we don't know about
280 // or an id number that hasn't been read yet. We may be referencing something
281 // forward, so just create an entry to be resolved later and get to it...
282 //
283 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
284
Chris Lattner9232b992003-04-22 18:42:41 +0000285 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000286 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000287
Chris Lattner9232b992003-04-22 18:42:41 +0000288 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000289 if (I != LateResolver.end()) {
290 return I->second;
291 }
Chris Lattner30c89792001-09-07 16:35:17 +0000292
Chris Lattner82269592001-10-22 06:01:08 +0000293 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000294 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000295 return Typ;
296}
297
Chris Lattner9232b992003-04-22 18:42:41 +0000298static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000299 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000300 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000301 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000302 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000303}
304
Chris Lattner2079fde2001-10-13 06:41:08 +0000305// getValNonImprovising - Look up the value specified by the provided type and
306// the provided ValID. If the value exists and has already been defined, return
307// it. Otherwise return null.
308//
309static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000310 if (isa<FunctionType>(Ty))
311 ThrowException("Functions are not values and "
312 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000313
Chris Lattner30c89792001-09-07 16:35:17 +0000314 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000315 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000316 unsigned Num = (unsigned)D.Num;
317
318 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000319 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000320 if (VI != CurModule.Values.end()) {
321 if (Num < VI->second.size())
322 return VI->second[Num];
323 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000324 }
325
326 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000327 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000328 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000329
330 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000331 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000332
Chris Lattner16af11d2004-02-09 21:03:38 +0000333 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000334 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000335
Chris Lattner1a1cb112001-09-30 22:46:54 +0000336 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000337 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000338 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000339
340 D.destroy(); // Free old strdup'd memory...
341 return N;
342 }
343
Chris Lattner2079fde2001-10-13 06:41:08 +0000344 // Check to make sure that "Ty" is an integral type, and that our
345 // value will fit into the specified type...
346 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000347 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
348 ThrowException("Signed integral constant '" +
349 itostr(D.ConstPool64) + "' is invalid for type '" +
350 Ty->getDescription() + "'!");
351 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000352
353 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000354 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
355 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000356 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
357 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000358 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000359 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000360 }
361 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000362 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000363 }
364
Chris Lattner2079fde2001-10-13 06:41:08 +0000365 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000366 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000367 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000368 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000369
370 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000371 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000372 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000373 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000374
Chris Lattnerd78700d2002-08-16 21:14:40 +0000375 case ValID::ConstantVal: // Fully resolved constant?
376 if (D.ConstantValue->getType() != Ty)
377 ThrowException("Constant expression type different from required type!");
378 return D.ConstantValue;
379
Chris Lattner30c89792001-09-07 16:35:17 +0000380 default:
381 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000382 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000383 } // End of switch
384
Chris Lattner2079fde2001-10-13 06:41:08 +0000385 assert(0 && "Unhandled case!");
386 return 0;
387}
388
Chris Lattner2079fde2001-10-13 06:41:08 +0000389// getVal - This function is identical to getValNonImprovising, except that if a
390// value is not already defined, it "improvises" by creating a placeholder var
391// that looks and acts just like the requested variable. When the value is
392// defined later, all uses of the placeholder variable are replaced with the
393// real thing.
394//
Chris Lattner64116f92004-07-14 01:33:11 +0000395static Value *getVal(const Type *Ty, const ValID &ID) {
396 if (Ty == Type::LabelTy)
397 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000398
Chris Lattner64116f92004-07-14 01:33:11 +0000399 // See if the value has already been defined.
400 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000401 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000402
403 // If we reached here, we referenced either a symbol that we don't know about
404 // or an id number that hasn't been read yet. We may be referencing something
405 // forward, so just create an entry to be resolved later and get to it...
406 //
Chris Lattner64116f92004-07-14 01:33:11 +0000407 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000408
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000409 // Remember where this forward reference came from. FIXME, shouldn't we try
410 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000411 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000412 llvmAsmlineno)));
413
Chris Lattner79df7c02002-03-26 18:01:55 +0000414 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000415 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000416 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000417 InsertValue(V, CurModule.LateResolveValues);
418 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000419}
420
Chris Lattner2e2ed292004-07-14 06:28:35 +0000421/// getBBVal - This is used for two purposes:
422/// * If isDefinition is true, a new basic block with the specified ID is being
423/// defined.
424/// * If isDefinition is true, this is a reference to a basic block, which may
425/// or may not be a forward reference.
426///
427static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000428 assert(inFunctionScope() && "Can't get basic block at global scope!");
429
Chris Lattner2e2ed292004-07-14 06:28:35 +0000430 std::string Name;
431 BasicBlock *BB = 0;
432 switch (ID.Type) {
433 default: ThrowException("Illegal label reference " + ID.getName());
434 case ValID::NumberVal: // Is it a numbered definition?
435 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
436 CurFun.NumberedBlocks.resize(ID.Num+1);
437 BB = CurFun.NumberedBlocks[ID.Num];
438 break;
439 case ValID::NameVal: // Is it a named definition?
440 Name = ID.Name;
441 if (Value *N = lookupInSymbolTable(Type::LabelTy, Name))
442 BB = cast<BasicBlock>(N);
443 break;
444 }
Chris Lattner64116f92004-07-14 01:33:11 +0000445
Chris Lattner2e2ed292004-07-14 06:28:35 +0000446 // See if the block has already been defined.
447 if (BB) {
448 // If this is the definition of the block, make sure the existing value was
449 // just a forward reference. If it was a forward reference, there will be
450 // an entry for it in the PlaceHolderInfo map.
451 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
452 // The existing value was a definition, not a forward reference.
453 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000454
Chris Lattner2e2ed292004-07-14 06:28:35 +0000455 ID.destroy(); // Free strdup'd memory.
456 return BB;
457 }
458
459 // Otherwise this block has not been seen before.
460 BB = new BasicBlock("", CurFun.CurrentFunction);
461 if (ID.Type == ValID::NameVal) {
462 BB->setName(ID.Name);
463 } else {
464 CurFun.NumberedBlocks[ID.Num] = BB;
465 }
466
467 // If this is not a definition, keep track of it so we can use it as a forward
468 // reference.
469 if (!isDefinition) {
470 // Remember where this forward reference came from.
471 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
472 } else {
473 // The forward declaration could have been inserted anywhere in the
474 // function: insert it into the correct place now.
475 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
476 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
477 }
478
Chris Lattner64116f92004-07-14 01:33:11 +0000479 return BB;
480}
481
Chris Lattner00950542001-06-06 20:29:01 +0000482
483//===----------------------------------------------------------------------===//
484// Code to handle forward references in instructions
485//===----------------------------------------------------------------------===//
486//
487// This code handles the late binding needed with statements that reference
488// values not defined yet... for example, a forward branch, or the PHI node for
489// a loop body.
490//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000491// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000492// and back patchs after we are done.
493//
494
495// ResolveDefinitions - If we could not resolve some defs at parsing
496// time (forward branches, phi functions for loops, etc...) resolve the
497// defs now...
498//
Chris Lattner735f2702004-07-08 22:30:50 +0000499static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
500 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000501 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000502 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000503 E = LateResolvers.end(); LRI != E; ++LRI) {
504 ValueList &List = LRI->second;
505 while (!List.empty()) {
506 Value *V = List.back();
507 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000508
509 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
510 CurModule.PlaceHolderInfo.find(V);
511 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
512
513 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000514
Chris Lattner735f2702004-07-08 22:30:50 +0000515 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000516 if (TheRealValue) {
517 V->replaceAllUsesWith(TheRealValue);
518 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000519 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000520 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000521 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000522 // resolver table
523 InsertValue(V, *FutureLateResolvers);
524 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000525 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000526 ThrowException("Reference to an invalid definition: '" +DID.getName()+
527 "' of type '" + V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000528 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000529 else
530 ThrowException("Reference to an invalid definition: #" +
531 itostr(DID.Num) + " of type '" +
532 V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000533 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000534 }
Chris Lattner00950542001-06-06 20:29:01 +0000535 }
536 }
537
538 LateResolvers.clear();
539}
540
Chris Lattner4a42e902001-10-22 05:56:09 +0000541// ResolveTypeTo - A brand new type was just declared. This means that (if
542// name is not null) things referencing Name can be resolved. Otherwise, things
543// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000544//
Chris Lattner4a42e902001-10-22 05:56:09 +0000545static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000546 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000547 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000548
Chris Lattner4a42e902001-10-22 05:56:09 +0000549 ValID D;
550 if (Name) D = ValID::create(Name);
551 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000552
Chris Lattner9232b992003-04-22 18:42:41 +0000553 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000554 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000555
Chris Lattner9232b992003-04-22 18:42:41 +0000556 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000557 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000558 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000559 LateResolver.erase(I);
560 }
561}
562
563// ResolveTypes - At this point, all types should be resolved. Any that aren't
564// are errors.
565//
Chris Lattner9232b992003-04-22 18:42:41 +0000566static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000567 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000568 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000569
570 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000571 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000572 else
Chris Lattner82269592001-10-22 06:01:08 +0000573 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000574 }
575}
576
Chris Lattner1781aca2001-09-18 04:00:54 +0000577// setValueName - Set the specified value to the name given. The name may be
578// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000579// assumed to be a malloc'd string buffer, and is free'd by this function.
580//
581static void setValueName(Value *V, char *NameStr) {
582 if (NameStr) {
583 std::string Name(NameStr); // Copy string
584 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000585
586 if (V->getType() == Type::VoidTy)
587 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000588
589 assert(inFunctionScope() && "Must be in function scope!");
590 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
591 if (ST.lookup(V->getType(), Name))
592 ThrowException("Redefinition of value named '" + Name + "' in the '" +
593 V->getType()->getDescription() + "' type plane!");
594
Chris Lattnerc9aea522004-07-13 08:12:39 +0000595 // Set the name.
596 V->setName(Name, &ST);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000597 }
598}
599
Chris Lattnerd88998d2004-07-14 08:23:52 +0000600/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
601/// this is a declaration, otherwise it is a definition.
602static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
603 bool isConstantGlobal, const Type *Ty,
604 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000605 if (isa<FunctionType>(Ty))
606 ThrowException("Cannot declare global vars of function type!");
607
608 // If this global has a name, check to see if there is already a definition
609 // of this global in the module. If so, merge as appropriate. Note that
610 // this is really just a hack around problems in the CFE. :(
611 std::string Name;
612 if (NameStr) {
613 Name = NameStr; // Copy string
614 free(NameStr); // Free old string
615
616 SymbolTable &ST = CurModule.CurrentModule->getSymbolTable();
617 if (Value *Existing = ST.lookup(Ty, Name)) {
618 // We are a simple redefinition of a value, check to see if it is defined
619 // the same as the old one...
620 GlobalVariable *EGV = cast<GlobalVariable>(Existing);
621
622 // We are allowed to redefine a global variable in two circumstances:
623 // 1. If at least one of the globals is uninitialized or
624 // 2. If both initializers have the same value.
625 //
626 if (!EGV->hasInitializer() || !Initializer ||
627 EGV->getInitializer() == Initializer) {
628
629 // Make sure the existing global version gets the initializer! Make
630 // sure that it also gets marked const if the new version is.
631 if (Initializer && !EGV->hasInitializer())
632 EGV->setInitializer(Initializer);
633 if (isConstantGlobal)
634 EGV->setConstant(true);
635 EGV->setLinkage(Linkage);
636 return;
637 }
638
639 ThrowException("Redefinition of value named '" + Name + "' in the '" +
640 Ty->getDescription() + "' type plane!");
Chris Lattnerd88998d2004-07-14 08:23:52 +0000641 }
642 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000643
Chris Lattner8a327842004-07-14 21:44:00 +0000644 const PointerType *PTy = PointerType::get(Ty);
645
646 // See if this global value was forward referenced. If so, recycle the
647 // object.
648 ValID ID;
649 if (!Name.empty()) {
650 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000651 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000652 ID = ValID::create((int)CurModule.Values[PTy].size());
653 }
654
655 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
656 // Move the global to the end of the list, from whereever it was
657 // previously inserted.
658 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
659 CurModule.CurrentModule->getGlobalList().remove(GV);
660 CurModule.CurrentModule->getGlobalList().push_back(GV);
661 GV->setInitializer(Initializer);
662 GV->setLinkage(Linkage);
663 GV->setConstant(isConstantGlobal);
664 } else {
665 // Otherwise there is no existing GV to use, create one now.
666 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
667 CurModule.CurrentModule);
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000668 }
Chris Lattnerd88998d2004-07-14 08:23:52 +0000669}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000670
Reid Spencer75719bf2004-05-26 21:48:31 +0000671// setTypeName - Set the specified type to the name given. The name may be
672// null potentially, in which case this is a noop. The string passed in is
673// assumed to be a malloc'd string buffer, and is freed by this function.
674//
675// This function returns true if the type has already been defined, but is
676// allowed to be redefined in the specified context. If the name is a new name
677// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000678static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000679 if (NameStr == 0) return false;
680
681 std::string Name(NameStr); // Copy string
682 free(NameStr); // Free old string
683
684 // We don't allow assigning names to void type
685 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000686 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000687
688 SymbolTable &ST = inFunctionScope() ?
689 CurFun.CurrentFunction->getSymbolTable() :
690 CurModule.CurrentModule->getSymbolTable();
691
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000692 // Inserting a name that is already defined???
693 if (Type *Existing = ST.lookupType(Name)) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000694 // There is only one case where this is allowed: when we are refining an
695 // opaque type. In this case, Existing will be an opaque type.
696 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
697 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000698 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000699 return true;
700 }
701
702 // Otherwise, this is an attempt to redefine a type. That's okay if
703 // the redefinition is identical to the original. This will be so if
704 // Existing and T point to the same Type object. In this one case we
705 // allow the equivalent redefinition.
706 if (Existing == T) return true; // Yes, it's equal.
707
708 // Any other kind of (non-equivalent) redefinition is an error.
709 ThrowException("Redefinition of type named '" + Name + "' in the '" +
710 T->getDescription() + "' type plane!");
711 }
712
713 // Okay, its a newly named type. Set its name.
Chris Lattner8ca2dc02004-07-09 16:43:55 +0000714 if (!Name.empty()) ST.insert(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000715
Reid Spencer75719bf2004-05-26 21:48:31 +0000716 return false;
717}
Chris Lattner8896eda2001-07-09 19:38:36 +0000718
Chris Lattner30c89792001-09-07 16:35:17 +0000719//===----------------------------------------------------------------------===//
720// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000721//
Chris Lattner8896eda2001-07-09 19:38:36 +0000722
Chris Lattner3b073862004-02-09 03:19:29 +0000723// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000724//
725static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000726 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
727}
728
729namespace {
730 struct UpRefRecord {
731 // NestingLevel - The number of nesting levels that need to be popped before
732 // this type is resolved.
733 unsigned NestingLevel;
734
735 // LastContainedTy - This is the type at the current binding level for the
736 // type. Every time we reduce the nesting level, this gets updated.
737 const Type *LastContainedTy;
738
739 // UpRefTy - This is the actual opaque type that the upreference is
740 // represented with.
741 OpaqueType *UpRefTy;
742
743 UpRefRecord(unsigned NL, OpaqueType *URTy)
744 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
745 };
Chris Lattner30c89792001-09-07 16:35:17 +0000746}
Chris Lattner698b56e2001-07-20 19:15:08 +0000747
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000748// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000749static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000750
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000751/// HandleUpRefs - Every time we finish a new layer of types, this function is
752/// called. It loops through the UpRefs vector, which is a list of the
753/// currently active types. For each type, if the up reference is contained in
754/// the newly completed type, we decrement the level count. When the level
755/// count reaches zero, the upreferenced type is the type that is passed in:
756/// thus we can complete the cycle.
757///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000758static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000759 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000760 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000761 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000762 "' newly formed. Resolving upreferences.\n" <<
763 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000764
765 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
766 // to zero), we resolve them all together before we resolve them to Ty. At
767 // the end of the loop, if there is anything to resolve to Ty, it will be in
768 // this variable.
769 OpaqueType *TypeToResolve = 0;
770
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000771 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000772 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000773 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000774 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000775 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
776 // Decrement level of upreference
777 unsigned Level = --UpRefs[i].NestingLevel;
778 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000779 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000780 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000781 if (!TypeToResolve) {
782 TypeToResolve = UpRefs[i].UpRefTy;
783 } else {
784 UR_OUT(" * Resolving upreference for "
785 << UpRefs[i].second->getDescription() << "\n";
786 std::string OldName = UpRefs[i].UpRefTy->getDescription());
787 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
788 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
789 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
790 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000791 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
792 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000793 }
794 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000795 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000796
Chris Lattner026a8ce2004-02-09 18:53:54 +0000797 if (TypeToResolve) {
798 UR_OUT(" * Resolving upreference for "
799 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000800 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000801 TypeToResolve->refineAbstractTypeTo(Ty);
802 }
803
Chris Lattner8896eda2001-07-09 19:38:36 +0000804 return Ty;
805}
806
Chris Lattner30c89792001-09-07 16:35:17 +0000807
Chris Lattner00950542001-06-06 20:29:01 +0000808//===----------------------------------------------------------------------===//
809// RunVMAsmParser - Define an interface to this parser
810//===----------------------------------------------------------------------===//
811//
Chris Lattner86427632004-07-14 06:39:48 +0000812Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000813 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000814 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000815 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000816 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000817
Chris Lattner75f20532003-04-22 18:02:52 +0000818 // Allocate a new module to read
819 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000820
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000821 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000822
Chris Lattner00950542001-06-06 20:29:01 +0000823 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000824
825 // Check to see if they called va_start but not va_arg..
826 if (!ObsoleteVarArgs)
827 if (Function *F = Result->getNamedFunction("llvm.va_start"))
828 if (F->asize() == 1) {
829 std::cerr << "WARNING: this file uses obsolete features. "
830 << "Assemble and disassemble to update it.\n";
831 ObsoleteVarArgs = true;
832 }
833
Chris Lattner99e7ab72003-10-18 05:53:13 +0000834 if (ObsoleteVarArgs) {
835 // If the user is making use of obsolete varargs intrinsics, adjust them for
836 // the user.
837 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
838 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
839
840 const Type *RetTy = F->getFunctionType()->getParamType(0);
841 RetTy = cast<PointerType>(RetTy)->getElementType();
842 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
843
844 while (!F->use_empty()) {
845 CallInst *CI = cast<CallInst>(F->use_back());
846 Value *V = new CallInst(NF, "", CI);
847 new StoreInst(V, CI->getOperand(1), CI);
848 CI->getParent()->getInstList().erase(CI);
849 }
850 Result->getFunctionList().erase(F);
851 }
852
853 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
854 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
855 const Type *ArgTy = F->getFunctionType()->getParamType(0);
856 ArgTy = cast<PointerType>(ArgTy)->getElementType();
857 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
858 ArgTy, 0);
859
860 while (!F->use_empty()) {
861 CallInst *CI = cast<CallInst>(F->use_back());
862 Value *V = new LoadInst(CI->getOperand(1), "", CI);
863 new CallInst(NF, V, "", CI);
864 CI->getParent()->getInstList().erase(CI);
865 }
866 Result->getFunctionList().erase(F);
867 }
868
869 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
870 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
871 const Type *ArgTy = F->getFunctionType()->getParamType(0);
872 ArgTy = cast<PointerType>(ArgTy)->getElementType();
873 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
874 ArgTy, 0);
875
876 while (!F->use_empty()) {
877 CallInst *CI = cast<CallInst>(F->use_back());
878 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
879 new StoreInst(V, CI->getOperand(1), CI);
880 CI->getParent()->getInstList().erase(CI);
881 }
882 Result->getFunctionList().erase(F);
883 }
884 }
885
Chris Lattner00950542001-06-06 20:29:01 +0000886 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
887 ParserResult = 0;
888
889 return Result;
890}
891
892%}
893
894%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000895 llvm::Module *ModuleVal;
896 llvm::Function *FunctionVal;
897 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
898 llvm::BasicBlock *BasicBlockVal;
899 llvm::TerminatorInst *TermInstVal;
900 llvm::Instruction *InstVal;
901 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000902
Brian Gaeked0fde302003-11-11 22:41:34 +0000903 const llvm::Type *PrimType;
904 llvm::PATypeHolder *TypeVal;
905 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000906
Brian Gaeked0fde302003-11-11 22:41:34 +0000907 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
908 std::vector<llvm::Value*> *ValueList;
909 std::list<llvm::PATypeHolder> *TypeList;
910 std::list<std::pair<llvm::Value*,
911 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
912 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
913 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000914
Brian Gaeked0fde302003-11-11 22:41:34 +0000915 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000916 int64_t SInt64Val;
917 uint64_t UInt64Val;
918 int SIntVal;
919 unsigned UIntVal;
920 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000921 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000922
Chris Lattner30c89792001-09-07 16:35:17 +0000923 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000924 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000925
Brian Gaeked0fde302003-11-11 22:41:34 +0000926 llvm::Instruction::BinaryOps BinaryOpVal;
927 llvm::Instruction::TermOps TermOpVal;
928 llvm::Instruction::MemoryOps MemOpVal;
929 llvm::Instruction::OtherOps OtherOpVal;
930 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000931}
932
Chris Lattner79df7c02002-03-26 18:01:55 +0000933%type <ModuleVal> Module FunctionList
934%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000935%type <BasicBlockVal> BasicBlock InstructionList
936%type <TermInstVal> BBTerminatorInst
937%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000938%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000939%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000940%type <ArgList> ArgList ArgListH
941%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000942%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000943%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000944%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000945%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000946%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000947%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000948%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000949%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000950%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000951
Chris Lattner2079fde2001-10-13 06:41:08 +0000952// ValueRef - Unresolved reference to a definition or BB
953%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000954%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000955// Tokens and types for handling constant integer values
956//
957// ESINT64VAL - A negative number within long long range
958%token <SInt64Val> ESINT64VAL
959
960// EUINT64VAL - A positive number within uns. long long range
961%token <UInt64Val> EUINT64VAL
962%type <SInt64Val> EINT64VAL
963
964%token <SIntVal> SINTVAL // Signed 32 bit ints...
965%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
966%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000967%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000968
969// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000970%type <TypeVal> Types TypesV UpRTypes UpRTypesV
971%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000972%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
973%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000974
Chris Lattner6c23f572003-08-22 05:42:10 +0000975%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
976%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000977
978
Chris Lattner91ef4602004-03-31 03:49:47 +0000979%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000980%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000981%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000982%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000983
984// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000985%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000986
Chris Lattner00950542001-06-06 20:29:01 +0000987// Binary Operators
988%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000989%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000990%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000991%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000992
993// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000994%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000995
Chris Lattner027dcc52001-07-08 21:10:27 +0000996// Other Operators
997%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000998%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000999%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +00001000
Chris Lattner00950542001-06-06 20:29:01 +00001001%start Module
1002%%
1003
1004// Handle constant integer size restriction and conversion...
1005//
Chris Lattner51727be2002-06-04 21:58:56 +00001006INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +00001007INTVAL : UINTVAL {
1008 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
1009 ThrowException("Value too large for type!");
1010 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +00001011};
Chris Lattner00950542001-06-06 20:29:01 +00001012
1013
Chris Lattner51727be2002-06-04 21:58:56 +00001014EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +00001015EINT64VAL : EUINT64VAL {
1016 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1017 ThrowException("Value too large for type!");
1018 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +00001019};
Chris Lattner00950542001-06-06 20:29:01 +00001020
Chris Lattner00950542001-06-06 20:29:01 +00001021// Operations that are notably excluded from this list include:
1022// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1023//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001024ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1025LogicalOps : AND | OR | XOR;
1026SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
1027BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
1028
Chris Lattner51727be2002-06-04 21:58:56 +00001029ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001030
Chris Lattnere98dda62001-07-14 06:10:16 +00001031// These are some types that allow classification if we only want a particular
1032// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001033SIntType : LONG | INT | SHORT | SBYTE;
1034UIntType : ULONG | UINT | USHORT | UBYTE;
1035IntType : SIntType | UIntType;
1036FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001037
Chris Lattnere98dda62001-07-14 06:10:16 +00001038// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001039OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001040 $$ = $1;
1041 }
1042 | /*empty*/ {
1043 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001044 };
Chris Lattner00950542001-06-06 20:29:01 +00001045
Chris Lattner4ad02e72003-04-16 20:28:45 +00001046OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1047 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001048 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001049 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1050 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001051
1052//===----------------------------------------------------------------------===//
1053// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001054// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001055// access to it, a user must explicitly use TypesV.
1056//
1057
1058// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001059TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1060UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001061
1062Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001063 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001064 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1065 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001066 };
Chris Lattner30c89792001-09-07 16:35:17 +00001067
1068
1069// Derived types are added later...
1070//
Chris Lattner51727be2002-06-04 21:58:56 +00001071PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1072PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001073UpRTypes : OPAQUE {
1074 $$ = new PATypeHolder(OpaqueType::get());
1075 }
1076 | PrimType {
1077 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001078 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001079UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001080 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001081};
Chris Lattner30c89792001-09-07 16:35:17 +00001082
Chris Lattner30c89792001-09-07 16:35:17 +00001083// Include derived types in the Types production.
1084//
1085UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001086 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001087 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001088 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001089 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001090 UR_OUT("New Upreference!\n");
1091 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001092 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001093 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001094 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001095 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001096 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1097 if (isVarArg) Params.pop_back();
1098
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001099 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001100 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001101 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001102 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001103 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001104 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001105 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001106 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001107 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001108 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001109 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001110 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001111
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001112 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001113 delete $2;
1114 }
1115 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001116 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001117 }
1118 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001119 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001120 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001121 };
Chris Lattner30c89792001-09-07 16:35:17 +00001122
Chris Lattner7e708292002-06-25 16:13:24 +00001123// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001124// declaration type lists
1125//
1126TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001127 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001128 $$->push_back(*$1); delete $1;
1129 }
1130 | TypeListI ',' UpRTypes {
1131 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001132 };
Chris Lattner30c89792001-09-07 16:35:17 +00001133
Chris Lattner7e708292002-06-25 16:13:24 +00001134// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001135ArgTypeListI : TypeListI
1136 | TypeListI ',' DOTDOTDOT {
1137 ($$=$1)->push_back(Type::VoidTy);
1138 }
1139 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001140 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001141 }
1142 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001143 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001144 };
Chris Lattner30c89792001-09-07 16:35:17 +00001145
Chris Lattnere98dda62001-07-14 06:10:16 +00001146// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001147// production is used ONLY to represent constants that show up AFTER a 'const',
1148// 'constant' or 'global' token at global scope. Constants that can be inlined
1149// into other expressions (such as integers and constexprs) are handled by the
1150// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001151//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001152ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001153 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001154 if (ATy == 0)
1155 ThrowException("Cannot make array constant with type: '" +
1156 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001157 const Type *ETy = ATy->getElementType();
1158 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001159
Chris Lattner30c89792001-09-07 16:35:17 +00001160 // Verify that we have the correct size...
1161 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001162 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001163 utostr($3->size()) + " arguments, but has size of " +
1164 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001165
Chris Lattner30c89792001-09-07 16:35:17 +00001166 // Verify all elements are correct type!
1167 for (unsigned i = 0; i < $3->size(); i++) {
1168 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001169 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001170 ETy->getDescription() +"' as required!\nIt is of type '"+
1171 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001172 }
1173
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001174 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001175 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001176 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001177 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001178 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001179 if (ATy == 0)
1180 ThrowException("Cannot make array constant with type: '" +
1181 (*$1)->getDescription() + "'!");
1182
1183 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001184 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001185 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001186 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001187 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001188 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001189 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001190 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001191 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001192 if (ATy == 0)
1193 ThrowException("Cannot make array constant with type: '" +
1194 (*$1)->getDescription() + "'!");
1195
Chris Lattner30c89792001-09-07 16:35:17 +00001196 int NumElements = ATy->getNumElements();
1197 const Type *ETy = ATy->getElementType();
1198 char *EndStr = UnEscapeLexed($3, true);
1199 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001200 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001201 itostr((int)(EndStr-$3)) +
1202 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001203 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001204 if (ETy == Type::SByteTy) {
1205 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001206 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001207 } else if (ETy == Type::UByteTy) {
1208 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001209 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001210 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001211 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001212 ThrowException("Cannot build string arrays of non byte sized elements!");
1213 }
Chris Lattner30c89792001-09-07 16:35:17 +00001214 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001215 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001216 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001217 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001218 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001219 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001220 if (STy == 0)
1221 ThrowException("Cannot make struct constant with type: '" +
1222 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001223
Chris Lattnerc6212f12003-05-21 16:06:56 +00001224 if ($3->size() != STy->getNumContainedTypes())
1225 ThrowException("Illegal number of initializers for structure type!");
1226
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001227 // Check to ensure that constants are compatible with the type initializer!
1228 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001229 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001230 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001231 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001232 "' for element #" + utostr(i) +
1233 " of structure initializer!");
1234
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001235 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001236 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001237 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001238 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001239 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001240 if (STy == 0)
1241 ThrowException("Cannot make struct constant with type: '" +
1242 (*$1)->getDescription() + "'!");
1243
1244 if (STy->getNumContainedTypes() != 0)
1245 ThrowException("Illegal number of initializers for structure type!");
1246
1247 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1248 delete $1;
1249 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001250 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001251 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001252 if (PTy == 0)
1253 ThrowException("Cannot make null pointer constant with type: '" +
1254 (*$1)->getDescription() + "'!");
1255
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001256 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001257 delete $1;
1258 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001259 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001260 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001261 if (Ty == 0)
1262 ThrowException("Global const reference must be a pointer type!");
1263
Chris Lattner3101c252002-08-15 17:58:33 +00001264 // ConstExprs can exist in the body of a function, thus creating
1265 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1266 // the context of a function, getValNonImprovising will search the functions
1267 // symbol table instead of the module symbol table for the global symbol,
1268 // which throws things all off. To get around this, we just tell
1269 // getValNonImprovising that we are at global scope here.
1270 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001271 Function *SavedCurFn = CurFun.CurrentFunction;
1272 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001273
Chris Lattner2079fde2001-10-13 06:41:08 +00001274 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001275
Chris Lattner394f2eb2003-10-16 20:12:13 +00001276 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001277
Chris Lattner2079fde2001-10-13 06:41:08 +00001278 // If this is an initializer for a constant pointer, which is referencing a
1279 // (currently) undefined variable, create a stub now that shall be replaced
1280 // in the future with the right type of variable.
1281 //
1282 if (V == 0) {
1283 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1284 const PointerType *PT = cast<PointerType>(Ty);
1285
1286 // First check to see if the forward references value is already created!
1287 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001288 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001289
1290 if (I != CurModule.GlobalRefs.end()) {
1291 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001292 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001293 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001294 std::string Name;
1295
1296 /// FIXME: We shouldn't be creating global vars as forward refs for
1297 /// functions at all here!
1298 if (!isa<FunctionType>(PT->getElementType()))
1299 if ($2.Type == ValID::NameVal) Name = $2.Name;
1300
Chris Lattner2079fde2001-10-13 06:41:08 +00001301 // Create a placeholder for the global variable reference...
Chris Lattner8a327842004-07-14 21:44:00 +00001302 GlobalVariable *GV = new GlobalVariable(PT->getElementType(), false,
1303 GlobalValue::ExternalLinkage,0,
1304 Name, CurModule.CurrentModule);
1305
Chris Lattner2079fde2001-10-13 06:41:08 +00001306 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001307 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001308 V = GV;
1309 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001310 }
1311
Chris Lattner2079fde2001-10-13 06:41:08 +00001312 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001313 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001314 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001315 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001316 | Types ConstExpr {
1317 if ($1->get() != $2->getType())
1318 ThrowException("Mismatched types for constant expression!");
1319 $$ = $2;
1320 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001321 }
1322 | Types ZEROINITIALIZER {
1323 $$ = Constant::getNullValue($1->get());
1324 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001325 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001326
Chris Lattnere43f40b2002-10-09 00:25:32 +00001327ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001328 if (!ConstantSInt::isValueValidForType($1, $2))
1329 ThrowException("Constant value doesn't fit in type!");
1330 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001331 }
1332 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001333 if (!ConstantUInt::isValueValidForType($1, $2))
1334 ThrowException("Constant value doesn't fit in type!");
1335 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001336 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001337 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001338 $$ = ConstantBool::True;
1339 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001340 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001341 $$ = ConstantBool::False;
1342 }
1343 | FPType FPVAL { // Float & Double constants
1344 $$ = ConstantFP::get($1, $2);
1345 };
1346
Chris Lattner00950542001-06-06 20:29:01 +00001347
Chris Lattnerd78700d2002-08-16 21:14:40 +00001348ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001349 if (!$3->getType()->isFirstClassType())
1350 ThrowException("cast constant expression from a non-primitive type: '" +
1351 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001352 if (!$5->get()->isFirstClassType())
1353 ThrowException("cast constant expression to a non-primitive type: '" +
1354 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001355 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001356 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001357 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001358 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1359 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001360 ThrowException("GetElementPtr requires a pointer operand!");
1361
Chris Lattner830b6f92004-04-05 01:30:04 +00001362 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1363 // indices to uint struct indices for compatibility.
1364 generic_gep_type_iterator<std::vector<Value*>::iterator>
1365 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1366 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1367 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1368 if (isa<StructType>(*GTI)) // Only change struct indices
1369 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1370 if (CUI->getType() == Type::UByteTy)
1371 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1372
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001373 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001374 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001375 if (!IdxTy)
1376 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001377
Chris Lattner9232b992003-04-22 18:42:41 +00001378 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001379 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1380 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001381 IdxVec.push_back(C);
1382 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001383 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001384
Chris Lattnerd78700d2002-08-16 21:14:40 +00001385 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001386
Chris Lattnerd78700d2002-08-16 21:14:40 +00001387 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001388 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001389 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1390 if ($3->getType() != Type::BoolTy)
1391 ThrowException("Select condition must be of boolean type!");
1392 if ($5->getType() != $7->getType())
1393 ThrowException("Select operand types must match!");
1394 $$ = ConstantExpr::getSelect($3, $5, $7);
1395 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001396 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001397 if ($3->getType() != $5->getType())
1398 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001399 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001400 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001401 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001402 if ($5->getType() != Type::UByteTy)
1403 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001404 if (!$3->getType()->isInteger())
1405 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001406 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001407 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001408
1409
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001410// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001411ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001412 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001413 }
1414 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001415 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001416 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001417 };
Chris Lattner00950542001-06-06 20:29:01 +00001418
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001419
Chris Lattner1781aca2001-09-18 04:00:54 +00001420// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001421GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001422
Chris Lattner00950542001-06-06 20:29:01 +00001423
Chris Lattner0e73ce62002-05-02 19:11:13 +00001424//===----------------------------------------------------------------------===//
1425// Rules to match Modules
1426//===----------------------------------------------------------------------===//
1427
1428// Module rule: Capture the result of parsing the whole file into a result
1429// variable...
1430//
1431Module : FunctionList {
1432 $$ = ParserResult = $1;
1433 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001434};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001435
Chris Lattner7e708292002-06-25 16:13:24 +00001436// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001437//
1438FunctionList : FunctionList Function {
1439 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001440 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001441 }
1442 | FunctionList FunctionProto {
1443 $$ = $1;
1444 }
1445 | FunctionList IMPLEMENTATION {
1446 $$ = $1;
1447 }
1448 | ConstPool {
1449 $$ = CurModule.CurrentModule;
1450 // Resolve circular types before we parse the body of the module
1451 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001452 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001453
Chris Lattnere98dda62001-07-14 06:10:16 +00001454// ConstPool - Constants with optional names assigned to them.
Chris Lattnere0026942004-07-14 06:44:56 +00001455ConstPool : ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001456 // Eagerly resolve types. This is not an optimization, this is a
1457 // requirement that is due to the fact that we could have this:
1458 //
1459 // %list = type { %list * }
1460 // %list = type { %list * } ; repeated type decl
1461 //
1462 // If types are not resolved eagerly, then the two types will not be
1463 // determined to be the same type!
1464 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001465 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001466
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001467 if (!setTypeName(*$4, $2) && !$2) {
1468 // If this is a named type that is not a redefinition, add it to the slot
1469 // table.
Chris Lattner64116f92004-07-14 01:33:11 +00001470 if (inFunctionScope())
1471 CurFun.Types.push_back(*$4);
1472 else
1473 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001474 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001475
1476 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001477 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001478 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001479 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001480 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001481 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1482 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Chris Lattner1781aca2001-09-18 04:00:54 +00001483 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001484 | ConstPool OptAssign EXTERNAL GlobalType Types {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001485 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001486 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001487 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001488 | ConstPool TARGET TargetDefinition {
1489 }
Chris Lattner00950542001-06-06 20:29:01 +00001490 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001491 };
Chris Lattner00950542001-06-06 20:29:01 +00001492
1493
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001494
1495BigOrLittle : BIG { $$ = Module::BigEndian; };
1496BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1497
1498TargetDefinition : ENDIAN '=' BigOrLittle {
1499 CurModule.CurrentModule->setEndianness($3);
1500 }
1501 | POINTERSIZE '=' EUINT64VAL {
1502 if ($3 == 32)
1503 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1504 else if ($3 == 64)
1505 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1506 else
1507 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1508 };
1509
1510
Chris Lattner00950542001-06-06 20:29:01 +00001511//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001512// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001513//===----------------------------------------------------------------------===//
1514
Chris Lattner6c23f572003-08-22 05:42:10 +00001515Name : VAR_ID | STRINGCONSTANT;
1516OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001517
Chris Lattner6c23f572003-08-22 05:42:10 +00001518ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001519 if (*$1 == Type::VoidTy)
1520 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001521 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001522};
Chris Lattner00950542001-06-06 20:29:01 +00001523
Chris Lattner69da5cf2002-10-13 20:57:00 +00001524ArgListH : ArgListH ',' ArgVal {
1525 $$ = $1;
1526 $1->push_back(*$3);
1527 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001528 }
1529 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001530 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001531 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001532 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001533 };
Chris Lattner00950542001-06-06 20:29:01 +00001534
1535ArgList : ArgListH {
1536 $$ = $1;
1537 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001538 | ArgListH ',' DOTDOTDOT {
1539 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001540 $$->push_back(std::pair<PATypeHolder*,
1541 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001542 }
1543 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001544 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1545 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001546 }
Chris Lattner00950542001-06-06 20:29:01 +00001547 | /* empty */ {
1548 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001549 };
Chris Lattner00950542001-06-06 20:29:01 +00001550
Chris Lattner6c23f572003-08-22 05:42:10 +00001551FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001552 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001553 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001554
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001555 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1556 ThrowException("LLVM functions cannot return aggregate types!");
1557
Chris Lattner9232b992003-04-22 18:42:41 +00001558 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001559 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001560 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001561 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001562 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001563 }
Chris Lattner00950542001-06-06 20:29:01 +00001564
Chris Lattner2079fde2001-10-13 06:41:08 +00001565 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1566 if (isVarArg) ParamTypeList.pop_back();
1567
Chris Lattner1f862af2003-04-16 18:13:57 +00001568 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001569 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001570 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001571
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001572 Function *Fn = 0;
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001573 // Is the function already in symtab?
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001574 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1575 // Yes it is. If this is the case, either we need to be a forward decl,
1576 // or it needs to be.
1577 if (!CurFun.isDeclare && !Fn->isExternal())
1578 ThrowException("Redefinition of function '" + FunctionName + "'!");
1579
1580 // Make sure to strip off any argument names so we can't get conflicts...
1581 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1582 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001583
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001584 } else { // Not already defined?
1585 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1586 CurModule.CurrentModule);
1587 InsertValue(Fn, CurModule.Values);
1588 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
1589 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001590 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001591
Chris Lattner394f2eb2003-10-16 20:12:13 +00001592 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001593
Chris Lattner7e708292002-06-25 16:13:24 +00001594 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001595 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001596 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001597 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001598 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001599 delete $4->back().first;
1600 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001601 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001602 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001603 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001604 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001605 delete I->first; // Delete the typeholder...
1606
Chris Lattner8ab406d2004-07-13 07:59:27 +00001607 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001608 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001609 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001610
Chris Lattner1f862af2003-04-16 18:13:57 +00001611 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001612 }
Chris Lattner51727be2002-06-04 21:58:56 +00001613};
Chris Lattner00950542001-06-06 20:29:01 +00001614
Chris Lattner9b02cc32002-05-03 18:23:48 +00001615BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1616
Chris Lattner4ad02e72003-04-16 20:28:45 +00001617FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001618 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001619
Chris Lattner4ad02e72003-04-16 20:28:45 +00001620 // Make sure that we keep track of the linkage type even if there was a
1621 // previous "declare".
1622 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001623
Chris Lattner7e708292002-06-25 16:13:24 +00001624 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001625 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001626};
Chris Lattner00950542001-06-06 20:29:01 +00001627
Chris Lattner9b02cc32002-05-03 18:23:48 +00001628END : ENDTOK | '}'; // Allow end of '}' to end a function
1629
Chris Lattner79df7c02002-03-26 18:01:55 +00001630Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001631 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001632};
Chris Lattner00950542001-06-06 20:29:01 +00001633
Chris Lattner394f2eb2003-10-16 20:12:13 +00001634FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1635 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001636 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001637};
Chris Lattner00950542001-06-06 20:29:01 +00001638
1639//===----------------------------------------------------------------------===//
1640// Rules to match Basic Blocks
1641//===----------------------------------------------------------------------===//
1642
1643ConstValueRef : ESINT64VAL { // A reference to a direct constant
1644 $$ = ValID::create($1);
1645 }
1646 | EUINT64VAL {
1647 $$ = ValID::create($1);
1648 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001649 | FPVAL { // Perhaps it's an FP constant?
1650 $$ = ValID::create($1);
1651 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001652 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001653 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001654 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001655 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001656 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001657 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001658 | NULL_TOK {
1659 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001660 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001661 | ConstExpr {
1662 $$ = ValID::create($1);
1663 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001664
Chris Lattner2079fde2001-10-13 06:41:08 +00001665// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1666// another value.
1667//
1668SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001669 $$ = ValID::create($1);
1670 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001671 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001672 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001673 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001674
1675// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001676ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001677
Chris Lattner00950542001-06-06 20:29:01 +00001678
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001679// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1680// type immediately preceeds the value reference, and allows complex constant
1681// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001682ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001683 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001684 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001685
Chris Lattner00950542001-06-06 20:29:01 +00001686BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001687 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001688 }
Chris Lattner7e708292002-06-25 16:13:24 +00001689 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001690 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001691 };
Chris Lattner00950542001-06-06 20:29:01 +00001692
1693
1694// Basic blocks are terminated by branching instructions:
1695// br, br/cc, switch, ret
1696//
Chris Lattner2079fde2001-10-13 06:41:08 +00001697BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001698 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001699 InsertValue($3);
1700
1701 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001702 InsertValue($1);
1703 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001704 };
Chris Lattner00950542001-06-06 20:29:01 +00001705
1706InstructionList : InstructionList Inst {
1707 $1->getInstList().push_back($2);
1708 $$ = $1;
1709 }
1710 | /* empty */ {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001711 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner22ae2322004-07-13 08:39:15 +00001712 }
1713 | LABELSTR {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001714 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner51727be2002-06-04 21:58:56 +00001715 };
Chris Lattner00950542001-06-06 20:29:01 +00001716
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001717BBTerminatorInst : RET ResolvedVal { // Return with a result...
1718 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001719 }
1720 | RET VOID { // Return with no result...
1721 $$ = new ReturnInst();
1722 }
1723 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001724 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001725 } // Conditional Branch...
1726 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001727 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001728 }
1729 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001730 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner00950542001-06-06 20:29:01 +00001731 $$ = S;
1732
Chris Lattner9232b992003-04-22 18:42:41 +00001733 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001734 E = $8->end();
1735 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001736 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001737 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001738 }
Chris Lattner196850c2003-05-15 21:30:00 +00001739 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001740 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner196850c2003-05-15 21:30:00 +00001741 $$ = S;
1742 }
Chris Lattner64116f92004-07-14 01:33:11 +00001743 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1744 UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001745 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001746 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001747
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001748 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1749 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001750 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001751 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001752 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001753 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1754 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001755 ParamTypes.push_back((*I)->getType());
1756 }
1757
1758 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1759 if (isVarArg) ParamTypes.pop_back();
1760
Chris Lattner79df7c02002-03-26 18:01:55 +00001761 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001762 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001763 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001764
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001765 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001766
Chris Lattner64116f92004-07-14 01:33:11 +00001767 BasicBlock *Normal = getBBVal($9);
1768 BasicBlock *Except = getBBVal($12);
Chris Lattner2079fde2001-10-13 06:41:08 +00001769
1770 // Create the call node...
1771 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001772 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001773 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001774 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001775 // correctly!
1776 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001777 FunctionType::param_iterator I = Ty->param_begin();
1778 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001779 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001780
1781 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1782 if ((*ArgI)->getType() != *I)
1783 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001784 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001785
1786 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1787 ThrowException("Invalid number of parameters detected!");
1788
Chris Lattner6cdb0112001-11-26 16:54:11 +00001789 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001790 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001791 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001792 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001793 }
1794 | UNWIND {
1795 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001796 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001797
1798
Chris Lattner00950542001-06-06 20:29:01 +00001799
1800JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1801 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001802 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001803 if (V == 0)
1804 ThrowException("May only switch on a constant pool value!");
1805
Chris Lattner64116f92004-07-14 01:33:11 +00001806 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001807 }
1808 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001809 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001810 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001811
1812 if (V == 0)
1813 ThrowException("May only switch on a constant pool value!");
1814
Chris Lattner64116f92004-07-14 01:33:11 +00001815 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001816 };
Chris Lattner00950542001-06-06 20:29:01 +00001817
1818Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001819 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001820 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001821 InsertValue($2);
1822 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001823};
Chris Lattner00950542001-06-06 20:29:01 +00001824
Chris Lattnerc24d2082001-06-11 15:04:20 +00001825PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001826 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001827 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001828 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001829 }
1830 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1831 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001832 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001833 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001834 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001835
1836
Chris Lattner30c89792001-09-07 16:35:17 +00001837ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001838 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001839 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001840 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001841 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001842 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001843 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001844 };
Chris Lattner00950542001-06-06 20:29:01 +00001845
1846// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001847ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001848
Chris Lattner4a6482b2002-09-10 19:57:26 +00001849InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1850 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1851 ThrowException("Arithmetic operator requires integer or FP operands!");
1852 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1853 if ($$ == 0)
1854 ThrowException("binary operator returned null!");
1855 delete $2;
1856 }
1857 | LogicalOps Types ValueRef ',' ValueRef {
1858 if (!(*$2)->isIntegral())
1859 ThrowException("Logical operator requires integral operands!");
1860 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1861 if ($$ == 0)
1862 ThrowException("binary operator returned null!");
1863 delete $2;
1864 }
1865 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001866 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001867 if ($$ == 0)
1868 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001869 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001870 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001871 | NOT ResolvedVal {
1872 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1873 << " Replacing with 'xor'.\n";
1874
1875 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1876 if (Ones == 0)
1877 ThrowException("Expected integral type for not instruction!");
1878
1879 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001880 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001881 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001882 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001883 | ShiftOps ResolvedVal ',' ResolvedVal {
1884 if ($4->getType() != Type::UByteTy)
1885 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001886 if (!$2->getType()->isInteger())
1887 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001888 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001889 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001890 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001891 if (!$4->get()->isFirstClassType())
1892 ThrowException("cast instruction to a non-primitive type: '" +
1893 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001894 $$ = new CastInst($2, *$4);
1895 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001896 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001897 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1898 if ($2->getType() != Type::BoolTy)
1899 ThrowException("select condition must be boolean!");
1900 if ($4->getType() != $6->getType())
1901 ThrowException("select value types should match!");
1902 $$ = new SelectInst($2, $4, $6);
1903 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001904 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001905 // FIXME: This is emulation code for an obsolete syntax. This should be
1906 // removed at some point.
1907 if (!ObsoleteVarArgs) {
1908 std::cerr << "WARNING: this file uses obsolete features. "
1909 << "Assemble and disassemble to update it.\n";
1910 ObsoleteVarArgs = true;
1911 }
1912
1913 // First, load the valist...
1914 Instruction *CurVAList = new LoadInst($2, "");
1915 CurBB->getInstList().push_back(CurVAList);
1916
1917 // Emit the vaarg instruction.
1918 $$ = new VAArgInst(CurVAList, *$4);
1919
1920 // Now we must advance the pointer and update it in memory.
1921 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1922 CurBB->getInstList().push_back(TheVANext);
1923
1924 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1925 delete $4;
1926 }
1927 | VAARG ResolvedVal ',' Types {
1928 $$ = new VAArgInst($2, *$4);
1929 delete $4;
1930 }
1931 | VANEXT ResolvedVal ',' Types {
1932 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001933 delete $4;
1934 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001935 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001936 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001937 if (!Ty->isFirstClassType())
1938 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001939 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001940 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001941 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001942 if ($2->front().first->getType() != Ty)
1943 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001944 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001945 $2->pop_front();
1946 }
1947 delete $2; // Free the list...
1948 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001949 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001950 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001951 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001952
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001953 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1954 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001955 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001956 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001957 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001958 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1959 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001960 ParamTypes.push_back((*I)->getType());
1961 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001962
1963 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1964 if (isVarArg) ParamTypes.pop_back();
1965
Chris Lattner79df7c02002-03-26 18:01:55 +00001966 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001967 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001968 }
Chris Lattner00950542001-06-06 20:29:01 +00001969
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001970 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001971
Chris Lattner8b81bf52001-07-25 22:47:46 +00001972 // Create the call node...
1973 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001974 // Make sure no arguments is a good thing!
1975 if (Ty->getNumParams() != 0)
1976 ThrowException("No arguments passed to a function that "
1977 "expects arguments!");
1978
Chris Lattner9232b992003-04-22 18:42:41 +00001979 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001980 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001981 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001982 // correctly!
1983 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001984 FunctionType::param_iterator I = Ty->param_begin();
1985 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001986 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001987
1988 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1989 if ((*ArgI)->getType() != *I)
1990 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001991 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001992
Chris Lattner8b81bf52001-07-25 22:47:46 +00001993 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001994 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001995
Chris Lattner6cdb0112001-11-26 16:54:11 +00001996 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001997 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001998 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001999 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00002000 }
2001 | MemoryInst {
2002 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002003 };
Chris Lattner00950542001-06-06 20:29:01 +00002004
Chris Lattner6cdb0112001-11-26 16:54:11 +00002005
2006// IndexList - List of indices for GEP based instructions...
2007IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002008 $$ = $2;
2009 } | /* empty */ {
2010 $$ = new std::vector<Value*>();
2011 };
2012
2013OptVolatile : VOLATILE {
2014 $$ = true;
2015 }
2016 | /* empty */ {
2017 $$ = false;
2018 };
2019
Chris Lattner027dcc52001-07-08 21:10:27 +00002020
Chris Lattner00950542001-06-06 20:29:01 +00002021MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002022 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002023 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002024 }
2025 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002026 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002027 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002028 }
2029 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002030 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002031 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002032 }
2033 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002034 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002035 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002036 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002037 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002038 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002039 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002040 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002041 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002042 }
2043
Chris Lattner15c9c032003-09-08 18:20:29 +00002044 | OptVolatile LOAD Types ValueRef {
2045 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002046 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002047 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002048 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002049 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002050 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002051 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2052 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002053 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002054 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002055 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002056 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002057 if (ElTy != $3->getType())
2058 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002059 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002060
Chris Lattner79ad13802003-09-08 20:29:46 +00002061 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002062 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002063 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002064 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002065 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002066 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002067
2068 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2069 // indices to uint struct indices for compatibility.
2070 generic_gep_type_iterator<std::vector<Value*>::iterator>
2071 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2072 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2073 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2074 if (isa<StructType>(*GTI)) // Only change struct indices
2075 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2076 if (CUI->getType() == Type::UByteTy)
2077 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2078
Chris Lattner30c89792001-09-07 16:35:17 +00002079 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002080 ThrowException("Invalid getelementptr indices for type '" +
2081 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002082 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2083 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002084 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002085
Brian Gaeked0fde302003-11-11 22:41:34 +00002086
Chris Lattner00950542001-06-06 20:29:01 +00002087%%
Chris Lattner09083092001-07-08 04:57:15 +00002088int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002089 std::string where
2090 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002091 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002092 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002093 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002094 errMsg += "end-of-file.";
2095 else
Chris Lattner9232b992003-04-22 18:42:41 +00002096 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002097 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002098 return 0;
2099}