blob: d77b7eebfc76d103bc3f90085bbeb0700fe70b7f [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
75 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000076 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000077 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
78
Chris Lattner2079fde2001-10-13 06:41:08 +000079 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
80 // references to global values. Global values may be referenced before they
81 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000082 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000083 //
Chris Lattner9232b992003-04-22 18:42:41 +000084 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000085 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000086 GlobalRefsType GlobalRefs;
87
Chris Lattner00950542001-06-06 20:29:01 +000088 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000089 // If we could not resolve some functions at function compilation time
90 // (calls to functions before they are defined), resolve them now... Types
91 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000092 //
Chris Lattner00950542001-06-06 20:29:01 +000093 ResolveDefinitions(LateResolveValues);
94
Chris Lattner2079fde2001-10-13 06:41:08 +000095 // Check to make sure that all global value forward references have been
96 // resolved!
97 //
98 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000099 std::string UndefinedReferences = "Unresolved global references exist:\n";
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 Lattnera8e8f162005-05-06 20:27:19 +0000984%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK
985%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; } |
1057 FASTCC_TOK { $$ = CallingConv::Fast; } |
1058 COLDCC_TOK { $$ = CallingConv::Cold; } |
1059 CC_TOK EUINT64VAL {
1060 if ((unsigned)$2 != $2)
1061 ThrowException("Calling conv too large!");
1062 $$ = $2;
1063 };
1064
Chris Lattner66db8e42005-11-06 06:34:12 +00001065// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1066// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001067OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001068 ALIGN EUINT64VAL {
1069 $$ = $2;
1070 if ($$ != 0 && !isPowerOf2_32($$))
1071 ThrowException("Alignment must be a power of two!");
1072};
Chris Lattner66db8e42005-11-06 06:34:12 +00001073OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001074 ',' ALIGN EUINT64VAL {
1075 $$ = $3;
1076 if ($$ != 0 && !isPowerOf2_32($$))
1077 ThrowException("Alignment must be a power of two!");
1078};
1079
Chris Lattner66db8e42005-11-06 06:34:12 +00001080
Chris Lattner164c3782005-11-12 00:11:10 +00001081SectionString : SECTION STRINGCONSTANT {
1082 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1083 if ($2[i] == '"' || $2[i] == '\\')
1084 ThrowException("Invalid character in section name!");
1085 $$ = $2;
1086};
1087
1088OptSection : /*empty*/ { $$ = 0; } |
1089 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001090
Chris Lattnerb2b96672005-11-12 18:21:21 +00001091// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1092// is set to be the global we are processing.
1093//
1094GlobalVarAttributes : /* empty */ {} |
1095 ',' GlobalVarAttribute GlobalVarAttributes {};
1096GlobalVarAttribute : SectionString {
1097 CurGV->setSection($1);
1098 free($1);
1099 }
1100 | ALIGN EUINT64VAL {
1101 if ($2 != 0 && !isPowerOf2_32($2))
1102 ThrowException("Alignment must be a power of two!");
1103 CurGV->setAlignment($2);
1104 };
Chris Lattner164c3782005-11-12 00:11:10 +00001105
Chris Lattner30c89792001-09-07 16:35:17 +00001106//===----------------------------------------------------------------------===//
1107// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001108// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001109// access to it, a user must explicitly use TypesV.
1110//
1111
1112// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001113TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1114UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001115
1116Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001117 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001118 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1119 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001120 };
Chris Lattner30c89792001-09-07 16:35:17 +00001121
1122
1123// Derived types are added later...
1124//
Chris Lattner51727be2002-06-04 21:58:56 +00001125PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1126PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001127UpRTypes : OPAQUE {
1128 $$ = new PATypeHolder(OpaqueType::get());
1129 }
1130 | PrimType {
1131 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001132 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001133UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001134 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001135};
Chris Lattner30c89792001-09-07 16:35:17 +00001136
Chris Lattner30c89792001-09-07 16:35:17 +00001137// Include derived types in the Types production.
1138//
1139UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001140 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001141 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001142 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001143 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001144 UR_OUT("New Upreference!\n");
1145 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001146 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001147 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001148 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1149 E = $3->end(); I != E; ++I)
1150 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001151 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1152 if (isVarArg) Params.pop_back();
1153
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001154 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001155 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001156 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001157 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001158 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001159 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001160 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001161 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001162 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1163 const llvm::Type* ElemTy = $4->get();
Chris Lattner9547d7f2005-11-10 01:42:43 +00001164 if ((unsigned)$2 != $2)
Brian Gaeke715c90b2004-08-20 06:00:58 +00001165 ThrowException("Unsigned result not equal to signed result");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001166 if (!ElemTy->isPrimitiveType())
Brian Gaeke715c90b2004-08-20 06:00:58 +00001167 ThrowException("Elemental type of a PackedType must be primitive");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001168 if (!isPowerOf2_32($2))
1169 ThrowException("Vector length should be a power of 2!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001170 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1171 delete $4;
1172 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001173 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001174 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001175 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1176 E = $2->end(); I != E; ++I)
1177 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001178
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001179 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001180 delete $2;
1181 }
1182 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001183 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001184 }
1185 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001186 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001187 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001188 };
Chris Lattner30c89792001-09-07 16:35:17 +00001189
Chris Lattner7e708292002-06-25 16:13:24 +00001190// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001191// declaration type lists
1192//
1193TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001194 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001195 $$->push_back(*$1); delete $1;
1196 }
1197 | TypeListI ',' UpRTypes {
1198 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001199 };
Chris Lattner30c89792001-09-07 16:35:17 +00001200
Chris Lattner7e708292002-06-25 16:13:24 +00001201// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001202ArgTypeListI : TypeListI
1203 | TypeListI ',' DOTDOTDOT {
1204 ($$=$1)->push_back(Type::VoidTy);
1205 }
1206 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001207 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001208 }
1209 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001210 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001211 };
Chris Lattner30c89792001-09-07 16:35:17 +00001212
Chris Lattnere98dda62001-07-14 06:10:16 +00001213// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001214// production is used ONLY to represent constants that show up AFTER a 'const',
1215// 'constant' or 'global' token at global scope. Constants that can be inlined
1216// into other expressions (such as integers and constexprs) are handled by the
1217// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001218//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001219ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001220 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001221 if (ATy == 0)
1222 ThrowException("Cannot make array constant with type: '" +
1223 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001224 const Type *ETy = ATy->getElementType();
1225 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001226
Chris Lattner30c89792001-09-07 16:35:17 +00001227 // Verify that we have the correct size...
1228 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001229 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001230 utostr($3->size()) + " arguments, but has size of " +
1231 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001232
Chris Lattner30c89792001-09-07 16:35:17 +00001233 // Verify all elements are correct type!
1234 for (unsigned i = 0; i < $3->size(); i++) {
1235 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001236 ThrowException("Element #" + utostr(i) + " is not of type '" +
1237 ETy->getDescription() +"' as required!\nIt is of type '"+
1238 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001239 }
1240
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001241 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001242 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001243 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001244 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001245 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001246 if (ATy == 0)
1247 ThrowException("Cannot make array constant with type: '" +
1248 (*$1)->getDescription() + "'!");
1249
1250 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001251 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001252 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001253 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001254 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001255 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001256 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001257 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001258 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001259 if (ATy == 0)
1260 ThrowException("Cannot make array constant with type: '" +
1261 (*$1)->getDescription() + "'!");
1262
Chris Lattner30c89792001-09-07 16:35:17 +00001263 int NumElements = ATy->getNumElements();
1264 const Type *ETy = ATy->getElementType();
1265 char *EndStr = UnEscapeLexed($3, true);
1266 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001267 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001268 itostr((int)(EndStr-$3)) +
1269 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001270 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001271 if (ETy == Type::SByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001272 for (signed char *C = (signed char *)$3; C != (signed char *)EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001273 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001274 } else if (ETy == Type::UByteTy) {
Chris Lattneree454772006-01-23 23:05:15 +00001275 for (unsigned char *C = (unsigned char *)$3;
1276 C != (unsigned char*)EndStr; ++C)
1277 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001278 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001279 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001280 ThrowException("Cannot build string arrays of non byte sized elements!");
1281 }
Chris Lattner30c89792001-09-07 16:35:17 +00001282 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001283 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001284 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001285 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001286 | Types '<' ConstVector '>' { // Nonempty unsized arr
1287 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1288 if (PTy == 0)
1289 ThrowException("Cannot make packed constant with type: '" +
1290 (*$1)->getDescription() + "'!");
1291 const Type *ETy = PTy->getElementType();
1292 int NumElements = PTy->getNumElements();
1293
1294 // Verify that we have the correct size...
1295 if (NumElements != -1 && NumElements != (int)$3->size())
1296 ThrowException("Type mismatch: constant sized packed initialized with " +
1297 utostr($3->size()) + " arguments, but has size of " +
1298 itostr(NumElements) + "!");
1299
1300 // Verify all elements are correct type!
1301 for (unsigned i = 0; i < $3->size(); i++) {
1302 if (ETy != (*$3)[i]->getType())
1303 ThrowException("Element #" + utostr(i) + " is not of type '" +
1304 ETy->getDescription() +"' as required!\nIt is of type '"+
1305 (*$3)[i]->getType()->getDescription() + "'.");
1306 }
1307
1308 $$ = ConstantPacked::get(PTy, *$3);
1309 delete $1; delete $3;
1310 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001311 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001312 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001313 if (STy == 0)
1314 ThrowException("Cannot make struct constant with type: '" +
1315 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001316
Chris Lattnerc6212f12003-05-21 16:06:56 +00001317 if ($3->size() != STy->getNumContainedTypes())
1318 ThrowException("Illegal number of initializers for structure type!");
1319
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001320 // Check to ensure that constants are compatible with the type initializer!
1321 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001322 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001323 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001324 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001325 "' for element #" + utostr(i) +
1326 " of structure initializer!");
1327
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001328 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001329 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001330 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001331 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001332 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001333 if (STy == 0)
1334 ThrowException("Cannot make struct constant with type: '" +
1335 (*$1)->getDescription() + "'!");
1336
1337 if (STy->getNumContainedTypes() != 0)
1338 ThrowException("Illegal number of initializers for structure type!");
1339
1340 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1341 delete $1;
1342 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001343 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001344 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001345 if (PTy == 0)
1346 ThrowException("Cannot make null pointer constant with type: '" +
1347 (*$1)->getDescription() + "'!");
1348
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001349 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001350 delete $1;
1351 }
Chris Lattner16710e92004-10-16 18:17:13 +00001352 | Types UNDEF {
1353 $$ = UndefValue::get($1->get());
1354 delete $1;
1355 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001356 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001357 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001358 if (Ty == 0)
1359 ThrowException("Global const reference must be a pointer type!");
1360
Chris Lattner3101c252002-08-15 17:58:33 +00001361 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001362 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001363 // the context of a function, getValNonImprovising will search the functions
1364 // symbol table instead of the module symbol table for the global symbol,
1365 // which throws things all off. To get around this, we just tell
1366 // getValNonImprovising that we are at global scope here.
1367 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001368 Function *SavedCurFn = CurFun.CurrentFunction;
1369 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001370
Chris Lattner2079fde2001-10-13 06:41:08 +00001371 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001372
Chris Lattner394f2eb2003-10-16 20:12:13 +00001373 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001374
Chris Lattner2079fde2001-10-13 06:41:08 +00001375 // If this is an initializer for a constant pointer, which is referencing a
1376 // (currently) undefined variable, create a stub now that shall be replaced
1377 // in the future with the right type of variable.
1378 //
1379 if (V == 0) {
1380 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1381 const PointerType *PT = cast<PointerType>(Ty);
1382
1383 // First check to see if the forward references value is already created!
1384 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001385 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001386
1387 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001388 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001389 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001390 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001391 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001392 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001393
Reid Spencer3271ed52004-07-18 00:08:11 +00001394 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001395 GlobalValue *GV;
1396 if (const FunctionType *FTy =
1397 dyn_cast<FunctionType>(PT->getElementType())) {
1398 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1399 CurModule.CurrentModule);
1400 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001401 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001402 GlobalValue::ExternalLinkage, 0,
1403 Name, CurModule.CurrentModule);
1404 }
Chris Lattner8a327842004-07-14 21:44:00 +00001405
Reid Spencer3271ed52004-07-18 00:08:11 +00001406 // Keep track of the fact that we have a forward ref to recycle it
1407 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1408 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001409 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001410 }
1411
Reid Spencer3271ed52004-07-18 00:08:11 +00001412 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001413 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001414 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001415 | Types ConstExpr {
1416 if ($1->get() != $2->getType())
1417 ThrowException("Mismatched types for constant expression!");
1418 $$ = $2;
1419 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001420 }
1421 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001422 const Type *Ty = $1->get();
1423 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1424 ThrowException("Cannot create a null initialized value of this type!");
1425 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001426 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001427 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001428
Chris Lattnere43f40b2002-10-09 00:25:32 +00001429ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001430 if (!ConstantSInt::isValueValidForType($1, $2))
1431 ThrowException("Constant value doesn't fit in type!");
1432 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001433 }
1434 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001435 if (!ConstantUInt::isValueValidForType($1, $2))
1436 ThrowException("Constant value doesn't fit in type!");
1437 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001438 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001439 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001440 $$ = ConstantBool::True;
1441 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001442 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001443 $$ = ConstantBool::False;
1444 }
1445 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001446 if (!ConstantFP::isValueValidForType($1, $2))
1447 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001448 $$ = ConstantFP::get($1, $2);
1449 };
1450
Chris Lattner00950542001-06-06 20:29:01 +00001451
Chris Lattnerd78700d2002-08-16 21:14:40 +00001452ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001453 if (!$3->getType()->isFirstClassType())
1454 ThrowException("cast constant expression from a non-primitive type: '" +
1455 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001456 if (!$5->get()->isFirstClassType())
1457 ThrowException("cast constant expression to a non-primitive type: '" +
1458 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001459 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001460 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001461 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001462 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1463 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001464 ThrowException("GetElementPtr requires a pointer operand!");
1465
Chris Lattner830b6f92004-04-05 01:30:04 +00001466 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1467 // indices to uint struct indices for compatibility.
1468 generic_gep_type_iterator<std::vector<Value*>::iterator>
1469 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1470 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1471 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1472 if (isa<StructType>(*GTI)) // Only change struct indices
1473 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1474 if (CUI->getType() == Type::UByteTy)
1475 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1476
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001477 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001478 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001479 if (!IdxTy)
1480 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001481
Chris Lattner9232b992003-04-22 18:42:41 +00001482 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001483 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1484 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001485 IdxVec.push_back(C);
1486 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001487 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001488
Chris Lattnerd78700d2002-08-16 21:14:40 +00001489 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001490
Chris Lattnerd78700d2002-08-16 21:14:40 +00001491 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001492 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001493 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1494 if ($3->getType() != Type::BoolTy)
1495 ThrowException("Select condition must be of boolean type!");
1496 if ($5->getType() != $7->getType())
1497 ThrowException("Select operand types must match!");
1498 $$ = ConstantExpr::getSelect($3, $5, $7);
1499 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001500 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001501 if ($3->getType() != $5->getType())
1502 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001503 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1504 // To retain backward compatibility with these early compilers, we emit a
1505 // cast to the appropriate integer type automatically if we are in the
1506 // broken case. See PR424 for more information.
1507 if (!isa<PointerType>($3->getType())) {
1508 $$ = ConstantExpr::get($1, $3, $5);
1509 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001510 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001511 switch (CurModule.CurrentModule->getPointerSize()) {
1512 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1513 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1514 default: ThrowException("invalid pointer binary constant expr!");
1515 }
1516 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1517 ConstantExpr::getCast($5, IntPtrTy));
1518 $$ = ConstantExpr::getCast($$, $3->getType());
1519 }
1520 }
1521 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1522 if ($3->getType() != $5->getType())
1523 ThrowException("Logical operator types must match!");
Chris Lattner0a017832005-12-21 18:31:29 +00001524 if (!$3->getType()->isIntegral()) {
1525 if (!isa<PackedType>($3->getType()) ||
1526 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
1527 ThrowException("Logical operator requires integral operands!");
1528 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001529 $$ = ConstantExpr::get($1, $3, $5);
1530 }
1531 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1532 if ($3->getType() != $5->getType())
1533 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001534 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001535 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001536 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001537 if ($5->getType() != Type::UByteTy)
1538 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001539 if (!$3->getType()->isInteger())
1540 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001541 $$ = ConstantExpr::get($1, $3, $5);
Robert Bocchino9c62b562006-01-10 19:04:32 +00001542 }
1543 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
1544 if (!isa<PackedType>($3->getType()))
1545 ThrowException("First operand of extractelement must be "
1546 "packed type!");
1547 if ($5->getType() != Type::UIntTy)
1548 ThrowException("Second operand of extractelement must be uint!");
1549 $$ = ConstantExpr::getExtractElement($3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001550 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001551
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001552// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001553ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001554 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001555 }
1556 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001557 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001558 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001559 };
Chris Lattner00950542001-06-06 20:29:01 +00001560
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001561
Chris Lattner1781aca2001-09-18 04:00:54 +00001562// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001563GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001564
Chris Lattner00950542001-06-06 20:29:01 +00001565
Chris Lattner0e73ce62002-05-02 19:11:13 +00001566//===----------------------------------------------------------------------===//
1567// Rules to match Modules
1568//===----------------------------------------------------------------------===//
1569
1570// Module rule: Capture the result of parsing the whole file into a result
1571// variable...
1572//
1573Module : FunctionList {
1574 $$ = ParserResult = $1;
1575 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001576};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001577
Chris Lattner7e708292002-06-25 16:13:24 +00001578// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001579//
1580FunctionList : FunctionList Function {
1581 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001582 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001583 }
1584 | FunctionList FunctionProto {
1585 $$ = $1;
1586 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001587 | FunctionList MODULE ASM_TOK AsmBlock {
Chris Lattneree454772006-01-23 23:05:15 +00001588 $$ = $1;
1589 }
Chris Lattner0e73ce62002-05-02 19:11:13 +00001590 | FunctionList IMPLEMENTATION {
1591 $$ = $1;
1592 }
1593 | ConstPool {
1594 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001595 // Emit an error if there are any unresolved types left.
1596 if (!CurModule.LateResolveTypes.empty()) {
1597 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1598 if (DID.Type == ValID::NameVal)
1599 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1600 else
1601 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1602 }
Chris Lattner51727be2002-06-04 21:58:56 +00001603 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001604
Chris Lattnere98dda62001-07-14 06:10:16 +00001605// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001606ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001607 // Eagerly resolve types. This is not an optimization, this is a
1608 // requirement that is due to the fact that we could have this:
1609 //
1610 // %list = type { %list * }
1611 // %list = type { %list * } ; repeated type decl
1612 //
1613 // If types are not resolved eagerly, then the two types will not be
1614 // determined to be the same type!
1615 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001616 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001617
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001618 if (!setTypeName(*$4, $2) && !$2) {
1619 // If this is a named type that is not a redefinition, add it to the slot
1620 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001621 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001622 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001623
1624 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001625 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001626 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001627 }
Chris Lattner71cdba32006-01-24 00:40:17 +00001628 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Chris Lattneree454772006-01-23 23:05:15 +00001629 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001630 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001631 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001632 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
1633 } GlobalVarAttributes {
1634 CurGV = 0;
Chris Lattner1781aca2001-09-18 04:00:54 +00001635 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001636 | ConstPool OptAssign EXTERNAL GlobalType Types {
1637 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage,
1638 $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001639 delete $5;
Chris Lattnerb2b96672005-11-12 18:21:21 +00001640 } GlobalVarAttributes {
1641 CurGV = 0;
Chris Lattnere98dda62001-07-14 06:10:16 +00001642 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001643 | ConstPool TARGET TargetDefinition {
1644 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001645 | ConstPool DEPLIBS '=' LibrariesDefinition {
1646 }
Chris Lattner00950542001-06-06 20:29:01 +00001647 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001648 };
Chris Lattner00950542001-06-06 20:29:01 +00001649
1650
Chris Lattneree454772006-01-23 23:05:15 +00001651AsmBlock : STRINGCONSTANT {
Chris Lattner66316012006-01-24 04:14:29 +00001652 const std::string &AsmSoFar = CurModule.CurrentModule->getModuleInlineAsm();
Chris Lattneree454772006-01-23 23:05:15 +00001653 char *EndStr = UnEscapeLexed($1, true);
1654 std::string NewAsm($1, EndStr);
1655 free($1);
1656
1657 if (AsmSoFar.empty())
Chris Lattner66316012006-01-24 04:14:29 +00001658 CurModule.CurrentModule->setModuleInlineAsm(NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00001659 else
Chris Lattner66316012006-01-24 04:14:29 +00001660 CurModule.CurrentModule->setModuleInlineAsm(AsmSoFar+"\n"+NewAsm);
Chris Lattneree454772006-01-23 23:05:15 +00001661};
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001662
1663BigOrLittle : BIG { $$ = Module::BigEndian; };
1664BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1665
1666TargetDefinition : ENDIAN '=' BigOrLittle {
1667 CurModule.CurrentModule->setEndianness($3);
1668 }
1669 | POINTERSIZE '=' EUINT64VAL {
1670 if ($3 == 32)
1671 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1672 else if ($3 == 64)
1673 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1674 else
1675 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001676 }
1677 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001678 CurModule.CurrentModule->setTargetTriple($3);
1679 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001680 };
1681
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001682LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001683
1684LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001685 CurModule.CurrentModule->addLibrary($3);
1686 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001687 }
1688 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001689 CurModule.CurrentModule->addLibrary($1);
1690 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001691 }
1692 | /* empty: end of list */ {
1693 }
1694 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001695
Chris Lattner00950542001-06-06 20:29:01 +00001696//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001697// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001698//===----------------------------------------------------------------------===//
1699
Chris Lattner6c23f572003-08-22 05:42:10 +00001700Name : VAR_ID | STRINGCONSTANT;
1701OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001702
Chris Lattner6c23f572003-08-22 05:42:10 +00001703ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001704 if (*$1 == Type::VoidTy)
1705 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001706 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001707};
Chris Lattner00950542001-06-06 20:29:01 +00001708
Chris Lattner69da5cf2002-10-13 20:57:00 +00001709ArgListH : ArgListH ',' ArgVal {
1710 $$ = $1;
1711 $1->push_back(*$3);
1712 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001713 }
1714 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001715 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001716 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001717 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001718 };
Chris Lattner00950542001-06-06 20:29:01 +00001719
1720ArgList : ArgListH {
1721 $$ = $1;
1722 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001723 | ArgListH ',' DOTDOTDOT {
1724 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001725 $$->push_back(std::pair<PATypeHolder*,
1726 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001727 }
1728 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001729 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1730 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001731 }
Chris Lattner00950542001-06-06 20:29:01 +00001732 | /* empty */ {
1733 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001734 };
Chris Lattner00950542001-06-06 20:29:01 +00001735
Chris Lattner164c3782005-11-12 00:11:10 +00001736FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1737 OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001738 UnEscapeLexed($3);
1739 std::string FunctionName($3);
1740 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001741
Chris Lattnera8e8f162005-05-06 20:27:19 +00001742 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001743 ThrowException("LLVM functions cannot return aggregate types!");
1744
Chris Lattner9232b992003-04-22 18:42:41 +00001745 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001746 if ($5) { // If there are arguments...
1747 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1748 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001749 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001750 }
Chris Lattner00950542001-06-06 20:29:01 +00001751
Chris Lattner2079fde2001-10-13 06:41:08 +00001752 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1753 if (isVarArg) ParamTypeList.pop_back();
1754
Chris Lattnera8e8f162005-05-06 20:27:19 +00001755 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001756 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001757 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001758
Chris Lattnera09000d2004-07-14 23:03:46 +00001759 ValID ID;
1760 if (!FunctionName.empty()) {
1761 ID = ValID::create((char*)FunctionName.c_str());
1762 } else {
1763 ID = ValID::create((int)CurModule.Values[PFT].size());
1764 }
1765
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001766 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001767 // See if this function was forward referenced. If so, recycle the object.
1768 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1769 // Move the function to the end of the list, from whereever it was
1770 // previously inserted.
1771 Fn = cast<Function>(FWRef);
1772 CurModule.CurrentModule->getFunctionList().remove(Fn);
1773 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1774 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1775 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1776 // If this is the case, either we need to be a forward decl, or it needs
1777 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001778 if (!CurFun.isDeclare && !Fn->isExternal())
1779 ThrowException("Redefinition of function '" + FunctionName + "'!");
1780
Chris Lattnera09000d2004-07-14 23:03:46 +00001781 // Make sure to strip off any argument names so we can't get conflicts.
1782 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001783 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001784 AI != AE; ++AI)
1785 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001786
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001787 } else { // Not already defined?
1788 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1789 CurModule.CurrentModule);
1790 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001791 }
Chris Lattner00950542001-06-06 20:29:01 +00001792
Chris Lattner394f2eb2003-10-16 20:12:13 +00001793 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001794 Fn->setCallingConv($1);
Chris Lattner164c3782005-11-12 00:11:10 +00001795 Fn->setAlignment($8);
1796 if ($7) {
1797 Fn->setSection($7);
1798 free($7);
1799 }
Chris Lattner00950542001-06-06 20:29:01 +00001800
Chris Lattner7e708292002-06-25 16:13:24 +00001801 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001802 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001803 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001804 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001805 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001806 delete $5->back().first;
1807 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001808 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001809 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001810 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1811 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001812 delete I->first; // Delete the typeholder...
1813
Chris Lattner8ab406d2004-07-13 07:59:27 +00001814 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001815 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001816 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001817
Chris Lattnera8e8f162005-05-06 20:27:19 +00001818 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001819 }
Chris Lattner51727be2002-06-04 21:58:56 +00001820};
Chris Lattner00950542001-06-06 20:29:01 +00001821
Chris Lattner9b02cc32002-05-03 18:23:48 +00001822BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1823
Chris Lattner4ad02e72003-04-16 20:28:45 +00001824FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001825 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001826
Chris Lattner4ad02e72003-04-16 20:28:45 +00001827 // Make sure that we keep track of the linkage type even if there was a
1828 // previous "declare".
1829 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001830};
Chris Lattner00950542001-06-06 20:29:01 +00001831
Chris Lattner9b02cc32002-05-03 18:23:48 +00001832END : ENDTOK | '}'; // Allow end of '}' to end a function
1833
Chris Lattner79df7c02002-03-26 18:01:55 +00001834Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001835 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001836};
Chris Lattner00950542001-06-06 20:29:01 +00001837
Chris Lattner394f2eb2003-10-16 20:12:13 +00001838FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1839 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001840 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001841};
Chris Lattner00950542001-06-06 20:29:01 +00001842
1843//===----------------------------------------------------------------------===//
1844// Rules to match Basic Blocks
1845//===----------------------------------------------------------------------===//
1846
Chris Lattneraa2c8532006-01-25 22:26:43 +00001847OptSideEffect : /* empty */ {
1848 $$ = false;
1849 }
1850 | SIDEEFFECT {
1851 $$ = true;
1852 };
1853
Chris Lattner00950542001-06-06 20:29:01 +00001854ConstValueRef : ESINT64VAL { // A reference to a direct constant
1855 $$ = ValID::create($1);
1856 }
1857 | EUINT64VAL {
1858 $$ = ValID::create($1);
1859 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001860 | FPVAL { // Perhaps it's an FP constant?
1861 $$ = ValID::create($1);
1862 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001863 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001864 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001865 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001866 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001867 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001868 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001869 | NULL_TOK {
1870 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001871 }
Chris Lattner16710e92004-10-16 18:17:13 +00001872 | UNDEF {
1873 $$ = ValID::createUndef();
1874 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00001875 | ZEROINITIALIZER { // A vector zero constant.
1876 $$ = ValID::createZeroInit();
1877 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001878 | '<' ConstVector '>' { // Nonempty unsized packed vector
1879 const Type *ETy = (*$2)[0]->getType();
1880 int NumElements = $2->size();
1881
1882 PackedType* pt = PackedType::get(ETy, NumElements);
1883 PATypeHolder* PTy = new PATypeHolder(
1884 HandleUpRefs(
1885 PackedType::get(
1886 ETy,
1887 NumElements)
1888 )
1889 );
1890
1891 // Verify all elements are correct type!
1892 for (unsigned i = 0; i < $2->size(); i++) {
1893 if (ETy != (*$2)[i]->getType())
1894 ThrowException("Element #" + utostr(i) + " is not of type '" +
1895 ETy->getDescription() +"' as required!\nIt is of type '" +
1896 (*$2)[i]->getType()->getDescription() + "'.");
1897 }
1898
1899 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1900 delete PTy; delete $2;
1901 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001902 | ConstExpr {
1903 $$ = ValID::create($1);
Chris Lattneraa2c8532006-01-25 22:26:43 +00001904 }
1905 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1906 char *End = UnEscapeLexed($3, true);
1907 std::string AsmStr = std::string($3, End);
1908 End = UnEscapeLexed($5, true);
1909 std::string Constraints = std::string($5, End);
1910 $$ = ValID::createInlineAsm(AsmStr, Constraints, $2);
1911 free($3);
1912 free($5);
Chris Lattnerd78700d2002-08-16 21:14:40 +00001913 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001914
Chris Lattner2079fde2001-10-13 06:41:08 +00001915// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1916// another value.
1917//
1918SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001919 $$ = ValID::create($1);
1920 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001921 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001922 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001923 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001924
1925// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001926ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001927
Chris Lattner00950542001-06-06 20:29:01 +00001928
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001929// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1930// type immediately preceeds the value reference, and allows complex constant
1931// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001932ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001933 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001934 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001935
Chris Lattner00950542001-06-06 20:29:01 +00001936BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001937 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001938 }
Chris Lattner7e708292002-06-25 16:13:24 +00001939 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001940 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001941 };
Chris Lattner00950542001-06-06 20:29:01 +00001942
1943
1944// Basic blocks are terminated by branching instructions:
1945// br, br/cc, switch, ret
1946//
Chris Lattner2079fde2001-10-13 06:41:08 +00001947BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001948 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001949 InsertValue($3);
1950
1951 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001952 InsertValue($1);
1953 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001954 };
Chris Lattner00950542001-06-06 20:29:01 +00001955
1956InstructionList : InstructionList Inst {
1957 $1->getInstList().push_back($2);
1958 $$ = $1;
1959 }
1960 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001961 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001962
1963 // Make sure to move the basic block to the correct location in the
1964 // function, instead of leaving it inserted wherever it was first
1965 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001966 Function::BasicBlockListType &BBL =
1967 CurFun.CurrentFunction->getBasicBlockList();
1968 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner22ae2322004-07-13 08:39:15 +00001969 }
1970 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001971 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001972
1973 // Make sure to move the basic block to the correct location in the
1974 // function, instead of leaving it inserted wherever it was first
1975 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001976 Function::BasicBlockListType &BBL =
1977 CurFun.CurrentFunction->getBasicBlockList();
1978 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner51727be2002-06-04 21:58:56 +00001979 };
Chris Lattner00950542001-06-06 20:29:01 +00001980
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001981BBTerminatorInst : RET ResolvedVal { // Return with a result...
1982 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001983 }
1984 | RET VOID { // Return with no result...
1985 $$ = new ReturnInst();
1986 }
1987 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001988 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001989 } // Conditional Branch...
1990 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001991 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001992 }
1993 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001994 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001995 $$ = S;
1996
Chris Lattner9232b992003-04-22 18:42:41 +00001997 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001998 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001999 for (; I != E; ++I) {
2000 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
2001 S->addCase(CI, I->second);
2002 else
2003 ThrowException("Switch case is constant, but not a simple integer!");
2004 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00002005 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00002006 }
Chris Lattner196850c2003-05-15 21:30:00 +00002007 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002008 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00002009 $$ = S;
2010 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002011 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
2012 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002013 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002014 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00002015
Chris Lattnera8e8f162005-05-06 20:27:19 +00002016 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002017 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00002018 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002019 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002020 if ($6) {
2021 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002022 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00002023 ParamTypes.push_back((*I)->getType());
2024 }
2025
2026 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2027 if (isVarArg) ParamTypes.pop_back();
2028
Chris Lattnera8e8f162005-05-06 20:27:19 +00002029 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002030 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00002031 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002032
Chris Lattnera8e8f162005-05-06 20:27:19 +00002033 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00002034
Chris Lattnera8e8f162005-05-06 20:27:19 +00002035 BasicBlock *Normal = getBBVal($10);
2036 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00002037
2038 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002039 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00002040 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00002041 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002042 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00002043 // correctly!
2044 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002045 FunctionType::param_iterator I = Ty->param_begin();
2046 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002047 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00002048
2049 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002050 if ((*ArgI)->getType() != *I)
2051 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2052 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002053
2054 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002055 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002056
Chris Lattnera8e8f162005-05-06 20:27:19 +00002057 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00002058 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002059 cast<InvokeInst>($$)->setCallingConv($2);
2060
2061 delete $3;
2062 delete $6;
Chris Lattner36143fc2003-09-08 18:54:55 +00002063 }
2064 | UNWIND {
2065 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00002066 }
2067 | UNREACHABLE {
2068 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00002069 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002070
2071
Chris Lattner00950542001-06-06 20:29:01 +00002072
2073JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2074 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002075 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00002076 if (V == 0)
2077 ThrowException("May only switch on a constant pool value!");
2078
Chris Lattner64116f92004-07-14 01:33:11 +00002079 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00002080 }
2081 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002082 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002083 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00002084
2085 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($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00002089 };
Chris Lattner00950542001-06-06 20:29:01 +00002090
2091Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00002092 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00002093 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00002094 InsertValue($2);
2095 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00002096};
Chris Lattner00950542001-06-06 20:29:01 +00002097
Chris Lattnerc24d2082001-06-11 15:04:20 +00002098PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00002099 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00002100 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00002101 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002102 }
2103 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2104 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00002105 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00002106 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00002107 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002108
2109
Chris Lattner30c89792001-09-07 16:35:17 +00002110ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00002111 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002112 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00002113 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002114 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00002115 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002116 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00002117 };
Chris Lattner00950542001-06-06 20:29:01 +00002118
2119// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00002120ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00002121
Chris Lattnerddb6db42005-05-06 05:51:46 +00002122OptTailCall : TAIL CALL {
2123 $$ = true;
2124 }
2125 | CALL {
2126 $$ = false;
2127 };
2128
2129
2130
Chris Lattner4a6482b2002-09-10 19:57:26 +00002131InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00002132 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2133 !isa<PackedType>((*$2).get()))
2134 ThrowException(
2135 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00002136 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
2137 ThrowException("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002138 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2139 if ($$ == 0)
2140 ThrowException("binary operator returned null!");
2141 delete $2;
2142 }
2143 | LogicalOps Types ValueRef ',' ValueRef {
Chris Lattner0a017832005-12-21 18:31:29 +00002144 if (!(*$2)->isIntegral()) {
2145 if (!isa<PackedType>($2->get()) ||
2146 !cast<PackedType>($2->get())->getElementType()->isIntegral())
2147 ThrowException("Logical operator requires integral operands!");
2148 }
Chris Lattner4a6482b2002-09-10 19:57:26 +00002149 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2150 if ($$ == 0)
2151 ThrowException("binary operator returned null!");
2152 delete $2;
2153 }
2154 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002155 if(isa<PackedType>((*$2).get())) {
2156 ThrowException(
2157 "PackedTypes currently not supported in setcc instructions!");
2158 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00002159 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00002160 if ($$ == 0)
2161 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002162 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002163 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002164 | NOT ResolvedVal {
2165 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2166 << " Replacing with 'xor'.\n";
2167
2168 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2169 if (Ones == 0)
2170 ThrowException("Expected integral type for not instruction!");
2171
2172 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002173 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00002174 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00002175 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002176 | ShiftOps ResolvedVal ',' ResolvedVal {
2177 if ($4->getType() != Type::UByteTy)
2178 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002179 if (!$2->getType()->isInteger())
2180 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002181 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00002182 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002183 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002184 if (!$4->get()->isFirstClassType())
2185 ThrowException("cast instruction to a non-primitive type: '" +
2186 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002187 $$ = new CastInst($2, *$4);
2188 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002189 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002190 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2191 if ($2->getType() != Type::BoolTy)
2192 ThrowException("select condition must be boolean!");
2193 if ($4->getType() != $6->getType())
2194 ThrowException("select value types should match!");
2195 $$ = new SelectInst($2, $4, $6);
2196 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002197 | VAARG ResolvedVal ',' Types {
Andrew Lenharthe78f50d2005-06-19 03:53:56 +00002198 NewVarArgs = true;
Chris Lattner99e7ab72003-10-18 05:53:13 +00002199 $$ = new VAArgInst($2, *$4);
2200 delete $4;
2201 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002202 | VAARG_old ResolvedVal ',' Types {
2203 ObsoleteVarArgs = true;
2204 const Type* ArgTy = $2->getType();
2205 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002206 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002207
2208 //b = vaarg a, t ->
2209 //foo = alloca 1 of t
2210 //bar = vacopy a
2211 //store bar -> foo
2212 //b = vaarg foo, t
2213 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2214 CurBB->getInstList().push_back(foo);
2215 CallInst* bar = new CallInst(NF, $2);
2216 CurBB->getInstList().push_back(bar);
2217 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2218 $$ = new VAArgInst(foo, *$4);
2219 delete $4;
2220 }
2221 | VANEXT_old ResolvedVal ',' Types {
2222 ObsoleteVarArgs = true;
2223 const Type* ArgTy = $2->getType();
2224 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002225 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002226
2227 //b = vanext a, t ->
2228 //foo = alloca 1 of t
2229 //bar = vacopy a
2230 //store bar -> foo
2231 //tmp = vaarg foo, t
2232 //b = load foo
2233 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2234 CurBB->getInstList().push_back(foo);
2235 CallInst* bar = new CallInst(NF, $2);
2236 CurBB->getInstList().push_back(bar);
2237 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2238 Instruction* tmp = new VAArgInst(foo, *$4);
2239 CurBB->getInstList().push_back(tmp);
2240 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002241 delete $4;
2242 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002243 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
2244 if (!isa<PackedType>($2->getType()))
Robert Bocchino2def1b32006-01-17 20:06:25 +00002245 ThrowException("First operand of extractelement must be "
2246 "packed type!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00002247 if ($4->getType() != Type::UIntTy)
Robert Bocchino2def1b32006-01-17 20:06:25 +00002248 ThrowException("Second operand of extractelement must be uint!");
Robert Bocchino9c62b562006-01-10 19:04:32 +00002249 $$ = new ExtractElementInst($2, $4);
2250 }
Robert Bocchino2def1b32006-01-17 20:06:25 +00002251 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2252 if (!isa<PackedType>($2->getType()))
2253 ThrowException("First operand of insertelement must be "
2254 "packed type!");
2255 if ($4->getType() !=
2256 cast<PackedType>($2->getType())->getElementType())
2257 ThrowException("Second operand of insertelement must be "
2258 "packed element type!");
2259 if ($6->getType() != Type::UIntTy)
2260 ThrowException("Third operand of insertelement must be uint!");
2261 $$ = new InsertElementInst($2, $4, $6);
2262 }
Chris Lattner4c949082006-04-08 01:18:35 +00002263 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2264 if (!ShuffleVectorInst::isValidOperands($2, $4, $6))
2265 ThrowException("Invalid shufflevector operands!");
2266 $$ = new ShuffleVectorInst($2, $4, $6);
2267 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002268 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002269 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002270 if (!Ty->isFirstClassType())
2271 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002272 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002273 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002274 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002275 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00002276 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002277 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002278 $2->pop_front();
2279 }
2280 delete $2; // Free the list...
Chris Lattnerddb6db42005-05-06 05:51:46 +00002281 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002282 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002283 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002284 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002285
Chris Lattnera8e8f162005-05-06 20:27:19 +00002286 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002287 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002288 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002289 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002290 if ($6) {
2291 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002292 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002293 ParamTypes.push_back((*I)->getType());
2294 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002295
2296 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2297 if (isVarArg) ParamTypes.pop_back();
2298
Chris Lattnera8e8f162005-05-06 20:27:19 +00002299 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Chris Lattner595f06c2005-02-01 01:47:42 +00002300 ThrowException("LLVM functions cannot return aggregate types!");
2301
Chris Lattnera8e8f162005-05-06 20:27:19 +00002302 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002303 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002304 }
Chris Lattner00950542001-06-06 20:29:01 +00002305
Chris Lattnera8e8f162005-05-06 20:27:19 +00002306 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002307
Chris Lattner8b81bf52001-07-25 22:47:46 +00002308 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002309 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002310 // Make sure no arguments is a good thing!
2311 if (Ty->getNumParams() != 0)
2312 ThrowException("No arguments passed to a function that "
2313 "expects arguments!");
2314
Chris Lattner9232b992003-04-22 18:42:41 +00002315 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002316 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002317 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002318 // correctly!
2319 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002320 FunctionType::param_iterator I = Ty->param_begin();
2321 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002322 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002323
2324 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002325 if ((*ArgI)->getType() != *I)
2326 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2327 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002328
Chris Lattner8b81bf52001-07-25 22:47:46 +00002329 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002330 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002331
Chris Lattnera8e8f162005-05-06 20:27:19 +00002332 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002333 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002334 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002335 cast<CallInst>($$)->setCallingConv($2);
2336 delete $3;
2337 delete $6;
Chris Lattner00950542001-06-06 20:29:01 +00002338 }
2339 | MemoryInst {
2340 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002341 };
Chris Lattner00950542001-06-06 20:29:01 +00002342
Chris Lattner6cdb0112001-11-26 16:54:11 +00002343
2344// IndexList - List of indices for GEP based instructions...
2345IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002346 $$ = $2;
2347 } | /* empty */ {
2348 $$ = new std::vector<Value*>();
2349 };
2350
2351OptVolatile : VOLATILE {
2352 $$ = true;
2353 }
2354 | /* empty */ {
2355 $$ = false;
2356 };
2357
Chris Lattner027dcc52001-07-08 21:10:27 +00002358
Chris Lattnerddb6db42005-05-06 05:51:46 +00002359
Chris Lattner66db8e42005-11-06 06:34:12 +00002360MemoryInst : MALLOC Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002361 $$ = new MallocInst(*$2, 0, $3);
Chris Lattner30c89792001-09-07 16:35:17 +00002362 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002363 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002364 | MALLOC Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002365 $$ = new MallocInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002366 delete $2;
2367 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002368 | ALLOCA Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002369 $$ = new AllocaInst(*$2, 0, $3);
Nate Begeman14b05292005-11-05 09:21:28 +00002370 delete $2;
2371 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002372 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002373 $$ = new AllocaInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002374 delete $2;
2375 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002376 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002377 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002378 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002379 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002380 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002381 }
2382
Chris Lattner15c9c032003-09-08 18:20:29 +00002383 | OptVolatile LOAD Types ValueRef {
2384 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002385 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002386 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002387 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2388 ThrowException("Can't load from pointer of non-first-class type: " +
2389 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002390 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002391 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002392 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002393 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2394 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002395 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002396 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002397 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002398 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002399 if (ElTy != $3->getType())
2400 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002401 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002402
Chris Lattner79ad13802003-09-08 20:29:46 +00002403 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002404 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002405 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002406 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002407 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002408 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002409
2410 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2411 // indices to uint struct indices for compatibility.
2412 generic_gep_type_iterator<std::vector<Value*>::iterator>
2413 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2414 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2415 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2416 if (isa<StructType>(*GTI)) // Only change struct indices
2417 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2418 if (CUI->getType() == Type::UByteTy)
2419 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2420
Chris Lattner30c89792001-09-07 16:35:17 +00002421 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002422 ThrowException("Invalid getelementptr indices for type '" +
2423 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002424 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2425 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002426 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002427
Brian Gaeked0fde302003-11-11 22:41:34 +00002428
Chris Lattner00950542001-06-06 20:29:01 +00002429%%
Chris Lattner09083092001-07-08 04:57:15 +00002430int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002431 std::string where
2432 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002433 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002434 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002435 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002436 errMsg += "end-of-file.";
2437 else
Chris Lattner9232b992003-04-22 18:42:41 +00002438 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002439 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002440 return 0;
2441}