blob: 4dece6e21ca0cd07a1bd40582ec34770cb1f27d2 [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
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000115 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000116 // is used to remove things from the forward declaration map, resolving them
117 // to the correct thing as needed.
118 //
119 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
120 // Check to see if there is a forward reference to this global variable...
121 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000122 GlobalRefsType::iterator I =
123 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000124
125 if (I != GlobalRefs.end()) {
Chris Lattnerbc207592004-03-08 06:09:57 +0000126 GlobalValue *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000127 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner38ab6bf2004-07-13 06:58:07 +0000128
129 // Replace all uses of the placeholder with the new GV
130 OldGV->replaceAllUsesWith(GV);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000131
132 // Remove OldGV from the module...
Chris Lattnerbc207592004-03-08 06:09:57 +0000133 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
134 CurrentModule->getGlobalList().erase(GVar);
135 else
136 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000137
Chris Lattner2079fde2001-10-13 06:41:08 +0000138 // Remove the map entry for the global now that it has been created...
139 GlobalRefs.erase(I);
140 }
141 }
142
Chris Lattner00950542001-06-06 20:29:01 +0000143} CurModule;
144
Chris Lattner79df7c02002-03-26 18:01:55 +0000145static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000146 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000147
Chris Lattner735f2702004-07-08 22:30:50 +0000148 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
149 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000150 std::vector<PATypeHolder> Types;
151 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000152 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000153
Chris Lattner2e2ed292004-07-14 06:28:35 +0000154 /// BBForwardRefs - When we see forward references to basic blocks, keep
155 /// track of them here.
156 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
157 std::vector<BasicBlock*> NumberedBlocks;
158 unsigned NextBBNum;
159
Chris Lattner79df7c02002-03-26 18:01:55 +0000160 inline PerFunctionInfo() {
161 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000162 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000163 }
164
Chris Lattner79df7c02002-03-26 18:01:55 +0000165 inline void FunctionStart(Function *M) {
166 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000167 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000168 }
169
Chris Lattner79df7c02002-03-26 18:01:55 +0000170 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000171 NumberedBlocks.clear();
172
173 // Any forward referenced blocks left?
174 if (!BBForwardRefs.empty())
175 ThrowException("Undefined reference to label " +
176 BBForwardRefs.begin()->second.first.getName());
177
178 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000179 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000180
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000181 // Make sure to resolve any constant expr references that might exist within
182 // the function we just declared itself.
183 ValID FID;
184 if (CurrentFunction->hasName()) {
185 FID = ValID::create((char*)CurrentFunction->getName().c_str());
186 } else {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000187 // Figure out which slot number if is...
Chris Lattner735f2702004-07-08 22:30:50 +0000188 ValueList &List = CurModule.Values[CurrentFunction->getType()];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000189 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000190 assert(i < List.size() && "Function not found!");
191 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000192 FID = ValID::create((int)i);
193 break;
194 }
195 }
196 }
197 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
198
Chris Lattner7e708292002-06-25 16:13:24 +0000199 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000200 Types.clear(); // Clear out function local types
Chris Lattner79df7c02002-03-26 18:01:55 +0000201 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000202 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000203 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000204} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000205
Chris Lattner394f2eb2003-10-16 20:12:13 +0000206static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000207
Chris Lattner00950542001-06-06 20:29:01 +0000208
209//===----------------------------------------------------------------------===//
210// Code to handle definitions of all the types
211//===----------------------------------------------------------------------===//
212
Chris Lattner735f2702004-07-08 22:30:50 +0000213static int InsertValue(Value *V,
214 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
215 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000216
217 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000218 ValueList &List = ValueTab[V->getType()];
219 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000220 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000221}
222
Chris Lattner30c89792001-09-07 16:35:17 +0000223static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000224 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000225 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000226 unsigned Num = (unsigned)D.Num;
227
228 // Module constants occupy the lowest numbered slots...
229 if (Num < CurModule.Types.size())
230 return CurModule.Types[Num];
231
232 Num -= CurModule.Types.size();
233
234 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000235 if (Num <= CurFun.Types.size())
236 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000237 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000238 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000239 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000240 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000241 SymbolTable *SymTab = 0;
Reid Spencer8ce1da72004-07-04 12:19:05 +0000242 Type *N = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000243 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000244 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000245 N = SymTab->lookupType(Name);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000246 }
Chris Lattner30c89792001-09-07 16:35:17 +0000247
248 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000249 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000250 // hasn't been added to the module...
251 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000252 SymTab = &CurModule.CurrentModule->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000253 N = SymTab->lookupType(Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000254 if (N == 0) break;
255 }
256
257 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000258 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000259 }
260 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000261 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000262 }
263
264 // If we reached here, we referenced either a symbol that we don't know about
265 // or an id number that hasn't been read yet. We may be referencing something
266 // forward, so just create an entry to be resolved later and get to it...
267 //
268 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
269
Chris Lattner9232b992003-04-22 18:42:41 +0000270 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000271 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000272
Chris Lattner9232b992003-04-22 18:42:41 +0000273 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000274 if (I != LateResolver.end()) {
275 return I->second;
276 }
Chris Lattner30c89792001-09-07 16:35:17 +0000277
Chris Lattner82269592001-10-22 06:01:08 +0000278 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000279 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000280 return Typ;
281}
282
Chris Lattner9232b992003-04-22 18:42:41 +0000283static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000284 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000285 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000286 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000287 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000288}
289
Chris Lattner2079fde2001-10-13 06:41:08 +0000290// getValNonImprovising - Look up the value specified by the provided type and
291// the provided ValID. If the value exists and has already been defined, return
292// it. Otherwise return null.
293//
294static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000295 if (isa<FunctionType>(Ty))
296 ThrowException("Functions are not values and "
297 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000298
Chris Lattner30c89792001-09-07 16:35:17 +0000299 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000300 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000301 unsigned Num = (unsigned)D.Num;
302
303 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000304 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000305 if (VI != CurModule.Values.end()) {
306 if (Num < VI->second.size())
307 return VI->second[Num];
308 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000309 }
310
311 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000312 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000313 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000314
315 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000316 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000317
Chris Lattner16af11d2004-02-09 21:03:38 +0000318 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000319 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000320
Chris Lattner1a1cb112001-09-30 22:46:54 +0000321 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000322 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000323 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000324
325 D.destroy(); // Free old strdup'd memory...
326 return N;
327 }
328
Chris Lattner2079fde2001-10-13 06:41:08 +0000329 // Check to make sure that "Ty" is an integral type, and that our
330 // value will fit into the specified type...
331 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000332 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
333 ThrowException("Signed integral constant '" +
334 itostr(D.ConstPool64) + "' is invalid for type '" +
335 Ty->getDescription() + "'!");
336 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000337
338 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000339 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
340 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000341 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
342 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000343 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000344 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000345 }
346 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000347 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000348 }
349
Chris Lattner2079fde2001-10-13 06:41:08 +0000350 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000351 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000352 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000353 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000354
355 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000356 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000357 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000358 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000359
Chris Lattnerd78700d2002-08-16 21:14:40 +0000360 case ValID::ConstantVal: // Fully resolved constant?
361 if (D.ConstantValue->getType() != Ty)
362 ThrowException("Constant expression type different from required type!");
363 return D.ConstantValue;
364
Chris Lattner30c89792001-09-07 16:35:17 +0000365 default:
366 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000367 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000368 } // End of switch
369
Chris Lattner2079fde2001-10-13 06:41:08 +0000370 assert(0 && "Unhandled case!");
371 return 0;
372}
373
Chris Lattner2079fde2001-10-13 06:41:08 +0000374// getVal - This function is identical to getValNonImprovising, except that if a
375// value is not already defined, it "improvises" by creating a placeholder var
376// that looks and acts just like the requested variable. When the value is
377// defined later, all uses of the placeholder variable are replaced with the
378// real thing.
379//
Chris Lattner64116f92004-07-14 01:33:11 +0000380static Value *getVal(const Type *Ty, const ValID &ID) {
381 if (Ty == Type::LabelTy)
382 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000383
Chris Lattner64116f92004-07-14 01:33:11 +0000384 // See if the value has already been defined.
385 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000386 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000387
388 // If we reached here, we referenced either a symbol that we don't know about
389 // or an id number that hasn't been read yet. We may be referencing something
390 // forward, so just create an entry to be resolved later and get to it...
391 //
Chris Lattner64116f92004-07-14 01:33:11 +0000392 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000393
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000394 // Remember where this forward reference came from. FIXME, shouldn't we try
395 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000396 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000397 llvmAsmlineno)));
398
Chris Lattner79df7c02002-03-26 18:01:55 +0000399 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000400 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000401 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000402 InsertValue(V, CurModule.LateResolveValues);
403 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000404}
405
Chris Lattner2e2ed292004-07-14 06:28:35 +0000406/// getBBVal - This is used for two purposes:
407/// * If isDefinition is true, a new basic block with the specified ID is being
408/// defined.
409/// * If isDefinition is true, this is a reference to a basic block, which may
410/// or may not be a forward reference.
411///
412static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000413 assert(inFunctionScope() && "Can't get basic block at global scope!");
414
Chris Lattner2e2ed292004-07-14 06:28:35 +0000415 std::string Name;
416 BasicBlock *BB = 0;
417 switch (ID.Type) {
418 default: ThrowException("Illegal label reference " + ID.getName());
419 case ValID::NumberVal: // Is it a numbered definition?
420 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
421 CurFun.NumberedBlocks.resize(ID.Num+1);
422 BB = CurFun.NumberedBlocks[ID.Num];
423 break;
424 case ValID::NameVal: // Is it a named definition?
425 Name = ID.Name;
426 if (Value *N = lookupInSymbolTable(Type::LabelTy, Name))
427 BB = cast<BasicBlock>(N);
428 break;
429 }
Chris Lattner64116f92004-07-14 01:33:11 +0000430
Chris Lattner2e2ed292004-07-14 06:28:35 +0000431 // See if the block has already been defined.
432 if (BB) {
433 // If this is the definition of the block, make sure the existing value was
434 // just a forward reference. If it was a forward reference, there will be
435 // an entry for it in the PlaceHolderInfo map.
436 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
437 // The existing value was a definition, not a forward reference.
438 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000439
Chris Lattner2e2ed292004-07-14 06:28:35 +0000440 ID.destroy(); // Free strdup'd memory.
441 return BB;
442 }
443
444 // Otherwise this block has not been seen before.
445 BB = new BasicBlock("", CurFun.CurrentFunction);
446 if (ID.Type == ValID::NameVal) {
447 BB->setName(ID.Name);
448 } else {
449 CurFun.NumberedBlocks[ID.Num] = BB;
450 }
451
452 // If this is not a definition, keep track of it so we can use it as a forward
453 // reference.
454 if (!isDefinition) {
455 // Remember where this forward reference came from.
456 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
457 } else {
458 // The forward declaration could have been inserted anywhere in the
459 // function: insert it into the correct place now.
460 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
461 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
462 }
463
Chris Lattner64116f92004-07-14 01:33:11 +0000464 return BB;
465}
466
Chris Lattner00950542001-06-06 20:29:01 +0000467
468//===----------------------------------------------------------------------===//
469// Code to handle forward references in instructions
470//===----------------------------------------------------------------------===//
471//
472// This code handles the late binding needed with statements that reference
473// values not defined yet... for example, a forward branch, or the PHI node for
474// a loop body.
475//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000476// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000477// and back patchs after we are done.
478//
479
480// ResolveDefinitions - If we could not resolve some defs at parsing
481// time (forward branches, phi functions for loops, etc...) resolve the
482// defs now...
483//
Chris Lattner735f2702004-07-08 22:30:50 +0000484static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
485 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000486 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000487 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000488 E = LateResolvers.end(); LRI != E; ++LRI) {
489 ValueList &List = LRI->second;
490 while (!List.empty()) {
491 Value *V = List.back();
492 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000493
494 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
495 CurModule.PlaceHolderInfo.find(V);
496 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
497
498 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000499
Chris Lattner735f2702004-07-08 22:30:50 +0000500 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000501 if (TheRealValue) {
502 V->replaceAllUsesWith(TheRealValue);
503 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000504 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000505 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000506 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000507 // resolver table
508 InsertValue(V, *FutureLateResolvers);
509 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000510 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000511 ThrowException("Reference to an invalid definition: '" +DID.getName()+
512 "' of type '" + V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000513 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000514 else
515 ThrowException("Reference to an invalid definition: #" +
516 itostr(DID.Num) + " of type '" +
517 V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000518 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000519 }
Chris Lattner00950542001-06-06 20:29:01 +0000520 }
521 }
522
523 LateResolvers.clear();
524}
525
Chris Lattner4a42e902001-10-22 05:56:09 +0000526// ResolveTypeTo - A brand new type was just declared. This means that (if
527// name is not null) things referencing Name can be resolved. Otherwise, things
528// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000529//
Chris Lattner4a42e902001-10-22 05:56:09 +0000530static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000531 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000532 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000533
Chris Lattner4a42e902001-10-22 05:56:09 +0000534 ValID D;
535 if (Name) D = ValID::create(Name);
536 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000537
Chris Lattner9232b992003-04-22 18:42:41 +0000538 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000539 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000540
Chris Lattner9232b992003-04-22 18:42:41 +0000541 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000542 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000543 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000544 LateResolver.erase(I);
545 }
546}
547
548// ResolveTypes - At this point, all types should be resolved. Any that aren't
549// are errors.
550//
Chris Lattner9232b992003-04-22 18:42:41 +0000551static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000552 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000553 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000554
555 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000556 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000557 else
Chris Lattner82269592001-10-22 06:01:08 +0000558 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000559 }
560}
561
Chris Lattner1781aca2001-09-18 04:00:54 +0000562// setValueName - Set the specified value to the name given. The name may be
563// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000564// assumed to be a malloc'd string buffer, and is free'd by this function.
565//
566static void setValueName(Value *V, char *NameStr) {
567 if (NameStr) {
568 std::string Name(NameStr); // Copy string
569 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000570
571 if (V->getType() == Type::VoidTy)
572 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000573
574 assert(inFunctionScope() && "Must be in function scope!");
575 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
576 if (ST.lookup(V->getType(), Name))
577 ThrowException("Redefinition of value named '" + Name + "' in the '" +
578 V->getType()->getDescription() + "' type plane!");
579
Chris Lattnerc9aea522004-07-13 08:12:39 +0000580 // Set the name.
581 V->setName(Name, &ST);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000582 }
583}
584
585// setValueNameMergingDuplicates - Set the specified value to the name given.
586// The name may be null potentially, in which case this is a noop. The string
587// passed in is assumed to be a malloc'd string buffer, and is free'd by this
588// function.
Chris Lattner1781aca2001-09-18 04:00:54 +0000589//
Chris Lattnerb7474512001-10-03 15:39:04 +0000590// This function returns true if the value has already been defined, but is
591// allowed to be redefined in the specified context. If the name is a new name
592// for the typeplane, false is returned.
593//
Chris Lattnerd88998d2004-07-14 08:23:52 +0000594static bool setValueNameMergingDuplicates(GlobalVariable *V, char *NameStr) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000595 if (NameStr == 0) return false;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000596
Chris Lattner9232b992003-04-22 18:42:41 +0000597 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000598 free(NameStr); // Free old string
599
Chris Lattner66028f22004-07-14 07:12:48 +0000600 SymbolTable &ST = CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000601
Reid Spencer75719bf2004-05-26 21:48:31 +0000602 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000603 if (Existing) { // Inserting a name that is already defined???
Chris Lattner66028f22004-07-14 07:12:48 +0000604
Chris Lattner8ab406d2004-07-13 07:59:27 +0000605 // We are a simple redefinition of a value, check to see if it is defined
606 // the same as the old one...
607 if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000608 // We are allowed to redefine a global variable in two circumstances:
609 // 1. If at least one of the globals is uninitialized or
610 // 2. If both initializers have the same value.
611 //
Chris Lattnerd88998d2004-07-14 08:23:52 +0000612 if (!EGV->hasInitializer() || !V->hasInitializer() ||
613 EGV->getInitializer() == V->getInitializer()) {
614
615 // Make sure the existing global version gets the initializer! Make
616 // sure that it also gets marked const if the new version is.
617 if (V->hasInitializer() && !EGV->hasInitializer())
618 EGV->setInitializer(V->getInitializer());
619 if (V->isConstant())
620 EGV->setConstant(true);
621 EGV->setLinkage(V->getLinkage());
622
623 delete V; // Destroy the duplicate!
624 return true; // They are equivalent!
Chris Lattnerb7474512001-10-03 15:39:04 +0000625 }
Chris Lattner9636a912001-10-01 16:18:37 +0000626 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000627
Chris Lattner2079fde2001-10-13 06:41:08 +0000628 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000629 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000630 }
Chris Lattnerc9aea522004-07-13 08:12:39 +0000631
632 // Set the name.
633 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000634 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000635}
636
Chris Lattnerd88998d2004-07-14 08:23:52 +0000637/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
638/// this is a declaration, otherwise it is a definition.
639static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
640 bool isConstantGlobal, const Type *Ty,
641 Constant *Initializer) {
642 // Global declarations appear in Constant Pool
643 GlobalVariable *GV = new GlobalVariable(Ty, isConstantGlobal, Linkage,
644 Initializer);
645 if (!setValueNameMergingDuplicates(GV, NameStr)) { // If not redefining...
646 CurModule.CurrentModule->getGlobalList().push_back(GV);
647 int Slot = InsertValue(GV, CurModule.Values);
648
649 if (Slot != -1) {
650 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
651 } else {
652 CurModule.DeclareNewGlobalValue(GV,
653 ValID::create((char*)GV->getName().c_str()));
654 }
655 }
656}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000657
Reid Spencer75719bf2004-05-26 21:48:31 +0000658// setTypeName - Set the specified type to the name given. The name may be
659// null potentially, in which case this is a noop. The string passed in is
660// assumed to be a malloc'd string buffer, and is freed by this function.
661//
662// This function returns true if the type has already been defined, but is
663// allowed to be redefined in the specified context. If the name is a new name
664// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000665static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000666 if (NameStr == 0) return false;
667
668 std::string Name(NameStr); // Copy string
669 free(NameStr); // Free old string
670
671 // We don't allow assigning names to void type
672 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000673 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000674
675 SymbolTable &ST = inFunctionScope() ?
676 CurFun.CurrentFunction->getSymbolTable() :
677 CurModule.CurrentModule->getSymbolTable();
678
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000679 // Inserting a name that is already defined???
680 if (Type *Existing = ST.lookupType(Name)) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000681 // There is only one case where this is allowed: when we are refining an
682 // opaque type. In this case, Existing will be an opaque type.
683 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
684 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000685 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000686 return true;
687 }
688
689 // Otherwise, this is an attempt to redefine a type. That's okay if
690 // the redefinition is identical to the original. This will be so if
691 // Existing and T point to the same Type object. In this one case we
692 // allow the equivalent redefinition.
693 if (Existing == T) return true; // Yes, it's equal.
694
695 // Any other kind of (non-equivalent) redefinition is an error.
696 ThrowException("Redefinition of type named '" + Name + "' in the '" +
697 T->getDescription() + "' type plane!");
698 }
699
700 // Okay, its a newly named type. Set its name.
Chris Lattner8ca2dc02004-07-09 16:43:55 +0000701 if (!Name.empty()) ST.insert(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000702
Reid Spencer75719bf2004-05-26 21:48:31 +0000703 return false;
704}
Chris Lattner8896eda2001-07-09 19:38:36 +0000705
Chris Lattner30c89792001-09-07 16:35:17 +0000706//===----------------------------------------------------------------------===//
707// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000708//
Chris Lattner8896eda2001-07-09 19:38:36 +0000709
Chris Lattner3b073862004-02-09 03:19:29 +0000710// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000711//
712static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000713 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
714}
715
716namespace {
717 struct UpRefRecord {
718 // NestingLevel - The number of nesting levels that need to be popped before
719 // this type is resolved.
720 unsigned NestingLevel;
721
722 // LastContainedTy - This is the type at the current binding level for the
723 // type. Every time we reduce the nesting level, this gets updated.
724 const Type *LastContainedTy;
725
726 // UpRefTy - This is the actual opaque type that the upreference is
727 // represented with.
728 OpaqueType *UpRefTy;
729
730 UpRefRecord(unsigned NL, OpaqueType *URTy)
731 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
732 };
Chris Lattner30c89792001-09-07 16:35:17 +0000733}
Chris Lattner698b56e2001-07-20 19:15:08 +0000734
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000735// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000736static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000737
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000738/// HandleUpRefs - Every time we finish a new layer of types, this function is
739/// called. It loops through the UpRefs vector, which is a list of the
740/// currently active types. For each type, if the up reference is contained in
741/// the newly completed type, we decrement the level count. When the level
742/// count reaches zero, the upreferenced type is the type that is passed in:
743/// thus we can complete the cycle.
744///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000745static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000746 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000747 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000748 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000749 "' newly formed. Resolving upreferences.\n" <<
750 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000751
752 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
753 // to zero), we resolve them all together before we resolve them to Ty. At
754 // the end of the loop, if there is anything to resolve to Ty, it will be in
755 // this variable.
756 OpaqueType *TypeToResolve = 0;
757
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000758 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000759 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000760 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000761 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000762 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
763 // Decrement level of upreference
764 unsigned Level = --UpRefs[i].NestingLevel;
765 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000766 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000767 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000768 if (!TypeToResolve) {
769 TypeToResolve = UpRefs[i].UpRefTy;
770 } else {
771 UR_OUT(" * Resolving upreference for "
772 << UpRefs[i].second->getDescription() << "\n";
773 std::string OldName = UpRefs[i].UpRefTy->getDescription());
774 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
775 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
776 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
777 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000778 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
779 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000780 }
781 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000782 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000783
Chris Lattner026a8ce2004-02-09 18:53:54 +0000784 if (TypeToResolve) {
785 UR_OUT(" * Resolving upreference for "
786 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000787 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000788 TypeToResolve->refineAbstractTypeTo(Ty);
789 }
790
Chris Lattner8896eda2001-07-09 19:38:36 +0000791 return Ty;
792}
793
Chris Lattner30c89792001-09-07 16:35:17 +0000794
Chris Lattner00950542001-06-06 20:29:01 +0000795//===----------------------------------------------------------------------===//
796// RunVMAsmParser - Define an interface to this parser
797//===----------------------------------------------------------------------===//
798//
Chris Lattner86427632004-07-14 06:39:48 +0000799Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000800 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000801 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000802 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000803 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000804
Chris Lattner75f20532003-04-22 18:02:52 +0000805 // Allocate a new module to read
806 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000807
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000808 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000809
Chris Lattner00950542001-06-06 20:29:01 +0000810 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000811
812 // Check to see if they called va_start but not va_arg..
813 if (!ObsoleteVarArgs)
814 if (Function *F = Result->getNamedFunction("llvm.va_start"))
815 if (F->asize() == 1) {
816 std::cerr << "WARNING: this file uses obsolete features. "
817 << "Assemble and disassemble to update it.\n";
818 ObsoleteVarArgs = true;
819 }
820
Chris Lattner99e7ab72003-10-18 05:53:13 +0000821 if (ObsoleteVarArgs) {
822 // If the user is making use of obsolete varargs intrinsics, adjust them for
823 // the user.
824 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
825 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
826
827 const Type *RetTy = F->getFunctionType()->getParamType(0);
828 RetTy = cast<PointerType>(RetTy)->getElementType();
829 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
830
831 while (!F->use_empty()) {
832 CallInst *CI = cast<CallInst>(F->use_back());
833 Value *V = new CallInst(NF, "", CI);
834 new StoreInst(V, CI->getOperand(1), CI);
835 CI->getParent()->getInstList().erase(CI);
836 }
837 Result->getFunctionList().erase(F);
838 }
839
840 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
841 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
842 const Type *ArgTy = F->getFunctionType()->getParamType(0);
843 ArgTy = cast<PointerType>(ArgTy)->getElementType();
844 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
845 ArgTy, 0);
846
847 while (!F->use_empty()) {
848 CallInst *CI = cast<CallInst>(F->use_back());
849 Value *V = new LoadInst(CI->getOperand(1), "", CI);
850 new CallInst(NF, V, "", CI);
851 CI->getParent()->getInstList().erase(CI);
852 }
853 Result->getFunctionList().erase(F);
854 }
855
856 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
857 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
858 const Type *ArgTy = F->getFunctionType()->getParamType(0);
859 ArgTy = cast<PointerType>(ArgTy)->getElementType();
860 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
861 ArgTy, 0);
862
863 while (!F->use_empty()) {
864 CallInst *CI = cast<CallInst>(F->use_back());
865 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
866 new StoreInst(V, CI->getOperand(1), CI);
867 CI->getParent()->getInstList().erase(CI);
868 }
869 Result->getFunctionList().erase(F);
870 }
871 }
872
Chris Lattner00950542001-06-06 20:29:01 +0000873 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
874 ParserResult = 0;
875
876 return Result;
877}
878
879%}
880
881%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000882 llvm::Module *ModuleVal;
883 llvm::Function *FunctionVal;
884 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
885 llvm::BasicBlock *BasicBlockVal;
886 llvm::TerminatorInst *TermInstVal;
887 llvm::Instruction *InstVal;
888 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000889
Brian Gaeked0fde302003-11-11 22:41:34 +0000890 const llvm::Type *PrimType;
891 llvm::PATypeHolder *TypeVal;
892 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000893
Brian Gaeked0fde302003-11-11 22:41:34 +0000894 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
895 std::vector<llvm::Value*> *ValueList;
896 std::list<llvm::PATypeHolder> *TypeList;
897 std::list<std::pair<llvm::Value*,
898 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
899 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
900 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000901
Brian Gaeked0fde302003-11-11 22:41:34 +0000902 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000903 int64_t SInt64Val;
904 uint64_t UInt64Val;
905 int SIntVal;
906 unsigned UIntVal;
907 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000908 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000909
Chris Lattner30c89792001-09-07 16:35:17 +0000910 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000911 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000912
Brian Gaeked0fde302003-11-11 22:41:34 +0000913 llvm::Instruction::BinaryOps BinaryOpVal;
914 llvm::Instruction::TermOps TermOpVal;
915 llvm::Instruction::MemoryOps MemOpVal;
916 llvm::Instruction::OtherOps OtherOpVal;
917 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000918}
919
Chris Lattner79df7c02002-03-26 18:01:55 +0000920%type <ModuleVal> Module FunctionList
921%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000922%type <BasicBlockVal> BasicBlock InstructionList
923%type <TermInstVal> BBTerminatorInst
924%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000925%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000926%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000927%type <ArgList> ArgList ArgListH
928%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000929%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000930%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000931%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000932%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000933%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000934%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000935%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000936%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000937%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000938
Chris Lattner2079fde2001-10-13 06:41:08 +0000939// ValueRef - Unresolved reference to a definition or BB
940%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000941%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000942// Tokens and types for handling constant integer values
943//
944// ESINT64VAL - A negative number within long long range
945%token <SInt64Val> ESINT64VAL
946
947// EUINT64VAL - A positive number within uns. long long range
948%token <UInt64Val> EUINT64VAL
949%type <SInt64Val> EINT64VAL
950
951%token <SIntVal> SINTVAL // Signed 32 bit ints...
952%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
953%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000954%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000955
956// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000957%type <TypeVal> Types TypesV UpRTypes UpRTypesV
958%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000959%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
960%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000961
Chris Lattner6c23f572003-08-22 05:42:10 +0000962%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
963%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000964
965
Chris Lattner91ef4602004-03-31 03:49:47 +0000966%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000967%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000968%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000969%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000970
971// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000972%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000973
Chris Lattner00950542001-06-06 20:29:01 +0000974// Binary Operators
975%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000976%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000977%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000978%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000979
980// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000981%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000982
Chris Lattner027dcc52001-07-08 21:10:27 +0000983// Other Operators
984%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000985%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000986%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000987
Chris Lattner00950542001-06-06 20:29:01 +0000988%start Module
989%%
990
991// Handle constant integer size restriction and conversion...
992//
Chris Lattner51727be2002-06-04 21:58:56 +0000993INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000994INTVAL : UINTVAL {
995 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
996 ThrowException("Value too large for type!");
997 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000998};
Chris Lattner00950542001-06-06 20:29:01 +0000999
1000
Chris Lattner51727be2002-06-04 21:58:56 +00001001EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +00001002EINT64VAL : EUINT64VAL {
1003 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1004 ThrowException("Value too large for type!");
1005 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +00001006};
Chris Lattner00950542001-06-06 20:29:01 +00001007
Chris Lattner00950542001-06-06 20:29:01 +00001008// Operations that are notably excluded from this list include:
1009// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1010//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001011ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1012LogicalOps : AND | OR | XOR;
1013SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
1014BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
1015
Chris Lattner51727be2002-06-04 21:58:56 +00001016ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001017
Chris Lattnere98dda62001-07-14 06:10:16 +00001018// These are some types that allow classification if we only want a particular
1019// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001020SIntType : LONG | INT | SHORT | SBYTE;
1021UIntType : ULONG | UINT | USHORT | UBYTE;
1022IntType : SIntType | UIntType;
1023FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001024
Chris Lattnere98dda62001-07-14 06:10:16 +00001025// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001026OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001027 $$ = $1;
1028 }
1029 | /*empty*/ {
1030 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001031 };
Chris Lattner00950542001-06-06 20:29:01 +00001032
Chris Lattner4ad02e72003-04-16 20:28:45 +00001033OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1034 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001035 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001036 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1037 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001038
1039//===----------------------------------------------------------------------===//
1040// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001041// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001042// access to it, a user must explicitly use TypesV.
1043//
1044
1045// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001046TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1047UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001048
1049Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001050 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001051 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1052 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001053 };
Chris Lattner30c89792001-09-07 16:35:17 +00001054
1055
1056// Derived types are added later...
1057//
Chris Lattner51727be2002-06-04 21:58:56 +00001058PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1059PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001060UpRTypes : OPAQUE {
1061 $$ = new PATypeHolder(OpaqueType::get());
1062 }
1063 | PrimType {
1064 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001065 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001066UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001067 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001068};
Chris Lattner30c89792001-09-07 16:35:17 +00001069
Chris Lattner30c89792001-09-07 16:35:17 +00001070// Include derived types in the Types production.
1071//
1072UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001073 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001074 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001075 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001076 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001077 UR_OUT("New Upreference!\n");
1078 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001079 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001080 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001081 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001082 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001083 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1084 if (isVarArg) Params.pop_back();
1085
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001086 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001087 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001088 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001089 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001090 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001091 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001092 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001093 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001094 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001095 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001096 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001097 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001098
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001099 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001100 delete $2;
1101 }
1102 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001103 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001104 }
1105 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001106 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001107 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001108 };
Chris Lattner30c89792001-09-07 16:35:17 +00001109
Chris Lattner7e708292002-06-25 16:13:24 +00001110// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001111// declaration type lists
1112//
1113TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001114 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001115 $$->push_back(*$1); delete $1;
1116 }
1117 | TypeListI ',' UpRTypes {
1118 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001119 };
Chris Lattner30c89792001-09-07 16:35:17 +00001120
Chris Lattner7e708292002-06-25 16:13:24 +00001121// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001122ArgTypeListI : TypeListI
1123 | TypeListI ',' DOTDOTDOT {
1124 ($$=$1)->push_back(Type::VoidTy);
1125 }
1126 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001127 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001128 }
1129 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001130 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001131 };
Chris Lattner30c89792001-09-07 16:35:17 +00001132
Chris Lattnere98dda62001-07-14 06:10:16 +00001133// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001134// production is used ONLY to represent constants that show up AFTER a 'const',
1135// 'constant' or 'global' token at global scope. Constants that can be inlined
1136// into other expressions (such as integers and constexprs) are handled by the
1137// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001138//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001139ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001140 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001141 if (ATy == 0)
1142 ThrowException("Cannot make array constant with type: '" +
1143 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001144 const Type *ETy = ATy->getElementType();
1145 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001146
Chris Lattner30c89792001-09-07 16:35:17 +00001147 // Verify that we have the correct size...
1148 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001149 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001150 utostr($3->size()) + " arguments, but has size of " +
1151 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001152
Chris Lattner30c89792001-09-07 16:35:17 +00001153 // Verify all elements are correct type!
1154 for (unsigned i = 0; i < $3->size(); i++) {
1155 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001156 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001157 ETy->getDescription() +"' as required!\nIt is of type '"+
1158 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001159 }
1160
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001161 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001162 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001163 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001164 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001165 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001166 if (ATy == 0)
1167 ThrowException("Cannot make array constant with type: '" +
1168 (*$1)->getDescription() + "'!");
1169
1170 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001171 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001172 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001173 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001174 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001175 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001176 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001177 | Types 'c' STRINGCONSTANT {
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
Chris Lattner30c89792001-09-07 16:35:17 +00001183 int NumElements = ATy->getNumElements();
1184 const Type *ETy = ATy->getElementType();
1185 char *EndStr = UnEscapeLexed($3, true);
1186 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001187 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001188 itostr((int)(EndStr-$3)) +
1189 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001190 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001191 if (ETy == Type::SByteTy) {
1192 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001193 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001194 } else if (ETy == Type::UByteTy) {
1195 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001196 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001197 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001198 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001199 ThrowException("Cannot build string arrays of non byte sized elements!");
1200 }
Chris Lattner30c89792001-09-07 16:35:17 +00001201 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001202 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001203 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001204 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001205 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001206 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001207 if (STy == 0)
1208 ThrowException("Cannot make struct constant with type: '" +
1209 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001210
Chris Lattnerc6212f12003-05-21 16:06:56 +00001211 if ($3->size() != STy->getNumContainedTypes())
1212 ThrowException("Illegal number of initializers for structure type!");
1213
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001214 // Check to ensure that constants are compatible with the type initializer!
1215 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001216 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001217 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001218 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001219 "' for element #" + utostr(i) +
1220 " of structure initializer!");
1221
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001222 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001223 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001224 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001225 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001226 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001227 if (STy == 0)
1228 ThrowException("Cannot make struct constant with type: '" +
1229 (*$1)->getDescription() + "'!");
1230
1231 if (STy->getNumContainedTypes() != 0)
1232 ThrowException("Illegal number of initializers for structure type!");
1233
1234 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1235 delete $1;
1236 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001237 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001238 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001239 if (PTy == 0)
1240 ThrowException("Cannot make null pointer constant with type: '" +
1241 (*$1)->getDescription() + "'!");
1242
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001243 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001244 delete $1;
1245 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001246 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001247 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001248 if (Ty == 0)
1249 ThrowException("Global const reference must be a pointer type!");
1250
Chris Lattner3101c252002-08-15 17:58:33 +00001251 // ConstExprs can exist in the body of a function, thus creating
1252 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1253 // the context of a function, getValNonImprovising will search the functions
1254 // symbol table instead of the module symbol table for the global symbol,
1255 // which throws things all off. To get around this, we just tell
1256 // getValNonImprovising that we are at global scope here.
1257 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001258 Function *SavedCurFn = CurFun.CurrentFunction;
1259 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001260
Chris Lattner2079fde2001-10-13 06:41:08 +00001261 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001262
Chris Lattner394f2eb2003-10-16 20:12:13 +00001263 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001264
Chris Lattner2079fde2001-10-13 06:41:08 +00001265 // If this is an initializer for a constant pointer, which is referencing a
1266 // (currently) undefined variable, create a stub now that shall be replaced
1267 // in the future with the right type of variable.
1268 //
1269 if (V == 0) {
1270 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1271 const PointerType *PT = cast<PointerType>(Ty);
1272
1273 // First check to see if the forward references value is already created!
1274 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001275 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001276
1277 if (I != CurModule.GlobalRefs.end()) {
1278 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001279 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001280 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001281 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001282 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001283 false,
1284 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001285 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001286 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001287
1288 // Must temporarily push this value into the module table...
1289 CurModule.CurrentModule->getGlobalList().push_back(GV);
1290 V = GV;
1291 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001292 }
1293
Chris Lattner2079fde2001-10-13 06:41:08 +00001294 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001295 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001296 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001297 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001298 | Types ConstExpr {
1299 if ($1->get() != $2->getType())
1300 ThrowException("Mismatched types for constant expression!");
1301 $$ = $2;
1302 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001303 }
1304 | Types ZEROINITIALIZER {
1305 $$ = Constant::getNullValue($1->get());
1306 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001307 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001308
Chris Lattnere43f40b2002-10-09 00:25:32 +00001309ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001310 if (!ConstantSInt::isValueValidForType($1, $2))
1311 ThrowException("Constant value doesn't fit in type!");
1312 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001313 }
1314 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001315 if (!ConstantUInt::isValueValidForType($1, $2))
1316 ThrowException("Constant value doesn't fit in type!");
1317 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001318 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001319 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001320 $$ = ConstantBool::True;
1321 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001322 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001323 $$ = ConstantBool::False;
1324 }
1325 | FPType FPVAL { // Float & Double constants
1326 $$ = ConstantFP::get($1, $2);
1327 };
1328
Chris Lattner00950542001-06-06 20:29:01 +00001329
Chris Lattnerd78700d2002-08-16 21:14:40 +00001330ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001331 if (!$3->getType()->isFirstClassType())
1332 ThrowException("cast constant expression from a non-primitive type: '" +
1333 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001334 if (!$5->get()->isFirstClassType())
1335 ThrowException("cast constant expression to a non-primitive type: '" +
1336 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001337 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001338 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001339 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001340 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1341 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001342 ThrowException("GetElementPtr requires a pointer operand!");
1343
Chris Lattner830b6f92004-04-05 01:30:04 +00001344 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1345 // indices to uint struct indices for compatibility.
1346 generic_gep_type_iterator<std::vector<Value*>::iterator>
1347 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1348 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1349 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1350 if (isa<StructType>(*GTI)) // Only change struct indices
1351 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1352 if (CUI->getType() == Type::UByteTy)
1353 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1354
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001355 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001356 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001357 if (!IdxTy)
1358 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001359
Chris Lattner9232b992003-04-22 18:42:41 +00001360 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001361 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1362 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001363 IdxVec.push_back(C);
1364 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001365 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001366
Chris Lattnerd78700d2002-08-16 21:14:40 +00001367 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001368
Chris Lattnerd78700d2002-08-16 21:14:40 +00001369 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001370 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001371 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1372 if ($3->getType() != Type::BoolTy)
1373 ThrowException("Select condition must be of boolean type!");
1374 if ($5->getType() != $7->getType())
1375 ThrowException("Select operand types must match!");
1376 $$ = ConstantExpr::getSelect($3, $5, $7);
1377 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001378 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001379 if ($3->getType() != $5->getType())
1380 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001381 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001382 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001383 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001384 if ($5->getType() != Type::UByteTy)
1385 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001386 if (!$3->getType()->isInteger())
1387 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001388 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001389 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001390
1391
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001392// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001393ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001394 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001395 }
1396 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001397 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001398 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001399 };
Chris Lattner00950542001-06-06 20:29:01 +00001400
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001401
Chris Lattner1781aca2001-09-18 04:00:54 +00001402// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001403GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001404
Chris Lattner00950542001-06-06 20:29:01 +00001405
Chris Lattner0e73ce62002-05-02 19:11:13 +00001406//===----------------------------------------------------------------------===//
1407// Rules to match Modules
1408//===----------------------------------------------------------------------===//
1409
1410// Module rule: Capture the result of parsing the whole file into a result
1411// variable...
1412//
1413Module : FunctionList {
1414 $$ = ParserResult = $1;
1415 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001416};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001417
Chris Lattner7e708292002-06-25 16:13:24 +00001418// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001419//
1420FunctionList : FunctionList Function {
1421 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001422 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001423 }
1424 | FunctionList FunctionProto {
1425 $$ = $1;
1426 }
1427 | FunctionList IMPLEMENTATION {
1428 $$ = $1;
1429 }
1430 | ConstPool {
1431 $$ = CurModule.CurrentModule;
1432 // Resolve circular types before we parse the body of the module
1433 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001434 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001435
Chris Lattnere98dda62001-07-14 06:10:16 +00001436// ConstPool - Constants with optional names assigned to them.
Chris Lattnere0026942004-07-14 06:44:56 +00001437ConstPool : ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001438 // Eagerly resolve types. This is not an optimization, this is a
1439 // requirement that is due to the fact that we could have this:
1440 //
1441 // %list = type { %list * }
1442 // %list = type { %list * } ; repeated type decl
1443 //
1444 // If types are not resolved eagerly, then the two types will not be
1445 // determined to be the same type!
1446 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001447 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001448
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001449 if (!setTypeName(*$4, $2) && !$2) {
1450 // If this is a named type that is not a redefinition, add it to the slot
1451 // table.
Chris Lattner64116f92004-07-14 01:33:11 +00001452 if (inFunctionScope())
1453 CurFun.Types.push_back(*$4);
1454 else
1455 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001456 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001457
1458 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001459 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001460 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001461 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001462 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001463 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1464 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Chris Lattner1781aca2001-09-18 04:00:54 +00001465 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001466 | ConstPool OptAssign EXTERNAL GlobalType Types {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001467 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001468 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001469 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001470 | ConstPool TARGET TargetDefinition {
1471 }
Chris Lattner00950542001-06-06 20:29:01 +00001472 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001473 };
Chris Lattner00950542001-06-06 20:29:01 +00001474
1475
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001476
1477BigOrLittle : BIG { $$ = Module::BigEndian; };
1478BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1479
1480TargetDefinition : ENDIAN '=' BigOrLittle {
1481 CurModule.CurrentModule->setEndianness($3);
1482 }
1483 | POINTERSIZE '=' EUINT64VAL {
1484 if ($3 == 32)
1485 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1486 else if ($3 == 64)
1487 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1488 else
1489 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1490 };
1491
1492
Chris Lattner00950542001-06-06 20:29:01 +00001493//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001494// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001495//===----------------------------------------------------------------------===//
1496
Chris Lattner6c23f572003-08-22 05:42:10 +00001497Name : VAR_ID | STRINGCONSTANT;
1498OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001499
Chris Lattner6c23f572003-08-22 05:42:10 +00001500ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001501 if (*$1 == Type::VoidTy)
1502 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001503 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001504};
Chris Lattner00950542001-06-06 20:29:01 +00001505
Chris Lattner69da5cf2002-10-13 20:57:00 +00001506ArgListH : ArgListH ',' ArgVal {
1507 $$ = $1;
1508 $1->push_back(*$3);
1509 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001510 }
1511 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001512 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001513 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001514 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001515 };
Chris Lattner00950542001-06-06 20:29:01 +00001516
1517ArgList : ArgListH {
1518 $$ = $1;
1519 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001520 | ArgListH ',' DOTDOTDOT {
1521 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001522 $$->push_back(std::pair<PATypeHolder*,
1523 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001524 }
1525 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001526 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1527 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001528 }
Chris Lattner00950542001-06-06 20:29:01 +00001529 | /* empty */ {
1530 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001531 };
Chris Lattner00950542001-06-06 20:29:01 +00001532
Chris Lattner6c23f572003-08-22 05:42:10 +00001533FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001534 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001535 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001536
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001537 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1538 ThrowException("LLVM functions cannot return aggregate types!");
1539
Chris Lattner9232b992003-04-22 18:42:41 +00001540 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001541 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001542 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001543 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001544 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001545 }
Chris Lattner00950542001-06-06 20:29:01 +00001546
Chris Lattner2079fde2001-10-13 06:41:08 +00001547 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1548 if (isVarArg) ParamTypeList.pop_back();
1549
Chris Lattner1f862af2003-04-16 18:13:57 +00001550 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001551 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001552 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001553
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001554 // Is the function already in symtab?
Chris Lattner66028f22004-07-14 07:12:48 +00001555 if (CurModule.CurrentModule->getFunction(FunctionName, FT))
1556 ThrowException("Redefinition of function '" + FunctionName + "'!");
Chris Lattner5659dd12002-07-15 00:10:33 +00001557
Chris Lattner66028f22004-07-14 07:12:48 +00001558 Function *Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1559 CurModule.CurrentModule);
1560 InsertValue(Fn, CurModule.Values);
1561 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattner1f862af2003-04-16 18:13:57 +00001562 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001563
Chris Lattner394f2eb2003-10-16 20:12:13 +00001564 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001565
Chris Lattner7e708292002-06-25 16:13:24 +00001566 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001567 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001568 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001569 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001570 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001571 delete $4->back().first;
1572 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001573 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001574 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001575 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001576 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001577 delete I->first; // Delete the typeholder...
1578
Chris Lattner8ab406d2004-07-13 07:59:27 +00001579 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001580 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001581 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001582
Chris Lattner1f862af2003-04-16 18:13:57 +00001583 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001584 }
Chris Lattner51727be2002-06-04 21:58:56 +00001585};
Chris Lattner00950542001-06-06 20:29:01 +00001586
Chris Lattner9b02cc32002-05-03 18:23:48 +00001587BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1588
Chris Lattner4ad02e72003-04-16 20:28:45 +00001589FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001590 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001591
Chris Lattner4ad02e72003-04-16 20:28:45 +00001592 // Make sure that we keep track of the linkage type even if there was a
1593 // previous "declare".
1594 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001595
Chris Lattner7e708292002-06-25 16:13:24 +00001596 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001597 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001598};
Chris Lattner00950542001-06-06 20:29:01 +00001599
Chris Lattner9b02cc32002-05-03 18:23:48 +00001600END : ENDTOK | '}'; // Allow end of '}' to end a function
1601
Chris Lattner79df7c02002-03-26 18:01:55 +00001602Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001603 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001604};
Chris Lattner00950542001-06-06 20:29:01 +00001605
Chris Lattner394f2eb2003-10-16 20:12:13 +00001606FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1607 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001608 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001609};
Chris Lattner00950542001-06-06 20:29:01 +00001610
1611//===----------------------------------------------------------------------===//
1612// Rules to match Basic Blocks
1613//===----------------------------------------------------------------------===//
1614
1615ConstValueRef : ESINT64VAL { // A reference to a direct constant
1616 $$ = ValID::create($1);
1617 }
1618 | EUINT64VAL {
1619 $$ = ValID::create($1);
1620 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001621 | FPVAL { // Perhaps it's an FP constant?
1622 $$ = ValID::create($1);
1623 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001624 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001625 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001626 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001627 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001628 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001629 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001630 | NULL_TOK {
1631 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001632 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001633 | ConstExpr {
1634 $$ = ValID::create($1);
1635 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001636
Chris Lattner2079fde2001-10-13 06:41:08 +00001637// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1638// another value.
1639//
1640SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001641 $$ = ValID::create($1);
1642 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001643 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001644 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001645 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001646
1647// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001648ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001649
Chris Lattner00950542001-06-06 20:29:01 +00001650
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001651// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1652// type immediately preceeds the value reference, and allows complex constant
1653// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001654ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001655 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001656 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001657
Chris Lattner00950542001-06-06 20:29:01 +00001658BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001659 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001660 }
Chris Lattner7e708292002-06-25 16:13:24 +00001661 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001662 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001663 };
Chris Lattner00950542001-06-06 20:29:01 +00001664
1665
1666// Basic blocks are terminated by branching instructions:
1667// br, br/cc, switch, ret
1668//
Chris Lattner2079fde2001-10-13 06:41:08 +00001669BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001670 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001671 InsertValue($3);
1672
1673 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001674 InsertValue($1);
1675 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001676 };
Chris Lattner00950542001-06-06 20:29:01 +00001677
1678InstructionList : InstructionList Inst {
1679 $1->getInstList().push_back($2);
1680 $$ = $1;
1681 }
1682 | /* empty */ {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001683 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner22ae2322004-07-13 08:39:15 +00001684 }
1685 | LABELSTR {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001686 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner51727be2002-06-04 21:58:56 +00001687 };
Chris Lattner00950542001-06-06 20:29:01 +00001688
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001689BBTerminatorInst : RET ResolvedVal { // Return with a result...
1690 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001691 }
1692 | RET VOID { // Return with no result...
1693 $$ = new ReturnInst();
1694 }
1695 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001696 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001697 } // Conditional Branch...
1698 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001699 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001700 }
1701 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001702 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner00950542001-06-06 20:29:01 +00001703 $$ = S;
1704
Chris Lattner9232b992003-04-22 18:42:41 +00001705 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001706 E = $8->end();
1707 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001708 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001709 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001710 }
Chris Lattner196850c2003-05-15 21:30:00 +00001711 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001712 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner196850c2003-05-15 21:30:00 +00001713 $$ = S;
1714 }
Chris Lattner64116f92004-07-14 01:33:11 +00001715 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1716 UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001717 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001718 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001719
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001720 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1721 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001722 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001723 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001724 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001725 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1726 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001727 ParamTypes.push_back((*I)->getType());
1728 }
1729
1730 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1731 if (isVarArg) ParamTypes.pop_back();
1732
Chris Lattner79df7c02002-03-26 18:01:55 +00001733 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001734 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001735 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001736
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001737 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001738
Chris Lattner64116f92004-07-14 01:33:11 +00001739 BasicBlock *Normal = getBBVal($9);
1740 BasicBlock *Except = getBBVal($12);
Chris Lattner2079fde2001-10-13 06:41:08 +00001741
1742 // Create the call node...
1743 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001744 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001745 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001746 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001747 // correctly!
1748 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001749 FunctionType::param_iterator I = Ty->param_begin();
1750 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001751 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001752
1753 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1754 if ((*ArgI)->getType() != *I)
1755 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001756 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001757
1758 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1759 ThrowException("Invalid number of parameters detected!");
1760
Chris Lattner6cdb0112001-11-26 16:54:11 +00001761 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001762 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001763 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001764 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001765 }
1766 | UNWIND {
1767 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001768 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001769
1770
Chris Lattner00950542001-06-06 20:29:01 +00001771
1772JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1773 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001774 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001775 if (V == 0)
1776 ThrowException("May only switch on a constant pool value!");
1777
Chris Lattner64116f92004-07-14 01:33:11 +00001778 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001779 }
1780 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001781 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001782 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001783
1784 if (V == 0)
1785 ThrowException("May only switch on a constant pool value!");
1786
Chris Lattner64116f92004-07-14 01:33:11 +00001787 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001788 };
Chris Lattner00950542001-06-06 20:29:01 +00001789
1790Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001791 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001792 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001793 InsertValue($2);
1794 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001795};
Chris Lattner00950542001-06-06 20:29:01 +00001796
Chris Lattnerc24d2082001-06-11 15:04:20 +00001797PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001798 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001799 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001800 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001801 }
1802 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1803 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001804 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001805 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001806 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001807
1808
Chris Lattner30c89792001-09-07 16:35:17 +00001809ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001810 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001811 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001812 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001813 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001814 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001815 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001816 };
Chris Lattner00950542001-06-06 20:29:01 +00001817
1818// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001819ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001820
Chris Lattner4a6482b2002-09-10 19:57:26 +00001821InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1822 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1823 ThrowException("Arithmetic operator requires integer or FP operands!");
1824 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1825 if ($$ == 0)
1826 ThrowException("binary operator returned null!");
1827 delete $2;
1828 }
1829 | LogicalOps Types ValueRef ',' ValueRef {
1830 if (!(*$2)->isIntegral())
1831 ThrowException("Logical operator requires integral operands!");
1832 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1833 if ($$ == 0)
1834 ThrowException("binary operator returned null!");
1835 delete $2;
1836 }
1837 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001838 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001839 if ($$ == 0)
1840 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001841 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001842 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001843 | NOT ResolvedVal {
1844 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1845 << " Replacing with 'xor'.\n";
1846
1847 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1848 if (Ones == 0)
1849 ThrowException("Expected integral type for not instruction!");
1850
1851 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001852 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001853 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001854 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001855 | ShiftOps ResolvedVal ',' ResolvedVal {
1856 if ($4->getType() != Type::UByteTy)
1857 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001858 if (!$2->getType()->isInteger())
1859 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001860 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001861 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001862 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001863 if (!$4->get()->isFirstClassType())
1864 ThrowException("cast instruction to a non-primitive type: '" +
1865 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001866 $$ = new CastInst($2, *$4);
1867 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001868 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001869 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1870 if ($2->getType() != Type::BoolTy)
1871 ThrowException("select condition must be boolean!");
1872 if ($4->getType() != $6->getType())
1873 ThrowException("select value types should match!");
1874 $$ = new SelectInst($2, $4, $6);
1875 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001876 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001877 // FIXME: This is emulation code for an obsolete syntax. This should be
1878 // removed at some point.
1879 if (!ObsoleteVarArgs) {
1880 std::cerr << "WARNING: this file uses obsolete features. "
1881 << "Assemble and disassemble to update it.\n";
1882 ObsoleteVarArgs = true;
1883 }
1884
1885 // First, load the valist...
1886 Instruction *CurVAList = new LoadInst($2, "");
1887 CurBB->getInstList().push_back(CurVAList);
1888
1889 // Emit the vaarg instruction.
1890 $$ = new VAArgInst(CurVAList, *$4);
1891
1892 // Now we must advance the pointer and update it in memory.
1893 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1894 CurBB->getInstList().push_back(TheVANext);
1895
1896 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1897 delete $4;
1898 }
1899 | VAARG ResolvedVal ',' Types {
1900 $$ = new VAArgInst($2, *$4);
1901 delete $4;
1902 }
1903 | VANEXT ResolvedVal ',' Types {
1904 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001905 delete $4;
1906 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001907 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001908 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001909 if (!Ty->isFirstClassType())
1910 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001911 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001912 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001913 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001914 if ($2->front().first->getType() != Ty)
1915 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001916 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001917 $2->pop_front();
1918 }
1919 delete $2; // Free the list...
1920 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001921 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001922 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001923 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001924
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001925 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1926 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001927 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001928 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001929 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001930 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1931 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001932 ParamTypes.push_back((*I)->getType());
1933 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001934
1935 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1936 if (isVarArg) ParamTypes.pop_back();
1937
Chris Lattner79df7c02002-03-26 18:01:55 +00001938 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001939 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001940 }
Chris Lattner00950542001-06-06 20:29:01 +00001941
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001942 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001943
Chris Lattner8b81bf52001-07-25 22:47:46 +00001944 // Create the call node...
1945 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001946 // Make sure no arguments is a good thing!
1947 if (Ty->getNumParams() != 0)
1948 ThrowException("No arguments passed to a function that "
1949 "expects arguments!");
1950
Chris Lattner9232b992003-04-22 18:42:41 +00001951 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001952 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001953 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001954 // correctly!
1955 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001956 FunctionType::param_iterator I = Ty->param_begin();
1957 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001958 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001959
1960 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1961 if ((*ArgI)->getType() != *I)
1962 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001963 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001964
Chris Lattner8b81bf52001-07-25 22:47:46 +00001965 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001966 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001967
Chris Lattner6cdb0112001-11-26 16:54:11 +00001968 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001969 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001970 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001971 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001972 }
1973 | MemoryInst {
1974 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001975 };
Chris Lattner00950542001-06-06 20:29:01 +00001976
Chris Lattner6cdb0112001-11-26 16:54:11 +00001977
1978// IndexList - List of indices for GEP based instructions...
1979IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001980 $$ = $2;
1981 } | /* empty */ {
1982 $$ = new std::vector<Value*>();
1983 };
1984
1985OptVolatile : VOLATILE {
1986 $$ = true;
1987 }
1988 | /* empty */ {
1989 $$ = false;
1990 };
1991
Chris Lattner027dcc52001-07-08 21:10:27 +00001992
Chris Lattner00950542001-06-06 20:29:01 +00001993MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001994 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001995 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001996 }
1997 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001998 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001999 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002000 }
2001 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002002 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002003 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002004 }
2005 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002006 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002007 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002008 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002009 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002010 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002011 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002012 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002013 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002014 }
2015
Chris Lattner15c9c032003-09-08 18:20:29 +00002016 | OptVolatile LOAD Types ValueRef {
2017 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002018 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002019 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002020 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002021 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002022 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002023 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2024 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002025 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002026 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002027 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002028 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002029 if (ElTy != $3->getType())
2030 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002031 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002032
Chris Lattner79ad13802003-09-08 20:29:46 +00002033 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002034 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002035 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002036 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002037 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002038 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002039
2040 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2041 // indices to uint struct indices for compatibility.
2042 generic_gep_type_iterator<std::vector<Value*>::iterator>
2043 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2044 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2045 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2046 if (isa<StructType>(*GTI)) // Only change struct indices
2047 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2048 if (CUI->getType() == Type::UByteTy)
2049 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2050
Chris Lattner30c89792001-09-07 16:35:17 +00002051 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002052 ThrowException("Invalid getelementptr indices for type '" +
2053 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002054 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2055 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002056 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002057
Brian Gaeked0fde302003-11-11 22:41:34 +00002058
Chris Lattner00950542001-06-06 20:29:01 +00002059%%
Chris Lattner09083092001-07-08 04:57:15 +00002060int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002061 std::string where
2062 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002063 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002064 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002065 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002066 errMsg += "end-of-file.";
2067 else
Chris Lattner9232b992003-04-22 18:42:41 +00002068 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002069 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002070 return 0;
2071}