blob: b8ac7f2867a9a4d1274448e331c66f8d839bae2e [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"
Chris Lattneraa2c8532006-01-25 22:26:43 +000017#include "llvm/InlineAsm.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000018#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000020#include "llvm/SymbolTable.h"
Reid Spencer0b118202006-01-16 21:12:35 +000021#include "llvm/Assembly/AutoUpgrade.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000023#include "llvm/ADT/STLExtras.h"
Chris Lattner87ac9722005-11-06 06:46:28 +000024#include "llvm/Support/MathExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000025#include <algorithm>
26#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000027#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000028#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000029
Chris Lattner386a3b72001-10-16 19:54:17 +000030int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000031int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000032int yyparse();
33
Brian Gaeked0fde302003-11-11 22:41:34 +000034namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000035 std::string CurFilename;
36}
37using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000038
Chris Lattner00950542001-06-06 20:29:01 +000039static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000040
Chris Lattner30c89792001-09-07 16:35:17 +000041// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
42// relating to upreferences in the input stream.
43//
44//#define DEBUG_UPREFS 1
45#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000046#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000047#else
48#define UR_OUT(X)
49#endif
50
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000051#define YYERROR_VERBOSE 1
52
Andrew Lenharth558bc882005-06-18 18:34:52 +000053static bool ObsoleteVarArgs;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +000054static bool NewVarArgs;
Chris Lattnerb2b96672005-11-12 18:21:21 +000055static BasicBlock *CurBB;
56static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000057
58
Chris Lattner7e708292002-06-25 16:13:24 +000059// This contains info used when building the body of a function. It is
60// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000061//
Chris Lattner9232b992003-04-22 18:42:41 +000062typedef std::vector<Value *> ValueList; // Numbered defs
Misha Brukman5a2a3822005-05-10 22:02:28 +000063static void
64ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
65 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000066
67static struct PerModuleInfo {
68 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000069 std::map<const Type *, ValueList> Values; // Module level numbered definitions
70 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000071 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000072 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000073
Chris Lattnerbc721ed2004-07-13 08:28:21 +000074 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
Reid Spencerfd20c0a2006-05-29 02:34:34 +000075 /// how they were referenced and on which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000076 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000077 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
78
Chris Lattner2079fde2001-10-13 06:41:08 +000079 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
80 // references to global values. Global values may be referenced before they
81 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000082 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000083 //
Chris Lattner9232b992003-04-22 18:42:41 +000084 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000085 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000086 GlobalRefsType GlobalRefs;
87
Chris Lattner00950542001-06-06 20:29:01 +000088 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000089 // If we could not resolve some functions at function compilation time
90 // (calls to functions before they are defined), resolve them now... Types
91 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000092 //
Chris Lattner00950542001-06-06 20:29:01 +000093 ResolveDefinitions(LateResolveValues);
94
Chris Lattner2079fde2001-10-13 06:41:08 +000095 // Check to make sure that all global value forward references have been
96 // resolved!
97 //
98 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000099 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +0000100
Chris Lattner749ce032002-03-11 22:12:39 +0000101 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
102 I != E; ++I) {
103 UndefinedReferences += " " + I->first.first->getDescription() + " " +
104 I->first.second.getName() + "\n";
105 }
106 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000107 }
108
Reid Spencere812fb22006-01-19 01:21:04 +0000109 // Look for intrinsic functions and CallInst that need to be upgraded
Chris Lattner7d5c1e12006-03-04 07:53:16 +0000110 for (Module::iterator FI = CurrentModule->begin(),
111 FE = CurrentModule->end(); FI != FE; )
112 UpgradeCallsToIntrinsic(FI++);
Reid Spencer0b118202006-01-16 21:12:35 +0000113
Chris Lattner7e708292002-06-25 16:13:24 +0000114 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000115 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000116 CurrentModule = 0;
117 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000118
Chris Lattner8a327842004-07-14 21:44:00 +0000119 // GetForwardRefForGlobal - Check to see if there is a forward reference
120 // for this global. If so, remove it from the GlobalRefs map and return it.
121 // If not, just return null.
122 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
123 // Check to see if there is a forward reference to this global variable...
124 // if there is, eliminate it and patch the reference to use the new def'n.
125 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
126 GlobalValue *Ret = 0;
127 if (I != GlobalRefs.end()) {
128 Ret = I->second;
129 GlobalRefs.erase(I);
130 }
131 return Ret;
132 }
Chris Lattner00950542001-06-06 20:29:01 +0000133} CurModule;
134
Chris Lattner79df7c02002-03-26 18:01:55 +0000135static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000136 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000137
Chris Lattner735f2702004-07-08 22:30:50 +0000138 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
139 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000140 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000141
Chris Lattner2e2ed292004-07-14 06:28:35 +0000142 /// BBForwardRefs - When we see forward references to basic blocks, keep
143 /// track of them here.
144 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
145 std::vector<BasicBlock*> NumberedBlocks;
146 unsigned NextBBNum;
147
Chris Lattner79df7c02002-03-26 18:01:55 +0000148 inline PerFunctionInfo() {
149 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000150 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000151 }
152
Chris Lattner79df7c02002-03-26 18:01:55 +0000153 inline void FunctionStart(Function *M) {
154 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000155 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000156 }
157
Chris Lattner79df7c02002-03-26 18:01:55 +0000158 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000159 NumberedBlocks.clear();
160
161 // Any forward referenced blocks left?
162 if (!BBForwardRefs.empty())
163 ThrowException("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000164 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000165
166 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000167 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000168
Chris Lattner7e708292002-06-25 16:13:24 +0000169 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000170 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000171 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000172 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000173} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000174
Chris Lattner394f2eb2003-10-16 20:12:13 +0000175static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000176
Chris Lattner00950542001-06-06 20:29:01 +0000177
178//===----------------------------------------------------------------------===//
179// Code to handle definitions of all the types
180//===----------------------------------------------------------------------===//
181
Chris Lattner735f2702004-07-08 22:30:50 +0000182static int InsertValue(Value *V,
183 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
184 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000185
186 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000187 ValueList &List = ValueTab[V->getType()];
188 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000189 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000190}
191
Chris Lattner30c89792001-09-07 16:35:17 +0000192static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000193 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000194 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000195 // Module constants occupy the lowest numbered slots...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000196 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000197 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000198 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000199 case ValID::NameVal: // Is it a named definition?
200 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
201 D.destroy(); // Free old strdup'd memory...
202 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000203 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000204 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000205 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000206 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000207 }
208
209 // If we reached here, we referenced either a symbol that we don't know about
210 // or an id number that hasn't been read yet. We may be referencing something
211 // forward, so just create an entry to be resolved later and get to it...
212 //
213 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
214
Chris Lattner355ad1f2005-03-21 06:27:42 +0000215
216 if (inFunctionScope()) {
217 if (D.Type == ValID::NameVal)
218 ThrowException("Reference to an undefined type: '" + D.getName() + "'");
219 else
220 ThrowException("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000221 }
Chris Lattner30c89792001-09-07 16:35:17 +0000222
Chris Lattner355ad1f2005-03-21 06:27:42 +0000223 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
224 if (I != CurModule.LateResolveTypes.end())
225 return I->second;
226
Chris Lattner82269592001-10-22 06:01:08 +0000227 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000228 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000229 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000230 }
Chris Lattner30c89792001-09-07 16:35:17 +0000231
Chris Lattner9232b992003-04-22 18:42:41 +0000232static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000233 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000234 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000235 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000236 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000237}
238
Chris Lattner2079fde2001-10-13 06:41:08 +0000239// getValNonImprovising - Look up the value specified by the provided type and
240// the provided ValID. If the value exists and has already been defined, return
241// it. Otherwise return null.
242//
243static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000244 if (isa<FunctionType>(Ty))
245 ThrowException("Functions are not values and "
246 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000247
Chris Lattner30c89792001-09-07 16:35:17 +0000248 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000249 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000250 unsigned Num = (unsigned)D.Num;
251
252 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000253 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000254 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000255 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000256 return VI->second[Num];
257 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000258 }
259
260 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000261 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000262 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000263
264 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000265 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000266
Chris Lattner16af11d2004-02-09 21:03:38 +0000267 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000268 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000269
Chris Lattner1a1cb112001-09-30 22:46:54 +0000270 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000271 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000272 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000273
274 D.destroy(); // Free old strdup'd memory...
275 return N;
276 }
277
Misha Brukman5a2a3822005-05-10 22:02:28 +0000278 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000279 // value will fit into the specified type...
280 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000281 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
282 ThrowException("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000283 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000284 Ty->getDescription() + "'!");
285 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000286
287 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000288 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
289 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000290 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000291 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000292 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000293 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000294 }
295 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000296 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000297 }
298
Chris Lattner2079fde2001-10-13 06:41:08 +0000299 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000300 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000301 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000302 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000303
Chris Lattner2079fde2001-10-13 06:41:08 +0000304 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000305 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000306 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000307 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000308
Chris Lattner16710e92004-10-16 18:17:13 +0000309 case ValID::ConstUndefVal: // Is it an undef value?
310 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000311
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000312 case ValID::ConstZeroVal: // Is it a zero value?
313 return Constant::getNullValue(Ty);
314
Chris Lattnerd78700d2002-08-16 21:14:40 +0000315 case ValID::ConstantVal: // Fully resolved constant?
316 if (D.ConstantValue->getType() != Ty)
317 ThrowException("Constant expression type different from required type!");
318 return D.ConstantValue;
319
Chris Lattneraa2c8532006-01-25 22:26:43 +0000320 case ValID::InlineAsmVal: { // Inline asm expression
321 const PointerType *PTy = dyn_cast<PointerType>(Ty);
322 const FunctionType *FTy =
323 PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0;
324 if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints))
325 ThrowException("Invalid type for asm constraint string!");
326 InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints,
327 D.IAD->HasSideEffects);
328 D.destroy(); // Free InlineAsmDescriptor.
329 return IA;
330 }
Chris Lattner30c89792001-09-07 16:35:17 +0000331 default:
332 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000333 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000334 } // End of switch
335
Chris Lattner2079fde2001-10-13 06:41:08 +0000336 assert(0 && "Unhandled case!");
337 return 0;
338}
339
Chris Lattner2079fde2001-10-13 06:41:08 +0000340// getVal - This function is identical to getValNonImprovising, except that if a
341// value is not already defined, it "improvises" by creating a placeholder var
342// that looks and acts just like the requested variable. When the value is
343// defined later, all uses of the placeholder variable are replaced with the
344// real thing.
345//
Chris Lattner64116f92004-07-14 01:33:11 +0000346static Value *getVal(const Type *Ty, const ValID &ID) {
347 if (Ty == Type::LabelTy)
348 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000349
Chris Lattner64116f92004-07-14 01:33:11 +0000350 // See if the value has already been defined.
351 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000352 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000353
Chris Lattner60cd9552005-03-23 01:29:26 +0000354 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
355 ThrowException("Invalid use of a composite type!");
356
Chris Lattner00950542001-06-06 20:29:01 +0000357 // If we reached here, we referenced either a symbol that we don't know about
358 // or an id number that hasn't been read yet. We may be referencing something
359 // forward, so just create an entry to be resolved later and get to it...
360 //
Chris Lattner64116f92004-07-14 01:33:11 +0000361 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000362
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000363 // Remember where this forward reference came from. FIXME, shouldn't we try
364 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000365 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000366 llvmAsmlineno)));
367
Chris Lattner79df7c02002-03-26 18:01:55 +0000368 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000369 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000370 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000371 InsertValue(V, CurModule.LateResolveValues);
372 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000373}
374
Chris Lattner2e2ed292004-07-14 06:28:35 +0000375/// getBBVal - This is used for two purposes:
376/// * If isDefinition is true, a new basic block with the specified ID is being
377/// defined.
378/// * If isDefinition is true, this is a reference to a basic block, which may
379/// or may not be a forward reference.
380///
381static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000382 assert(inFunctionScope() && "Can't get basic block at global scope!");
383
Chris Lattner2e2ed292004-07-14 06:28:35 +0000384 std::string Name;
385 BasicBlock *BB = 0;
386 switch (ID.Type) {
387 default: ThrowException("Illegal label reference " + ID.getName());
388 case ValID::NumberVal: // Is it a numbered definition?
389 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
390 CurFun.NumberedBlocks.resize(ID.Num+1);
391 BB = CurFun.NumberedBlocks[ID.Num];
392 break;
393 case ValID::NameVal: // Is it a named definition?
394 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000395 if (Value *N = CurFun.CurrentFunction->
396 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000397 BB = cast<BasicBlock>(N);
398 break;
399 }
Chris Lattner64116f92004-07-14 01:33:11 +0000400
Chris Lattner2e2ed292004-07-14 06:28:35 +0000401 // See if the block has already been defined.
402 if (BB) {
403 // If this is the definition of the block, make sure the existing value was
404 // just a forward reference. If it was a forward reference, there will be
405 // an entry for it in the PlaceHolderInfo map.
406 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
407 // The existing value was a definition, not a forward reference.
408 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000409
Chris Lattner2e2ed292004-07-14 06:28:35 +0000410 ID.destroy(); // Free strdup'd memory.
411 return BB;
412 }
413
414 // Otherwise this block has not been seen before.
415 BB = new BasicBlock("", CurFun.CurrentFunction);
416 if (ID.Type == ValID::NameVal) {
417 BB->setName(ID.Name);
418 } else {
419 CurFun.NumberedBlocks[ID.Num] = BB;
420 }
421
422 // If this is not a definition, keep track of it so we can use it as a forward
423 // reference.
424 if (!isDefinition) {
425 // Remember where this forward reference came from.
426 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
427 } else {
428 // The forward declaration could have been inserted anywhere in the
429 // function: insert it into the correct place now.
430 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
431 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
432 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000433 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000434 return BB;
435}
436
Chris Lattner00950542001-06-06 20:29:01 +0000437
438//===----------------------------------------------------------------------===//
439// Code to handle forward references in instructions
440//===----------------------------------------------------------------------===//
441//
442// This code handles the late binding needed with statements that reference
443// values not defined yet... for example, a forward branch, or the PHI node for
444// a loop body.
445//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000446// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000447// and back patchs after we are done.
448//
449
Misha Brukman5a2a3822005-05-10 22:02:28 +0000450// ResolveDefinitions - If we could not resolve some defs at parsing
451// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000452// defs now...
453//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000454static void
455ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
456 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000457 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000458 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000459 E = LateResolvers.end(); LRI != E; ++LRI) {
460 ValueList &List = LRI->second;
461 while (!List.empty()) {
462 Value *V = List.back();
463 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000464
465 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
466 CurModule.PlaceHolderInfo.find(V);
467 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
468
469 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000470
Chris Lattner735f2702004-07-08 22:30:50 +0000471 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000472 if (TheRealValue) {
473 V->replaceAllUsesWith(TheRealValue);
474 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000475 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000476 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000477 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000478 // resolver table
479 InsertValue(V, *FutureLateResolvers);
480 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000481 if (DID.Type == ValID::NameVal)
482 ThrowException("Reference to an invalid definition: '" +DID.getName()+
483 "' of type '" + V->getType()->getDescription() + "'",
484 PHI->second.second);
485 else
486 ThrowException("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000487 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000488 V->getType()->getDescription() + "'",
489 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000490 }
Chris Lattner00950542001-06-06 20:29:01 +0000491 }
492 }
493
494 LateResolvers.clear();
495}
496
Chris Lattner4a42e902001-10-22 05:56:09 +0000497// ResolveTypeTo - A brand new type was just declared. This means that (if
498// name is not null) things referencing Name can be resolved. Otherwise, things
499// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000500//
Chris Lattner4a42e902001-10-22 05:56:09 +0000501static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000502 ValID D;
503 if (Name) D = ValID::create(Name);
504 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000505
Chris Lattner355ad1f2005-03-21 06:27:42 +0000506 std::map<ValID, PATypeHolder>::iterator I =
507 CurModule.LateResolveTypes.find(D);
508 if (I != CurModule.LateResolveTypes.end()) {
509 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
510 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000511 }
512}
513
Chris Lattner1781aca2001-09-18 04:00:54 +0000514// setValueName - Set the specified value to the name given. The name may be
515// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000516// assumed to be a malloc'd string buffer, and is free'd by this function.
517//
518static void setValueName(Value *V, char *NameStr) {
519 if (NameStr) {
520 std::string Name(NameStr); // Copy string
521 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000522
Misha Brukman5a2a3822005-05-10 22:02:28 +0000523 if (V->getType() == Type::VoidTy)
Chris Lattnerc9aea522004-07-13 08:12:39 +0000524 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000525
Chris Lattner8ab406d2004-07-13 07:59:27 +0000526 assert(inFunctionScope() && "Must be in function scope!");
527 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
528 if (ST.lookup(V->getType(), Name))
529 ThrowException("Redefinition of value named '" + Name + "' in the '" +
530 V->getType()->getDescription() + "' type plane!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000531
Chris Lattnerc9aea522004-07-13 08:12:39 +0000532 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000533 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000534 }
535}
536
Chris Lattnerd88998d2004-07-14 08:23:52 +0000537/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
538/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000539static GlobalVariable *
540ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
541 bool isConstantGlobal, const Type *Ty,
542 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000543 if (isa<FunctionType>(Ty))
544 ThrowException("Cannot declare global vars of function type!");
545
Misha Brukman5a2a3822005-05-10 22:02:28 +0000546 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000547
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000548 std::string Name;
549 if (NameStr) {
550 Name = NameStr; // Copy string
551 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000552 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000553
Chris Lattner8a327842004-07-14 21:44:00 +0000554 // See if this global value was forward referenced. If so, recycle the
555 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000556 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000557 if (!Name.empty()) {
558 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000559 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000560 ID = ValID::create((int)CurModule.Values[PTy].size());
561 }
562
563 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000564 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000565 // previously inserted.
566 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
567 CurModule.CurrentModule->getGlobalList().remove(GV);
568 CurModule.CurrentModule->getGlobalList().push_back(GV);
569 GV->setInitializer(Initializer);
570 GV->setLinkage(Linkage);
571 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000572 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000573 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000574 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000575
576 // If this global has a name, check to see if there is already a definition
577 // of this global in the module. If so, merge as appropriate. Note that
578 // this is really just a hack around problems in the CFE. :(
579 if (!Name.empty()) {
580 // We are a simple redefinition of a value, check to see if it is defined
581 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000582 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000583 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
584 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000585 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000586 // 2. If both initializers have the same value.
587 //
588 if (!EGV->hasInitializer() || !Initializer ||
589 EGV->getInitializer() == Initializer) {
590
591 // Make sure the existing global version gets the initializer! Make
592 // sure that it also gets marked const if the new version is.
593 if (Initializer && !EGV->hasInitializer())
594 EGV->setInitializer(Initializer);
595 if (isConstantGlobal)
596 EGV->setConstant(true);
597 EGV->setLinkage(Linkage);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000598 return EGV;
Chris Lattnera09000d2004-07-14 23:03:46 +0000599 }
600
Misha Brukman5a2a3822005-05-10 22:02:28 +0000601 ThrowException("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000602 "' in the '" + Ty->getDescription() + "' type plane!");
603 }
604 }
605
606 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000607 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000608 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000609 CurModule.CurrentModule);
610 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000611 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000612}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000613
Reid Spencer75719bf2004-05-26 21:48:31 +0000614// setTypeName - Set the specified type to the name given. The name may be
615// null potentially, in which case this is a noop. The string passed in is
616// assumed to be a malloc'd string buffer, and is freed by this function.
617//
618// This function returns true if the type has already been defined, but is
619// allowed to be redefined in the specified context. If the name is a new name
620// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000621static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000622 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000623 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000624
Reid Spencer75719bf2004-05-26 21:48:31 +0000625 std::string Name(NameStr); // Copy string
626 free(NameStr); // Free old string
627
628 // We don't allow assigning names to void type
Misha Brukman5a2a3822005-05-10 22:02:28 +0000629 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000630 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000631
Chris Lattnera09000d2004-07-14 23:03:46 +0000632 // Set the type name, checking for conflicts as we do so.
633 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000634
Chris Lattnera09000d2004-07-14 23:03:46 +0000635 if (AlreadyExists) { // Inserting a name that is already defined???
636 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
637 assert(Existing && "Conflict but no matching type?");
638
Reid Spencer75719bf2004-05-26 21:48:31 +0000639 // There is only one case where this is allowed: when we are refining an
640 // opaque type. In this case, Existing will be an opaque type.
641 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
642 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000643 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000644 return true;
645 }
646
647 // Otherwise, this is an attempt to redefine a type. That's okay if
648 // the redefinition is identical to the original. This will be so if
649 // Existing and T point to the same Type object. In this one case we
650 // allow the equivalent redefinition.
651 if (Existing == T) return true; // Yes, it's equal.
652
653 // Any other kind of (non-equivalent) redefinition is an error.
654 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000655 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000656 }
657
Reid Spencer75719bf2004-05-26 21:48:31 +0000658 return false;
659}
Chris Lattner8896eda2001-07-09 19:38:36 +0000660
Chris Lattner30c89792001-09-07 16:35:17 +0000661//===----------------------------------------------------------------------===//
662// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000663//
Chris Lattner8896eda2001-07-09 19:38:36 +0000664
Chris Lattner3b073862004-02-09 03:19:29 +0000665// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000666//
667static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000668 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000669 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000670}
671
672namespace {
673 struct UpRefRecord {
674 // NestingLevel - The number of nesting levels that need to be popped before
675 // this type is resolved.
676 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000677
Chris Lattner3b073862004-02-09 03:19:29 +0000678 // LastContainedTy - This is the type at the current binding level for the
679 // type. Every time we reduce the nesting level, this gets updated.
680 const Type *LastContainedTy;
681
682 // UpRefTy - This is the actual opaque type that the upreference is
683 // represented with.
684 OpaqueType *UpRefTy;
685
686 UpRefRecord(unsigned NL, OpaqueType *URTy)
687 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
688 };
Chris Lattner30c89792001-09-07 16:35:17 +0000689}
Chris Lattner698b56e2001-07-20 19:15:08 +0000690
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000691// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000692static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000693
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000694/// HandleUpRefs - Every time we finish a new layer of types, this function is
695/// called. It loops through the UpRefs vector, which is a list of the
696/// currently active types. For each type, if the up reference is contained in
697/// the newly completed type, we decrement the level count. When the level
698/// count reaches zero, the upreferenced type is the type that is passed in:
699/// thus we can complete the cycle.
700///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000701static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000702 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000703 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000704 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000705 "' newly formed. Resolving upreferences.\n" <<
706 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000707
708 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
709 // to zero), we resolve them all together before we resolve them to Ty. At
710 // the end of the loop, if there is anything to resolve to Ty, it will be in
711 // this variable.
712 OpaqueType *TypeToResolve = 0;
713
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000714 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000715 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
716 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000717 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000718 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
719 // Decrement level of upreference
720 unsigned Level = --UpRefs[i].NestingLevel;
721 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000722 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000723 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000724 if (!TypeToResolve) {
725 TypeToResolve = UpRefs[i].UpRefTy;
726 } else {
727 UR_OUT(" * Resolving upreference for "
728 << UpRefs[i].second->getDescription() << "\n";
729 std::string OldName = UpRefs[i].UpRefTy->getDescription());
730 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
731 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
732 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
733 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000734 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000735 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000736 }
737 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000738 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000739
Chris Lattner026a8ce2004-02-09 18:53:54 +0000740 if (TypeToResolve) {
741 UR_OUT(" * Resolving upreference for "
742 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000743 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000744 TypeToResolve->refineAbstractTypeTo(Ty);
745 }
746
Chris Lattner8896eda2001-07-09 19:38:36 +0000747 return Ty;
748}
749
Chris Lattner30c89792001-09-07 16:35:17 +0000750
Chris Lattner6184feb2005-05-20 03:25:47 +0000751// common code from the two 'RunVMAsmParser' functions
752 static Module * RunParser(Module * M) {
753
754 llvmAsmlineno = 1; // Reset the current line number...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000755 ObsoleteVarArgs = false;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000756 NewVarArgs = false;
Chris Lattner6184feb2005-05-20 03:25:47 +0000757
758 CurModule.CurrentModule = M;
759 yyparse(); // Parse the file, potentially throwing exception
760
761 Module *Result = ParserResult;
762 ParserResult = 0;
763
Andrew Lenharth31c98bf2005-06-20 15:41:37 +0000764 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
765 {
766 Function* F;
767 if ((F = Result->getNamedFunction("llvm.va_start"))
768 && F->getFunctionType()->getNumParams() == 0)
769 ObsoleteVarArgs = true;
770 if((F = Result->getNamedFunction("llvm.va_copy"))
771 && F->getFunctionType()->getNumParams() == 1)
772 ObsoleteVarArgs = true;
773 }
774
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000775 if (ObsoleteVarArgs && NewVarArgs)
Chris Lattner548021f2005-06-24 18:00:40 +0000776 ThrowException("This file is corrupt: it uses both new and old style varargs");
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000777
Andrew Lenharth558bc882005-06-18 18:34:52 +0000778 if(ObsoleteVarArgs) {
779 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000780 if (F->arg_size() != 0)
781 ThrowException("Obsolete va_start takes 0 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000782
783 //foo = va_start()
784 // ->
785 //bar = alloca typeof(foo)
786 //va_start(bar)
787 //foo = load bar
788
789 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
790 const Type* ArgTy = F->getFunctionType()->getReturnType();
791 const Type* ArgTyPtr = PointerType::get(ArgTy);
792 Function* NF = Result->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000793 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000794
795 while (!F->use_empty()) {
796 CallInst* CI = cast<CallInst>(F->use_back());
797 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
798 new CallInst(NF, bar, "", CI);
799 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
800 CI->replaceAllUsesWith(foo);
801 CI->getParent()->getInstList().erase(CI);
802 }
803 Result->getFunctionList().erase(F);
804 }
805
806 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000807 if(F->arg_size() != 1)
808 ThrowException("Obsolete va_end takes 1 argument!");
809
Andrew Lenharth558bc882005-06-18 18:34:52 +0000810 //vaend foo
811 // ->
812 //bar = alloca 1 of typeof(foo)
813 //vaend bar
814 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
815 const Type* ArgTy = F->getFunctionType()->getParamType(0);
816 const Type* ArgTyPtr = PointerType::get(ArgTy);
817 Function* NF = Result->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000818 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000819
820 while (!F->use_empty()) {
821 CallInst* CI = cast<CallInst>(F->use_back());
822 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000823 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000824 new CallInst(NF, bar, "", CI);
825 CI->getParent()->getInstList().erase(CI);
826 }
827 Result->getFunctionList().erase(F);
828 }
829
830 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000831 if(F->arg_size() != 1)
832 ThrowException("Obsolete va_copy takes 1 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000833 //foo = vacopy(bar)
834 // ->
835 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000836 //b = alloca 1 of typeof(foo)
837 //store bar -> b
838 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000839 //foo = load a
840
841 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
842 const Type* ArgTy = F->getFunctionType()->getReturnType();
843 const Type* ArgTyPtr = PointerType::get(ArgTy);
844 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000845 RetTy, ArgTyPtr, ArgTyPtr,
846 (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000847
848 while (!F->use_empty()) {
849 CallInst* CI = cast<CallInst>(F->use_back());
850 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000851 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
852 new StoreInst(CI->getOperand(1), b, CI);
853 new CallInst(NF, a, b, "", CI);
854 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000855 CI->replaceAllUsesWith(foo);
856 CI->getParent()->getInstList().erase(CI);
857 }
858 Result->getFunctionList().erase(F);
859 }
860 }
861
Chris Lattner6184feb2005-05-20 03:25:47 +0000862 return Result;
863
864 }
865
Chris Lattner00950542001-06-06 20:29:01 +0000866//===----------------------------------------------------------------------===//
867// RunVMAsmParser - Define an interface to this parser
868//===----------------------------------------------------------------------===//
869//
Chris Lattner86427632004-07-14 06:39:48 +0000870Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000871 set_scan_file(F);
872
Chris Lattnera2850432001-07-22 18:36:00 +0000873 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000874 return RunParser(new Module(CurFilename));
875}
Chris Lattner00950542001-06-06 20:29:01 +0000876
Chris Lattner6184feb2005-05-20 03:25:47 +0000877Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
878 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000879
Chris Lattner6184feb2005-05-20 03:25:47 +0000880 CurFilename = "from_memory";
881 if (M == NULL) {
882 return RunParser(new Module (CurFilename));
883 } else {
884 return RunParser(M);
885 }
Chris Lattner00950542001-06-06 20:29:01 +0000886}
887
888%}
889
890%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000891 llvm::Module *ModuleVal;
892 llvm::Function *FunctionVal;
893 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
894 llvm::BasicBlock *BasicBlockVal;
895 llvm::TerminatorInst *TermInstVal;
896 llvm::Instruction *InstVal;
897 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000898
Brian Gaeked0fde302003-11-11 22:41:34 +0000899 const llvm::Type *PrimType;
900 llvm::PATypeHolder *TypeVal;
901 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000902
Brian Gaeked0fde302003-11-11 22:41:34 +0000903 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
904 std::vector<llvm::Value*> *ValueList;
905 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000906 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000907 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000908 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000909 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
910 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000911
Brian Gaeked0fde302003-11-11 22:41:34 +0000912 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000913 int64_t SInt64Val;
914 uint64_t UInt64Val;
915 int SIntVal;
916 unsigned UIntVal;
917 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000918 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000919
Chris Lattner30c89792001-09-07 16:35:17 +0000920 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000921 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000922
Brian Gaeked0fde302003-11-11 22:41:34 +0000923 llvm::Instruction::BinaryOps BinaryOpVal;
924 llvm::Instruction::TermOps TermOpVal;
925 llvm::Instruction::MemoryOps MemOpVal;
926 llvm::Instruction::OtherOps OtherOpVal;
927 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000928}
929
Chris Lattner79df7c02002-03-26 18:01:55 +0000930%type <ModuleVal> Module FunctionList
931%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000932%type <BasicBlockVal> BasicBlock InstructionList
933%type <TermInstVal> BBTerminatorInst
934%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000935%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000936%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000937%type <ArgList> ArgList ArgListH
938%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000939%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000940%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000941%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000942%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000943%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000944%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000945%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000946%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattneraa2c8532006-01-25 22:26:43 +0000947%type <BoolVal> OptSideEffect // 'sideeffect' or not.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000948%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000949%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000950
Chris Lattner2079fde2001-10-13 06:41:08 +0000951// ValueRef - Unresolved reference to a definition or BB
952%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000953%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000954// Tokens and types for handling constant integer values
955//
956// ESINT64VAL - A negative number within long long range
957%token <SInt64Val> ESINT64VAL
958
959// EUINT64VAL - A positive number within uns. long long range
960%token <UInt64Val> EUINT64VAL
961%type <SInt64Val> EINT64VAL
962
963%token <SIntVal> SINTVAL // Signed 32 bit ints...
964%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
965%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000966%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000967
968// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000969%type <TypeVal> Types TypesV UpRTypes UpRTypesV
970%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000971%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
972%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000973
Chris Lattner6c23f572003-08-22 05:42:10 +0000974%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
975%type <StrVal> Name OptName OptAssign
Chris Lattner87ac9722005-11-06 06:46:28 +0000976%type <UIntVal> OptAlign OptCAlign
Chris Lattnerb2b96672005-11-12 18:21:21 +0000977%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +0000978
Chris Lattner91ef4602004-03-31 03:49:47 +0000979%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner164c3782005-11-12 00:11:10 +0000980%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000981%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Nate Begeman14b05292005-11-05 09:21:28 +0000982%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
Chris Lattneraa2c8532006-01-25 22:26:43 +0000983%token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
Chris Lattner515906d2006-05-19 21:28:34 +0000984%token CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
Chris Lattnera8e8f162005-05-06 20:27:19 +0000985%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +0000986
Misha Brukman5a2a3822005-05-10 22:02:28 +0000987// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000988%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000989
Misha Brukman5a2a3822005-05-10 22:02:28 +0000990// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000991%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000992%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000993%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000994
995// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000996%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000997
Chris Lattner027dcc52001-07-08 21:10:27 +0000998// Other Operators
999%type <OtherOpVal> ShiftOps
Robert Bocchino2def1b32006-01-17 20:06:25 +00001000%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG
Chris Lattner4c949082006-04-08 01:18:35 +00001001%token <OtherOpVal> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Andrew Lenharth558bc882005-06-18 18:34:52 +00001002%token VAARG_old VANEXT_old //OBSOLETE
Chris Lattnerddb6db42005-05-06 05:51:46 +00001003
Chris Lattner027dcc52001-07-08 21:10:27 +00001004
Chris Lattner00950542001-06-06 20:29:01 +00001005%start Module
1006%%
1007
1008// Handle constant integer size restriction and conversion...
1009//
Chris Lattner51727be2002-06-04 21:58:56 +00001010INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +00001011INTVAL : UINTVAL {
1012 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
1013 ThrowException("Value too large for type!");
1014 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +00001015};
Chris Lattner00950542001-06-06 20:29:01 +00001016
1017
Chris Lattner51727be2002-06-04 21:58:56 +00001018EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +00001019EINT64VAL : EUINT64VAL {
1020 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1021 ThrowException("Value too large for type!");
1022 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +00001023};
Chris Lattner00950542001-06-06 20:29:01 +00001024
Misha Brukman5a2a3822005-05-10 22:02:28 +00001025// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001026// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1027//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001028ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1029LogicalOps : AND | OR | XOR;
1030SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +00001031
Chris Lattner51727be2002-06-04 21:58:56 +00001032ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001033
Chris Lattnere98dda62001-07-14 06:10:16 +00001034// These are some types that allow classification if we only want a particular
1035// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001036SIntType : LONG | INT | SHORT | SBYTE;
1037UIntType : ULONG | UINT | USHORT | UBYTE;
1038IntType : SIntType | UIntType;
1039FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001040
Chris Lattnere98dda62001-07-14 06:10:16 +00001041// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001042OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001043 $$ = $1;
1044 }
Misha Brukman5a2a3822005-05-10 22:02:28 +00001045 | /*empty*/ {
1046 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001047 };
Chris Lattner00950542001-06-06 20:29:01 +00001048
Chris Lattner4ad02e72003-04-16 20:28:45 +00001049OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1050 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001051 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001052 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1053 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001054
Chris Lattnera8e8f162005-05-06 20:27:19 +00001055OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1056 CCC_TOK { $$ = CallingConv::C; } |
Chris Lattner515906d2006-05-19 21:28:34 +00001057 CSRETCC_TOK { $$ = CallingConv::CSRet; } |
Chris Lattnera8e8f162005-05-06 20:27:19 +00001058 FASTCC_TOK { $$ = CallingConv::Fast; } |
1059 COLDCC_TOK { $$ = CallingConv::Cold; } |
1060 CC_TOK EUINT64VAL {
1061 if ((unsigned)$2 != $2)
1062 ThrowException("Calling conv too large!");
1063 $$ = $2;
1064 };
1065
Chris Lattner66db8e42005-11-06 06:34:12 +00001066// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1067// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001068OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001069 ALIGN EUINT64VAL {
1070 $$ = $2;
1071 if ($$ != 0 && !isPowerOf2_32($$))
1072 ThrowException("Alignment must be a power of two!");
1073};
Chris Lattner66db8e42005-11-06 06:34:12 +00001074OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001075 ',' ALIGN EUINT64VAL {
1076 $$ = $3;
1077 if ($$ != 0 && !isPowerOf2_32($$))
1078 ThrowException("Alignment must be a power of two!");
1079};
1080
Chris Lattner66db8e42005-11-06 06:34:12 +00001081
Chris Lattner164c3782005-11-12 00:11:10 +00001082SectionString : SECTION STRINGCONSTANT {
1083 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1084 if ($2[i] == '"' || $2[i] == '\\')
1085 ThrowException("Invalid character in section name!");
1086 $$ = $2;
1087};
1088
1089OptSection : /*empty*/ { $$ = 0; } |
1090 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001091
Chris Lattnerb2b96672005-11-12 18:21:21 +00001092// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1093// is set to be the global we are processing.
1094//
1095GlobalVarAttributes : /* empty */ {} |
1096 ',' GlobalVarAttribute GlobalVarAttributes {};
1097GlobalVarAttribute : SectionString {
1098 CurGV->setSection($1);
1099 free($1);
1100 }
1101 | ALIGN EUINT64VAL {
1102 if ($2 != 0 && !isPowerOf2_32($2))
1103 ThrowException("Alignment must be a power of two!");
1104 CurGV->setAlignment($2);
1105 };
Chris Lattner164c3782005-11-12 00:11:10 +00001106
Chris Lattner30c89792001-09-07 16:35:17 +00001107//===----------------------------------------------------------------------===//
1108// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001109// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001110// access to it, a user must explicitly use TypesV.
1111//
1112
1113// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001114TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1115UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001116
1117Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001118 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001119 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1120 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001121 };
Chris Lattner30c89792001-09-07 16:35:17 +00001122
1123
1124// Derived types are added later...
1125//
Chris Lattner51727be2002-06-04 21:58:56 +00001126PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1127PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001128UpRTypes : OPAQUE {
1129 $$ = new PATypeHolder(OpaqueType::get());
1130 }
1131 | PrimType {
1132 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001133 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001134UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001135 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001136};
Chris Lattner30c89792001-09-07 16:35:17 +00001137
Chris Lattner30c89792001-09-07 16:35:17 +00001138// Include derived types in the Types production.
1139//
1140UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001141 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001142 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001143 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001144 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001145 UR_OUT("New Upreference!\n");
1146 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001147 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001148 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001149 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1150 E = $3->end(); I != E; ++I)
1151 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001152 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1153 if (isVarArg) Params.pop_back();
1154
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001155 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001156 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001157 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001158 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001159 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001160 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001161 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001162 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001163 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1164 const llvm::Type* ElemTy = $4->get();
Chris Lattner9547d7f2005-11-10 01:42:43 +00001165 if ((unsigned)$2 != $2)
Brian Gaeke715c90b2004-08-20 06:00:58 +00001166 ThrowException("Unsigned result not equal to signed result");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001167 if (!ElemTy->isPrimitiveType())
Brian Gaeke715c90b2004-08-20 06:00:58 +00001168 ThrowException("Elemental type of a PackedType must be primitive");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001169 if (!isPowerOf2_32($2))
1170 ThrowException("Vector length should be a power of 2!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001171 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1172 delete $4;
1173 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001174 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001175 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001176 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1177 E = $2->end(); I != E; ++I)
1178 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001179
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001180 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001181 delete $2;
1182 }
1183 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001184 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001185 }
1186 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001187 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001188 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001189 };
Chris Lattner30c89792001-09-07 16:35:17 +00001190
Chris Lattner7e708292002-06-25 16:13:24 +00001191// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001192// declaration type lists
1193//
1194TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001195 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001196 $$->push_back(*$1); delete $1;
1197 }
1198 | TypeListI ',' UpRTypes {
1199 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001200 };
Chris Lattner30c89792001-09-07 16:35:17 +00001201
Chris Lattner7e708292002-06-25 16:13:24 +00001202// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001203ArgTypeListI : TypeListI
1204 | TypeListI ',' DOTDOTDOT {
1205 ($$=$1)->push_back(Type::VoidTy);
1206 }
1207 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001208 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001209 }
1210 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001211 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001212 };
Chris Lattner30c89792001-09-07 16:35:17 +00001213
Chris Lattnere98dda62001-07-14 06:10:16 +00001214// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001215// production is used ONLY to represent constants that show up AFTER a 'const',
1216// 'constant' or 'global' token at global scope. Constants that can be inlined
1217// into other expressions (such as integers and constexprs) are handled by the
1218// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001219//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001220ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001221 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001222 if (ATy == 0)
1223 ThrowException("Cannot make array constant with type: '" +
1224 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001225 const Type *ETy = ATy->getElementType();
1226 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001227
Chris Lattner30c89792001-09-07 16:35:17 +00001228 // Verify that we have the correct size...
1229 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001230 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001231 utostr($3->size()) + " arguments, but has size of " +
1232 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001233
Chris Lattner30c89792001-09-07 16:35:17 +00001234 // Verify all elements are correct type!
1235 for (unsigned i = 0; i < $3->size(); i++) {
1236 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001237 ThrowException("Element #" + utostr(i) + " is not of type '" +
1238 ETy->getDescription() +"' as required!\nIt is of type '"+
1239 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001240 }
1241
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001242 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001243 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001244 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001245 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001246 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001247 if (ATy == 0)
1248 ThrowException("Cannot make array constant with type: '" +
1249 (*$1)->getDescription() + "'!");
1250
1251 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001252 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001253 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001254 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001255 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001256 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001257 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001258 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001259 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001260 if (ATy == 0)
1261 ThrowException("Cannot make array constant with type: '" +
1262 (*$1)->getDescription() + "'!");
1263
Chris Lattner30c89792001-09-07 16:35:17 +00001264 int NumElements = ATy->getNumElements();
1265 const Type *ETy = ATy->getElementType();
1266 char *EndStr = UnEscapeLexed($3, true);
1267 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001268 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001269 itostr((int)(EndStr-$3)) +
1270 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001271 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001272 if (ETy == Type::SByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001273 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001274 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001275 } else if (ETy == Type::UByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001276 for (unsigned char *C = (unsigned char *)$3;
1277 C != (unsigned char*)EndStr; ++C)
1278 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001279 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001280 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001281 ThrowException("Cannot build string arrays of non byte sized elements!");
1282 }
Chris Lattner30c89792001-09-07 16:35:17 +00001283 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001284 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001285 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001286 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001287 | Types '<' ConstVector '>' { // Nonempty unsized arr
1288 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1289 if (PTy == 0)
1290 ThrowException("Cannot make packed constant with type: '" +
1291 (*$1)->getDescription() + "'!");
1292 const Type *ETy = PTy->getElementType();
1293 int NumElements = PTy->getNumElements();
1294
1295 // Verify that we have the correct size...
1296 if (NumElements != -1 && NumElements != (int)$3->size())
1297 ThrowException("Type mismatch: constant sized packed initialized with " +
1298 utostr($3->size()) + " arguments, but has size of " +
1299 itostr(NumElements) + "!");
1300
1301 // Verify all elements are correct type!
1302 for (unsigned i = 0; i < $3->size(); i++) {
1303 if (ETy != (*$3)[i]->getType())
1304 ThrowException("Element #" + utostr(i) + " is not of type '" +
1305 ETy->getDescription() +"' as required!\nIt is of type '"+
1306 (*$3)[i]->getType()->getDescription() + "'.");
1307 }
1308
1309 $$ = ConstantPacked::get(PTy, *$3);
1310 delete $1; delete $3;
1311 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001312 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001313 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001314 if (STy == 0)
1315 ThrowException("Cannot make struct constant with type: '" +
1316 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001317
Chris Lattnerc6212f12003-05-21 16:06:56 +00001318 if ($3->size() != STy->getNumContainedTypes())
1319 ThrowException("Illegal number of initializers for structure type!");
1320
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001321 // Check to ensure that constants are compatible with the type initializer!
1322 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001323 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001324 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001325 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001326 "' for element #" + utostr(i) +
1327 " of structure initializer!");
1328
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001329 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001330 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001331 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001332 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001333 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001334 if (STy == 0)
1335 ThrowException("Cannot make struct constant with type: '" +
1336 (*$1)->getDescription() + "'!");
1337
1338 if (STy->getNumContainedTypes() != 0)
1339 ThrowException("Illegal number of initializers for structure type!");
1340
1341 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1342 delete $1;
1343 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001344 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001345 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001346 if (PTy == 0)
1347 ThrowException("Cannot make null pointer constant with type: '" +
1348 (*$1)->getDescription() + "'!");
1349
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001350 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001351 delete $1;
1352 }
Chris Lattner16710e92004-10-16 18:17:13 +00001353 | Types UNDEF {
1354 $$ = UndefValue::get($1->get());
1355 delete $1;
1356 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001357 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001358 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001359 if (Ty == 0)
1360 ThrowException("Global const reference must be a pointer type!");
1361
Chris Lattner3101c252002-08-15 17:58:33 +00001362 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001363 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001364 // the context of a function, getValNonImprovising will search the functions
1365 // symbol table instead of the module symbol table for the global symbol,
1366 // which throws things all off. To get around this, we just tell
1367 // getValNonImprovising that we are at global scope here.
1368 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001369 Function *SavedCurFn = CurFun.CurrentFunction;
1370 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001371
Chris Lattner2079fde2001-10-13 06:41:08 +00001372 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001373
Chris Lattner394f2eb2003-10-16 20:12:13 +00001374 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001375
Chris Lattner2079fde2001-10-13 06:41:08 +00001376 // If this is an initializer for a constant pointer, which is referencing a
1377 // (currently) undefined variable, create a stub now that shall be replaced
1378 // in the future with the right type of variable.
1379 //
1380 if (V == 0) {
1381 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1382 const PointerType *PT = cast<PointerType>(Ty);
1383
1384 // First check to see if the forward references value is already created!
1385 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001386 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001387
1388 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001389 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001390 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001391 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001392 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001393 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001394
Reid Spencer3271ed52004-07-18 00:08:11 +00001395 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001396 GlobalValue *GV;
1397 if (const FunctionType *FTy =
1398 dyn_cast<FunctionType>(PT->getElementType())) {
1399 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1400 CurModule.CurrentModule);
1401 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001402 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001403 GlobalValue::ExternalLinkage, 0,
1404 Name, CurModule.CurrentModule);
1405 }
Chris Lattner8a327842004-07-14 21:44:00 +00001406
Reid Spencer3271ed52004-07-18 00:08:11 +00001407 // Keep track of the fact that we have a forward ref to recycle it
1408 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1409 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001410 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001411 }
1412
Reid Spencer3271ed52004-07-18 00:08:11 +00001413 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001414 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001415 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001416 | Types ConstExpr {
1417 if ($1->get() != $2->getType())
1418 ThrowException("Mismatched types for constant expression!");
1419 $$ = $2;
1420 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001421 }
1422 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001423 const Type *Ty = $1->get();
1424 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1425 ThrowException("Cannot create a null initialized value of this type!");
1426 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001427 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001428 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001429
Chris Lattnere43f40b2002-10-09 00:25:32 +00001430ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001431 if (!ConstantSInt::isValueValidForType($1, $2))
1432 ThrowException("Constant value doesn't fit in type!");
1433 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001434 }
1435 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001436 if (!ConstantUInt::isValueValidForType($1, $2))
1437 ThrowException("Constant value doesn't fit in type!");
1438 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001439 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001440 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001441 $$ = ConstantBool::True;
1442 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001443 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001444 $$ = ConstantBool::False;
1445 }
1446 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001447 if (!ConstantFP::isValueValidForType($1, $2))
1448 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001449 $$ = ConstantFP::get($1, $2);
1450 };
1451
Chris Lattner00950542001-06-06 20:29:01 +00001452
Chris Lattnerd78700d2002-08-16 21:14:40 +00001453ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001454 if (!$3->getType()->isFirstClassType())
1455 ThrowException("cast constant expression from a non-primitive type: '" +
1456 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001457 if (!$5->get()->isFirstClassType())
1458 ThrowException("cast constant expression to a non-primitive type: '" +
1459 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001460 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001461 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001462 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001463 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1464 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001465 ThrowException("GetElementPtr requires a pointer operand!");
1466
Chris Lattner830b6f92004-04-05 01:30:04 +00001467 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1468 // indices to uint struct indices for compatibility.
1469 generic_gep_type_iterator<std::vector<Value*>::iterator>
1470 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1471 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1472 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1473 if (isa<StructType>(*GTI)) // Only change struct indices
1474 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1475 if (CUI->getType() == Type::UByteTy)
1476 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1477
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001478 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001479 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001480 if (!IdxTy)
1481 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001482
Chris Lattner9232b992003-04-22 18:42:41 +00001483 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001484 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1485 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001486 IdxVec.push_back(C);
1487 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001488 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001489
Chris Lattnerd78700d2002-08-16 21:14:40 +00001490 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001491
Chris Lattnerd78700d2002-08-16 21:14:40 +00001492 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001493 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001494 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1495 if ($3->getType() != Type::BoolTy)
1496 ThrowException("Select condition must be of boolean type!");
1497 if ($5->getType() != $7->getType())
1498 ThrowException("Select operand types must match!");
1499 $$ = ConstantExpr::getSelect($3, $5, $7);
1500 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001501 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001502 if ($3->getType() != $5->getType())
1503 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001504 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1505 // To retain backward compatibility with these early compilers, we emit a
1506 // cast to the appropriate integer type automatically if we are in the
1507 // broken case. See PR424 for more information.
1508 if (!isa<PointerType>($3->getType())) {
1509 $$ = ConstantExpr::get($1, $3, $5);
1510 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001511 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001512 switch (CurModule.CurrentModule->getPointerSize()) {
1513 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1514 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1515 default: ThrowException("invalid pointer binary constant expr!");
1516 }
1517 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1518 ConstantExpr::getCast($5, IntPtrTy));
1519 $$ = ConstantExpr::getCast($$, $3->getType());
1520 }
1521 }
1522 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1523 if ($3->getType() != $5->getType())
1524 ThrowException("Logical operator types must match!");
Chris Lattner0a017832005-12-21 18:31:29 +00001525 if (!$3->getType()->isIntegral()) {
1526 if (!isa<PackedType>($3->getType()) ||
1527 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
1528 ThrowException("Logical operator requires integral operands!");
1529 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001530 $$ = ConstantExpr::get($1, $3, $5);
1531 }
1532 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1533 if ($3->getType() != $5->getType())
1534 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001535 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001536 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001537 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001538 if ($5->getType() != Type::UByteTy)
1539 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001540 if (!$3->getType()->isInteger())
1541 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001542 $$ = ConstantExpr::get($1, $3, $5);
Robert Bocchino9c62b562006-01-10 19:04:32 +00001543 }
1544 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Chris Lattner2d7349a2006-04-08 04:08:32 +00001545 if (!ExtractElementInst::isValidOperands($3, $5))
1546 ThrowException("Invalid extractelement operands!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00001547 $$ = ConstantExpr::getExtractElement($3, $5);
Chris Lattnerca73cd82006-04-08 03:53:34 +00001548 }
1549 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Chris Lattner2d7349a2006-04-08 04:08:32 +00001550 if (!InsertElementInst::isValidOperands($3, $5, $7))
1551 ThrowException("Invalid insertelement operands!");
Chris Lattnerca73cd82006-04-08 03:53:34 +00001552 $$ = ConstantExpr::getInsertElement($3, $5, $7);
1553 }
1554 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1555 if (!ShuffleVectorInst::isValidOperands($3, $5, $7))
1556 ThrowException("Invalid shufflevector operands!");
1557 $$ = ConstantExpr::getShuffleVector($3, $5, $7);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001558 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001559
Chris Lattnerca73cd82006-04-08 03:53:34 +00001560
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001561// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001562ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001563 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001564 }
1565 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001566 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001567 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001568 };
Chris Lattner00950542001-06-06 20:29:01 +00001569
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001570
Chris Lattner1781aca2001-09-18 04:00:54 +00001571// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001572GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001573
Chris Lattner00950542001-06-06 20:29:01 +00001574
Chris Lattner0e73ce62002-05-02 19:11:13 +00001575//===----------------------------------------------------------------------===//
1576// Rules to match Modules
1577//===----------------------------------------------------------------------===//
1578
1579// Module rule: Capture the result of parsing the whole file into a result
1580// variable...
1581//
1582Module : FunctionList {
1583 $$ = ParserResult = $1;
1584 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001585};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001586
Chris Lattner7e708292002-06-25 16:13:24 +00001587// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001588//
1589FunctionList : FunctionList Function {
1590 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001591 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001592 }
1593 | FunctionList FunctionProto {
1594 $$ = $1;
1595 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001596 | FunctionList MODULE ASM_TOK AsmBlock {
Chris Lattneree454772006-01-23 23:05:15 +00001597 $$ = $1;
1598 }
Chris Lattner0e73ce62002-05-02 19:11:13 +00001599 | FunctionList IMPLEMENTATION {
1600 $$ = $1;
1601 }
1602 | ConstPool {
1603 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001604 // Emit an error if there are any unresolved types left.
1605 if (!CurModule.LateResolveTypes.empty()) {
1606 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1607 if (DID.Type == ValID::NameVal)
1608 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1609 else
1610 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1611 }
Chris Lattner51727be2002-06-04 21:58:56 +00001612 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001613
Chris Lattnere98dda62001-07-14 06:10:16 +00001614// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001615ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001616 // Eagerly resolve types. This is not an optimization, this is a
1617 // requirement that is due to the fact that we could have this:
1618 //
1619 // %list = type { %list * }
1620 // %list = type { %list * } ; repeated type decl
1621 //
1622 // If types are not resolved eagerly, then the two types will not be
1623 // determined to be the same type!
1624 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001625 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001626
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001627 if (!setTypeName(*$4, $2) && !$2) {
1628 // If this is a named type that is not a redefinition, add it to the slot
1629 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001630 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001631 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001632
1633 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001634 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001635 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001636 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001637 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Chris Lattneree454772006-01-23 23:05:15 +00001638 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001639 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001640 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001641 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
1642 } GlobalVarAttributes {
1643 CurGV = 0;
Chris Lattner1781aca2001-09-18 04:00:54 +00001644 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001645 | ConstPool OptAssign EXTERNAL GlobalType Types {
1646 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage,
1647 $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001648 delete $5;
Chris Lattnerb2b96672005-11-12 18:21:21 +00001649 } GlobalVarAttributes {
1650 CurGV = 0;
Chris Lattnere98dda62001-07-14 06:10:16 +00001651 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001652 | ConstPool TARGET TargetDefinition {
1653 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001654 | ConstPool DEPLIBS '=' LibrariesDefinition {
1655 }
Chris Lattner00950542001-06-06 20:29:01 +00001656 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001657 };
Chris Lattner00950542001-06-06 20:29:01 +00001658
1659
Chris Lattneree454772006-01-23 23:05:15 +00001660AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00001661 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00001662 char *EndStr = UnEscapeLexed($1, true);
1663 std::string NewAsm($1, EndStr);
1664 free($1);
1665
1666 if (AsmSoFar.empty())
Chris Lattner66316012006-01-24 04:14:29 +00001667 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00001668 else
Chris Lattner66316012006-01-24 04:14:29 +00001669 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00001670};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001671
1672BigOrLittle : BIG { $$ = Module::BigEndian; };
1673BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1674
1675TargetDefinition : ENDIAN '=' BigOrLittle {
1676 CurModule.CurrentModule->setEndianness($3);
1677 }
1678 | POINTERSIZE '=' EUINT64VAL {
1679 if ($3 == 32)
1680 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1681 else if ($3 == 64)
1682 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1683 else
1684 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001685 }
1686 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001687 CurModule.CurrentModule->setTargetTriple($3);
1688 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001689 };
1690
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001691LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001692
1693LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001694 CurModule.CurrentModule->addLibrary($3);
1695 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001696 }
1697 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001698 CurModule.CurrentModule->addLibrary($1);
1699 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001700 }
1701 | /* empty: end of list */ {
1702 }
1703 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001704
Chris Lattner00950542001-06-06 20:29:01 +00001705//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001706// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001707//===----------------------------------------------------------------------===//
1708
Chris Lattner6c23f572003-08-22 05:42:10 +00001709Name : VAR_ID | STRINGCONSTANT;
1710OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001711
Chris Lattner6c23f572003-08-22 05:42:10 +00001712ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001713 if (*$1 == Type::VoidTy)
1714 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001715 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001716};
Chris Lattner00950542001-06-06 20:29:01 +00001717
Chris Lattner69da5cf2002-10-13 20:57:00 +00001718ArgListH : ArgListH ',' ArgVal {
1719 $$ = $1;
1720 $1->push_back(*$3);
1721 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001722 }
1723 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001724 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001725 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001726 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001727 };
Chris Lattner00950542001-06-06 20:29:01 +00001728
1729ArgList : ArgListH {
1730 $$ = $1;
1731 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001732 | ArgListH ',' DOTDOTDOT {
1733 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001734 $$->push_back(std::pair<PATypeHolder*,
1735 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001736 }
1737 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001738 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1739 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001740 }
Chris Lattner00950542001-06-06 20:29:01 +00001741 | /* empty */ {
1742 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001743 };
Chris Lattner00950542001-06-06 20:29:01 +00001744
Chris Lattner164c3782005-11-12 00:11:10 +00001745FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1746 OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001747 UnEscapeLexed($3);
1748 std::string FunctionName($3);
1749 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001750
Chris Lattnera8e8f162005-05-06 20:27:19 +00001751 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001752 ThrowException("LLVM functions cannot return aggregate types!");
1753
Chris Lattner9232b992003-04-22 18:42:41 +00001754 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001755 if ($5) { // If there are arguments...
1756 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1757 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001758 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001759 }
Chris Lattner00950542001-06-06 20:29:01 +00001760
Chris Lattner2079fde2001-10-13 06:41:08 +00001761 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1762 if (isVarArg) ParamTypeList.pop_back();
1763
Chris Lattnera8e8f162005-05-06 20:27:19 +00001764 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001765 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001766 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001767
Chris Lattnera09000d2004-07-14 23:03:46 +00001768 ValID ID;
1769 if (!FunctionName.empty()) {
1770 ID = ValID::create((char*)FunctionName.c_str());
1771 } else {
1772 ID = ValID::create((int)CurModule.Values[PFT].size());
1773 }
1774
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001775 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001776 // See if this function was forward referenced. If so, recycle the object.
1777 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1778 // Move the function to the end of the list, from whereever it was
1779 // previously inserted.
1780 Fn = cast<Function>(FWRef);
1781 CurModule.CurrentModule->getFunctionList().remove(Fn);
1782 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1783 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1784 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1785 // If this is the case, either we need to be a forward decl, or it needs
1786 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001787 if (!CurFun.isDeclare && !Fn->isExternal())
1788 ThrowException("Redefinition of function '" + FunctionName + "'!");
1789
Chris Lattnera09000d2004-07-14 23:03:46 +00001790 // Make sure to strip off any argument names so we can't get conflicts.
1791 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001792 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001793 AI != AE; ++AI)
1794 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001795
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001796 } else { // Not already defined?
1797 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1798 CurModule.CurrentModule);
1799 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001800 }
Chris Lattner00950542001-06-06 20:29:01 +00001801
Chris Lattner394f2eb2003-10-16 20:12:13 +00001802 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001803 Fn->setCallingConv($1);
Chris Lattner164c3782005-11-12 00:11:10 +00001804 Fn->setAlignment($8);
1805 if ($7) {
1806 Fn->setSection($7);
1807 free($7);
1808 }
Chris Lattner00950542001-06-06 20:29:01 +00001809
Chris Lattner7e708292002-06-25 16:13:24 +00001810 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001811 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001812 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001813 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001814 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001815 delete $5->back().first;
1816 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001817 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001818 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001819 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1820 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001821 delete I->first; // Delete the typeholder...
1822
Chris Lattner8ab406d2004-07-13 07:59:27 +00001823 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001824 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001825 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001826
Chris Lattnera8e8f162005-05-06 20:27:19 +00001827 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001828 }
Chris Lattner51727be2002-06-04 21:58:56 +00001829};
Chris Lattner00950542001-06-06 20:29:01 +00001830
Chris Lattner9b02cc32002-05-03 18:23:48 +00001831BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1832
Chris Lattner4ad02e72003-04-16 20:28:45 +00001833FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001834 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001835
Chris Lattner4ad02e72003-04-16 20:28:45 +00001836 // Make sure that we keep track of the linkage type even if there was a
1837 // previous "declare".
1838 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001839};
Chris Lattner00950542001-06-06 20:29:01 +00001840
Chris Lattner9b02cc32002-05-03 18:23:48 +00001841END : ENDTOK | '}'; // Allow end of '}' to end a function
1842
Chris Lattner79df7c02002-03-26 18:01:55 +00001843Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001844 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001845};
Chris Lattner00950542001-06-06 20:29:01 +00001846
Chris Lattner394f2eb2003-10-16 20:12:13 +00001847FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1848 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001849 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001850};
Chris Lattner00950542001-06-06 20:29:01 +00001851
1852//===----------------------------------------------------------------------===//
1853// Rules to match Basic Blocks
1854//===----------------------------------------------------------------------===//
1855
Chris Lattneraa2c8532006-01-25 22:26:43 +00001856OptSideEffect : /* empty */ {
1857 $$ = false;
1858 }
1859 | SIDEEFFECT {
1860 $$ = true;
1861 };
1862
Chris Lattner00950542001-06-06 20:29:01 +00001863ConstValueRef : ESINT64VAL { // A reference to a direct constant
1864 $$ = ValID::create($1);
1865 }
1866 | EUINT64VAL {
1867 $$ = ValID::create($1);
1868 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001869 | FPVAL { // Perhaps it's an FP constant?
1870 $$ = ValID::create($1);
1871 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001872 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001873 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001874 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001875 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001876 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001877 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001878 | NULL_TOK {
1879 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001880 }
Chris Lattner16710e92004-10-16 18:17:13 +00001881 | UNDEF {
1882 $$ = ValID::createUndef();
1883 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00001884 | ZEROINITIALIZER { // A vector zero constant.
1885 $$ = ValID::createZeroInit();
1886 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001887 | '<' ConstVector '>' { // Nonempty unsized packed vector
1888 const Type *ETy = (*$2)[0]->getType();
1889 int NumElements = $2->size();
1890
1891 PackedType* pt = PackedType::get(ETy, NumElements);
1892 PATypeHolder* PTy = new PATypeHolder(
1893 HandleUpRefs(
1894 PackedType::get(
1895 ETy,
1896 NumElements)
1897 )
1898 );
1899
1900 // Verify all elements are correct type!
1901 for (unsigned i = 0; i < $2->size(); i++) {
1902 if (ETy != (*$2)[i]->getType())
1903 ThrowException("Element #" + utostr(i) + " is not of type '" +
1904 ETy->getDescription() +"' as required!\nIt is of type '" +
1905 (*$2)[i]->getType()->getDescription() + "'.");
1906 }
1907
1908 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1909 delete PTy; delete $2;
1910 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001911 | ConstExpr {
1912 $$ = ValID::create($1);
Chris Lattneraa2c8532006-01-25 22:26:43 +00001913 }
1914 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1915 char *End = UnEscapeLexed($3, true);
1916 std::string AsmStr = std::string($3, End);
1917 End = UnEscapeLexed($5, true);
1918 std::string Constraints = std::string($5, End);
1919 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
1920 free($3);
1921 free($5);
Chris Lattnerd78700d2002-08-16 21:14:40 +00001922 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001923
Chris Lattner2079fde2001-10-13 06:41:08 +00001924// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1925// another value.
1926//
1927SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001928 $$ = ValID::create($1);
1929 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001930 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001931 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001932 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001933
1934// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001935ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001936
Chris Lattner00950542001-06-06 20:29:01 +00001937
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001938// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1939// type immediately preceeds the value reference, and allows complex constant
1940// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001941ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001942 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001943 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001944
Chris Lattner00950542001-06-06 20:29:01 +00001945BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001946 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001947 }
Chris Lattner7e708292002-06-25 16:13:24 +00001948 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001949 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001950 };
Chris Lattner00950542001-06-06 20:29:01 +00001951
1952
1953// Basic blocks are terminated by branching instructions:
1954// br, br/cc, switch, ret
1955//
Chris Lattner2079fde2001-10-13 06:41:08 +00001956BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001957 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001958 InsertValue($3);
1959
1960 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001961 InsertValue($1);
1962 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001963 };
Chris Lattner00950542001-06-06 20:29:01 +00001964
1965InstructionList : InstructionList Inst {
1966 $1->getInstList().push_back($2);
1967 $$ = $1;
1968 }
1969 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001970 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001971
1972 // Make sure to move the basic block to the correct location in the
1973 // function, instead of leaving it inserted wherever it was first
1974 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001975 Function::BasicBlockListType &BBL =
1976 CurFun.CurrentFunction->getBasicBlockList();
1977 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner22ae2322004-07-13 08:39:15 +00001978 }
1979 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001980 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001981
1982 // Make sure to move the basic block to the correct location in the
1983 // function, instead of leaving it inserted wherever it was first
1984 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001985 Function::BasicBlockListType &BBL =
1986 CurFun.CurrentFunction->getBasicBlockList();
1987 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner51727be2002-06-04 21:58:56 +00001988 };
Chris Lattner00950542001-06-06 20:29:01 +00001989
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001990BBTerminatorInst : RET ResolvedVal { // Return with a result...
1991 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001992 }
1993 | RET VOID { // Return with no result...
1994 $$ = new ReturnInst();
1995 }
1996 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001997 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001998 } // Conditional Branch...
1999 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00002000 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00002001 }
2002 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002003 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00002004 $$ = S;
2005
Chris Lattner9232b992003-04-22 18:42:41 +00002006 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00002007 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00002008 for (; I != E; ++I) {
2009 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2010 S->addCase(CI, I->second);
2011 else
2012 ThrowException("Switch case is constant, but not a simple integer!");
2013 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002014 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00002015 }
Chris Lattner196850c2003-05-15 21:30:00 +00002016 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002017 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002018 $$ = S;
2019 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002020 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2021 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002022 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002023 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00002024
Chris Lattnera8e8f162005-05-06 20:27:19 +00002025 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002026 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002027 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002028 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002029 if ($6) {
2030 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002031 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00002032 ParamTypes.push_back((*I)->getType());
2033 }
2034
2035 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2036 if (isVarArg) ParamTypes.pop_back();
2037
Chris Lattnera8e8f162005-05-06 20:27:19 +00002038 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002039 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002040 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002041
Chris Lattnera8e8f162005-05-06 20:27:19 +00002042 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00002043
Chris Lattnera8e8f162005-05-06 20:27:19 +00002044 BasicBlock *Normal = getBBVal($10);
2045 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00002046
2047 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002048 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00002049 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00002050 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002051 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002052 // correctly!
2053 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002054 FunctionType::param_iterator I = Ty->param_begin();
2055 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002056 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00002057
2058 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002059 if ((*ArgI)->getType() != *I)
2060 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2061 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002062
2063 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002064 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002065
Chris Lattnera8e8f162005-05-06 20:27:19 +00002066 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00002067 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002068 cast<InvokeInst>($$)->setCallingConv($2);
2069
2070 delete $3;
2071 delete $6;
Chris Lattner36143fc2003-09-08 18:54:55 +00002072 }
2073 | UNWIND {
2074 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00002075 }
2076 | UNREACHABLE {
2077 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00002078 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002079
2080
Chris Lattner00950542001-06-06 20:29:01 +00002081
2082JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2083 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002084 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00002085 if (V == 0)
2086 ThrowException("May only switch on a constant pool value!");
2087
Chris Lattner64116f92004-07-14 01:33:11 +00002088 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00002089 }
2090 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002091 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002092 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00002093
2094 if (V == 0)
2095 ThrowException("May only switch on a constant pool value!");
2096
Chris Lattner64116f92004-07-14 01:33:11 +00002097 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00002098 };
Chris Lattner00950542001-06-06 20:29:01 +00002099
2100Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00002101 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00002102 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00002103 InsertValue($2);
2104 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00002105};
Chris Lattner00950542001-06-06 20:29:01 +00002106
Chris Lattnerc24d2082001-06-11 15:04:20 +00002107PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00002108 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00002109 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00002110 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002111 }
2112 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2113 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00002114 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00002115 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00002116 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002117
2118
Chris Lattner30c89792001-09-07 16:35:17 +00002119ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00002120 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002121 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00002122 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002123 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00002124 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002125 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00002126 };
Chris Lattner00950542001-06-06 20:29:01 +00002127
2128// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00002129ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00002130
Chris Lattnerddb6db42005-05-06 05:51:46 +00002131OptTailCall : TAIL CALL {
2132 $$ = true;
2133 }
2134 | CALL {
2135 $$ = false;
2136 };
2137
2138
2139
Chris Lattner4a6482b2002-09-10 19:57:26 +00002140InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00002141 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2142 !isa<PackedType>((*$2).get()))
2143 ThrowException(
2144 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00002145 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
2146 ThrowException("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002147 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2148 if ($$ == 0)
2149 ThrowException("binary operator returned null!");
2150 delete $2;
2151 }
2152 | LogicalOps Types ValueRef ',' ValueRef {
Chris Lattner0a017832005-12-21 18:31:29 +00002153 if (!(*$2)->isIntegral()) {
2154 if (!isa<PackedType>($2->get()) ||
2155 !cast<PackedType>($2->get())->getElementType()->isIntegral())
2156 ThrowException("Logical operator requires integral operands!");
2157 }
Chris Lattner4a6482b2002-09-10 19:57:26 +00002158 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2159 if ($$ == 0)
2160 ThrowException("binary operator returned null!");
2161 delete $2;
2162 }
2163 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002164 if(isa<PackedType>((*$2).get())) {
2165 ThrowException(
2166 "PackedTypes currently not supported in setcc instructions!");
2167 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00002168 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00002169 if ($$ == 0)
2170 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002171 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002172 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002173 | NOT ResolvedVal {
2174 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2175 << " Replacing with 'xor'.\n";
2176
2177 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2178 if (Ones == 0)
2179 ThrowException("Expected integral type for not instruction!");
2180
2181 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002182 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00002183 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00002184 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002185 | ShiftOps ResolvedVal ',' ResolvedVal {
2186 if ($4->getType() != Type::UByteTy)
2187 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002188 if (!$2->getType()->isInteger())
2189 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002190 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00002191 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002192 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002193 if (!$4->get()->isFirstClassType())
2194 ThrowException("cast instruction to a non-primitive type: '" +
2195 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002196 $$ = new CastInst($2, *$4);
2197 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002198 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002199 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2200 if ($2->getType() != Type::BoolTy)
2201 ThrowException("select condition must be boolean!");
2202 if ($4->getType() != $6->getType())
2203 ThrowException("select value types should match!");
2204 $$ = new SelectInst($2, $4, $6);
2205 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002206 | VAARG ResolvedVal ',' Types {
Andrew Lenharthe78f50d2005-06-19 03:53:56 +00002207 NewVarArgs = true;
Chris Lattner99e7ab72003-10-18 05:53:13 +00002208 $$ = new VAArgInst($2, *$4);
2209 delete $4;
2210 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002211 | VAARG_old ResolvedVal ',' Types {
2212 ObsoleteVarArgs = true;
2213 const Type* ArgTy = $2->getType();
2214 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002215 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002216
2217 //b = vaarg a, t ->
2218 //foo = alloca 1 of t
2219 //bar = vacopy a
2220 //store bar -> foo
2221 //b = vaarg foo, t
2222 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2223 CurBB->getInstList().push_back(foo);
2224 CallInst* bar = new CallInst(NF, $2);
2225 CurBB->getInstList().push_back(bar);
2226 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2227 $$ = new VAArgInst(foo, *$4);
2228 delete $4;
2229 }
2230 | VANEXT_old ResolvedVal ',' Types {
2231 ObsoleteVarArgs = true;
2232 const Type* ArgTy = $2->getType();
2233 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002234 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002235
2236 //b = vanext a, t ->
2237 //foo = alloca 1 of t
2238 //bar = vacopy a
2239 //store bar -> foo
2240 //tmp = vaarg foo, t
2241 //b = load foo
2242 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2243 CurBB->getInstList().push_back(foo);
2244 CallInst* bar = new CallInst(NF, $2);
2245 CurBB->getInstList().push_back(bar);
2246 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2247 Instruction* tmp = new VAArgInst(foo, *$4);
2248 CurBB->getInstList().push_back(tmp);
2249 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002250 delete $4;
2251 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002252 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Chris Lattner2d7349a2006-04-08 04:08:32 +00002253 if (!ExtractElementInst::isValidOperands($2, $4))
2254 ThrowException("Invalid extractelement operands!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00002255 $$ = new ExtractElementInst($2, $4);
2256 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002257 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Chris Lattner2d7349a2006-04-08 04:08:32 +00002258 if (!InsertElementInst::isValidOperands($2, $4, $6))
2259 ThrowException("Invalid insertelement operands!");
Robert Bocchino2def1b32006-01-17 20:06:25 +00002260 $$ = new InsertElementInst($2, $4, $6);
2261 }
Chris Lattner4c949082006-04-08 01:18:35 +00002262 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2263 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
2264 ThrowException("Invalid shufflevector operands!");
2265 $$ = new ShuffleVectorInst($2, $4, $6);
2266 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002267 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002268 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002269 if (!Ty->isFirstClassType())
2270 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002271 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002272 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002273 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002274 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00002275 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002276 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002277 $2->pop_front();
2278 }
2279 delete $2; // Free the list...
Chris Lattnerddb6db42005-05-06 05:51:46 +00002280 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002281 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002282 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002283 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002284
Chris Lattnera8e8f162005-05-06 20:27:19 +00002285 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002286 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002287 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002288 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002289 if ($6) {
2290 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002291 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002292 ParamTypes.push_back((*I)->getType());
2293 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002294
2295 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2296 if (isVarArg) ParamTypes.pop_back();
2297
Chris Lattnera8e8f162005-05-06 20:27:19 +00002298 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Chris Lattner595f06c2005-02-01 01:47:42 +00002299 ThrowException("LLVM functions cannot return aggregate types!");
2300
Chris Lattnera8e8f162005-05-06 20:27:19 +00002301 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002302 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002303 }
Chris Lattner00950542001-06-06 20:29:01 +00002304
Chris Lattnera8e8f162005-05-06 20:27:19 +00002305 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002306
Chris Lattner8b81bf52001-07-25 22:47:46 +00002307 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002308 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002309 // Make sure no arguments is a good thing!
2310 if (Ty->getNumParams() != 0)
2311 ThrowException("No arguments passed to a function that "
2312 "expects arguments!");
2313
Chris Lattner9232b992003-04-22 18:42:41 +00002314 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002315 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002316 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002317 // correctly!
2318 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002319 FunctionType::param_iterator I = Ty->param_begin();
2320 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002321 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002322
2323 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002324 if ((*ArgI)->getType() != *I)
2325 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2326 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002327
Chris Lattner8b81bf52001-07-25 22:47:46 +00002328 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002329 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002330
Chris Lattnera8e8f162005-05-06 20:27:19 +00002331 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002332 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002333 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002334 cast<CallInst>($$)->setCallingConv($2);
2335 delete $3;
2336 delete $6;
Chris Lattner00950542001-06-06 20:29:01 +00002337 }
2338 | MemoryInst {
2339 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002340 };
Chris Lattner00950542001-06-06 20:29:01 +00002341
Chris Lattner6cdb0112001-11-26 16:54:11 +00002342
2343// IndexList - List of indices for GEP based instructions...
2344IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002345 $$ = $2;
2346 } | /* empty */ {
2347 $$ = new std::vector<Value*>();
2348 };
2349
2350OptVolatile : VOLATILE {
2351 $$ = true;
2352 }
2353 | /* empty */ {
2354 $$ = false;
2355 };
2356
Chris Lattner027dcc52001-07-08 21:10:27 +00002357
Chris Lattnerddb6db42005-05-06 05:51:46 +00002358
Chris Lattner66db8e42005-11-06 06:34:12 +00002359MemoryInst : MALLOC Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002360 $$ = new MallocInst(*$2, 0, $3);
Chris Lattner30c89792001-09-07 16:35:17 +00002361 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002362 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002363 | MALLOC Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002364 $$ = new MallocInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002365 delete $2;
2366 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002367 | ALLOCA Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002368 $$ = new AllocaInst(*$2, 0, $3);
Nate Begeman14b05292005-11-05 09:21:28 +00002369 delete $2;
2370 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002371 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002372 $$ = new AllocaInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002373 delete $2;
2374 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002375 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002376 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002377 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002378 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002379 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002380 }
2381
Chris Lattner15c9c032003-09-08 18:20:29 +00002382 | OptVolatile LOAD Types ValueRef {
2383 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002384 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002385 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002386 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2387 ThrowException("Can't load from pointer of non-first-class type: " +
2388 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002389 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002390 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002391 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002392 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2393 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002394 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002395 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002396 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002397 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002398 if (ElTy != $3->getType())
2399 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002400 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002401
Chris Lattner79ad13802003-09-08 20:29:46 +00002402 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002403 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002404 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002405 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002406 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002407 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002408
2409 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2410 // indices to uint struct indices for compatibility.
2411 generic_gep_type_iterator<std::vector<Value*>::iterator>
2412 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2413 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2414 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2415 if (isa<StructType>(*GTI)) // Only change struct indices
2416 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2417 if (CUI->getType() == Type::UByteTy)
2418 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2419
Chris Lattner30c89792001-09-07 16:35:17 +00002420 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002421 ThrowException("Invalid getelementptr indices for type '" +
2422 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002423 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2424 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002425 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002426
Brian Gaeked0fde302003-11-11 22:41:34 +00002427
Chris Lattner00950542001-06-06 20:29:01 +00002428%%
Chris Lattner09083092001-07-08 04:57:15 +00002429int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002430 std::string where
2431 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002432 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002433 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002434 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002435 errMsg += "end-of-file.";
2436 else
Chris Lattner9232b992003-04-22 18:42:41 +00002437 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002438 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002439 return 0;
2440}