blob: b8597785fd2b4073cc33d129d3db0f0669a1f07e [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// 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.
Misha Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
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 Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000017#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/SymbolTable.h"
Reid Spencer0b118202006-01-16 21:12:35 +000020#include "llvm/Assembly/AutoUpgrade.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000021#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/ADT/STLExtras.h"
Chris Lattner87ac9722005-11-06 06:46:28 +000023#include "llvm/Support/MathExtras.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
Andrew Lenharth558bc882005-06-18 18:34:52 +000052static bool ObsoleteVarArgs;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +000053static bool NewVarArgs;
Chris Lattnerb2b96672005-11-12 18:21:21 +000054static BasicBlock *CurBB;
55static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000056
57
Chris Lattner7e708292002-06-25 16:13:24 +000058// This contains info used when building the body of a function. It is
59// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000060//
Chris Lattner9232b992003-04-22 18:42:41 +000061typedef std::vector<Value *> ValueList; // Numbered defs
Misha Brukman5a2a3822005-05-10 22:02:28 +000062static void
63ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
64 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000065
66static struct PerModuleInfo {
67 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000068 std::map<const Type *, ValueList> Values; // Module level numbered definitions
69 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000070 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000071 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000072
Chris Lattnerbc721ed2004-07-13 08:28:21 +000073 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
74 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000075 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000076 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
77
Chris Lattner2079fde2001-10-13 06:41:08 +000078 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
79 // references to global values. Global values may be referenced before they
80 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000081 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000082 //
Chris Lattner9232b992003-04-22 18:42:41 +000083 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000084 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000085 GlobalRefsType GlobalRefs;
86
Chris Lattner00950542001-06-06 20:29:01 +000087 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000088 // If we could not resolve some functions at function compilation time
89 // (calls to functions before they are defined), resolve them now... Types
90 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000091 //
Chris Lattner00950542001-06-06 20:29:01 +000092 ResolveDefinitions(LateResolveValues);
93
Chris Lattner2079fde2001-10-13 06:41:08 +000094 // Check to make sure that all global value forward references have been
95 // resolved!
96 //
97 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000098 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +000099
Chris Lattner749ce032002-03-11 22:12:39 +0000100 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
101 I != E; ++I) {
102 UndefinedReferences += " " + I->first.first->getDescription() + " " +
103 I->first.second.getName() + "\n";
104 }
105 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000106 }
107
Reid Spencere812fb22006-01-19 01:21:04 +0000108 // Look for intrinsic functions and CallInst that need to be upgraded
109 for (Module::iterator FI = CurrentModule->begin(),FE = CurrentModule->end();
110 FI != FE; ++FI)
111 UpgradeCallsToIntrinsic(FI);
Reid Spencer0b118202006-01-16 21:12:35 +0000112
Chris Lattner7e708292002-06-25 16:13:24 +0000113 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000114 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000115 CurrentModule = 0;
116 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000117
Chris Lattner8a327842004-07-14 21:44:00 +0000118 // GetForwardRefForGlobal - Check to see if there is a forward reference
119 // for this global. If so, remove it from the GlobalRefs map and return it.
120 // If not, just return null.
121 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
122 // Check to see if there is a forward reference to this global variable...
123 // if there is, eliminate it and patch the reference to use the new def'n.
124 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
125 GlobalValue *Ret = 0;
126 if (I != GlobalRefs.end()) {
127 Ret = I->second;
128 GlobalRefs.erase(I);
129 }
130 return Ret;
131 }
Chris Lattner00950542001-06-06 20:29:01 +0000132} CurModule;
133
Chris Lattner79df7c02002-03-26 18:01:55 +0000134static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000135 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000136
Chris Lattner735f2702004-07-08 22:30:50 +0000137 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
138 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000139 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000140
Chris Lattner2e2ed292004-07-14 06:28:35 +0000141 /// BBForwardRefs - When we see forward references to basic blocks, keep
142 /// track of them here.
143 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
144 std::vector<BasicBlock*> NumberedBlocks;
145 unsigned NextBBNum;
146
Chris Lattner79df7c02002-03-26 18:01:55 +0000147 inline PerFunctionInfo() {
148 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000149 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000150 }
151
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 inline void FunctionStart(Function *M) {
153 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000154 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000155 }
156
Chris Lattner79df7c02002-03-26 18:01:55 +0000157 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000158 NumberedBlocks.clear();
159
160 // Any forward referenced blocks left?
161 if (!BBForwardRefs.empty())
162 ThrowException("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000163 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000164
165 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000166 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000167
Chris Lattner7e708292002-06-25 16:13:24 +0000168 Values.clear(); // Clear out function local definitions
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...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000195 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000196 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 Lattner355ad1f2005-03-21 06:27:42 +0000214
215 if (inFunctionScope()) {
216 if (D.Type == ValID::NameVal)
217 ThrowException("Reference to an undefined type: '" + D.getName() + "'");
218 else
219 ThrowException("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000220 }
Chris Lattner30c89792001-09-07 16:35:17 +0000221
Chris Lattner355ad1f2005-03-21 06:27:42 +0000222 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
223 if (I != CurModule.LateResolveTypes.end())
224 return I->second;
225
Chris Lattner82269592001-10-22 06:01:08 +0000226 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000227 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000228 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000229 }
Chris Lattner30c89792001-09-07 16:35:17 +0000230
Chris Lattner9232b992003-04-22 18:42:41 +0000231static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000232 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000233 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000234 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000235 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000236}
237
Chris Lattner2079fde2001-10-13 06:41:08 +0000238// getValNonImprovising - Look up the value specified by the provided type and
239// the provided ValID. If the value exists and has already been defined, return
240// it. Otherwise return null.
241//
242static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000243 if (isa<FunctionType>(Ty))
244 ThrowException("Functions are not values and "
245 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000246
Chris Lattner30c89792001-09-07 16:35:17 +0000247 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000248 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000249 unsigned Num = (unsigned)D.Num;
250
251 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000252 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000253 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000254 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000255 return VI->second[Num];
256 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000257 }
258
259 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000260 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000261 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000262
263 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000264 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000265
Chris Lattner16af11d2004-02-09 21:03:38 +0000266 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000267 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000268
Chris Lattner1a1cb112001-09-30 22:46:54 +0000269 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000270 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000271 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000272
273 D.destroy(); // Free old strdup'd memory...
274 return N;
275 }
276
Misha Brukman5a2a3822005-05-10 22:02:28 +0000277 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000278 // value will fit into the specified type...
279 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000280 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
281 ThrowException("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000282 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000283 Ty->getDescription() + "'!");
284 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000285
286 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000287 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
288 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000289 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000290 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000291 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000292 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000293 }
294 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000295 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000296 }
297
Chris Lattner2079fde2001-10-13 06:41:08 +0000298 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000299 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000300 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000301 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000302
Chris Lattner2079fde2001-10-13 06:41:08 +0000303 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000304 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000305 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000306 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000307
Chris Lattner16710e92004-10-16 18:17:13 +0000308 case ValID::ConstUndefVal: // Is it an undef value?
309 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000310
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000311 case ValID::ConstZeroVal: // Is it a zero value?
312 return Constant::getNullValue(Ty);
313
Chris Lattnerd78700d2002-08-16 21:14:40 +0000314 case ValID::ConstantVal: // Fully resolved constant?
315 if (D.ConstantValue->getType() != Ty)
316 ThrowException("Constant expression type different from required type!");
317 return D.ConstantValue;
318
Chris Lattner30c89792001-09-07 16:35:17 +0000319 default:
320 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000321 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000322 } // End of switch
323
Chris Lattner2079fde2001-10-13 06:41:08 +0000324 assert(0 && "Unhandled case!");
325 return 0;
326}
327
Chris Lattner2079fde2001-10-13 06:41:08 +0000328// getVal - This function is identical to getValNonImprovising, except that if a
329// value is not already defined, it "improvises" by creating a placeholder var
330// that looks and acts just like the requested variable. When the value is
331// defined later, all uses of the placeholder variable are replaced with the
332// real thing.
333//
Chris Lattner64116f92004-07-14 01:33:11 +0000334static Value *getVal(const Type *Ty, const ValID &ID) {
335 if (Ty == Type::LabelTy)
336 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000337
Chris Lattner64116f92004-07-14 01:33:11 +0000338 // See if the value has already been defined.
339 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000340 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000341
Chris Lattner60cd9552005-03-23 01:29:26 +0000342 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
343 ThrowException("Invalid use of a composite type!");
344
Chris Lattner00950542001-06-06 20:29:01 +0000345 // If we reached here, we referenced either a symbol that we don't know about
346 // or an id number that hasn't been read yet. We may be referencing something
347 // forward, so just create an entry to be resolved later and get to it...
348 //
Chris Lattner64116f92004-07-14 01:33:11 +0000349 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000350
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000351 // Remember where this forward reference came from. FIXME, shouldn't we try
352 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000353 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000354 llvmAsmlineno)));
355
Chris Lattner79df7c02002-03-26 18:01:55 +0000356 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000357 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000358 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000359 InsertValue(V, CurModule.LateResolveValues);
360 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000361}
362
Chris Lattner2e2ed292004-07-14 06:28:35 +0000363/// getBBVal - This is used for two purposes:
364/// * If isDefinition is true, a new basic block with the specified ID is being
365/// defined.
366/// * If isDefinition is true, this is a reference to a basic block, which may
367/// or may not be a forward reference.
368///
369static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000370 assert(inFunctionScope() && "Can't get basic block at global scope!");
371
Chris Lattner2e2ed292004-07-14 06:28:35 +0000372 std::string Name;
373 BasicBlock *BB = 0;
374 switch (ID.Type) {
375 default: ThrowException("Illegal label reference " + ID.getName());
376 case ValID::NumberVal: // Is it a numbered definition?
377 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
378 CurFun.NumberedBlocks.resize(ID.Num+1);
379 BB = CurFun.NumberedBlocks[ID.Num];
380 break;
381 case ValID::NameVal: // Is it a named definition?
382 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000383 if (Value *N = CurFun.CurrentFunction->
384 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000385 BB = cast<BasicBlock>(N);
386 break;
387 }
Chris Lattner64116f92004-07-14 01:33:11 +0000388
Chris Lattner2e2ed292004-07-14 06:28:35 +0000389 // See if the block has already been defined.
390 if (BB) {
391 // If this is the definition of the block, make sure the existing value was
392 // just a forward reference. If it was a forward reference, there will be
393 // an entry for it in the PlaceHolderInfo map.
394 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
395 // The existing value was a definition, not a forward reference.
396 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000397
Chris Lattner2e2ed292004-07-14 06:28:35 +0000398 ID.destroy(); // Free strdup'd memory.
399 return BB;
400 }
401
402 // Otherwise this block has not been seen before.
403 BB = new BasicBlock("", CurFun.CurrentFunction);
404 if (ID.Type == ValID::NameVal) {
405 BB->setName(ID.Name);
406 } else {
407 CurFun.NumberedBlocks[ID.Num] = BB;
408 }
409
410 // If this is not a definition, keep track of it so we can use it as a forward
411 // reference.
412 if (!isDefinition) {
413 // Remember where this forward reference came from.
414 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
415 } else {
416 // The forward declaration could have been inserted anywhere in the
417 // function: insert it into the correct place now.
418 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
419 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
420 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000421 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000422 return BB;
423}
424
Chris Lattner00950542001-06-06 20:29:01 +0000425
426//===----------------------------------------------------------------------===//
427// Code to handle forward references in instructions
428//===----------------------------------------------------------------------===//
429//
430// This code handles the late binding needed with statements that reference
431// values not defined yet... for example, a forward branch, or the PHI node for
432// a loop body.
433//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000434// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000435// and back patchs after we are done.
436//
437
Misha Brukman5a2a3822005-05-10 22:02:28 +0000438// ResolveDefinitions - If we could not resolve some defs at parsing
439// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000440// defs now...
441//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000442static void
443ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
444 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000445 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000446 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000447 E = LateResolvers.end(); LRI != E; ++LRI) {
448 ValueList &List = LRI->second;
449 while (!List.empty()) {
450 Value *V = List.back();
451 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000452
453 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
454 CurModule.PlaceHolderInfo.find(V);
455 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
456
457 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000458
Chris Lattner735f2702004-07-08 22:30:50 +0000459 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000460 if (TheRealValue) {
461 V->replaceAllUsesWith(TheRealValue);
462 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000463 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000464 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000465 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000466 // resolver table
467 InsertValue(V, *FutureLateResolvers);
468 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000469 if (DID.Type == ValID::NameVal)
470 ThrowException("Reference to an invalid definition: '" +DID.getName()+
471 "' of type '" + V->getType()->getDescription() + "'",
472 PHI->second.second);
473 else
474 ThrowException("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000475 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000476 V->getType()->getDescription() + "'",
477 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000478 }
Chris Lattner00950542001-06-06 20:29:01 +0000479 }
480 }
481
482 LateResolvers.clear();
483}
484
Chris Lattner4a42e902001-10-22 05:56:09 +0000485// ResolveTypeTo - A brand new type was just declared. This means that (if
486// name is not null) things referencing Name can be resolved. Otherwise, things
487// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000488//
Chris Lattner4a42e902001-10-22 05:56:09 +0000489static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000490 ValID D;
491 if (Name) D = ValID::create(Name);
492 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000493
Chris Lattner355ad1f2005-03-21 06:27:42 +0000494 std::map<ValID, PATypeHolder>::iterator I =
495 CurModule.LateResolveTypes.find(D);
496 if (I != CurModule.LateResolveTypes.end()) {
497 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
498 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000499 }
500}
501
Chris Lattner1781aca2001-09-18 04:00:54 +0000502// setValueName - Set the specified value to the name given. The name may be
503// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000504// assumed to be a malloc'd string buffer, and is free'd by this function.
505//
506static void setValueName(Value *V, char *NameStr) {
507 if (NameStr) {
508 std::string Name(NameStr); // Copy string
509 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000510
Misha Brukman5a2a3822005-05-10 22:02:28 +0000511 if (V->getType() == Type::VoidTy)
Chris Lattnerc9aea522004-07-13 08:12:39 +0000512 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000513
Chris Lattner8ab406d2004-07-13 07:59:27 +0000514 assert(inFunctionScope() && "Must be in function scope!");
515 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
516 if (ST.lookup(V->getType(), Name))
517 ThrowException("Redefinition of value named '" + Name + "' in the '" +
518 V->getType()->getDescription() + "' type plane!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000519
Chris Lattnerc9aea522004-07-13 08:12:39 +0000520 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000521 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000522 }
523}
524
Chris Lattnerd88998d2004-07-14 08:23:52 +0000525/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
526/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000527static GlobalVariable *
528ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
529 bool isConstantGlobal, const Type *Ty,
530 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000531 if (isa<FunctionType>(Ty))
532 ThrowException("Cannot declare global vars of function type!");
533
Misha Brukman5a2a3822005-05-10 22:02:28 +0000534 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000535
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000536 std::string Name;
537 if (NameStr) {
538 Name = NameStr; // Copy string
539 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000540 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000541
Chris Lattner8a327842004-07-14 21:44:00 +0000542 // See if this global value was forward referenced. If so, recycle the
543 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000544 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000545 if (!Name.empty()) {
546 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000547 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000548 ID = ValID::create((int)CurModule.Values[PTy].size());
549 }
550
551 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000552 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000553 // previously inserted.
554 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
555 CurModule.CurrentModule->getGlobalList().remove(GV);
556 CurModule.CurrentModule->getGlobalList().push_back(GV);
557 GV->setInitializer(Initializer);
558 GV->setLinkage(Linkage);
559 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000560 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000561 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000562 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000563
564 // If this global has a name, check to see if there is already a definition
565 // of this global in the module. If so, merge as appropriate. Note that
566 // this is really just a hack around problems in the CFE. :(
567 if (!Name.empty()) {
568 // We are a simple redefinition of a value, check to see if it is defined
569 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000570 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000571 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
572 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000573 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000574 // 2. If both initializers have the same value.
575 //
576 if (!EGV->hasInitializer() || !Initializer ||
577 EGV->getInitializer() == Initializer) {
578
579 // Make sure the existing global version gets the initializer! Make
580 // sure that it also gets marked const if the new version is.
581 if (Initializer && !EGV->hasInitializer())
582 EGV->setInitializer(Initializer);
583 if (isConstantGlobal)
584 EGV->setConstant(true);
585 EGV->setLinkage(Linkage);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000586 return EGV;
Chris Lattnera09000d2004-07-14 23:03:46 +0000587 }
588
Misha Brukman5a2a3822005-05-10 22:02:28 +0000589 ThrowException("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000590 "' in the '" + Ty->getDescription() + "' type plane!");
591 }
592 }
593
594 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000595 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000596 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000597 CurModule.CurrentModule);
598 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000599 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000600}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000601
Reid Spencer75719bf2004-05-26 21:48:31 +0000602// setTypeName - Set the specified type to the name given. The name may be
603// null potentially, in which case this is a noop. The string passed in is
604// assumed to be a malloc'd string buffer, and is freed by this function.
605//
606// This function returns true if the type has already been defined, but is
607// allowed to be redefined in the specified context. If the name is a new name
608// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000609static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000610 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000611 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000612
Reid Spencer75719bf2004-05-26 21:48:31 +0000613 std::string Name(NameStr); // Copy string
614 free(NameStr); // Free old string
615
616 // We don't allow assigning names to void type
Misha Brukman5a2a3822005-05-10 22:02:28 +0000617 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000618 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000619
Chris Lattnera09000d2004-07-14 23:03:46 +0000620 // Set the type name, checking for conflicts as we do so.
621 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000622
Chris Lattnera09000d2004-07-14 23:03:46 +0000623 if (AlreadyExists) { // Inserting a name that is already defined???
624 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
625 assert(Existing && "Conflict but no matching type?");
626
Reid Spencer75719bf2004-05-26 21:48:31 +0000627 // There is only one case where this is allowed: when we are refining an
628 // opaque type. In this case, Existing will be an opaque type.
629 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
630 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000631 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000632 return true;
633 }
634
635 // Otherwise, this is an attempt to redefine a type. That's okay if
636 // the redefinition is identical to the original. This will be so if
637 // Existing and T point to the same Type object. In this one case we
638 // allow the equivalent redefinition.
639 if (Existing == T) return true; // Yes, it's equal.
640
641 // Any other kind of (non-equivalent) redefinition is an error.
642 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000643 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000644 }
645
Reid Spencer75719bf2004-05-26 21:48:31 +0000646 return false;
647}
Chris Lattner8896eda2001-07-09 19:38:36 +0000648
Chris Lattner30c89792001-09-07 16:35:17 +0000649//===----------------------------------------------------------------------===//
650// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000651//
Chris Lattner8896eda2001-07-09 19:38:36 +0000652
Chris Lattner3b073862004-02-09 03:19:29 +0000653// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000654//
655static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000656 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000657 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000658}
659
660namespace {
661 struct UpRefRecord {
662 // NestingLevel - The number of nesting levels that need to be popped before
663 // this type is resolved.
664 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000665
Chris Lattner3b073862004-02-09 03:19:29 +0000666 // LastContainedTy - This is the type at the current binding level for the
667 // type. Every time we reduce the nesting level, this gets updated.
668 const Type *LastContainedTy;
669
670 // UpRefTy - This is the actual opaque type that the upreference is
671 // represented with.
672 OpaqueType *UpRefTy;
673
674 UpRefRecord(unsigned NL, OpaqueType *URTy)
675 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
676 };
Chris Lattner30c89792001-09-07 16:35:17 +0000677}
Chris Lattner698b56e2001-07-20 19:15:08 +0000678
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000679// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000680static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000681
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000682/// HandleUpRefs - Every time we finish a new layer of types, this function is
683/// called. It loops through the UpRefs vector, which is a list of the
684/// currently active types. For each type, if the up reference is contained in
685/// the newly completed type, we decrement the level count. When the level
686/// count reaches zero, the upreferenced type is the type that is passed in:
687/// thus we can complete the cycle.
688///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000689static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000690 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000691 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000692 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000693 "' newly formed. Resolving upreferences.\n" <<
694 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000695
696 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
697 // to zero), we resolve them all together before we resolve them to Ty. At
698 // the end of the loop, if there is anything to resolve to Ty, it will be in
699 // this variable.
700 OpaqueType *TypeToResolve = 0;
701
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000702 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000703 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
704 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000705 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000706 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
707 // Decrement level of upreference
708 unsigned Level = --UpRefs[i].NestingLevel;
709 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000710 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000711 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000712 if (!TypeToResolve) {
713 TypeToResolve = UpRefs[i].UpRefTy;
714 } else {
715 UR_OUT(" * Resolving upreference for "
716 << UpRefs[i].second->getDescription() << "\n";
717 std::string OldName = UpRefs[i].UpRefTy->getDescription());
718 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
719 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
720 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
721 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000722 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000723 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000724 }
725 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000726 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000727
Chris Lattner026a8ce2004-02-09 18:53:54 +0000728 if (TypeToResolve) {
729 UR_OUT(" * Resolving upreference for "
730 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000731 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000732 TypeToResolve->refineAbstractTypeTo(Ty);
733 }
734
Chris Lattner8896eda2001-07-09 19:38:36 +0000735 return Ty;
736}
737
Chris Lattner30c89792001-09-07 16:35:17 +0000738
Chris Lattner6184feb2005-05-20 03:25:47 +0000739// common code from the two 'RunVMAsmParser' functions
740 static Module * RunParser(Module * M) {
741
742 llvmAsmlineno = 1; // Reset the current line number...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000743 ObsoleteVarArgs = false;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000744 NewVarArgs = false;
Chris Lattner6184feb2005-05-20 03:25:47 +0000745
746 CurModule.CurrentModule = M;
747 yyparse(); // Parse the file, potentially throwing exception
748
749 Module *Result = ParserResult;
750 ParserResult = 0;
751
Andrew Lenharth31c98bf2005-06-20 15:41:37 +0000752 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
753 {
754 Function* F;
755 if ((F = Result->getNamedFunction("llvm.va_start"))
756 && F->getFunctionType()->getNumParams() == 0)
757 ObsoleteVarArgs = true;
758 if((F = Result->getNamedFunction("llvm.va_copy"))
759 && F->getFunctionType()->getNumParams() == 1)
760 ObsoleteVarArgs = true;
761 }
762
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000763 if (ObsoleteVarArgs && NewVarArgs)
Chris Lattner548021f2005-06-24 18:00:40 +0000764 ThrowException("This file is corrupt: it uses both new and old style varargs");
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000765
Andrew Lenharth558bc882005-06-18 18:34:52 +0000766 if(ObsoleteVarArgs) {
767 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000768 if (F->arg_size() != 0)
769 ThrowException("Obsolete va_start takes 0 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000770
771 //foo = va_start()
772 // ->
773 //bar = alloca typeof(foo)
774 //va_start(bar)
775 //foo = load bar
776
777 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
778 const Type* ArgTy = F->getFunctionType()->getReturnType();
779 const Type* ArgTyPtr = PointerType::get(ArgTy);
780 Function* NF = Result->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000781 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000782
783 while (!F->use_empty()) {
784 CallInst* CI = cast<CallInst>(F->use_back());
785 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
786 new CallInst(NF, bar, "", CI);
787 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
788 CI->replaceAllUsesWith(foo);
789 CI->getParent()->getInstList().erase(CI);
790 }
791 Result->getFunctionList().erase(F);
792 }
793
794 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000795 if(F->arg_size() != 1)
796 ThrowException("Obsolete va_end takes 1 argument!");
797
Andrew Lenharth558bc882005-06-18 18:34:52 +0000798 //vaend foo
799 // ->
800 //bar = alloca 1 of typeof(foo)
801 //vaend bar
802 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
803 const Type* ArgTy = F->getFunctionType()->getParamType(0);
804 const Type* ArgTyPtr = PointerType::get(ArgTy);
805 Function* NF = Result->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000806 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000807
808 while (!F->use_empty()) {
809 CallInst* CI = cast<CallInst>(F->use_back());
810 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000811 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000812 new CallInst(NF, bar, "", CI);
813 CI->getParent()->getInstList().erase(CI);
814 }
815 Result->getFunctionList().erase(F);
816 }
817
818 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000819 if(F->arg_size() != 1)
820 ThrowException("Obsolete va_copy takes 1 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000821 //foo = vacopy(bar)
822 // ->
823 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000824 //b = alloca 1 of typeof(foo)
825 //store bar -> b
826 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000827 //foo = load a
828
829 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
830 const Type* ArgTy = F->getFunctionType()->getReturnType();
831 const Type* ArgTyPtr = PointerType::get(ArgTy);
832 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000833 RetTy, ArgTyPtr, ArgTyPtr,
834 (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000835
836 while (!F->use_empty()) {
837 CallInst* CI = cast<CallInst>(F->use_back());
838 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000839 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
840 new StoreInst(CI->getOperand(1), b, CI);
841 new CallInst(NF, a, b, "", CI);
842 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000843 CI->replaceAllUsesWith(foo);
844 CI->getParent()->getInstList().erase(CI);
845 }
846 Result->getFunctionList().erase(F);
847 }
848 }
849
Chris Lattner6184feb2005-05-20 03:25:47 +0000850 return Result;
851
852 }
853
Chris Lattner00950542001-06-06 20:29:01 +0000854//===----------------------------------------------------------------------===//
855// RunVMAsmParser - Define an interface to this parser
856//===----------------------------------------------------------------------===//
857//
Chris Lattner86427632004-07-14 06:39:48 +0000858Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000859 set_scan_file(F);
860
Chris Lattnera2850432001-07-22 18:36:00 +0000861 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000862 return RunParser(new Module(CurFilename));
863}
Chris Lattner00950542001-06-06 20:29:01 +0000864
Chris Lattner6184feb2005-05-20 03:25:47 +0000865Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
866 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000867
Chris Lattner6184feb2005-05-20 03:25:47 +0000868 CurFilename = "from_memory";
869 if (M == NULL) {
870 return RunParser(new Module (CurFilename));
871 } else {
872 return RunParser(M);
873 }
Chris Lattner00950542001-06-06 20:29:01 +0000874}
875
876%}
877
878%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000879 llvm::Module *ModuleVal;
880 llvm::Function *FunctionVal;
881 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
882 llvm::BasicBlock *BasicBlockVal;
883 llvm::TerminatorInst *TermInstVal;
884 llvm::Instruction *InstVal;
885 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000886
Brian Gaeked0fde302003-11-11 22:41:34 +0000887 const llvm::Type *PrimType;
888 llvm::PATypeHolder *TypeVal;
889 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000890
Brian Gaeked0fde302003-11-11 22:41:34 +0000891 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
892 std::vector<llvm::Value*> *ValueList;
893 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000894 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000895 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000896 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000897 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
898 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000899
Brian Gaeked0fde302003-11-11 22:41:34 +0000900 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000901 int64_t SInt64Val;
902 uint64_t UInt64Val;
903 int SIntVal;
904 unsigned UIntVal;
905 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000906 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000907
Chris Lattner30c89792001-09-07 16:35:17 +0000908 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000909 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000910
Brian Gaeked0fde302003-11-11 22:41:34 +0000911 llvm::Instruction::BinaryOps BinaryOpVal;
912 llvm::Instruction::TermOps TermOpVal;
913 llvm::Instruction::MemoryOps MemOpVal;
914 llvm::Instruction::OtherOps OtherOpVal;
915 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000916}
917
Chris Lattner79df7c02002-03-26 18:01:55 +0000918%type <ModuleVal> Module FunctionList
919%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000920%type <BasicBlockVal> BasicBlock InstructionList
921%type <TermInstVal> BBTerminatorInst
922%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000923%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000924%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000925%type <ArgList> ArgList ArgListH
926%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000927%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000928%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000929%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000930%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000931%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000932%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000933%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000934%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000935%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000936%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000937
Chris Lattner2079fde2001-10-13 06:41:08 +0000938// ValueRef - Unresolved reference to a definition or BB
939%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000940%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000941// Tokens and types for handling constant integer values
942//
943// ESINT64VAL - A negative number within long long range
944%token <SInt64Val> ESINT64VAL
945
946// EUINT64VAL - A positive number within uns. long long range
947%token <UInt64Val> EUINT64VAL
948%type <SInt64Val> EINT64VAL
949
950%token <SIntVal> SINTVAL // Signed 32 bit ints...
951%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
952%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000953%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000954
955// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000956%type <TypeVal> Types TypesV UpRTypes UpRTypesV
957%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000958%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
959%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000960
Chris Lattner6c23f572003-08-22 05:42:10 +0000961%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
962%type <StrVal> Name OptName OptAssign
Chris Lattner87ac9722005-11-06 06:46:28 +0000963%type <UIntVal> OptAlign OptCAlign
Chris Lattnerb2b96672005-11-12 18:21:21 +0000964%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +0000965
Chris Lattner91ef4602004-03-31 03:49:47 +0000966%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner164c3782005-11-12 00:11:10 +0000967%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000968%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Nate Begeman14b05292005-11-05 09:21:28 +0000969%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
Chris Lattner71cdba32006-01-24 00:40:17 +0000970%token DEPLIBS CALL TAIL ASM_TOK MODULE
Chris Lattnera8e8f162005-05-06 20:27:19 +0000971%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK
972%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +0000973
Misha Brukman5a2a3822005-05-10 22:02:28 +0000974// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000975%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000976
Misha Brukman5a2a3822005-05-10 22:02:28 +0000977// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000978%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000979%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000980%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000981
982// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000983%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000984
Chris Lattner027dcc52001-07-08 21:10:27 +0000985// Other Operators
986%type <OtherOpVal> ShiftOps
Robert Bocchino2def1b32006-01-17 20:06:25 +0000987%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG
988%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT
Andrew Lenharth558bc882005-06-18 18:34:52 +0000989%token VAARG_old VANEXT_old //OBSOLETE
Chris Lattnerddb6db42005-05-06 05:51:46 +0000990
Chris Lattner027dcc52001-07-08 21:10:27 +0000991
Chris Lattner00950542001-06-06 20:29:01 +0000992%start Module
993%%
994
995// Handle constant integer size restriction and conversion...
996//
Chris Lattner51727be2002-06-04 21:58:56 +0000997INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000998INTVAL : UINTVAL {
999 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
1000 ThrowException("Value too large for type!");
1001 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +00001002};
Chris Lattner00950542001-06-06 20:29:01 +00001003
1004
Chris Lattner51727be2002-06-04 21:58:56 +00001005EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +00001006EINT64VAL : EUINT64VAL {
1007 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1008 ThrowException("Value too large for type!");
1009 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +00001010};
Chris Lattner00950542001-06-06 20:29:01 +00001011
Misha Brukman5a2a3822005-05-10 22:02:28 +00001012// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001013// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1014//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001015ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1016LogicalOps : AND | OR | XOR;
1017SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +00001018
Chris Lattner51727be2002-06-04 21:58:56 +00001019ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001020
Chris Lattnere98dda62001-07-14 06:10:16 +00001021// These are some types that allow classification if we only want a particular
1022// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001023SIntType : LONG | INT | SHORT | SBYTE;
1024UIntType : ULONG | UINT | USHORT | UBYTE;
1025IntType : SIntType | UIntType;
1026FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001027
Chris Lattnere98dda62001-07-14 06:10:16 +00001028// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001029OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001030 $$ = $1;
1031 }
Misha Brukman5a2a3822005-05-10 22:02:28 +00001032 | /*empty*/ {
1033 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001034 };
Chris Lattner00950542001-06-06 20:29:01 +00001035
Chris Lattner4ad02e72003-04-16 20:28:45 +00001036OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1037 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001038 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001039 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1040 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001041
Chris Lattnera8e8f162005-05-06 20:27:19 +00001042OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1043 CCC_TOK { $$ = CallingConv::C; } |
1044 FASTCC_TOK { $$ = CallingConv::Fast; } |
1045 COLDCC_TOK { $$ = CallingConv::Cold; } |
1046 CC_TOK EUINT64VAL {
1047 if ((unsigned)$2 != $2)
1048 ThrowException("Calling conv too large!");
1049 $$ = $2;
1050 };
1051
Chris Lattner66db8e42005-11-06 06:34:12 +00001052// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1053// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001054OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001055 ALIGN EUINT64VAL {
1056 $$ = $2;
1057 if ($$ != 0 && !isPowerOf2_32($$))
1058 ThrowException("Alignment must be a power of two!");
1059};
Chris Lattner66db8e42005-11-06 06:34:12 +00001060OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001061 ',' ALIGN EUINT64VAL {
1062 $$ = $3;
1063 if ($$ != 0 && !isPowerOf2_32($$))
1064 ThrowException("Alignment must be a power of two!");
1065};
1066
Chris Lattner66db8e42005-11-06 06:34:12 +00001067
Chris Lattner164c3782005-11-12 00:11:10 +00001068SectionString : SECTION STRINGCONSTANT {
1069 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1070 if ($2[i] == '"' || $2[i] == '\\')
1071 ThrowException("Invalid character in section name!");
1072 $$ = $2;
1073};
1074
1075OptSection : /*empty*/ { $$ = 0; } |
1076 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001077
Chris Lattnerb2b96672005-11-12 18:21:21 +00001078// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1079// is set to be the global we are processing.
1080//
1081GlobalVarAttributes : /* empty */ {} |
1082 ',' GlobalVarAttribute GlobalVarAttributes {};
1083GlobalVarAttribute : SectionString {
1084 CurGV->setSection($1);
1085 free($1);
1086 }
1087 | ALIGN EUINT64VAL {
1088 if ($2 != 0 && !isPowerOf2_32($2))
1089 ThrowException("Alignment must be a power of two!");
1090 CurGV->setAlignment($2);
1091 };
Chris Lattner164c3782005-11-12 00:11:10 +00001092
Chris Lattner30c89792001-09-07 16:35:17 +00001093//===----------------------------------------------------------------------===//
1094// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001095// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001096// access to it, a user must explicitly use TypesV.
1097//
1098
1099// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001100TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1101UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001102
1103Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001104 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001105 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1106 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001107 };
Chris Lattner30c89792001-09-07 16:35:17 +00001108
1109
1110// Derived types are added later...
1111//
Chris Lattner51727be2002-06-04 21:58:56 +00001112PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1113PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001114UpRTypes : OPAQUE {
1115 $$ = new PATypeHolder(OpaqueType::get());
1116 }
1117 | PrimType {
1118 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001119 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001120UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001121 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001122};
Chris Lattner30c89792001-09-07 16:35:17 +00001123
Chris Lattner30c89792001-09-07 16:35:17 +00001124// Include derived types in the Types production.
1125//
1126UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001127 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001128 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001129 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001130 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001131 UR_OUT("New Upreference!\n");
1132 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001133 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001134 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001135 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1136 E = $3->end(); I != E; ++I)
1137 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001138 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1139 if (isVarArg) Params.pop_back();
1140
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001141 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001142 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001143 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001144 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001145 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001146 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001147 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001148 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001149 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1150 const llvm::Type* ElemTy = $4->get();
Chris Lattner9547d7f2005-11-10 01:42:43 +00001151 if ((unsigned)$2 != $2)
Brian Gaeke715c90b2004-08-20 06:00:58 +00001152 ThrowException("Unsigned result not equal to signed result");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001153 if (!ElemTy->isPrimitiveType())
Brian Gaeke715c90b2004-08-20 06:00:58 +00001154 ThrowException("Elemental type of a PackedType must be primitive");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001155 if (!isPowerOf2_32($2))
1156 ThrowException("Vector length should be a power of 2!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001157 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1158 delete $4;
1159 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001160 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001161 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001162 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1163 E = $2->end(); I != E; ++I)
1164 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001165
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001166 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001167 delete $2;
1168 }
1169 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001170 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001171 }
1172 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001173 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001174 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001175 };
Chris Lattner30c89792001-09-07 16:35:17 +00001176
Chris Lattner7e708292002-06-25 16:13:24 +00001177// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001178// declaration type lists
1179//
1180TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001181 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001182 $$->push_back(*$1); delete $1;
1183 }
1184 | TypeListI ',' UpRTypes {
1185 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001186 };
Chris Lattner30c89792001-09-07 16:35:17 +00001187
Chris Lattner7e708292002-06-25 16:13:24 +00001188// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001189ArgTypeListI : TypeListI
1190 | TypeListI ',' DOTDOTDOT {
1191 ($$=$1)->push_back(Type::VoidTy);
1192 }
1193 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001194 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001195 }
1196 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001197 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001198 };
Chris Lattner30c89792001-09-07 16:35:17 +00001199
Chris Lattnere98dda62001-07-14 06:10:16 +00001200// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001201// production is used ONLY to represent constants that show up AFTER a 'const',
1202// 'constant' or 'global' token at global scope. Constants that can be inlined
1203// into other expressions (such as integers and constexprs) are handled by the
1204// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001205//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001206ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001207 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001208 if (ATy == 0)
1209 ThrowException("Cannot make array constant with type: '" +
1210 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001211 const Type *ETy = ATy->getElementType();
1212 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001213
Chris Lattner30c89792001-09-07 16:35:17 +00001214 // Verify that we have the correct size...
1215 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001216 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001217 utostr($3->size()) + " arguments, but has size of " +
1218 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001219
Chris Lattner30c89792001-09-07 16:35:17 +00001220 // Verify all elements are correct type!
1221 for (unsigned i = 0; i < $3->size(); i++) {
1222 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001223 ThrowException("Element #" + utostr(i) + " is not of type '" +
1224 ETy->getDescription() +"' as required!\nIt is of type '"+
1225 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001226 }
1227
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001228 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001229 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001230 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001231 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001232 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001233 if (ATy == 0)
1234 ThrowException("Cannot make array constant with type: '" +
1235 (*$1)->getDescription() + "'!");
1236
1237 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001238 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001239 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001240 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001241 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001242 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001243 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001244 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001245 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001246 if (ATy == 0)
1247 ThrowException("Cannot make array constant with type: '" +
1248 (*$1)->getDescription() + "'!");
1249
Chris Lattner30c89792001-09-07 16:35:17 +00001250 int NumElements = ATy->getNumElements();
1251 const Type *ETy = ATy->getElementType();
1252 char *EndStr = UnEscapeLexed($3, true);
1253 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001254 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001255 itostr((int)(EndStr-$3)) +
1256 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001257 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001258 if (ETy == Type::SByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001259 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001260 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001261 } else if (ETy == Type::UByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001262 for (unsigned char *C = (unsigned char *)$3;
1263 C != (unsigned char*)EndStr; ++C)
1264 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001265 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001266 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001267 ThrowException("Cannot build string arrays of non byte sized elements!");
1268 }
Chris Lattner30c89792001-09-07 16:35:17 +00001269 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001270 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001271 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001272 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001273 | Types '<' ConstVector '>' { // Nonempty unsized arr
1274 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1275 if (PTy == 0)
1276 ThrowException("Cannot make packed constant with type: '" +
1277 (*$1)->getDescription() + "'!");
1278 const Type *ETy = PTy->getElementType();
1279 int NumElements = PTy->getNumElements();
1280
1281 // Verify that we have the correct size...
1282 if (NumElements != -1 && NumElements != (int)$3->size())
1283 ThrowException("Type mismatch: constant sized packed initialized with " +
1284 utostr($3->size()) + " arguments, but has size of " +
1285 itostr(NumElements) + "!");
1286
1287 // Verify all elements are correct type!
1288 for (unsigned i = 0; i < $3->size(); i++) {
1289 if (ETy != (*$3)[i]->getType())
1290 ThrowException("Element #" + utostr(i) + " is not of type '" +
1291 ETy->getDescription() +"' as required!\nIt is of type '"+
1292 (*$3)[i]->getType()->getDescription() + "'.");
1293 }
1294
1295 $$ = ConstantPacked::get(PTy, *$3);
1296 delete $1; delete $3;
1297 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001298 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001299 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001300 if (STy == 0)
1301 ThrowException("Cannot make struct constant with type: '" +
1302 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001303
Chris Lattnerc6212f12003-05-21 16:06:56 +00001304 if ($3->size() != STy->getNumContainedTypes())
1305 ThrowException("Illegal number of initializers for structure type!");
1306
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001307 // Check to ensure that constants are compatible with the type initializer!
1308 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001309 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001310 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001311 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001312 "' for element #" + utostr(i) +
1313 " of structure initializer!");
1314
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001315 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001316 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001317 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001318 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001319 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001320 if (STy == 0)
1321 ThrowException("Cannot make struct constant with type: '" +
1322 (*$1)->getDescription() + "'!");
1323
1324 if (STy->getNumContainedTypes() != 0)
1325 ThrowException("Illegal number of initializers for structure type!");
1326
1327 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1328 delete $1;
1329 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001330 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001331 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001332 if (PTy == 0)
1333 ThrowException("Cannot make null pointer constant with type: '" +
1334 (*$1)->getDescription() + "'!");
1335
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001336 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001337 delete $1;
1338 }
Chris Lattner16710e92004-10-16 18:17:13 +00001339 | Types UNDEF {
1340 $$ = UndefValue::get($1->get());
1341 delete $1;
1342 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001343 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001344 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001345 if (Ty == 0)
1346 ThrowException("Global const reference must be a pointer type!");
1347
Chris Lattner3101c252002-08-15 17:58:33 +00001348 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001349 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001350 // the context of a function, getValNonImprovising will search the functions
1351 // symbol table instead of the module symbol table for the global symbol,
1352 // which throws things all off. To get around this, we just tell
1353 // getValNonImprovising that we are at global scope here.
1354 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001355 Function *SavedCurFn = CurFun.CurrentFunction;
1356 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001357
Chris Lattner2079fde2001-10-13 06:41:08 +00001358 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001359
Chris Lattner394f2eb2003-10-16 20:12:13 +00001360 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001361
Chris Lattner2079fde2001-10-13 06:41:08 +00001362 // If this is an initializer for a constant pointer, which is referencing a
1363 // (currently) undefined variable, create a stub now that shall be replaced
1364 // in the future with the right type of variable.
1365 //
1366 if (V == 0) {
1367 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1368 const PointerType *PT = cast<PointerType>(Ty);
1369
1370 // First check to see if the forward references value is already created!
1371 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001372 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001373
1374 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001375 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001376 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001377 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001378 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001379 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001380
Reid Spencer3271ed52004-07-18 00:08:11 +00001381 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001382 GlobalValue *GV;
1383 if (const FunctionType *FTy =
1384 dyn_cast<FunctionType>(PT->getElementType())) {
1385 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1386 CurModule.CurrentModule);
1387 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001388 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001389 GlobalValue::ExternalLinkage, 0,
1390 Name, CurModule.CurrentModule);
1391 }
Chris Lattner8a327842004-07-14 21:44:00 +00001392
Reid Spencer3271ed52004-07-18 00:08:11 +00001393 // Keep track of the fact that we have a forward ref to recycle it
1394 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1395 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001396 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001397 }
1398
Reid Spencer3271ed52004-07-18 00:08:11 +00001399 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001400 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001401 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001402 | Types ConstExpr {
1403 if ($1->get() != $2->getType())
1404 ThrowException("Mismatched types for constant expression!");
1405 $$ = $2;
1406 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001407 }
1408 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001409 const Type *Ty = $1->get();
1410 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1411 ThrowException("Cannot create a null initialized value of this type!");
1412 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001413 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001414 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001415
Chris Lattnere43f40b2002-10-09 00:25:32 +00001416ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001417 if (!ConstantSInt::isValueValidForType($1, $2))
1418 ThrowException("Constant value doesn't fit in type!");
1419 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001420 }
1421 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001422 if (!ConstantUInt::isValueValidForType($1, $2))
1423 ThrowException("Constant value doesn't fit in type!");
1424 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001425 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001426 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001427 $$ = ConstantBool::True;
1428 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001429 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001430 $$ = ConstantBool::False;
1431 }
1432 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001433 if (!ConstantFP::isValueValidForType($1, $2))
1434 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001435 $$ = ConstantFP::get($1, $2);
1436 };
1437
Chris Lattner00950542001-06-06 20:29:01 +00001438
Chris Lattnerd78700d2002-08-16 21:14:40 +00001439ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001440 if (!$3->getType()->isFirstClassType())
1441 ThrowException("cast constant expression from a non-primitive type: '" +
1442 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001443 if (!$5->get()->isFirstClassType())
1444 ThrowException("cast constant expression to a non-primitive type: '" +
1445 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001446 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001447 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001448 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001449 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1450 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001451 ThrowException("GetElementPtr requires a pointer operand!");
1452
Chris Lattner830b6f92004-04-05 01:30:04 +00001453 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1454 // indices to uint struct indices for compatibility.
1455 generic_gep_type_iterator<std::vector<Value*>::iterator>
1456 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1457 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1458 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1459 if (isa<StructType>(*GTI)) // Only change struct indices
1460 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1461 if (CUI->getType() == Type::UByteTy)
1462 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1463
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001464 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001465 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001466 if (!IdxTy)
1467 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001468
Chris Lattner9232b992003-04-22 18:42:41 +00001469 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001470 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1471 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001472 IdxVec.push_back(C);
1473 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001474 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001475
Chris Lattnerd78700d2002-08-16 21:14:40 +00001476 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001477
Chris Lattnerd78700d2002-08-16 21:14:40 +00001478 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001479 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001480 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1481 if ($3->getType() != Type::BoolTy)
1482 ThrowException("Select condition must be of boolean type!");
1483 if ($5->getType() != $7->getType())
1484 ThrowException("Select operand types must match!");
1485 $$ = ConstantExpr::getSelect($3, $5, $7);
1486 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001487 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001488 if ($3->getType() != $5->getType())
1489 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001490 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1491 // To retain backward compatibility with these early compilers, we emit a
1492 // cast to the appropriate integer type automatically if we are in the
1493 // broken case. See PR424 for more information.
1494 if (!isa<PointerType>($3->getType())) {
1495 $$ = ConstantExpr::get($1, $3, $5);
1496 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001497 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001498 switch (CurModule.CurrentModule->getPointerSize()) {
1499 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1500 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1501 default: ThrowException("invalid pointer binary constant expr!");
1502 }
1503 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1504 ConstantExpr::getCast($5, IntPtrTy));
1505 $$ = ConstantExpr::getCast($$, $3->getType());
1506 }
1507 }
1508 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1509 if ($3->getType() != $5->getType())
1510 ThrowException("Logical operator types must match!");
Chris Lattner0a017832005-12-21 18:31:29 +00001511 if (!$3->getType()->isIntegral()) {
1512 if (!isa<PackedType>($3->getType()) ||
1513 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
1514 ThrowException("Logical operator requires integral operands!");
1515 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001516 $$ = ConstantExpr::get($1, $3, $5);
1517 }
1518 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1519 if ($3->getType() != $5->getType())
1520 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001521 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001522 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001523 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001524 if ($5->getType() != Type::UByteTy)
1525 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001526 if (!$3->getType()->isInteger())
1527 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001528 $$ = ConstantExpr::get($1, $3, $5);
Robert Bocchino9c62b562006-01-10 19:04:32 +00001529 }
1530 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
1531 if (!isa<PackedType>($3->getType()))
1532 ThrowException("First operand of extractelement must be "
1533 "packed type!");
1534 if ($5->getType() != Type::UIntTy)
1535 ThrowException("Second operand of extractelement must be uint!");
1536 $$ = ConstantExpr::getExtractElement($3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001537 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001538
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001539// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001540ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001541 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001542 }
1543 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001544 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001545 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001546 };
Chris Lattner00950542001-06-06 20:29:01 +00001547
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001548
Chris Lattner1781aca2001-09-18 04:00:54 +00001549// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001550GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001551
Chris Lattner00950542001-06-06 20:29:01 +00001552
Chris Lattner0e73ce62002-05-02 19:11:13 +00001553//===----------------------------------------------------------------------===//
1554// Rules to match Modules
1555//===----------------------------------------------------------------------===//
1556
1557// Module rule: Capture the result of parsing the whole file into a result
1558// variable...
1559//
1560Module : FunctionList {
1561 $$ = ParserResult = $1;
1562 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001563};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001564
Chris Lattner7e708292002-06-25 16:13:24 +00001565// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001566//
1567FunctionList : FunctionList Function {
1568 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001569 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001570 }
1571 | FunctionList FunctionProto {
1572 $$ = $1;
1573 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001574 | FunctionList MODULE ASM_TOK AsmBlock {
Chris Lattneree454772006-01-23 23:05:15 +00001575 $$ = $1;
1576 }
Chris Lattner0e73ce62002-05-02 19:11:13 +00001577 | FunctionList IMPLEMENTATION {
1578 $$ = $1;
1579 }
1580 | ConstPool {
1581 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001582 // Emit an error if there are any unresolved types left.
1583 if (!CurModule.LateResolveTypes.empty()) {
1584 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1585 if (DID.Type == ValID::NameVal)
1586 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1587 else
1588 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1589 }
Chris Lattner51727be2002-06-04 21:58:56 +00001590 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001591
Chris Lattnere98dda62001-07-14 06:10:16 +00001592// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001593ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001594 // Eagerly resolve types. This is not an optimization, this is a
1595 // requirement that is due to the fact that we could have this:
1596 //
1597 // %list = type { %list * }
1598 // %list = type { %list * } ; repeated type decl
1599 //
1600 // If types are not resolved eagerly, then the two types will not be
1601 // determined to be the same type!
1602 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001603 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001604
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001605 if (!setTypeName(*$4, $2) && !$2) {
1606 // If this is a named type that is not a redefinition, add it to the slot
1607 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001608 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001609 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001610
1611 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001612 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001613 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001614 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001615 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Chris Lattneree454772006-01-23 23:05:15 +00001616 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001617 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001618 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001619 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
1620 } GlobalVarAttributes {
1621 CurGV = 0;
Chris Lattner1781aca2001-09-18 04:00:54 +00001622 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001623 | ConstPool OptAssign EXTERNAL GlobalType Types {
1624 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage,
1625 $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001626 delete $5;
Chris Lattnerb2b96672005-11-12 18:21:21 +00001627 } GlobalVarAttributes {
1628 CurGV = 0;
Chris Lattnere98dda62001-07-14 06:10:16 +00001629 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001630 | ConstPool TARGET TargetDefinition {
1631 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001632 | ConstPool DEPLIBS '=' LibrariesDefinition {
1633 }
Chris Lattner00950542001-06-06 20:29:01 +00001634 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001635 };
Chris Lattner00950542001-06-06 20:29:01 +00001636
1637
Chris Lattneree454772006-01-23 23:05:15 +00001638AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00001639 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00001640 char *EndStr = UnEscapeLexed($1, true);
1641 std::string NewAsm($1, EndStr);
1642 free($1);
1643
1644 if (AsmSoFar.empty())
Chris Lattner66316012006-01-24 04:14:29 +00001645 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00001646 else
Chris Lattner66316012006-01-24 04:14:29 +00001647 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00001648};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001649
1650BigOrLittle : BIG { $$ = Module::BigEndian; };
1651BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1652
1653TargetDefinition : ENDIAN '=' BigOrLittle {
1654 CurModule.CurrentModule->setEndianness($3);
1655 }
1656 | POINTERSIZE '=' EUINT64VAL {
1657 if ($3 == 32)
1658 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1659 else if ($3 == 64)
1660 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1661 else
1662 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001663 }
1664 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001665 CurModule.CurrentModule->setTargetTriple($3);
1666 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001667 };
1668
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001669LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001670
1671LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001672 CurModule.CurrentModule->addLibrary($3);
1673 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001674 }
1675 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001676 CurModule.CurrentModule->addLibrary($1);
1677 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001678 }
1679 | /* empty: end of list */ {
1680 }
1681 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001682
Chris Lattner00950542001-06-06 20:29:01 +00001683//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001684// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001685//===----------------------------------------------------------------------===//
1686
Chris Lattner6c23f572003-08-22 05:42:10 +00001687Name : VAR_ID | STRINGCONSTANT;
1688OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001689
Chris Lattner6c23f572003-08-22 05:42:10 +00001690ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001691 if (*$1 == Type::VoidTy)
1692 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001693 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001694};
Chris Lattner00950542001-06-06 20:29:01 +00001695
Chris Lattner69da5cf2002-10-13 20:57:00 +00001696ArgListH : ArgListH ',' ArgVal {
1697 $$ = $1;
1698 $1->push_back(*$3);
1699 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001700 }
1701 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001702 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001703 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001704 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001705 };
Chris Lattner00950542001-06-06 20:29:01 +00001706
1707ArgList : ArgListH {
1708 $$ = $1;
1709 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001710 | ArgListH ',' DOTDOTDOT {
1711 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001712 $$->push_back(std::pair<PATypeHolder*,
1713 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001714 }
1715 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001716 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1717 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001718 }
Chris Lattner00950542001-06-06 20:29:01 +00001719 | /* empty */ {
1720 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001721 };
Chris Lattner00950542001-06-06 20:29:01 +00001722
Chris Lattner164c3782005-11-12 00:11:10 +00001723FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1724 OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001725 UnEscapeLexed($3);
1726 std::string FunctionName($3);
1727 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001728
Chris Lattnera8e8f162005-05-06 20:27:19 +00001729 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001730 ThrowException("LLVM functions cannot return aggregate types!");
1731
Chris Lattner9232b992003-04-22 18:42:41 +00001732 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001733 if ($5) { // If there are arguments...
1734 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1735 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001736 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001737 }
Chris Lattner00950542001-06-06 20:29:01 +00001738
Chris Lattner2079fde2001-10-13 06:41:08 +00001739 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1740 if (isVarArg) ParamTypeList.pop_back();
1741
Chris Lattnera8e8f162005-05-06 20:27:19 +00001742 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001743 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001744 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001745
Chris Lattnera09000d2004-07-14 23:03:46 +00001746 ValID ID;
1747 if (!FunctionName.empty()) {
1748 ID = ValID::create((char*)FunctionName.c_str());
1749 } else {
1750 ID = ValID::create((int)CurModule.Values[PFT].size());
1751 }
1752
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001753 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001754 // See if this function was forward referenced. If so, recycle the object.
1755 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1756 // Move the function to the end of the list, from whereever it was
1757 // previously inserted.
1758 Fn = cast<Function>(FWRef);
1759 CurModule.CurrentModule->getFunctionList().remove(Fn);
1760 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1761 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1762 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1763 // If this is the case, either we need to be a forward decl, or it needs
1764 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001765 if (!CurFun.isDeclare && !Fn->isExternal())
1766 ThrowException("Redefinition of function '" + FunctionName + "'!");
1767
Chris Lattnera09000d2004-07-14 23:03:46 +00001768 // Make sure to strip off any argument names so we can't get conflicts.
1769 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001770 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001771 AI != AE; ++AI)
1772 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001773
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001774 } else { // Not already defined?
1775 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1776 CurModule.CurrentModule);
1777 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001778 }
Chris Lattner00950542001-06-06 20:29:01 +00001779
Chris Lattner394f2eb2003-10-16 20:12:13 +00001780 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001781 Fn->setCallingConv($1);
Chris Lattner164c3782005-11-12 00:11:10 +00001782 Fn->setAlignment($8);
1783 if ($7) {
1784 Fn->setSection($7);
1785 free($7);
1786 }
Chris Lattner00950542001-06-06 20:29:01 +00001787
Chris Lattner7e708292002-06-25 16:13:24 +00001788 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001789 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001790 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001791 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001792 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001793 delete $5->back().first;
1794 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001795 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001796 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001797 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1798 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001799 delete I->first; // Delete the typeholder...
1800
Chris Lattner8ab406d2004-07-13 07:59:27 +00001801 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001802 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001803 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001804
Chris Lattnera8e8f162005-05-06 20:27:19 +00001805 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001806 }
Chris Lattner51727be2002-06-04 21:58:56 +00001807};
Chris Lattner00950542001-06-06 20:29:01 +00001808
Chris Lattner9b02cc32002-05-03 18:23:48 +00001809BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1810
Chris Lattner4ad02e72003-04-16 20:28:45 +00001811FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001812 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001813
Chris Lattner4ad02e72003-04-16 20:28:45 +00001814 // Make sure that we keep track of the linkage type even if there was a
1815 // previous "declare".
1816 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001817};
Chris Lattner00950542001-06-06 20:29:01 +00001818
Chris Lattner9b02cc32002-05-03 18:23:48 +00001819END : ENDTOK | '}'; // Allow end of '}' to end a function
1820
Chris Lattner79df7c02002-03-26 18:01:55 +00001821Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001822 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001823};
Chris Lattner00950542001-06-06 20:29:01 +00001824
Chris Lattner394f2eb2003-10-16 20:12:13 +00001825FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1826 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001827 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001828};
Chris Lattner00950542001-06-06 20:29:01 +00001829
1830//===----------------------------------------------------------------------===//
1831// Rules to match Basic Blocks
1832//===----------------------------------------------------------------------===//
1833
1834ConstValueRef : ESINT64VAL { // A reference to a direct constant
1835 $$ = ValID::create($1);
1836 }
1837 | EUINT64VAL {
1838 $$ = ValID::create($1);
1839 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001840 | FPVAL { // Perhaps it's an FP constant?
1841 $$ = ValID::create($1);
1842 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001843 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001844 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001845 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001846 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001847 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001848 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001849 | NULL_TOK {
1850 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001851 }
Chris Lattner16710e92004-10-16 18:17:13 +00001852 | UNDEF {
1853 $$ = ValID::createUndef();
1854 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00001855 | ZEROINITIALIZER { // A vector zero constant.
1856 $$ = ValID::createZeroInit();
1857 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001858 | '<' ConstVector '>' { // Nonempty unsized packed vector
1859 const Type *ETy = (*$2)[0]->getType();
1860 int NumElements = $2->size();
1861
1862 PackedType* pt = PackedType::get(ETy, NumElements);
1863 PATypeHolder* PTy = new PATypeHolder(
1864 HandleUpRefs(
1865 PackedType::get(
1866 ETy,
1867 NumElements)
1868 )
1869 );
1870
1871 // Verify all elements are correct type!
1872 for (unsigned i = 0; i < $2->size(); i++) {
1873 if (ETy != (*$2)[i]->getType())
1874 ThrowException("Element #" + utostr(i) + " is not of type '" +
1875 ETy->getDescription() +"' as required!\nIt is of type '" +
1876 (*$2)[i]->getType()->getDescription() + "'.");
1877 }
1878
1879 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1880 delete PTy; delete $2;
1881 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001882 | ConstExpr {
1883 $$ = ValID::create($1);
1884 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001885
Chris Lattner2079fde2001-10-13 06:41:08 +00001886// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1887// another value.
1888//
1889SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001890 $$ = ValID::create($1);
1891 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001892 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001893 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001894 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001895
1896// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001897ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001898
Chris Lattner00950542001-06-06 20:29:01 +00001899
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001900// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1901// type immediately preceeds the value reference, and allows complex constant
1902// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001903ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001904 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001905 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001906
Chris Lattner00950542001-06-06 20:29:01 +00001907BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001908 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001909 }
Chris Lattner7e708292002-06-25 16:13:24 +00001910 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001911 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001912 };
Chris Lattner00950542001-06-06 20:29:01 +00001913
1914
1915// Basic blocks are terminated by branching instructions:
1916// br, br/cc, switch, ret
1917//
Chris Lattner2079fde2001-10-13 06:41:08 +00001918BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001919 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001920 InsertValue($3);
1921
1922 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001923 InsertValue($1);
1924 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001925 };
Chris Lattner00950542001-06-06 20:29:01 +00001926
1927InstructionList : InstructionList Inst {
1928 $1->getInstList().push_back($2);
1929 $$ = $1;
1930 }
1931 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001932 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001933
1934 // Make sure to move the basic block to the correct location in the
1935 // function, instead of leaving it inserted wherever it was first
1936 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001937 Function::BasicBlockListType &BBL =
1938 CurFun.CurrentFunction->getBasicBlockList();
1939 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner22ae2322004-07-13 08:39:15 +00001940 }
1941 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001942 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001943
1944 // Make sure to move the basic block to the correct location in the
1945 // function, instead of leaving it inserted wherever it was first
1946 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001947 Function::BasicBlockListType &BBL =
1948 CurFun.CurrentFunction->getBasicBlockList();
1949 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner51727be2002-06-04 21:58:56 +00001950 };
Chris Lattner00950542001-06-06 20:29:01 +00001951
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001952BBTerminatorInst : RET ResolvedVal { // Return with a result...
1953 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001954 }
1955 | RET VOID { // Return with no result...
1956 $$ = new ReturnInst();
1957 }
1958 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001959 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001960 } // Conditional Branch...
1961 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001962 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001963 }
1964 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001965 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001966 $$ = S;
1967
Chris Lattner9232b992003-04-22 18:42:41 +00001968 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001969 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001970 for (; I != E; ++I) {
1971 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
1972 S->addCase(CI, I->second);
1973 else
1974 ThrowException("Switch case is constant, but not a simple integer!");
1975 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001976 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001977 }
Chris Lattner196850c2003-05-15 21:30:00 +00001978 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001979 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00001980 $$ = S;
1981 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001982 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1983 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001984 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001985 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001986
Chris Lattnera8e8f162005-05-06 20:27:19 +00001987 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001988 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001989 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001990 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001991 if ($6) {
1992 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00001993 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001994 ParamTypes.push_back((*I)->getType());
1995 }
1996
1997 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1998 if (isVarArg) ParamTypes.pop_back();
1999
Chris Lattnera8e8f162005-05-06 20:27:19 +00002000 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002001 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002002 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002003
Chris Lattnera8e8f162005-05-06 20:27:19 +00002004 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00002005
Chris Lattnera8e8f162005-05-06 20:27:19 +00002006 BasicBlock *Normal = getBBVal($10);
2007 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00002008
2009 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002010 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00002011 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00002012 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002013 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002014 // correctly!
2015 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002016 FunctionType::param_iterator I = Ty->param_begin();
2017 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002018 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00002019
2020 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002021 if ((*ArgI)->getType() != *I)
2022 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2023 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002024
2025 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002026 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002027
Chris Lattnera8e8f162005-05-06 20:27:19 +00002028 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00002029 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002030 cast<InvokeInst>($$)->setCallingConv($2);
2031
2032 delete $3;
2033 delete $6;
Chris Lattner36143fc2003-09-08 18:54:55 +00002034 }
2035 | UNWIND {
2036 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00002037 }
2038 | UNREACHABLE {
2039 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00002040 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002041
2042
Chris Lattner00950542001-06-06 20:29:01 +00002043
2044JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2045 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002046 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00002047 if (V == 0)
2048 ThrowException("May only switch on a constant pool value!");
2049
Chris Lattner64116f92004-07-14 01:33:11 +00002050 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00002051 }
2052 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002053 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002054 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00002055
2056 if (V == 0)
2057 ThrowException("May only switch on a constant pool value!");
2058
Chris Lattner64116f92004-07-14 01:33:11 +00002059 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00002060 };
Chris Lattner00950542001-06-06 20:29:01 +00002061
2062Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00002063 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00002064 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00002065 InsertValue($2);
2066 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00002067};
Chris Lattner00950542001-06-06 20:29:01 +00002068
Chris Lattnerc24d2082001-06-11 15:04:20 +00002069PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00002070 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00002071 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00002072 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002073 }
2074 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2075 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00002076 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00002077 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00002078 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002079
2080
Chris Lattner30c89792001-09-07 16:35:17 +00002081ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00002082 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002083 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00002084 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002085 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00002086 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002087 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00002088 };
Chris Lattner00950542001-06-06 20:29:01 +00002089
2090// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00002091ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00002092
Chris Lattnerddb6db42005-05-06 05:51:46 +00002093OptTailCall : TAIL CALL {
2094 $$ = true;
2095 }
2096 | CALL {
2097 $$ = false;
2098 };
2099
2100
2101
Chris Lattner4a6482b2002-09-10 19:57:26 +00002102InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00002103 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2104 !isa<PackedType>((*$2).get()))
2105 ThrowException(
2106 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00002107 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
2108 ThrowException("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002109 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2110 if ($$ == 0)
2111 ThrowException("binary operator returned null!");
2112 delete $2;
2113 }
2114 | LogicalOps Types ValueRef ',' ValueRef {
Chris Lattner0a017832005-12-21 18:31:29 +00002115 if (!(*$2)->isIntegral()) {
2116 if (!isa<PackedType>($2->get()) ||
2117 !cast<PackedType>($2->get())->getElementType()->isIntegral())
2118 ThrowException("Logical operator requires integral operands!");
2119 }
Chris Lattner4a6482b2002-09-10 19:57:26 +00002120 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2121 if ($$ == 0)
2122 ThrowException("binary operator returned null!");
2123 delete $2;
2124 }
2125 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002126 if(isa<PackedType>((*$2).get())) {
2127 ThrowException(
2128 "PackedTypes currently not supported in setcc instructions!");
2129 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00002130 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00002131 if ($$ == 0)
2132 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002133 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002134 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002135 | NOT ResolvedVal {
2136 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2137 << " Replacing with 'xor'.\n";
2138
2139 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2140 if (Ones == 0)
2141 ThrowException("Expected integral type for not instruction!");
2142
2143 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002144 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00002145 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00002146 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002147 | ShiftOps ResolvedVal ',' ResolvedVal {
2148 if ($4->getType() != Type::UByteTy)
2149 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002150 if (!$2->getType()->isInteger())
2151 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002152 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00002153 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002154 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002155 if (!$4->get()->isFirstClassType())
2156 ThrowException("cast instruction to a non-primitive type: '" +
2157 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002158 $$ = new CastInst($2, *$4);
2159 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002160 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002161 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2162 if ($2->getType() != Type::BoolTy)
2163 ThrowException("select condition must be boolean!");
2164 if ($4->getType() != $6->getType())
2165 ThrowException("select value types should match!");
2166 $$ = new SelectInst($2, $4, $6);
2167 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002168 | VAARG ResolvedVal ',' Types {
Andrew Lenharthe78f50d2005-06-19 03:53:56 +00002169 NewVarArgs = true;
Chris Lattner99e7ab72003-10-18 05:53:13 +00002170 $$ = new VAArgInst($2, *$4);
2171 delete $4;
2172 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002173 | VAARG_old ResolvedVal ',' Types {
2174 ObsoleteVarArgs = true;
2175 const Type* ArgTy = $2->getType();
2176 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002177 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002178
2179 //b = vaarg a, t ->
2180 //foo = alloca 1 of t
2181 //bar = vacopy a
2182 //store bar -> foo
2183 //b = vaarg foo, t
2184 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2185 CurBB->getInstList().push_back(foo);
2186 CallInst* bar = new CallInst(NF, $2);
2187 CurBB->getInstList().push_back(bar);
2188 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2189 $$ = new VAArgInst(foo, *$4);
2190 delete $4;
2191 }
2192 | VANEXT_old ResolvedVal ',' Types {
2193 ObsoleteVarArgs = true;
2194 const Type* ArgTy = $2->getType();
2195 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002196 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002197
2198 //b = vanext a, t ->
2199 //foo = alloca 1 of t
2200 //bar = vacopy a
2201 //store bar -> foo
2202 //tmp = vaarg foo, t
2203 //b = load foo
2204 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2205 CurBB->getInstList().push_back(foo);
2206 CallInst* bar = new CallInst(NF, $2);
2207 CurBB->getInstList().push_back(bar);
2208 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2209 Instruction* tmp = new VAArgInst(foo, *$4);
2210 CurBB->getInstList().push_back(tmp);
2211 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002212 delete $4;
2213 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002214 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
2215 if (!isa<PackedType>($2->getType()))
Robert Bocchino2def1b32006-01-17 20:06:25 +00002216 ThrowException("First operand of extractelement must be "
2217 "packed type!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00002218 if ($4->getType() != Type::UIntTy)
Robert Bocchino2def1b32006-01-17 20:06:25 +00002219 ThrowException("Second operand of extractelement must be uint!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00002220 $$ = new ExtractElementInst($2, $4);
2221 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002222 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2223 if (!isa<PackedType>($2->getType()))
2224 ThrowException("First operand of insertelement must be "
2225 "packed type!");
2226 if ($4->getType() !=
2227 cast<PackedType>($2->getType())->getElementType())
2228 ThrowException("Second operand of insertelement must be "
2229 "packed element type!");
2230 if ($6->getType() != Type::UIntTy)
2231 ThrowException("Third operand of insertelement must be uint!");
2232 $$ = new InsertElementInst($2, $4, $6);
2233 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002234 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002235 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002236 if (!Ty->isFirstClassType())
2237 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002238 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002239 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002240 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002241 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00002242 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002243 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002244 $2->pop_front();
2245 }
2246 delete $2; // Free the list...
Chris Lattnerddb6db42005-05-06 05:51:46 +00002247 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002248 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002249 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002250 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002251
Chris Lattnera8e8f162005-05-06 20:27:19 +00002252 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002253 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002254 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002255 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002256 if ($6) {
2257 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002258 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002259 ParamTypes.push_back((*I)->getType());
2260 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002261
2262 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2263 if (isVarArg) ParamTypes.pop_back();
2264
Chris Lattnera8e8f162005-05-06 20:27:19 +00002265 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Chris Lattner595f06c2005-02-01 01:47:42 +00002266 ThrowException("LLVM functions cannot return aggregate types!");
2267
Chris Lattnera8e8f162005-05-06 20:27:19 +00002268 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002269 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002270 }
Chris Lattner00950542001-06-06 20:29:01 +00002271
Chris Lattnera8e8f162005-05-06 20:27:19 +00002272 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002273
Chris Lattner8b81bf52001-07-25 22:47:46 +00002274 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002275 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002276 // Make sure no arguments is a good thing!
2277 if (Ty->getNumParams() != 0)
2278 ThrowException("No arguments passed to a function that "
2279 "expects arguments!");
2280
Chris Lattner9232b992003-04-22 18:42:41 +00002281 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002282 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002283 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002284 // correctly!
2285 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002286 FunctionType::param_iterator I = Ty->param_begin();
2287 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002288 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002289
2290 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002291 if ((*ArgI)->getType() != *I)
2292 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2293 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002294
Chris Lattner8b81bf52001-07-25 22:47:46 +00002295 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002296 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002297
Chris Lattnera8e8f162005-05-06 20:27:19 +00002298 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002299 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002300 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002301 cast<CallInst>($$)->setCallingConv($2);
2302 delete $3;
2303 delete $6;
Chris Lattner00950542001-06-06 20:29:01 +00002304 }
2305 | MemoryInst {
2306 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002307 };
Chris Lattner00950542001-06-06 20:29:01 +00002308
Chris Lattner6cdb0112001-11-26 16:54:11 +00002309
2310// IndexList - List of indices for GEP based instructions...
2311IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002312 $$ = $2;
2313 } | /* empty */ {
2314 $$ = new std::vector<Value*>();
2315 };
2316
2317OptVolatile : VOLATILE {
2318 $$ = true;
2319 }
2320 | /* empty */ {
2321 $$ = false;
2322 };
2323
Chris Lattner027dcc52001-07-08 21:10:27 +00002324
Chris Lattnerddb6db42005-05-06 05:51:46 +00002325
Chris Lattner66db8e42005-11-06 06:34:12 +00002326MemoryInst : MALLOC Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002327 $$ = new MallocInst(*$2, 0, $3);
Chris Lattner30c89792001-09-07 16:35:17 +00002328 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002329 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002330 | MALLOC Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002331 $$ = new MallocInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002332 delete $2;
2333 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002334 | ALLOCA Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002335 $$ = new AllocaInst(*$2, 0, $3);
Nate Begeman14b05292005-11-05 09:21:28 +00002336 delete $2;
2337 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002338 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002339 $$ = new AllocaInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002340 delete $2;
2341 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002342 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002343 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002344 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002345 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002346 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002347 }
2348
Chris Lattner15c9c032003-09-08 18:20:29 +00002349 | OptVolatile LOAD Types ValueRef {
2350 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002351 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002352 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002353 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2354 ThrowException("Can't load from pointer of non-first-class type: " +
2355 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002356 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002357 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002358 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002359 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2360 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002361 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002362 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002363 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002364 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002365 if (ElTy != $3->getType())
2366 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002367 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002368
Chris Lattner79ad13802003-09-08 20:29:46 +00002369 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002370 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002371 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002372 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002373 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002374 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002375
2376 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2377 // indices to uint struct indices for compatibility.
2378 generic_gep_type_iterator<std::vector<Value*>::iterator>
2379 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2380 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2381 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2382 if (isa<StructType>(*GTI)) // Only change struct indices
2383 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2384 if (CUI->getType() == Type::UByteTy)
2385 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2386
Chris Lattner30c89792001-09-07 16:35:17 +00002387 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002388 ThrowException("Invalid getelementptr indices for type '" +
2389 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002390 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2391 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002392 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002393
Brian Gaeked0fde302003-11-11 22:41:34 +00002394
Chris Lattner00950542001-06-06 20:29:01 +00002395%%
Chris Lattner09083092001-07-08 04:57:15 +00002396int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002397 std::string where
2398 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002399 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002400 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002401 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002402 errMsg += "end-of-file.";
2403 else
Chris Lattner9232b992003-04-22 18:42:41 +00002404 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002405 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002406 return 0;
2407}