blob: 440056de2dfd02a86544e4fbc2dd0acffb778932 [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
Reid Spencer3271ed52004-07-18 00:08:11 +000082 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000083 //
Chris Lattner9232b992003-04-22 18:42:41 +000084 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000085 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000086 GlobalRefsType GlobalRefs;
87
Chris Lattner00950542001-06-06 20:29:01 +000088 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000089 // If we could not resolve some functions at function compilation time
90 // (calls to functions before they are defined), resolve them now... Types
91 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000092 //
Chris Lattner00950542001-06-06 20:29:01 +000093 ResolveDefinitions(LateResolveValues);
94
Chris Lattner2079fde2001-10-13 06:41:08 +000095 // Check to make sure that all global value forward references have been
96 // resolved!
97 //
98 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000099 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +0000100
101 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
102 I != E; ++I) {
103 UndefinedReferences += " " + I->first.first->getDescription() + " " +
104 I->first.second.getName() + "\n";
105 }
106 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000107 }
108
Chris Lattner7e708292002-06-25 16:13:24 +0000109 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000110 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000111 CurrentModule = 0;
112 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000113
114
Chris Lattner8a327842004-07-14 21:44:00 +0000115 // GetForwardRefForGlobal - Check to see if there is a forward reference
116 // for this global. If so, remove it from the GlobalRefs map and return it.
117 // If not, just return null.
118 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
119 // Check to see if there is a forward reference to this global variable...
120 // if there is, eliminate it and patch the reference to use the new def'n.
121 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
122 GlobalValue *Ret = 0;
123 if (I != GlobalRefs.end()) {
124 Ret = I->second;
125 GlobalRefs.erase(I);
126 }
127 return Ret;
128 }
Chris Lattner00950542001-06-06 20:29:01 +0000129} CurModule;
130
Chris Lattner79df7c02002-03-26 18:01:55 +0000131static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000132 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000133
Chris Lattner735f2702004-07-08 22:30:50 +0000134 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
135 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000136 std::vector<PATypeHolder> Types;
137 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000138 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000139
Chris Lattner2e2ed292004-07-14 06:28:35 +0000140 /// BBForwardRefs - When we see forward references to basic blocks, keep
141 /// track of them here.
142 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
143 std::vector<BasicBlock*> NumberedBlocks;
144 unsigned NextBBNum;
145
Chris Lattner79df7c02002-03-26 18:01:55 +0000146 inline PerFunctionInfo() {
147 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000148 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000149 }
150
Chris Lattner79df7c02002-03-26 18:01:55 +0000151 inline void FunctionStart(Function *M) {
152 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000153 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000154 }
155
Chris Lattner79df7c02002-03-26 18:01:55 +0000156 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000157 NumberedBlocks.clear();
158
159 // Any forward referenced blocks left?
160 if (!BBForwardRefs.empty())
161 ThrowException("Undefined reference to label " +
162 BBForwardRefs.begin()->second.first.getName());
163
164 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000165 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000166
Chris Lattner7e708292002-06-25 16:13:24 +0000167 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000168 Types.clear(); // Clear out function local types
Chris Lattner79df7c02002-03-26 18:01:55 +0000169 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000170 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000171 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000172} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000173
Chris Lattner394f2eb2003-10-16 20:12:13 +0000174static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000175
Chris Lattner00950542001-06-06 20:29:01 +0000176
177//===----------------------------------------------------------------------===//
178// Code to handle definitions of all the types
179//===----------------------------------------------------------------------===//
180
Chris Lattner735f2702004-07-08 22:30:50 +0000181static int InsertValue(Value *V,
182 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
183 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000184
185 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000186 ValueList &List = ValueTab[V->getType()];
187 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000188 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000189}
190
Chris Lattner30c89792001-09-07 16:35:17 +0000191static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000192 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000193 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000194 // Module constants occupy the lowest numbered slots...
Chris Lattnera09000d2004-07-14 23:03:46 +0000195 if ((unsigned)D.Num < CurModule.Types.size())
196 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000197 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000198 case ValID::NameVal: // Is it a named definition?
199 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
200 D.destroy(); // Free old strdup'd memory...
201 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000202 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000203 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000204 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000205 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000206 }
207
208 // If we reached here, we referenced either a symbol that we don't know about
209 // or an id number that hasn't been read yet. We may be referencing something
210 // forward, so just create an entry to be resolved later and get to it...
211 //
212 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
213
Chris Lattner9232b992003-04-22 18:42:41 +0000214 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000215 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000216
Chris Lattner9232b992003-04-22 18:42:41 +0000217 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000218 if (I != LateResolver.end()) {
219 return I->second;
220 }
Chris Lattner30c89792001-09-07 16:35:17 +0000221
Chris Lattner82269592001-10-22 06:01:08 +0000222 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000223 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000224 return Typ;
225}
226
Chris Lattner9232b992003-04-22 18:42:41 +0000227static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000228 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000229 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000230 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000231 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000232}
233
Chris Lattner2079fde2001-10-13 06:41:08 +0000234// getValNonImprovising - Look up the value specified by the provided type and
235// the provided ValID. If the value exists and has already been defined, return
236// it. Otherwise return null.
237//
238static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000239 if (isa<FunctionType>(Ty))
240 ThrowException("Functions are not values and "
241 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000242
Chris Lattner30c89792001-09-07 16:35:17 +0000243 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000244 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000245 unsigned Num = (unsigned)D.Num;
246
247 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000248 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000249 if (VI != CurModule.Values.end()) {
250 if (Num < VI->second.size())
251 return VI->second[Num];
252 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000253 }
254
255 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000256 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000257 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000258
259 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000260 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000261
Chris Lattner16af11d2004-02-09 21:03:38 +0000262 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000263 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000264
Chris Lattner1a1cb112001-09-30 22:46:54 +0000265 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000266 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000267 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000268
269 D.destroy(); // Free old strdup'd memory...
270 return N;
271 }
272
Chris Lattner2079fde2001-10-13 06:41:08 +0000273 // Check to make sure that "Ty" is an integral type, and that our
274 // value will fit into the specified type...
275 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000276 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
277 ThrowException("Signed integral constant '" +
278 itostr(D.ConstPool64) + "' is invalid for type '" +
279 Ty->getDescription() + "'!");
280 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000281
282 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000283 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
284 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000285 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000286 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000287 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000288 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000289 }
290 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000291 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000292 }
293
Chris Lattner2079fde2001-10-13 06:41:08 +0000294 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000295 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000296 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000297 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000298
299 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000300 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000301 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000302 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000303
Chris Lattnerd78700d2002-08-16 21:14:40 +0000304 case ValID::ConstantVal: // Fully resolved constant?
305 if (D.ConstantValue->getType() != Ty)
306 ThrowException("Constant expression type different from required type!");
307 return D.ConstantValue;
308
Chris Lattner30c89792001-09-07 16:35:17 +0000309 default:
310 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000311 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000312 } // End of switch
313
Chris Lattner2079fde2001-10-13 06:41:08 +0000314 assert(0 && "Unhandled case!");
315 return 0;
316}
317
Chris Lattner2079fde2001-10-13 06:41:08 +0000318// getVal - This function is identical to getValNonImprovising, except that if a
319// value is not already defined, it "improvises" by creating a placeholder var
320// that looks and acts just like the requested variable. When the value is
321// defined later, all uses of the placeholder variable are replaced with the
322// real thing.
323//
Chris Lattner64116f92004-07-14 01:33:11 +0000324static Value *getVal(const Type *Ty, const ValID &ID) {
325 if (Ty == Type::LabelTy)
326 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000327
Chris Lattner64116f92004-07-14 01:33:11 +0000328 // See if the value has already been defined.
329 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000330 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000331
332 // If we reached here, we referenced either a symbol that we don't know about
333 // or an id number that hasn't been read yet. We may be referencing something
334 // forward, so just create an entry to be resolved later and get to it...
335 //
Chris Lattner64116f92004-07-14 01:33:11 +0000336 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000337
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000338 // Remember where this forward reference came from. FIXME, shouldn't we try
339 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000340 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000341 llvmAsmlineno)));
342
Chris Lattner79df7c02002-03-26 18:01:55 +0000343 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000344 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000345 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000346 InsertValue(V, CurModule.LateResolveValues);
347 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000348}
349
Chris Lattner2e2ed292004-07-14 06:28:35 +0000350/// getBBVal - This is used for two purposes:
351/// * If isDefinition is true, a new basic block with the specified ID is being
352/// defined.
353/// * If isDefinition is true, this is a reference to a basic block, which may
354/// or may not be a forward reference.
355///
356static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000357 assert(inFunctionScope() && "Can't get basic block at global scope!");
358
Chris Lattner2e2ed292004-07-14 06:28:35 +0000359 std::string Name;
360 BasicBlock *BB = 0;
361 switch (ID.Type) {
362 default: ThrowException("Illegal label reference " + ID.getName());
363 case ValID::NumberVal: // Is it a numbered definition?
364 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
365 CurFun.NumberedBlocks.resize(ID.Num+1);
366 BB = CurFun.NumberedBlocks[ID.Num];
367 break;
368 case ValID::NameVal: // Is it a named definition?
369 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000370 if (Value *N = CurFun.CurrentFunction->
371 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000372 BB = cast<BasicBlock>(N);
373 break;
374 }
Chris Lattner64116f92004-07-14 01:33:11 +0000375
Chris Lattner2e2ed292004-07-14 06:28:35 +0000376 // See if the block has already been defined.
377 if (BB) {
378 // If this is the definition of the block, make sure the existing value was
379 // just a forward reference. If it was a forward reference, there will be
380 // an entry for it in the PlaceHolderInfo map.
381 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
382 // The existing value was a definition, not a forward reference.
383 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000384
Chris Lattner2e2ed292004-07-14 06:28:35 +0000385 ID.destroy(); // Free strdup'd memory.
Chris Lattnerbadf0912004-07-26 01:22:59 +0000386
387 // Make sure to move the basic block to the correct location in the
388 // function, instead of leaving it inserted wherever it was first
389 // referenced.
390 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
391 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
Chris Lattner2e2ed292004-07-14 06:28:35 +0000392 return BB;
393 }
394
395 // Otherwise this block has not been seen before.
396 BB = new BasicBlock("", CurFun.CurrentFunction);
397 if (ID.Type == ValID::NameVal) {
398 BB->setName(ID.Name);
399 } else {
400 CurFun.NumberedBlocks[ID.Num] = BB;
401 }
402
403 // If this is not a definition, keep track of it so we can use it as a forward
404 // reference.
405 if (!isDefinition) {
406 // Remember where this forward reference came from.
407 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
408 } else {
409 // The forward declaration could have been inserted anywhere in the
410 // function: insert it into the correct place now.
411 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
412 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
413 }
414
Chris Lattner64116f92004-07-14 01:33:11 +0000415 return BB;
416}
417
Chris Lattner00950542001-06-06 20:29:01 +0000418
419//===----------------------------------------------------------------------===//
420// Code to handle forward references in instructions
421//===----------------------------------------------------------------------===//
422//
423// This code handles the late binding needed with statements that reference
424// values not defined yet... for example, a forward branch, or the PHI node for
425// a loop body.
426//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000427// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000428// and back patchs after we are done.
429//
430
431// ResolveDefinitions - If we could not resolve some defs at parsing
432// time (forward branches, phi functions for loops, etc...) resolve the
433// defs now...
434//
Chris Lattner735f2702004-07-08 22:30:50 +0000435static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
436 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000437 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000438 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000439 E = LateResolvers.end(); LRI != E; ++LRI) {
440 ValueList &List = LRI->second;
441 while (!List.empty()) {
442 Value *V = List.back();
443 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000444
445 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
446 CurModule.PlaceHolderInfo.find(V);
447 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
448
449 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000450
Chris Lattner735f2702004-07-08 22:30:50 +0000451 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000452 if (TheRealValue) {
453 V->replaceAllUsesWith(TheRealValue);
454 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000455 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000456 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000457 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000458 // resolver table
459 InsertValue(V, *FutureLateResolvers);
460 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000461 if (DID.Type == ValID::NameVal)
462 ThrowException("Reference to an invalid definition: '" +DID.getName()+
463 "' of type '" + V->getType()->getDescription() + "'",
464 PHI->second.second);
465 else
466 ThrowException("Reference to an invalid definition: #" +
467 itostr(DID.Num) + " of type '" +
468 V->getType()->getDescription() + "'",
469 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000470 }
Chris Lattner00950542001-06-06 20:29:01 +0000471 }
472 }
473
474 LateResolvers.clear();
475}
476
Chris Lattner4a42e902001-10-22 05:56:09 +0000477// ResolveTypeTo - A brand new type was just declared. This means that (if
478// name is not null) things referencing Name can be resolved. Otherwise, things
479// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000480//
Chris Lattner4a42e902001-10-22 05:56:09 +0000481static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000482 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000483 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000484
Chris Lattner4a42e902001-10-22 05:56:09 +0000485 ValID D;
486 if (Name) D = ValID::create(Name);
487 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000488
Chris Lattner9232b992003-04-22 18:42:41 +0000489 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000490 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000491
Chris Lattner9232b992003-04-22 18:42:41 +0000492 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000493 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000494 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000495 LateResolver.erase(I);
496 }
497}
498
499// ResolveTypes - At this point, all types should be resolved. Any that aren't
500// are errors.
501//
Chris Lattner9232b992003-04-22 18:42:41 +0000502static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000503 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000504 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000505
506 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000507 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000508 else
Chris Lattner82269592001-10-22 06:01:08 +0000509 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000510 }
511}
512
Chris Lattner1781aca2001-09-18 04:00:54 +0000513// setValueName - Set the specified value to the name given. The name may be
514// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000515// assumed to be a malloc'd string buffer, and is free'd by this function.
516//
517static void setValueName(Value *V, char *NameStr) {
518 if (NameStr) {
519 std::string Name(NameStr); // Copy string
520 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000521
522 if (V->getType() == Type::VoidTy)
523 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000524
525 assert(inFunctionScope() && "Must be in function scope!");
526 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
527 if (ST.lookup(V->getType(), Name))
528 ThrowException("Redefinition of value named '" + Name + "' in the '" +
529 V->getType()->getDescription() + "' type plane!");
530
Chris Lattnerc9aea522004-07-13 08:12:39 +0000531 // Set the name.
532 V->setName(Name, &ST);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000533 }
534}
535
Chris Lattnerd88998d2004-07-14 08:23:52 +0000536/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
537/// this is a declaration, otherwise it is a definition.
538static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
539 bool isConstantGlobal, const Type *Ty,
540 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000541 if (isa<FunctionType>(Ty))
542 ThrowException("Cannot declare global vars of function type!");
543
Chris Lattnera09000d2004-07-14 23:03:46 +0000544 const PointerType *PTy = PointerType::get(Ty);
545
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000546 std::string Name;
547 if (NameStr) {
548 Name = NameStr; // Copy string
549 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000550 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000551
Chris Lattner8a327842004-07-14 21:44:00 +0000552 // See if this global value was forward referenced. If so, recycle the
553 // object.
554 ValID ID;
555 if (!Name.empty()) {
556 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000557 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000558 ID = ValID::create((int)CurModule.Values[PTy].size());
559 }
560
561 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
562 // Move the global to the end of the list, from whereever it was
563 // previously inserted.
564 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
565 CurModule.CurrentModule->getGlobalList().remove(GV);
566 CurModule.CurrentModule->getGlobalList().push_back(GV);
567 GV->setInitializer(Initializer);
568 GV->setLinkage(Linkage);
569 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000570 InsertValue(GV, CurModule.Values);
Chris Lattnera09000d2004-07-14 23:03:46 +0000571 return;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000572 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000573
574 // If this global has a name, check to see if there is already a definition
575 // of this global in the module. If so, merge as appropriate. Note that
576 // this is really just a hack around problems in the CFE. :(
577 if (!Name.empty()) {
578 // We are a simple redefinition of a value, check to see if it is defined
579 // the same as the old one.
580 if (GlobalVariable *EGV =
581 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
582 // We are allowed to redefine a global variable in two circumstances:
583 // 1. If at least one of the globals is uninitialized or
584 // 2. If both initializers have the same value.
585 //
586 if (!EGV->hasInitializer() || !Initializer ||
587 EGV->getInitializer() == Initializer) {
588
589 // Make sure the existing global version gets the initializer! Make
590 // sure that it also gets marked const if the new version is.
591 if (Initializer && !EGV->hasInitializer())
592 EGV->setInitializer(Initializer);
593 if (isConstantGlobal)
594 EGV->setConstant(true);
595 EGV->setLinkage(Linkage);
596 return;
597 }
598
599 ThrowException("Redefinition of global variable named '" + Name +
600 "' in the '" + Ty->getDescription() + "' type plane!");
601 }
602 }
603
604 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000605 GlobalVariable *GV =
606 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
607 CurModule.CurrentModule);
608 InsertValue(GV, CurModule.Values);
Chris Lattnerd88998d2004-07-14 08:23:52 +0000609}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000610
Reid Spencer75719bf2004-05-26 21:48:31 +0000611// setTypeName - Set the specified type to the name given. The name may be
612// null potentially, in which case this is a noop. The string passed in is
613// assumed to be a malloc'd string buffer, and is freed by this function.
614//
615// This function returns true if the type has already been defined, but is
616// allowed to be redefined in the specified context. If the name is a new name
617// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000618static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000619 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000620 if (NameStr == 0) return false;
621
622 std::string Name(NameStr); // Copy string
623 free(NameStr); // Free old string
624
625 // We don't allow assigning names to void type
626 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000627 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000628
Chris Lattnera09000d2004-07-14 23:03:46 +0000629 // Set the type name, checking for conflicts as we do so.
630 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000631
Chris Lattnera09000d2004-07-14 23:03:46 +0000632 if (AlreadyExists) { // Inserting a name that is already defined???
633 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
634 assert(Existing && "Conflict but no matching type?");
635
Reid Spencer75719bf2004-05-26 21:48:31 +0000636 // There is only one case where this is allowed: when we are refining an
637 // opaque type. In this case, Existing will be an opaque type.
638 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
639 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000640 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000641 return true;
642 }
643
644 // Otherwise, this is an attempt to redefine a type. That's okay if
645 // the redefinition is identical to the original. This will be so if
646 // Existing and T point to the same Type object. In this one case we
647 // allow the equivalent redefinition.
648 if (Existing == T) return true; // Yes, it's equal.
649
650 // Any other kind of (non-equivalent) redefinition is an error.
651 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000652 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000653 }
654
Reid Spencer75719bf2004-05-26 21:48:31 +0000655 return false;
656}
Chris Lattner8896eda2001-07-09 19:38:36 +0000657
Chris Lattner30c89792001-09-07 16:35:17 +0000658//===----------------------------------------------------------------------===//
659// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000660//
Chris Lattner8896eda2001-07-09 19:38:36 +0000661
Chris Lattner3b073862004-02-09 03:19:29 +0000662// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000663//
664static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000665 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
666}
667
668namespace {
669 struct UpRefRecord {
670 // NestingLevel - The number of nesting levels that need to be popped before
671 // this type is resolved.
672 unsigned NestingLevel;
673
674 // LastContainedTy - This is the type at the current binding level for the
675 // type. Every time we reduce the nesting level, this gets updated.
676 const Type *LastContainedTy;
677
678 // UpRefTy - This is the actual opaque type that the upreference is
679 // represented with.
680 OpaqueType *UpRefTy;
681
682 UpRefRecord(unsigned NL, OpaqueType *URTy)
683 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
684 };
Chris Lattner30c89792001-09-07 16:35:17 +0000685}
Chris Lattner698b56e2001-07-20 19:15:08 +0000686
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000687// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000688static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000689
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000690/// HandleUpRefs - Every time we finish a new layer of types, this function is
691/// called. It loops through the UpRefs vector, which is a list of the
692/// currently active types. For each type, if the up reference is contained in
693/// the newly completed type, we decrement the level count. When the level
694/// count reaches zero, the upreferenced type is the type that is passed in:
695/// thus we can complete the cycle.
696///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000697static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000698 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000699 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000700 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000701 "' newly formed. Resolving upreferences.\n" <<
702 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000703
704 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
705 // to zero), we resolve them all together before we resolve them to Ty. At
706 // the end of the loop, if there is anything to resolve to Ty, it will be in
707 // this variable.
708 OpaqueType *TypeToResolve = 0;
709
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000710 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000711 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencer3271ed52004-07-18 00:08:11 +0000712 << UpRefs[i].second->getDescription() << ") = "
713 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000714 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
715 // Decrement level of upreference
716 unsigned Level = --UpRefs[i].NestingLevel;
717 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000718 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000719 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000720 if (!TypeToResolve) {
721 TypeToResolve = UpRefs[i].UpRefTy;
722 } else {
723 UR_OUT(" * Resolving upreference for "
724 << UpRefs[i].second->getDescription() << "\n";
725 std::string OldName = UpRefs[i].UpRefTy->getDescription());
726 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
727 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
728 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
729 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000730 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000731 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000732 }
733 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000734 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000735
Chris Lattner026a8ce2004-02-09 18:53:54 +0000736 if (TypeToResolve) {
737 UR_OUT(" * Resolving upreference for "
738 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000739 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000740 TypeToResolve->refineAbstractTypeTo(Ty);
741 }
742
Chris Lattner8896eda2001-07-09 19:38:36 +0000743 return Ty;
744}
745
Chris Lattner30c89792001-09-07 16:35:17 +0000746
Chris Lattner00950542001-06-06 20:29:01 +0000747//===----------------------------------------------------------------------===//
748// RunVMAsmParser - Define an interface to this parser
749//===----------------------------------------------------------------------===//
750//
Chris Lattner86427632004-07-14 06:39:48 +0000751Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000752 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000753 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000754 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000755 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000756
Chris Lattner75f20532003-04-22 18:02:52 +0000757 // Allocate a new module to read
758 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000759
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000760 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000761
Chris Lattner00950542001-06-06 20:29:01 +0000762 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000763
764 // Check to see if they called va_start but not va_arg..
765 if (!ObsoleteVarArgs)
766 if (Function *F = Result->getNamedFunction("llvm.va_start"))
767 if (F->asize() == 1) {
768 std::cerr << "WARNING: this file uses obsolete features. "
769 << "Assemble and disassemble to update it.\n";
770 ObsoleteVarArgs = true;
771 }
772
Chris Lattner99e7ab72003-10-18 05:53:13 +0000773 if (ObsoleteVarArgs) {
774 // If the user is making use of obsolete varargs intrinsics, adjust them for
775 // the user.
776 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
777 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
778
779 const Type *RetTy = F->getFunctionType()->getParamType(0);
780 RetTy = cast<PointerType>(RetTy)->getElementType();
781 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
782
783 while (!F->use_empty()) {
784 CallInst *CI = cast<CallInst>(F->use_back());
785 Value *V = new CallInst(NF, "", CI);
786 new StoreInst(V, CI->getOperand(1), CI);
787 CI->getParent()->getInstList().erase(CI);
788 }
789 Result->getFunctionList().erase(F);
790 }
791
792 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
793 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
794 const Type *ArgTy = F->getFunctionType()->getParamType(0);
795 ArgTy = cast<PointerType>(ArgTy)->getElementType();
796 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
797 ArgTy, 0);
798
799 while (!F->use_empty()) {
800 CallInst *CI = cast<CallInst>(F->use_back());
801 Value *V = new LoadInst(CI->getOperand(1), "", CI);
802 new CallInst(NF, V, "", CI);
803 CI->getParent()->getInstList().erase(CI);
804 }
805 Result->getFunctionList().erase(F);
806 }
807
808 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
809 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
810 const Type *ArgTy = F->getFunctionType()->getParamType(0);
811 ArgTy = cast<PointerType>(ArgTy)->getElementType();
812 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
813 ArgTy, 0);
814
815 while (!F->use_empty()) {
816 CallInst *CI = cast<CallInst>(F->use_back());
817 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
818 new StoreInst(V, CI->getOperand(1), CI);
819 CI->getParent()->getInstList().erase(CI);
820 }
821 Result->getFunctionList().erase(F);
822 }
823 }
824
Chris Lattner00950542001-06-06 20:29:01 +0000825 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
826 ParserResult = 0;
827
828 return Result;
829}
830
831%}
832
833%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000834 llvm::Module *ModuleVal;
835 llvm::Function *FunctionVal;
836 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
837 llvm::BasicBlock *BasicBlockVal;
838 llvm::TerminatorInst *TermInstVal;
839 llvm::Instruction *InstVal;
840 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000841
Brian Gaeked0fde302003-11-11 22:41:34 +0000842 const llvm::Type *PrimType;
843 llvm::PATypeHolder *TypeVal;
844 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000845
Brian Gaeked0fde302003-11-11 22:41:34 +0000846 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
847 std::vector<llvm::Value*> *ValueList;
848 std::list<llvm::PATypeHolder> *TypeList;
849 std::list<std::pair<llvm::Value*,
850 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
851 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
852 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000853
Brian Gaeked0fde302003-11-11 22:41:34 +0000854 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000855 int64_t SInt64Val;
856 uint64_t UInt64Val;
857 int SIntVal;
858 unsigned UIntVal;
859 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000860 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000861
Chris Lattner30c89792001-09-07 16:35:17 +0000862 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000863 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000864
Brian Gaeked0fde302003-11-11 22:41:34 +0000865 llvm::Instruction::BinaryOps BinaryOpVal;
866 llvm::Instruction::TermOps TermOpVal;
867 llvm::Instruction::MemoryOps MemOpVal;
868 llvm::Instruction::OtherOps OtherOpVal;
869 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000870}
871
Chris Lattner79df7c02002-03-26 18:01:55 +0000872%type <ModuleVal> Module FunctionList
873%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000874%type <BasicBlockVal> BasicBlock InstructionList
875%type <TermInstVal> BBTerminatorInst
876%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000877%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000878%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000879%type <ArgList> ArgList ArgListH
880%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000881%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000882%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000883%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000884%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000885%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000886%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000887%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000888%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000889%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000890
Chris Lattner2079fde2001-10-13 06:41:08 +0000891// ValueRef - Unresolved reference to a definition or BB
892%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000893%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000894// Tokens and types for handling constant integer values
895//
896// ESINT64VAL - A negative number within long long range
897%token <SInt64Val> ESINT64VAL
898
899// EUINT64VAL - A positive number within uns. long long range
900%token <UInt64Val> EUINT64VAL
901%type <SInt64Val> EINT64VAL
902
903%token <SIntVal> SINTVAL // Signed 32 bit ints...
904%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
905%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000906%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000907
908// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000909%type <TypeVal> Types TypesV UpRTypes UpRTypesV
910%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000911%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
912%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000913
Chris Lattner6c23f572003-08-22 05:42:10 +0000914%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
915%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000916
917
Chris Lattner91ef4602004-03-31 03:49:47 +0000918%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000919%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000920%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Reid Spencer0be13e72004-07-25 17:58:28 +0000921%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
922%token DEPLIBS
Chris Lattner00950542001-06-06 20:29:01 +0000923
924// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000925%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000926
Chris Lattner00950542001-06-06 20:29:01 +0000927// Binary Operators
928%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000929%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000930%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000931%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000932
933// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000934%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000935
Chris Lattner027dcc52001-07-08 21:10:27 +0000936// Other Operators
937%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000938%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000939%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000940
Chris Lattner00950542001-06-06 20:29:01 +0000941%start Module
942%%
943
944// Handle constant integer size restriction and conversion...
945//
Chris Lattner51727be2002-06-04 21:58:56 +0000946INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000947INTVAL : UINTVAL {
948 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
949 ThrowException("Value too large for type!");
950 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000951};
Chris Lattner00950542001-06-06 20:29:01 +0000952
953
Chris Lattner51727be2002-06-04 21:58:56 +0000954EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000955EINT64VAL : EUINT64VAL {
956 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
957 ThrowException("Value too large for type!");
958 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000959};
Chris Lattner00950542001-06-06 20:29:01 +0000960
Chris Lattner00950542001-06-06 20:29:01 +0000961// Operations that are notably excluded from this list include:
962// RET, BR, & SWITCH because they end basic blocks and are treated specially.
963//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000964ArithmeticOps: ADD | SUB | MUL | DIV | REM;
965LogicalOps : AND | OR | XOR;
966SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
967BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
968
Chris Lattner51727be2002-06-04 21:58:56 +0000969ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000970
Chris Lattnere98dda62001-07-14 06:10:16 +0000971// These are some types that allow classification if we only want a particular
972// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000973SIntType : LONG | INT | SHORT | SBYTE;
974UIntType : ULONG | UINT | USHORT | UBYTE;
975IntType : SIntType | UIntType;
976FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000977
Chris Lattnere98dda62001-07-14 06:10:16 +0000978// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000979OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000980 $$ = $1;
981 }
982 | /*empty*/ {
983 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000984 };
Chris Lattner00950542001-06-06 20:29:01 +0000985
Chris Lattner4ad02e72003-04-16 20:28:45 +0000986OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
987 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000988 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000989 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
990 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000991
992//===----------------------------------------------------------------------===//
993// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000994// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000995// access to it, a user must explicitly use TypesV.
996//
997
998// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000999TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1000UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001001
1002Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001003 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001004 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1005 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001006 };
Chris Lattner30c89792001-09-07 16:35:17 +00001007
1008
1009// Derived types are added later...
1010//
Chris Lattner51727be2002-06-04 21:58:56 +00001011PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1012PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001013UpRTypes : OPAQUE {
1014 $$ = new PATypeHolder(OpaqueType::get());
1015 }
1016 | PrimType {
1017 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001018 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001019UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001020 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001021};
Chris Lattner30c89792001-09-07 16:35:17 +00001022
Chris Lattner30c89792001-09-07 16:35:17 +00001023// Include derived types in the Types production.
1024//
1025UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001026 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001027 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001028 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001029 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001030 UR_OUT("New Upreference!\n");
1031 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001032 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001033 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001034 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Reid Spencer3271ed52004-07-18 00:08:11 +00001035 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001036 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1037 if (isVarArg) Params.pop_back();
1038
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001039 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001040 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001041 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001042 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001043 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001044 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001045 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001046 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001047 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001048 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001049 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Reid Spencer3271ed52004-07-18 00:08:11 +00001050 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001051
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001052 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001053 delete $2;
1054 }
1055 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001056 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001057 }
1058 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001059 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001060 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001061 };
Chris Lattner30c89792001-09-07 16:35:17 +00001062
Chris Lattner7e708292002-06-25 16:13:24 +00001063// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001064// declaration type lists
1065//
1066TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001067 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001068 $$->push_back(*$1); delete $1;
1069 }
1070 | TypeListI ',' UpRTypes {
1071 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001072 };
Chris Lattner30c89792001-09-07 16:35:17 +00001073
Chris Lattner7e708292002-06-25 16:13:24 +00001074// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001075ArgTypeListI : TypeListI
1076 | TypeListI ',' DOTDOTDOT {
1077 ($$=$1)->push_back(Type::VoidTy);
1078 }
1079 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001080 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001081 }
1082 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001083 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001084 };
Chris Lattner30c89792001-09-07 16:35:17 +00001085
Chris Lattnere98dda62001-07-14 06:10:16 +00001086// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001087// production is used ONLY to represent constants that show up AFTER a 'const',
1088// 'constant' or 'global' token at global scope. Constants that can be inlined
1089// into other expressions (such as integers and constexprs) are handled by the
1090// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001091//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001092ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001093 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001094 if (ATy == 0)
1095 ThrowException("Cannot make array constant with type: '" +
1096 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001097 const Type *ETy = ATy->getElementType();
1098 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001099
Chris Lattner30c89792001-09-07 16:35:17 +00001100 // Verify that we have the correct size...
1101 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001102 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001103 utostr($3->size()) + " arguments, but has size of " +
1104 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001105
Chris Lattner30c89792001-09-07 16:35:17 +00001106 // Verify all elements are correct type!
1107 for (unsigned i = 0; i < $3->size(); i++) {
1108 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001109 ThrowException("Element #" + utostr(i) + " is not of type '" +
1110 ETy->getDescription() +"' as required!\nIt is of type '"+
1111 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001112 }
1113
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001114 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001115 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001116 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001117 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001118 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001119 if (ATy == 0)
1120 ThrowException("Cannot make array constant with type: '" +
1121 (*$1)->getDescription() + "'!");
1122
1123 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001124 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001125 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001126 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001127 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001128 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001129 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001130 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001131 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001132 if (ATy == 0)
1133 ThrowException("Cannot make array constant with type: '" +
1134 (*$1)->getDescription() + "'!");
1135
Chris Lattner30c89792001-09-07 16:35:17 +00001136 int NumElements = ATy->getNumElements();
1137 const Type *ETy = ATy->getElementType();
1138 char *EndStr = UnEscapeLexed($3, true);
1139 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001140 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001141 itostr((int)(EndStr-$3)) +
1142 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001143 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001144 if (ETy == Type::SByteTy) {
1145 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001146 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001147 } else if (ETy == Type::UByteTy) {
1148 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001149 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001150 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001151 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001152 ThrowException("Cannot build string arrays of non byte sized elements!");
1153 }
Chris Lattner30c89792001-09-07 16:35:17 +00001154 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001155 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001156 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001157 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001158 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001159 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001160 if (STy == 0)
1161 ThrowException("Cannot make struct constant with type: '" +
1162 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001163
Chris Lattnerc6212f12003-05-21 16:06:56 +00001164 if ($3->size() != STy->getNumContainedTypes())
1165 ThrowException("Illegal number of initializers for structure type!");
1166
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001167 // Check to ensure that constants are compatible with the type initializer!
1168 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001169 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001170 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001171 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001172 "' for element #" + utostr(i) +
1173 " of structure initializer!");
1174
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001175 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001176 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001177 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001178 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001179 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001180 if (STy == 0)
1181 ThrowException("Cannot make struct constant with type: '" +
1182 (*$1)->getDescription() + "'!");
1183
1184 if (STy->getNumContainedTypes() != 0)
1185 ThrowException("Illegal number of initializers for structure type!");
1186
1187 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1188 delete $1;
1189 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001190 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001191 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001192 if (PTy == 0)
1193 ThrowException("Cannot make null pointer constant with type: '" +
1194 (*$1)->getDescription() + "'!");
1195
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001196 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001197 delete $1;
1198 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001199 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001200 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001201 if (Ty == 0)
1202 ThrowException("Global const reference must be a pointer type!");
1203
Chris Lattner3101c252002-08-15 17:58:33 +00001204 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001205 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001206 // the context of a function, getValNonImprovising will search the functions
1207 // symbol table instead of the module symbol table for the global symbol,
1208 // which throws things all off. To get around this, we just tell
1209 // getValNonImprovising that we are at global scope here.
1210 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001211 Function *SavedCurFn = CurFun.CurrentFunction;
1212 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001213
Chris Lattner2079fde2001-10-13 06:41:08 +00001214 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001215
Chris Lattner394f2eb2003-10-16 20:12:13 +00001216 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001217
Chris Lattner2079fde2001-10-13 06:41:08 +00001218 // If this is an initializer for a constant pointer, which is referencing a
1219 // (currently) undefined variable, create a stub now that shall be replaced
1220 // in the future with the right type of variable.
1221 //
1222 if (V == 0) {
1223 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1224 const PointerType *PT = cast<PointerType>(Ty);
1225
1226 // First check to see if the forward references value is already created!
1227 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001228 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001229
1230 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001231 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001232 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001233 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001234 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001235 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001236
Reid Spencer3271ed52004-07-18 00:08:11 +00001237 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001238 GlobalValue *GV;
1239 if (const FunctionType *FTy =
1240 dyn_cast<FunctionType>(PT->getElementType())) {
1241 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1242 CurModule.CurrentModule);
1243 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001244 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001245 GlobalValue::ExternalLinkage, 0,
1246 Name, CurModule.CurrentModule);
1247 }
Chris Lattner8a327842004-07-14 21:44:00 +00001248
Reid Spencer3271ed52004-07-18 00:08:11 +00001249 // Keep track of the fact that we have a forward ref to recycle it
1250 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1251 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001252 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001253 }
1254
Reid Spencer3271ed52004-07-18 00:08:11 +00001255 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001256 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001257 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001258 | Types ConstExpr {
1259 if ($1->get() != $2->getType())
1260 ThrowException("Mismatched types for constant expression!");
1261 $$ = $2;
1262 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001263 }
1264 | Types ZEROINITIALIZER {
1265 $$ = Constant::getNullValue($1->get());
1266 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001267 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001268
Chris Lattnere43f40b2002-10-09 00:25:32 +00001269ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001270 if (!ConstantSInt::isValueValidForType($1, $2))
1271 ThrowException("Constant value doesn't fit in type!");
1272 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001273 }
1274 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001275 if (!ConstantUInt::isValueValidForType($1, $2))
1276 ThrowException("Constant value doesn't fit in type!");
1277 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001278 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001279 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001280 $$ = ConstantBool::True;
1281 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001282 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001283 $$ = ConstantBool::False;
1284 }
1285 | FPType FPVAL { // Float & Double constants
1286 $$ = ConstantFP::get($1, $2);
1287 };
1288
Chris Lattner00950542001-06-06 20:29:01 +00001289
Chris Lattnerd78700d2002-08-16 21:14:40 +00001290ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001291 if (!$3->getType()->isFirstClassType())
1292 ThrowException("cast constant expression from a non-primitive type: '" +
1293 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001294 if (!$5->get()->isFirstClassType())
1295 ThrowException("cast constant expression to a non-primitive type: '" +
1296 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001297 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001298 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001299 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001300 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1301 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001302 ThrowException("GetElementPtr requires a pointer operand!");
1303
Chris Lattner830b6f92004-04-05 01:30:04 +00001304 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1305 // indices to uint struct indices for compatibility.
1306 generic_gep_type_iterator<std::vector<Value*>::iterator>
1307 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1308 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1309 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1310 if (isa<StructType>(*GTI)) // Only change struct indices
1311 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1312 if (CUI->getType() == Type::UByteTy)
1313 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1314
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001315 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001316 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001317 if (!IdxTy)
1318 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001319
Chris Lattner9232b992003-04-22 18:42:41 +00001320 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001321 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1322 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001323 IdxVec.push_back(C);
1324 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001325 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001326
Chris Lattnerd78700d2002-08-16 21:14:40 +00001327 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001328
Chris Lattnerd78700d2002-08-16 21:14:40 +00001329 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001330 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001331 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1332 if ($3->getType() != Type::BoolTy)
1333 ThrowException("Select condition must be of boolean type!");
1334 if ($5->getType() != $7->getType())
1335 ThrowException("Select operand types must match!");
1336 $$ = ConstantExpr::getSelect($3, $5, $7);
1337 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001338 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001339 if ($3->getType() != $5->getType())
1340 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001341 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001342 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001343 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001344 if ($5->getType() != Type::UByteTy)
1345 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001346 if (!$3->getType()->isInteger())
1347 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001348 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001349 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001350
1351
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001352// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001353ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001354 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001355 }
1356 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001357 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001358 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001359 };
Chris Lattner00950542001-06-06 20:29:01 +00001360
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001361
Chris Lattner1781aca2001-09-18 04:00:54 +00001362// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001363GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001364
Chris Lattner00950542001-06-06 20:29:01 +00001365
Chris Lattner0e73ce62002-05-02 19:11:13 +00001366//===----------------------------------------------------------------------===//
1367// Rules to match Modules
1368//===----------------------------------------------------------------------===//
1369
1370// Module rule: Capture the result of parsing the whole file into a result
1371// variable...
1372//
1373Module : FunctionList {
1374 $$ = ParserResult = $1;
1375 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001376};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001377
Chris Lattner7e708292002-06-25 16:13:24 +00001378// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001379//
1380FunctionList : FunctionList Function {
1381 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001382 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001383 }
1384 | FunctionList FunctionProto {
1385 $$ = $1;
1386 }
1387 | FunctionList IMPLEMENTATION {
1388 $$ = $1;
1389 }
1390 | ConstPool {
1391 $$ = CurModule.CurrentModule;
1392 // Resolve circular types before we parse the body of the module
1393 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001394 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001395
Chris Lattnere98dda62001-07-14 06:10:16 +00001396// ConstPool - Constants with optional names assigned to them.
Chris Lattnere0026942004-07-14 06:44:56 +00001397ConstPool : ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001398 // Eagerly resolve types. This is not an optimization, this is a
1399 // requirement that is due to the fact that we could have this:
1400 //
1401 // %list = type { %list * }
1402 // %list = type { %list * } ; repeated type decl
1403 //
1404 // If types are not resolved eagerly, then the two types will not be
1405 // determined to be the same type!
1406 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001407 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001408
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001409 if (!setTypeName(*$4, $2) && !$2) {
1410 // If this is a named type that is not a redefinition, add it to the slot
1411 // table.
Chris Lattner64116f92004-07-14 01:33:11 +00001412 if (inFunctionScope())
1413 CurFun.Types.push_back(*$4);
1414 else
1415 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001416 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001417
1418 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001419 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001420 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001421 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001422 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001423 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1424 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Chris Lattner1781aca2001-09-18 04:00:54 +00001425 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001426 | ConstPool OptAssign EXTERNAL GlobalType Types {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001427 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001428 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001429 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001430 | ConstPool TARGET TargetDefinition {
1431 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001432 | ConstPool DEPLIBS '=' LibrariesDefinition {
1433 }
Chris Lattner00950542001-06-06 20:29:01 +00001434 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001435 };
Chris Lattner00950542001-06-06 20:29:01 +00001436
1437
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001438
1439BigOrLittle : BIG { $$ = Module::BigEndian; };
1440BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1441
1442TargetDefinition : ENDIAN '=' BigOrLittle {
1443 CurModule.CurrentModule->setEndianness($3);
1444 }
1445 | POINTERSIZE '=' EUINT64VAL {
1446 if ($3 == 32)
1447 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1448 else if ($3 == 64)
1449 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1450 else
1451 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001452 }
1453 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001454 CurModule.CurrentModule->setTargetTriple($3);
1455 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001456 };
1457
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001458LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001459
1460LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001461 CurModule.CurrentModule->addLibrary($3);
1462 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001463 }
1464 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001465 CurModule.CurrentModule->addLibrary($1);
1466 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001467 }
1468 | /* empty: end of list */ {
1469 }
1470 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001471
Chris Lattner00950542001-06-06 20:29:01 +00001472//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001473// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001474//===----------------------------------------------------------------------===//
1475
Chris Lattner6c23f572003-08-22 05:42:10 +00001476Name : VAR_ID | STRINGCONSTANT;
1477OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001478
Chris Lattner6c23f572003-08-22 05:42:10 +00001479ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001480 if (*$1 == Type::VoidTy)
1481 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001482 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001483};
Chris Lattner00950542001-06-06 20:29:01 +00001484
Chris Lattner69da5cf2002-10-13 20:57:00 +00001485ArgListH : ArgListH ',' ArgVal {
1486 $$ = $1;
1487 $1->push_back(*$3);
1488 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001489 }
1490 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001491 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001492 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001493 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001494 };
Chris Lattner00950542001-06-06 20:29:01 +00001495
1496ArgList : ArgListH {
1497 $$ = $1;
1498 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001499 | ArgListH ',' DOTDOTDOT {
1500 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001501 $$->push_back(std::pair<PATypeHolder*,
1502 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001503 }
1504 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001505 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1506 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001507 }
Chris Lattner00950542001-06-06 20:29:01 +00001508 | /* empty */ {
1509 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001510 };
Chris Lattner00950542001-06-06 20:29:01 +00001511
Chris Lattner6c23f572003-08-22 05:42:10 +00001512FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001513 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001514 std::string FunctionName($2);
Chris Lattnera09000d2004-07-14 23:03:46 +00001515 free($2); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001516
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001517 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1518 ThrowException("LLVM functions cannot return aggregate types!");
1519
Chris Lattner9232b992003-04-22 18:42:41 +00001520 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001521 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001522 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001523 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001524 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001525 }
Chris Lattner00950542001-06-06 20:29:01 +00001526
Chris Lattner2079fde2001-10-13 06:41:08 +00001527 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1528 if (isVarArg) ParamTypeList.pop_back();
1529
Chris Lattner1f862af2003-04-16 18:13:57 +00001530 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001531 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001532 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001533
Chris Lattnera09000d2004-07-14 23:03:46 +00001534 ValID ID;
1535 if (!FunctionName.empty()) {
1536 ID = ValID::create((char*)FunctionName.c_str());
1537 } else {
1538 ID = ValID::create((int)CurModule.Values[PFT].size());
1539 }
1540
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001541 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001542 // See if this function was forward referenced. If so, recycle the object.
1543 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1544 // Move the function to the end of the list, from whereever it was
1545 // previously inserted.
1546 Fn = cast<Function>(FWRef);
1547 CurModule.CurrentModule->getFunctionList().remove(Fn);
1548 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1549 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1550 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1551 // If this is the case, either we need to be a forward decl, or it needs
1552 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001553 if (!CurFun.isDeclare && !Fn->isExternal())
1554 ThrowException("Redefinition of function '" + FunctionName + "'!");
1555
Chris Lattnera09000d2004-07-14 23:03:46 +00001556 // Make sure to strip off any argument names so we can't get conflicts.
1557 if (Fn->isExternal())
1558 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend();
1559 AI != AE; ++AI)
1560 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001561
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001562 } else { // Not already defined?
1563 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1564 CurModule.CurrentModule);
1565 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001566 }
Chris Lattner00950542001-06-06 20:29:01 +00001567
Chris Lattner394f2eb2003-10-16 20:12:13 +00001568 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001569
Chris Lattner7e708292002-06-25 16:13:24 +00001570 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001571 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001572 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001573 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001574 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001575 delete $4->back().first;
1576 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001577 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001578 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001579 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001580 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001581 delete I->first; // Delete the typeholder...
1582
Chris Lattner8ab406d2004-07-13 07:59:27 +00001583 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001584 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001585 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001586
Chris Lattner1f862af2003-04-16 18:13:57 +00001587 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001588 }
Chris Lattner51727be2002-06-04 21:58:56 +00001589};
Chris Lattner00950542001-06-06 20:29:01 +00001590
Chris Lattner9b02cc32002-05-03 18:23:48 +00001591BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1592
Chris Lattner4ad02e72003-04-16 20:28:45 +00001593FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001594 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001595
Chris Lattner4ad02e72003-04-16 20:28:45 +00001596 // Make sure that we keep track of the linkage type even if there was a
1597 // previous "declare".
1598 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001599
Chris Lattner7e708292002-06-25 16:13:24 +00001600 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001601 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001602};
Chris Lattner00950542001-06-06 20:29:01 +00001603
Chris Lattner9b02cc32002-05-03 18:23:48 +00001604END : ENDTOK | '}'; // Allow end of '}' to end a function
1605
Chris Lattner79df7c02002-03-26 18:01:55 +00001606Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001607 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001608};
Chris Lattner00950542001-06-06 20:29:01 +00001609
Chris Lattner394f2eb2003-10-16 20:12:13 +00001610FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1611 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001612 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001613};
Chris Lattner00950542001-06-06 20:29:01 +00001614
1615//===----------------------------------------------------------------------===//
1616// Rules to match Basic Blocks
1617//===----------------------------------------------------------------------===//
1618
1619ConstValueRef : ESINT64VAL { // A reference to a direct constant
1620 $$ = ValID::create($1);
1621 }
1622 | EUINT64VAL {
1623 $$ = ValID::create($1);
1624 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001625 | FPVAL { // Perhaps it's an FP constant?
1626 $$ = ValID::create($1);
1627 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001628 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001629 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001630 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001631 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001632 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001633 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001634 | NULL_TOK {
1635 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001636 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001637 | ConstExpr {
1638 $$ = ValID::create($1);
1639 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001640
Chris Lattner2079fde2001-10-13 06:41:08 +00001641// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1642// another value.
1643//
1644SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001645 $$ = ValID::create($1);
1646 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001647 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001648 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001649 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001650
1651// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001652ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001653
Chris Lattner00950542001-06-06 20:29:01 +00001654
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001655// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1656// type immediately preceeds the value reference, and allows complex constant
1657// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001658ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001659 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001660 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001661
Chris Lattner00950542001-06-06 20:29:01 +00001662BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001663 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001664 }
Chris Lattner7e708292002-06-25 16:13:24 +00001665 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001666 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001667 };
Chris Lattner00950542001-06-06 20:29:01 +00001668
1669
1670// Basic blocks are terminated by branching instructions:
1671// br, br/cc, switch, ret
1672//
Chris Lattner2079fde2001-10-13 06:41:08 +00001673BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001674 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001675 InsertValue($3);
1676
1677 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001678 InsertValue($1);
1679 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001680 };
Chris Lattner00950542001-06-06 20:29:01 +00001681
1682InstructionList : InstructionList Inst {
1683 $1->getInstList().push_back($2);
1684 $$ = $1;
1685 }
1686 | /* empty */ {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001687 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner22ae2322004-07-13 08:39:15 +00001688 }
1689 | LABELSTR {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001690 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner51727be2002-06-04 21:58:56 +00001691 };
Chris Lattner00950542001-06-06 20:29:01 +00001692
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001693BBTerminatorInst : RET ResolvedVal { // Return with a result...
1694 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001695 }
1696 | RET VOID { // Return with no result...
1697 $$ = new ReturnInst();
1698 }
1699 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001700 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001701 } // Conditional Branch...
1702 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001703 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001704 }
1705 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001706 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner00950542001-06-06 20:29:01 +00001707 $$ = S;
1708
Chris Lattner9232b992003-04-22 18:42:41 +00001709 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001710 E = $8->end();
1711 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001712 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001713 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001714 }
Chris Lattner196850c2003-05-15 21:30:00 +00001715 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001716 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner196850c2003-05-15 21:30:00 +00001717 $$ = S;
1718 }
Chris Lattner64116f92004-07-14 01:33:11 +00001719 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1720 UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001721 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001722 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001723
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001724 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1725 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001726 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001727 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001728 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001729 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1730 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001731 ParamTypes.push_back((*I)->getType());
1732 }
1733
1734 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1735 if (isVarArg) ParamTypes.pop_back();
1736
Chris Lattner79df7c02002-03-26 18:01:55 +00001737 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001738 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001739 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001740
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001741 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001742
Chris Lattner64116f92004-07-14 01:33:11 +00001743 BasicBlock *Normal = getBBVal($9);
1744 BasicBlock *Except = getBBVal($12);
Chris Lattner2079fde2001-10-13 06:41:08 +00001745
1746 // Create the call node...
1747 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001748 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001749 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001750 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001751 // correctly!
1752 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001753 FunctionType::param_iterator I = Ty->param_begin();
1754 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001755 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001756
1757 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001758 if ((*ArgI)->getType() != *I)
1759 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1760 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001761
1762 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001763 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001764
Chris Lattner6cdb0112001-11-26 16:54:11 +00001765 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001766 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001767 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001768 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001769 }
1770 | UNWIND {
1771 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001772 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001773
1774
Chris Lattner00950542001-06-06 20:29:01 +00001775
1776JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1777 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001778 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001779 if (V == 0)
1780 ThrowException("May only switch on a constant pool value!");
1781
Chris Lattner64116f92004-07-14 01:33:11 +00001782 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001783 }
1784 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001785 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001786 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001787
1788 if (V == 0)
1789 ThrowException("May only switch on a constant pool value!");
1790
Chris Lattner64116f92004-07-14 01:33:11 +00001791 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001792 };
Chris Lattner00950542001-06-06 20:29:01 +00001793
1794Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001795 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001796 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001797 InsertValue($2);
1798 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001799};
Chris Lattner00950542001-06-06 20:29:01 +00001800
Chris Lattnerc24d2082001-06-11 15:04:20 +00001801PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001802 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001803 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001804 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001805 }
1806 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1807 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001808 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001809 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001810 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001811
1812
Chris Lattner30c89792001-09-07 16:35:17 +00001813ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001814 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001815 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001816 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001817 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001818 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001819 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001820 };
Chris Lattner00950542001-06-06 20:29:01 +00001821
1822// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001823ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001824
Chris Lattner4a6482b2002-09-10 19:57:26 +00001825InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1826 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1827 ThrowException("Arithmetic operator requires integer or FP operands!");
1828 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1829 if ($$ == 0)
1830 ThrowException("binary operator returned null!");
1831 delete $2;
1832 }
1833 | LogicalOps Types ValueRef ',' ValueRef {
1834 if (!(*$2)->isIntegral())
1835 ThrowException("Logical operator requires integral operands!");
1836 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1837 if ($$ == 0)
1838 ThrowException("binary operator returned null!");
1839 delete $2;
1840 }
1841 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001842 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001843 if ($$ == 0)
1844 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001845 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001846 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001847 | NOT ResolvedVal {
1848 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1849 << " Replacing with 'xor'.\n";
1850
1851 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1852 if (Ones == 0)
1853 ThrowException("Expected integral type for not instruction!");
1854
1855 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001856 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001857 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001858 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001859 | ShiftOps ResolvedVal ',' ResolvedVal {
1860 if ($4->getType() != Type::UByteTy)
1861 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001862 if (!$2->getType()->isInteger())
1863 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001864 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001865 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001866 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001867 if (!$4->get()->isFirstClassType())
1868 ThrowException("cast instruction to a non-primitive type: '" +
1869 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001870 $$ = new CastInst($2, *$4);
1871 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001872 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001873 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1874 if ($2->getType() != Type::BoolTy)
1875 ThrowException("select condition must be boolean!");
1876 if ($4->getType() != $6->getType())
1877 ThrowException("select value types should match!");
1878 $$ = new SelectInst($2, $4, $6);
1879 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001880 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001881 // FIXME: This is emulation code for an obsolete syntax. This should be
1882 // removed at some point.
1883 if (!ObsoleteVarArgs) {
1884 std::cerr << "WARNING: this file uses obsolete features. "
1885 << "Assemble and disassemble to update it.\n";
1886 ObsoleteVarArgs = true;
1887 }
1888
1889 // First, load the valist...
1890 Instruction *CurVAList = new LoadInst($2, "");
1891 CurBB->getInstList().push_back(CurVAList);
1892
1893 // Emit the vaarg instruction.
1894 $$ = new VAArgInst(CurVAList, *$4);
1895
1896 // Now we must advance the pointer and update it in memory.
1897 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1898 CurBB->getInstList().push_back(TheVANext);
1899
1900 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1901 delete $4;
1902 }
1903 | VAARG ResolvedVal ',' Types {
1904 $$ = new VAArgInst($2, *$4);
1905 delete $4;
1906 }
1907 | VANEXT ResolvedVal ',' Types {
1908 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001909 delete $4;
1910 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001911 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001912 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001913 if (!Ty->isFirstClassType())
1914 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001915 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001916 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001917 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001918 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00001919 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001920 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001921 $2->pop_front();
1922 }
1923 delete $2; // Free the list...
1924 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001925 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001926 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001927 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001928
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001929 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1930 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001931 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001932 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001933 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001934 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1935 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001936 ParamTypes.push_back((*I)->getType());
1937 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001938
1939 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1940 if (isVarArg) ParamTypes.pop_back();
1941
Chris Lattner79df7c02002-03-26 18:01:55 +00001942 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001943 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001944 }
Chris Lattner00950542001-06-06 20:29:01 +00001945
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001946 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001947
Chris Lattner8b81bf52001-07-25 22:47:46 +00001948 // Create the call node...
1949 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001950 // Make sure no arguments is a good thing!
1951 if (Ty->getNumParams() != 0)
1952 ThrowException("No arguments passed to a function that "
1953 "expects arguments!");
1954
Chris Lattner9232b992003-04-22 18:42:41 +00001955 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001956 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001957 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001958 // correctly!
1959 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001960 FunctionType::param_iterator I = Ty->param_begin();
1961 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001962 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001963
1964 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001965 if ((*ArgI)->getType() != *I)
1966 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1967 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001968
Chris Lattner8b81bf52001-07-25 22:47:46 +00001969 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001970 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001971
Chris Lattner6cdb0112001-11-26 16:54:11 +00001972 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001973 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001974 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001975 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001976 }
1977 | MemoryInst {
1978 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001979 };
Chris Lattner00950542001-06-06 20:29:01 +00001980
Chris Lattner6cdb0112001-11-26 16:54:11 +00001981
1982// IndexList - List of indices for GEP based instructions...
1983IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001984 $$ = $2;
1985 } | /* empty */ {
1986 $$ = new std::vector<Value*>();
1987 };
1988
1989OptVolatile : VOLATILE {
1990 $$ = true;
1991 }
1992 | /* empty */ {
1993 $$ = false;
1994 };
1995
Chris Lattner027dcc52001-07-08 21:10:27 +00001996
Chris Lattner00950542001-06-06 20:29:01 +00001997MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001998 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001999 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002000 }
2001 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002002 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002003 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002004 }
2005 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002006 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002007 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002008 }
2009 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002010 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002011 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002012 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002013 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002014 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002015 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002016 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002017 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002018 }
2019
Chris Lattner15c9c032003-09-08 18:20:29 +00002020 | OptVolatile LOAD Types ValueRef {
2021 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002022 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002023 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002024 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002025 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002026 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002027 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2028 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002029 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002030 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002031 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002032 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002033 if (ElTy != $3->getType())
2034 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002035 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002036
Chris Lattner79ad13802003-09-08 20:29:46 +00002037 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002038 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002039 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002040 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002041 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002042 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002043
2044 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2045 // indices to uint struct indices for compatibility.
2046 generic_gep_type_iterator<std::vector<Value*>::iterator>
2047 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2048 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2049 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2050 if (isa<StructType>(*GTI)) // Only change struct indices
2051 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2052 if (CUI->getType() == Type::UByteTy)
2053 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2054
Chris Lattner30c89792001-09-07 16:35:17 +00002055 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002056 ThrowException("Invalid getelementptr indices for type '" +
2057 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002058 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2059 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002060 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002061
Brian Gaeked0fde302003-11-11 22:41:34 +00002062
Chris Lattner00950542001-06-06 20:29:01 +00002063%%
Chris Lattner09083092001-07-08 04:57:15 +00002064int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002065 std::string where
2066 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002067 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002068 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002069 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002070 errMsg += "end-of-file.";
2071 else
Chris Lattner9232b992003-04-22 18:42:41 +00002072 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002073 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002074 return 0;
2075}