blob: d73b008416ae4f3c5ea3640324eb5b0d493abd87 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000017#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/SymbolTable.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/STLExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000022#include <algorithm>
23#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000024#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000025#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000026
Chris Lattner386a3b72001-10-16 19:54:17 +000027int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000028int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000029int yyparse();
30
Brian Gaeked0fde302003-11-11 22:41:34 +000031namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000032 std::string CurFilename;
33}
34using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattner00950542001-06-06 20:29:01 +000036static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000043#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000044#else
45#define UR_OUT(X)
46#endif
47
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000048#define YYERROR_VERBOSE 1
49
Chris Lattner7e708292002-06-25 16:13:24 +000050// This contains info used when building the body of a function. It is
51// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000052//
Chris Lattner9232b992003-04-22 18:42:41 +000053typedef std::vector<Value *> ValueList; // Numbered defs
Misha Brukman5a2a3822005-05-10 22:02:28 +000054static void
55ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
56 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000057
58static struct PerModuleInfo {
59 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000060 std::map<const Type *, ValueList> Values; // Module level numbered definitions
61 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000062 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000063 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000064
Chris Lattnerbc721ed2004-07-13 08:28:21 +000065 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
66 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000067 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000068 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
69
Chris Lattner2079fde2001-10-13 06:41:08 +000070 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
71 // references to global values. Global values may be referenced before they
72 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000073 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000074 //
Chris Lattner9232b992003-04-22 18:42:41 +000075 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000076 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000077 GlobalRefsType GlobalRefs;
78
Chris Lattner00950542001-06-06 20:29:01 +000079 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000080 // If we could not resolve some functions at function compilation time
81 // (calls to functions before they are defined), resolve them now... Types
82 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000083 //
Chris Lattner00950542001-06-06 20:29:01 +000084 ResolveDefinitions(LateResolveValues);
85
Chris Lattner2079fde2001-10-13 06:41:08 +000086 // Check to make sure that all global value forward references have been
87 // resolved!
88 //
89 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000090 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +000091
Chris Lattner749ce032002-03-11 22:12:39 +000092 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
93 I != E; ++I) {
94 UndefinedReferences += " " + I->first.first->getDescription() + " " +
95 I->first.second.getName() + "\n";
96 }
97 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000098 }
99
Chris Lattner7e708292002-06-25 16:13:24 +0000100 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000101 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000102 CurrentModule = 0;
103 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000104
105
Chris Lattner8a327842004-07-14 21:44:00 +0000106 // GetForwardRefForGlobal - Check to see if there is a forward reference
107 // for this global. If so, remove it from the GlobalRefs map and return it.
108 // If not, just return null.
109 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
110 // Check to see if there is a forward reference to this global variable...
111 // if there is, eliminate it and patch the reference to use the new def'n.
112 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
113 GlobalValue *Ret = 0;
114 if (I != GlobalRefs.end()) {
115 Ret = I->second;
116 GlobalRefs.erase(I);
117 }
118 return Ret;
119 }
Chris Lattner00950542001-06-06 20:29:01 +0000120} CurModule;
121
Chris Lattner79df7c02002-03-26 18:01:55 +0000122static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000123 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000124
Chris Lattner735f2702004-07-08 22:30:50 +0000125 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
126 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000127 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000128
Chris Lattner2e2ed292004-07-14 06:28:35 +0000129 /// BBForwardRefs - When we see forward references to basic blocks, keep
130 /// track of them here.
131 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
132 std::vector<BasicBlock*> NumberedBlocks;
133 unsigned NextBBNum;
134
Chris Lattner79df7c02002-03-26 18:01:55 +0000135 inline PerFunctionInfo() {
136 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000137 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000138 }
139
Chris Lattner79df7c02002-03-26 18:01:55 +0000140 inline void FunctionStart(Function *M) {
141 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000142 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000143 }
144
Chris Lattner79df7c02002-03-26 18:01:55 +0000145 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000146 NumberedBlocks.clear();
147
148 // Any forward referenced blocks left?
149 if (!BBForwardRefs.empty())
150 ThrowException("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000151 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000152
153 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000154 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000155
Chris Lattner7e708292002-06-25 16:13:24 +0000156 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000157 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000158 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000159 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000160} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000161
Chris Lattner394f2eb2003-10-16 20:12:13 +0000162static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000163
Chris Lattner00950542001-06-06 20:29:01 +0000164
165//===----------------------------------------------------------------------===//
166// Code to handle definitions of all the types
167//===----------------------------------------------------------------------===//
168
Chris Lattner735f2702004-07-08 22:30:50 +0000169static int InsertValue(Value *V,
170 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
171 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000172
173 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000174 ValueList &List = ValueTab[V->getType()];
175 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000176 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000177}
178
Chris Lattner30c89792001-09-07 16:35:17 +0000179static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000180 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000181 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000182 // Module constants occupy the lowest numbered slots...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000183 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000184 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000185 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000186 case ValID::NameVal: // Is it a named definition?
187 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
188 D.destroy(); // Free old strdup'd memory...
189 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000190 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000191 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000192 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000193 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000194 }
195
196 // If we reached here, we referenced either a symbol that we don't know about
197 // or an id number that hasn't been read yet. We may be referencing something
198 // forward, so just create an entry to be resolved later and get to it...
199 //
200 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
201
Chris Lattner355ad1f2005-03-21 06:27:42 +0000202
203 if (inFunctionScope()) {
204 if (D.Type == ValID::NameVal)
205 ThrowException("Reference to an undefined type: '" + D.getName() + "'");
206 else
207 ThrowException("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000208 }
Chris Lattner30c89792001-09-07 16:35:17 +0000209
Chris Lattner355ad1f2005-03-21 06:27:42 +0000210 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
211 if (I != CurModule.LateResolveTypes.end())
212 return I->second;
213
Chris Lattner82269592001-10-22 06:01:08 +0000214 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000215 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000216 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000217 }
Chris Lattner30c89792001-09-07 16:35:17 +0000218
Chris Lattner9232b992003-04-22 18:42:41 +0000219static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000220 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000221 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000222 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000223 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000224}
225
Chris Lattner2079fde2001-10-13 06:41:08 +0000226// getValNonImprovising - Look up the value specified by the provided type and
227// the provided ValID. If the value exists and has already been defined, return
228// it. Otherwise return null.
229//
230static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000231 if (isa<FunctionType>(Ty))
232 ThrowException("Functions are not values and "
233 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000234
Chris Lattner30c89792001-09-07 16:35:17 +0000235 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000236 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000237 unsigned Num = (unsigned)D.Num;
238
239 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000240 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000241 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000242 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000243 return VI->second[Num];
244 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000245 }
246
247 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000248 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000249 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000250
251 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000252 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000253
Chris Lattner16af11d2004-02-09 21:03:38 +0000254 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000255 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000256
Chris Lattner1a1cb112001-09-30 22:46:54 +0000257 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000258 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000259 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000260
261 D.destroy(); // Free old strdup'd memory...
262 return N;
263 }
264
Misha Brukman5a2a3822005-05-10 22:02:28 +0000265 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000266 // value will fit into the specified type...
267 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000268 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
269 ThrowException("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000270 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000271 Ty->getDescription() + "'!");
272 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000273
274 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000275 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
276 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000277 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000278 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000279 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000280 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000281 }
282 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000283 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000284 }
285
Chris Lattner2079fde2001-10-13 06:41:08 +0000286 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000287 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000288 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000289 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000290
Chris Lattner2079fde2001-10-13 06:41:08 +0000291 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000292 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000293 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000294 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000295
Chris Lattner16710e92004-10-16 18:17:13 +0000296 case ValID::ConstUndefVal: // Is it an undef value?
297 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000298
Chris Lattnerd78700d2002-08-16 21:14:40 +0000299 case ValID::ConstantVal: // Fully resolved constant?
300 if (D.ConstantValue->getType() != Ty)
301 ThrowException("Constant expression type different from required type!");
302 return D.ConstantValue;
303
Chris Lattner30c89792001-09-07 16:35:17 +0000304 default:
305 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000306 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000307 } // End of switch
308
Chris Lattner2079fde2001-10-13 06:41:08 +0000309 assert(0 && "Unhandled case!");
310 return 0;
311}
312
Chris Lattner2079fde2001-10-13 06:41:08 +0000313// getVal - This function is identical to getValNonImprovising, except that if a
314// value is not already defined, it "improvises" by creating a placeholder var
315// that looks and acts just like the requested variable. When the value is
316// defined later, all uses of the placeholder variable are replaced with the
317// real thing.
318//
Chris Lattner64116f92004-07-14 01:33:11 +0000319static Value *getVal(const Type *Ty, const ValID &ID) {
320 if (Ty == Type::LabelTy)
321 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000322
Chris Lattner64116f92004-07-14 01:33:11 +0000323 // See if the value has already been defined.
324 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000325 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000326
Chris Lattner60cd9552005-03-23 01:29:26 +0000327 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
328 ThrowException("Invalid use of a composite type!");
329
Chris Lattner00950542001-06-06 20:29:01 +0000330 // If we reached here, we referenced either a symbol that we don't know about
331 // or an id number that hasn't been read yet. We may be referencing something
332 // forward, so just create an entry to be resolved later and get to it...
333 //
Chris Lattner64116f92004-07-14 01:33:11 +0000334 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000335
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000336 // Remember where this forward reference came from. FIXME, shouldn't we try
337 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000338 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000339 llvmAsmlineno)));
340
Chris Lattner79df7c02002-03-26 18:01:55 +0000341 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000342 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000343 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000344 InsertValue(V, CurModule.LateResolveValues);
345 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000346}
347
Chris Lattner2e2ed292004-07-14 06:28:35 +0000348/// getBBVal - This is used for two purposes:
349/// * If isDefinition is true, a new basic block with the specified ID is being
350/// defined.
351/// * If isDefinition is true, this is a reference to a basic block, which may
352/// or may not be a forward reference.
353///
354static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000355 assert(inFunctionScope() && "Can't get basic block at global scope!");
356
Chris Lattner2e2ed292004-07-14 06:28:35 +0000357 std::string Name;
358 BasicBlock *BB = 0;
359 switch (ID.Type) {
360 default: ThrowException("Illegal label reference " + ID.getName());
361 case ValID::NumberVal: // Is it a numbered definition?
362 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
363 CurFun.NumberedBlocks.resize(ID.Num+1);
364 BB = CurFun.NumberedBlocks[ID.Num];
365 break;
366 case ValID::NameVal: // Is it a named definition?
367 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000368 if (Value *N = CurFun.CurrentFunction->
369 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000370 BB = cast<BasicBlock>(N);
371 break;
372 }
Chris Lattner64116f92004-07-14 01:33:11 +0000373
Chris Lattner2e2ed292004-07-14 06:28:35 +0000374 // See if the block has already been defined.
375 if (BB) {
376 // If this is the definition of the block, make sure the existing value was
377 // just a forward reference. If it was a forward reference, there will be
378 // an entry for it in the PlaceHolderInfo map.
379 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
380 // The existing value was a definition, not a forward reference.
381 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000382
Chris Lattner2e2ed292004-07-14 06:28:35 +0000383 ID.destroy(); // Free strdup'd memory.
384 return BB;
385 }
386
387 // Otherwise this block has not been seen before.
388 BB = new BasicBlock("", CurFun.CurrentFunction);
389 if (ID.Type == ValID::NameVal) {
390 BB->setName(ID.Name);
391 } else {
392 CurFun.NumberedBlocks[ID.Num] = BB;
393 }
394
395 // If this is not a definition, keep track of it so we can use it as a forward
396 // reference.
397 if (!isDefinition) {
398 // Remember where this forward reference came from.
399 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
400 } else {
401 // The forward declaration could have been inserted anywhere in the
402 // function: insert it into the correct place now.
403 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
404 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
405 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000406 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000407 return BB;
408}
409
Chris Lattner00950542001-06-06 20:29:01 +0000410
411//===----------------------------------------------------------------------===//
412// Code to handle forward references in instructions
413//===----------------------------------------------------------------------===//
414//
415// This code handles the late binding needed with statements that reference
416// values not defined yet... for example, a forward branch, or the PHI node for
417// a loop body.
418//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000419// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000420// and back patchs after we are done.
421//
422
Misha Brukman5a2a3822005-05-10 22:02:28 +0000423// ResolveDefinitions - If we could not resolve some defs at parsing
424// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000425// defs now...
426//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000427static void
428ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
429 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000430 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000431 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000432 E = LateResolvers.end(); LRI != E; ++LRI) {
433 ValueList &List = LRI->second;
434 while (!List.empty()) {
435 Value *V = List.back();
436 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000437
438 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
439 CurModule.PlaceHolderInfo.find(V);
440 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
441
442 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000443
Chris Lattner735f2702004-07-08 22:30:50 +0000444 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000445 if (TheRealValue) {
446 V->replaceAllUsesWith(TheRealValue);
447 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000448 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000449 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000450 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000451 // resolver table
452 InsertValue(V, *FutureLateResolvers);
453 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000454 if (DID.Type == ValID::NameVal)
455 ThrowException("Reference to an invalid definition: '" +DID.getName()+
456 "' of type '" + V->getType()->getDescription() + "'",
457 PHI->second.second);
458 else
459 ThrowException("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000460 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000461 V->getType()->getDescription() + "'",
462 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000463 }
Chris Lattner00950542001-06-06 20:29:01 +0000464 }
465 }
466
467 LateResolvers.clear();
468}
469
Chris Lattner4a42e902001-10-22 05:56:09 +0000470// ResolveTypeTo - A brand new type was just declared. This means that (if
471// name is not null) things referencing Name can be resolved. Otherwise, things
472// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000473//
Chris Lattner4a42e902001-10-22 05:56:09 +0000474static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000475 ValID D;
476 if (Name) D = ValID::create(Name);
477 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000478
Chris Lattner355ad1f2005-03-21 06:27:42 +0000479 std::map<ValID, PATypeHolder>::iterator I =
480 CurModule.LateResolveTypes.find(D);
481 if (I != CurModule.LateResolveTypes.end()) {
482 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
483 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000484 }
485}
486
Chris Lattner1781aca2001-09-18 04:00:54 +0000487// setValueName - Set the specified value to the name given. The name may be
488// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000489// assumed to be a malloc'd string buffer, and is free'd by this function.
490//
491static void setValueName(Value *V, char *NameStr) {
492 if (NameStr) {
493 std::string Name(NameStr); // Copy string
494 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000495
Misha Brukman5a2a3822005-05-10 22:02:28 +0000496 if (V->getType() == Type::VoidTy)
Chris Lattnerc9aea522004-07-13 08:12:39 +0000497 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000498
Chris Lattner8ab406d2004-07-13 07:59:27 +0000499 assert(inFunctionScope() && "Must be in function scope!");
500 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
501 if (ST.lookup(V->getType(), Name))
502 ThrowException("Redefinition of value named '" + Name + "' in the '" +
503 V->getType()->getDescription() + "' type plane!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000504
Chris Lattnerc9aea522004-07-13 08:12:39 +0000505 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000506 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000507 }
508}
509
Chris Lattnerd88998d2004-07-14 08:23:52 +0000510/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
511/// this is a declaration, otherwise it is a definition.
512static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
513 bool isConstantGlobal, const Type *Ty,
514 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000515 if (isa<FunctionType>(Ty))
516 ThrowException("Cannot declare global vars of function type!");
517
Misha Brukman5a2a3822005-05-10 22:02:28 +0000518 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000519
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000520 std::string Name;
521 if (NameStr) {
522 Name = NameStr; // Copy string
523 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000524 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000525
Chris Lattner8a327842004-07-14 21:44:00 +0000526 // See if this global value was forward referenced. If so, recycle the
527 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000528 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000529 if (!Name.empty()) {
530 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000531 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000532 ID = ValID::create((int)CurModule.Values[PTy].size());
533 }
534
535 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000536 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000537 // previously inserted.
538 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
539 CurModule.CurrentModule->getGlobalList().remove(GV);
540 CurModule.CurrentModule->getGlobalList().push_back(GV);
541 GV->setInitializer(Initializer);
542 GV->setLinkage(Linkage);
543 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000544 InsertValue(GV, CurModule.Values);
Chris Lattnera09000d2004-07-14 23:03:46 +0000545 return;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000546 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000547
548 // If this global has a name, check to see if there is already a definition
549 // of this global in the module. If so, merge as appropriate. Note that
550 // this is really just a hack around problems in the CFE. :(
551 if (!Name.empty()) {
552 // We are a simple redefinition of a value, check to see if it is defined
553 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000554 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000555 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
556 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000557 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000558 // 2. If both initializers have the same value.
559 //
560 if (!EGV->hasInitializer() || !Initializer ||
561 EGV->getInitializer() == Initializer) {
562
563 // Make sure the existing global version gets the initializer! Make
564 // sure that it also gets marked const if the new version is.
565 if (Initializer && !EGV->hasInitializer())
566 EGV->setInitializer(Initializer);
567 if (isConstantGlobal)
568 EGV->setConstant(true);
569 EGV->setLinkage(Linkage);
570 return;
571 }
572
Misha Brukman5a2a3822005-05-10 22:02:28 +0000573 ThrowException("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000574 "' in the '" + Ty->getDescription() + "' type plane!");
575 }
576 }
577
578 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000579 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000580 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000581 CurModule.CurrentModule);
582 InsertValue(GV, CurModule.Values);
Chris Lattnerd88998d2004-07-14 08:23:52 +0000583}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000584
Reid Spencer75719bf2004-05-26 21:48:31 +0000585// setTypeName - Set the specified type to the name given. The name may be
586// null potentially, in which case this is a noop. The string passed in is
587// assumed to be a malloc'd string buffer, and is freed by this function.
588//
589// This function returns true if the type has already been defined, but is
590// allowed to be redefined in the specified context. If the name is a new name
591// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000592static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000593 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000594 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000595
Reid Spencer75719bf2004-05-26 21:48:31 +0000596 std::string Name(NameStr); // Copy string
597 free(NameStr); // Free old string
598
599 // We don't allow assigning names to void type
Misha Brukman5a2a3822005-05-10 22:02:28 +0000600 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000601 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000602
Chris Lattnera09000d2004-07-14 23:03:46 +0000603 // Set the type name, checking for conflicts as we do so.
604 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000605
Chris Lattnera09000d2004-07-14 23:03:46 +0000606 if (AlreadyExists) { // Inserting a name that is already defined???
607 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
608 assert(Existing && "Conflict but no matching type?");
609
Reid Spencer75719bf2004-05-26 21:48:31 +0000610 // There is only one case where this is allowed: when we are refining an
611 // opaque type. In this case, Existing will be an opaque type.
612 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
613 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000614 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000615 return true;
616 }
617
618 // Otherwise, this is an attempt to redefine a type. That's okay if
619 // the redefinition is identical to the original. This will be so if
620 // Existing and T point to the same Type object. In this one case we
621 // allow the equivalent redefinition.
622 if (Existing == T) return true; // Yes, it's equal.
623
624 // Any other kind of (non-equivalent) redefinition is an error.
625 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000626 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000627 }
628
Reid Spencer75719bf2004-05-26 21:48:31 +0000629 return false;
630}
Chris Lattner8896eda2001-07-09 19:38:36 +0000631
Chris Lattner30c89792001-09-07 16:35:17 +0000632//===----------------------------------------------------------------------===//
633// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000634//
Chris Lattner8896eda2001-07-09 19:38:36 +0000635
Chris Lattner3b073862004-02-09 03:19:29 +0000636// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000637//
638static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000639 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000640 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000641}
642
643namespace {
644 struct UpRefRecord {
645 // NestingLevel - The number of nesting levels that need to be popped before
646 // this type is resolved.
647 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000648
Chris Lattner3b073862004-02-09 03:19:29 +0000649 // LastContainedTy - This is the type at the current binding level for the
650 // type. Every time we reduce the nesting level, this gets updated.
651 const Type *LastContainedTy;
652
653 // UpRefTy - This is the actual opaque type that the upreference is
654 // represented with.
655 OpaqueType *UpRefTy;
656
657 UpRefRecord(unsigned NL, OpaqueType *URTy)
658 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
659 };
Chris Lattner30c89792001-09-07 16:35:17 +0000660}
Chris Lattner698b56e2001-07-20 19:15:08 +0000661
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000662// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000663static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000664
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000665/// HandleUpRefs - Every time we finish a new layer of types, this function is
666/// called. It loops through the UpRefs vector, which is a list of the
667/// currently active types. For each type, if the up reference is contained in
668/// the newly completed type, we decrement the level count. When the level
669/// count reaches zero, the upreferenced type is the type that is passed in:
670/// thus we can complete the cycle.
671///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000672static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000673 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000674 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000675 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000676 "' newly formed. Resolving upreferences.\n" <<
677 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000678
679 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
680 // to zero), we resolve them all together before we resolve them to Ty. At
681 // the end of the loop, if there is anything to resolve to Ty, it will be in
682 // this variable.
683 OpaqueType *TypeToResolve = 0;
684
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000685 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000686 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
687 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000688 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000689 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
690 // Decrement level of upreference
691 unsigned Level = --UpRefs[i].NestingLevel;
692 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000693 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000694 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000695 if (!TypeToResolve) {
696 TypeToResolve = UpRefs[i].UpRefTy;
697 } else {
698 UR_OUT(" * Resolving upreference for "
699 << UpRefs[i].second->getDescription() << "\n";
700 std::string OldName = UpRefs[i].UpRefTy->getDescription());
701 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
702 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
703 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
704 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000705 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000706 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000707 }
708 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000709 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000710
Chris Lattner026a8ce2004-02-09 18:53:54 +0000711 if (TypeToResolve) {
712 UR_OUT(" * Resolving upreference for "
713 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000714 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000715 TypeToResolve->refineAbstractTypeTo(Ty);
716 }
717
Chris Lattner8896eda2001-07-09 19:38:36 +0000718 return Ty;
719}
720
Chris Lattner30c89792001-09-07 16:35:17 +0000721
Chris Lattner6184feb2005-05-20 03:25:47 +0000722// common code from the two 'RunVMAsmParser' functions
723 static Module * RunParser(Module * M) {
724
725 llvmAsmlineno = 1; // Reset the current line number...
726
727 CurModule.CurrentModule = M;
728 yyparse(); // Parse the file, potentially throwing exception
729
730 Module *Result = ParserResult;
731 ParserResult = 0;
732
733 return Result;
734
735 }
736
Chris Lattner00950542001-06-06 20:29:01 +0000737//===----------------------------------------------------------------------===//
738// RunVMAsmParser - Define an interface to this parser
739//===----------------------------------------------------------------------===//
740//
Chris Lattner86427632004-07-14 06:39:48 +0000741Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000742 set_scan_file(F);
743
Chris Lattnera2850432001-07-22 18:36:00 +0000744 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000745 return RunParser(new Module(CurFilename));
746}
Chris Lattner00950542001-06-06 20:29:01 +0000747
Chris Lattner6184feb2005-05-20 03:25:47 +0000748Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
749 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000750
Chris Lattner6184feb2005-05-20 03:25:47 +0000751 CurFilename = "from_memory";
752 if (M == NULL) {
753 return RunParser(new Module (CurFilename));
754 } else {
755 return RunParser(M);
756 }
Chris Lattner00950542001-06-06 20:29:01 +0000757}
758
759%}
760
761%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000762 llvm::Module *ModuleVal;
763 llvm::Function *FunctionVal;
764 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
765 llvm::BasicBlock *BasicBlockVal;
766 llvm::TerminatorInst *TermInstVal;
767 llvm::Instruction *InstVal;
768 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000769
Brian Gaeked0fde302003-11-11 22:41:34 +0000770 const llvm::Type *PrimType;
771 llvm::PATypeHolder *TypeVal;
772 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000773
Brian Gaeked0fde302003-11-11 22:41:34 +0000774 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
775 std::vector<llvm::Value*> *ValueList;
776 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000777 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000778 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000779 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000780 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
781 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000782
Brian Gaeked0fde302003-11-11 22:41:34 +0000783 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000784 int64_t SInt64Val;
785 uint64_t UInt64Val;
786 int SIntVal;
787 unsigned UIntVal;
788 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000789 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000790
Chris Lattner30c89792001-09-07 16:35:17 +0000791 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000792 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000793
Brian Gaeked0fde302003-11-11 22:41:34 +0000794 llvm::Instruction::BinaryOps BinaryOpVal;
795 llvm::Instruction::TermOps TermOpVal;
796 llvm::Instruction::MemoryOps MemOpVal;
797 llvm::Instruction::OtherOps OtherOpVal;
798 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000799}
800
Chris Lattner79df7c02002-03-26 18:01:55 +0000801%type <ModuleVal> Module FunctionList
802%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000803%type <BasicBlockVal> BasicBlock InstructionList
804%type <TermInstVal> BBTerminatorInst
805%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000806%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000807%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000808%type <ArgList> ArgList ArgListH
809%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000810%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000811%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000812%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000813%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000814%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000815%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000816%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000817%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000818%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000819%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000820
Chris Lattner2079fde2001-10-13 06:41:08 +0000821// ValueRef - Unresolved reference to a definition or BB
822%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000823%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000824// Tokens and types for handling constant integer values
825//
826// ESINT64VAL - A negative number within long long range
827%token <SInt64Val> ESINT64VAL
828
829// EUINT64VAL - A positive number within uns. long long range
830%token <UInt64Val> EUINT64VAL
831%type <SInt64Val> EINT64VAL
832
833%token <SIntVal> SINTVAL // Signed 32 bit ints...
834%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
835%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000836%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000837
838// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000839%type <TypeVal> Types TypesV UpRTypes UpRTypesV
840%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000841%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
842%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000843
Chris Lattner6c23f572003-08-22 05:42:10 +0000844%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
845%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000846
847
Chris Lattner91ef4602004-03-31 03:49:47 +0000848%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000849%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000850%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Reid Spencer0be13e72004-07-25 17:58:28 +0000851%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Chris Lattnerddb6db42005-05-06 05:51:46 +0000852%token DEPLIBS CALL TAIL
Chris Lattnera8e8f162005-05-06 20:27:19 +0000853%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK
854%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +0000855
Misha Brukman5a2a3822005-05-10 22:02:28 +0000856// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000857%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000858
Misha Brukman5a2a3822005-05-10 22:02:28 +0000859// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000860%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000861%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000862%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000863
864// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000865%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000866
Chris Lattner027dcc52001-07-08 21:10:27 +0000867// Other Operators
868%type <OtherOpVal> ShiftOps
Chris Lattnerddb6db42005-05-06 05:51:46 +0000869%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG VANEXT
870
Chris Lattner027dcc52001-07-08 21:10:27 +0000871
Chris Lattner00950542001-06-06 20:29:01 +0000872%start Module
873%%
874
875// Handle constant integer size restriction and conversion...
876//
Chris Lattner51727be2002-06-04 21:58:56 +0000877INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000878INTVAL : UINTVAL {
879 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
880 ThrowException("Value too large for type!");
881 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000882};
Chris Lattner00950542001-06-06 20:29:01 +0000883
884
Chris Lattner51727be2002-06-04 21:58:56 +0000885EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000886EINT64VAL : EUINT64VAL {
887 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
888 ThrowException("Value too large for type!");
889 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000890};
Chris Lattner00950542001-06-06 20:29:01 +0000891
Misha Brukman5a2a3822005-05-10 22:02:28 +0000892// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +0000893// RET, BR, & SWITCH because they end basic blocks and are treated specially.
894//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000895ArithmeticOps: ADD | SUB | MUL | DIV | REM;
896LogicalOps : AND | OR | XOR;
897SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +0000898
Chris Lattner51727be2002-06-04 21:58:56 +0000899ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000900
Chris Lattnere98dda62001-07-14 06:10:16 +0000901// These are some types that allow classification if we only want a particular
902// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000903SIntType : LONG | INT | SHORT | SBYTE;
904UIntType : ULONG | UINT | USHORT | UBYTE;
905IntType : SIntType | UIntType;
906FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000907
Chris Lattnere98dda62001-07-14 06:10:16 +0000908// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000909OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000910 $$ = $1;
911 }
Misha Brukman5a2a3822005-05-10 22:02:28 +0000912 | /*empty*/ {
913 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000914 };
Chris Lattner00950542001-06-06 20:29:01 +0000915
Chris Lattner4ad02e72003-04-16 20:28:45 +0000916OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
917 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000918 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000919 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
920 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000921
Chris Lattnera8e8f162005-05-06 20:27:19 +0000922OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
923 CCC_TOK { $$ = CallingConv::C; } |
924 FASTCC_TOK { $$ = CallingConv::Fast; } |
925 COLDCC_TOK { $$ = CallingConv::Cold; } |
926 CC_TOK EUINT64VAL {
927 if ((unsigned)$2 != $2)
928 ThrowException("Calling conv too large!");
929 $$ = $2;
930 };
931
Chris Lattner30c89792001-09-07 16:35:17 +0000932//===----------------------------------------------------------------------===//
933// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000934// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000935// access to it, a user must explicitly use TypesV.
936//
937
938// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000939TypesV : Types | VOID { $$ = new PATypeHolder($1); };
940UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000941
942Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000943 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000944 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
945 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000946 };
Chris Lattner30c89792001-09-07 16:35:17 +0000947
948
949// Derived types are added later...
950//
Chris Lattner51727be2002-06-04 21:58:56 +0000951PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
952PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000953UpRTypes : OPAQUE {
954 $$ = new PATypeHolder(OpaqueType::get());
955 }
956 | PrimType {
957 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000958 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000959UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000960 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000961};
Chris Lattner30c89792001-09-07 16:35:17 +0000962
Chris Lattner30c89792001-09-07 16:35:17 +0000963// Include derived types in the Types production.
964//
965UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000966 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000967 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +0000968 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000969 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000970 UR_OUT("New Upreference!\n");
971 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000972 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000973 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +0000974 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
975 E = $3->end(); I != E; ++I)
976 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +0000977 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
978 if (isVarArg) Params.pop_back();
979
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000980 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000981 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000982 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +0000983 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000984 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000985 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000986 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000987 }
Brian Gaeke715c90b2004-08-20 06:00:58 +0000988 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
989 const llvm::Type* ElemTy = $4->get();
990 if ((unsigned)$2 != $2) {
991 ThrowException("Unsigned result not equal to signed result");
992 }
993 if(!ElemTy->isPrimitiveType()) {
994 ThrowException("Elemental type of a PackedType must be primitive");
995 }
996 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
997 delete $4;
998 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000999 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001000 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001001 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1002 E = $2->end(); I != E; ++I)
1003 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001004
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001005 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001006 delete $2;
1007 }
1008 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001009 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001010 }
1011 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001012 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001013 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001014 };
Chris Lattner30c89792001-09-07 16:35:17 +00001015
Chris Lattner7e708292002-06-25 16:13:24 +00001016// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001017// declaration type lists
1018//
1019TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001020 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001021 $$->push_back(*$1); delete $1;
1022 }
1023 | TypeListI ',' UpRTypes {
1024 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001025 };
Chris Lattner30c89792001-09-07 16:35:17 +00001026
Chris Lattner7e708292002-06-25 16:13:24 +00001027// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001028ArgTypeListI : TypeListI
1029 | TypeListI ',' DOTDOTDOT {
1030 ($$=$1)->push_back(Type::VoidTy);
1031 }
1032 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001033 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001034 }
1035 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001036 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001037 };
Chris Lattner30c89792001-09-07 16:35:17 +00001038
Chris Lattnere98dda62001-07-14 06:10:16 +00001039// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001040// production is used ONLY to represent constants that show up AFTER a 'const',
1041// 'constant' or 'global' token at global scope. Constants that can be inlined
1042// into other expressions (such as integers and constexprs) are handled by the
1043// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001044//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001045ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001046 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001047 if (ATy == 0)
1048 ThrowException("Cannot make array constant with type: '" +
1049 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001050 const Type *ETy = ATy->getElementType();
1051 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001052
Chris Lattner30c89792001-09-07 16:35:17 +00001053 // Verify that we have the correct size...
1054 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001055 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001056 utostr($3->size()) + " arguments, but has size of " +
1057 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001058
Chris Lattner30c89792001-09-07 16:35:17 +00001059 // Verify all elements are correct type!
1060 for (unsigned i = 0; i < $3->size(); i++) {
1061 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001062 ThrowException("Element #" + utostr(i) + " is not of type '" +
1063 ETy->getDescription() +"' as required!\nIt is of type '"+
1064 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001065 }
1066
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001067 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001068 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001069 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001070 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001071 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001072 if (ATy == 0)
1073 ThrowException("Cannot make array constant with type: '" +
1074 (*$1)->getDescription() + "'!");
1075
1076 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001077 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001078 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001079 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001080 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001081 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001082 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001083 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001084 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001085 if (ATy == 0)
1086 ThrowException("Cannot make array constant with type: '" +
1087 (*$1)->getDescription() + "'!");
1088
Chris Lattner30c89792001-09-07 16:35:17 +00001089 int NumElements = ATy->getNumElements();
1090 const Type *ETy = ATy->getElementType();
1091 char *EndStr = UnEscapeLexed($3, true);
1092 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001093 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001094 itostr((int)(EndStr-$3)) +
1095 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001096 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001097 if (ETy == Type::SByteTy) {
1098 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001099 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001100 } else if (ETy == Type::UByteTy) {
1101 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001102 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001103 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001104 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001105 ThrowException("Cannot build string arrays of non byte sized elements!");
1106 }
Chris Lattner30c89792001-09-07 16:35:17 +00001107 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001108 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001109 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001110 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001111 | Types '<' ConstVector '>' { // Nonempty unsized arr
1112 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1113 if (PTy == 0)
1114 ThrowException("Cannot make packed constant with type: '" +
1115 (*$1)->getDescription() + "'!");
1116 const Type *ETy = PTy->getElementType();
1117 int NumElements = PTy->getNumElements();
1118
1119 // Verify that we have the correct size...
1120 if (NumElements != -1 && NumElements != (int)$3->size())
1121 ThrowException("Type mismatch: constant sized packed initialized with " +
1122 utostr($3->size()) + " arguments, but has size of " +
1123 itostr(NumElements) + "!");
1124
1125 // Verify all elements are correct type!
1126 for (unsigned i = 0; i < $3->size(); i++) {
1127 if (ETy != (*$3)[i]->getType())
1128 ThrowException("Element #" + utostr(i) + " is not of type '" +
1129 ETy->getDescription() +"' as required!\nIt is of type '"+
1130 (*$3)[i]->getType()->getDescription() + "'.");
1131 }
1132
1133 $$ = ConstantPacked::get(PTy, *$3);
1134 delete $1; delete $3;
1135 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001136 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001137 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001138 if (STy == 0)
1139 ThrowException("Cannot make struct constant with type: '" +
1140 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001141
Chris Lattnerc6212f12003-05-21 16:06:56 +00001142 if ($3->size() != STy->getNumContainedTypes())
1143 ThrowException("Illegal number of initializers for structure type!");
1144
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001145 // Check to ensure that constants are compatible with the type initializer!
1146 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001147 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001148 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001149 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001150 "' for element #" + utostr(i) +
1151 " of structure initializer!");
1152
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001153 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001154 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001155 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001156 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001157 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001158 if (STy == 0)
1159 ThrowException("Cannot make struct constant with type: '" +
1160 (*$1)->getDescription() + "'!");
1161
1162 if (STy->getNumContainedTypes() != 0)
1163 ThrowException("Illegal number of initializers for structure type!");
1164
1165 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1166 delete $1;
1167 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001168 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001169 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001170 if (PTy == 0)
1171 ThrowException("Cannot make null pointer constant with type: '" +
1172 (*$1)->getDescription() + "'!");
1173
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001174 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001175 delete $1;
1176 }
Chris Lattner16710e92004-10-16 18:17:13 +00001177 | Types UNDEF {
1178 $$ = UndefValue::get($1->get());
1179 delete $1;
1180 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001181 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001182 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001183 if (Ty == 0)
1184 ThrowException("Global const reference must be a pointer type!");
1185
Chris Lattner3101c252002-08-15 17:58:33 +00001186 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001187 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001188 // the context of a function, getValNonImprovising will search the functions
1189 // symbol table instead of the module symbol table for the global symbol,
1190 // which throws things all off. To get around this, we just tell
1191 // getValNonImprovising that we are at global scope here.
1192 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001193 Function *SavedCurFn = CurFun.CurrentFunction;
1194 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001195
Chris Lattner2079fde2001-10-13 06:41:08 +00001196 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001197
Chris Lattner394f2eb2003-10-16 20:12:13 +00001198 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001199
Chris Lattner2079fde2001-10-13 06:41:08 +00001200 // If this is an initializer for a constant pointer, which is referencing a
1201 // (currently) undefined variable, create a stub now that shall be replaced
1202 // in the future with the right type of variable.
1203 //
1204 if (V == 0) {
1205 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1206 const PointerType *PT = cast<PointerType>(Ty);
1207
1208 // First check to see if the forward references value is already created!
1209 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001210 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001211
1212 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001213 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001214 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001215 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001216 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001217 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001218
Reid Spencer3271ed52004-07-18 00:08:11 +00001219 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001220 GlobalValue *GV;
1221 if (const FunctionType *FTy =
1222 dyn_cast<FunctionType>(PT->getElementType())) {
1223 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1224 CurModule.CurrentModule);
1225 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001226 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001227 GlobalValue::ExternalLinkage, 0,
1228 Name, CurModule.CurrentModule);
1229 }
Chris Lattner8a327842004-07-14 21:44:00 +00001230
Reid Spencer3271ed52004-07-18 00:08:11 +00001231 // Keep track of the fact that we have a forward ref to recycle it
1232 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1233 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001234 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001235 }
1236
Reid Spencer3271ed52004-07-18 00:08:11 +00001237 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001238 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001239 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001240 | Types ConstExpr {
1241 if ($1->get() != $2->getType())
1242 ThrowException("Mismatched types for constant expression!");
1243 $$ = $2;
1244 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001245 }
1246 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001247 const Type *Ty = $1->get();
1248 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1249 ThrowException("Cannot create a null initialized value of this type!");
1250 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001251 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001252 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001253
Chris Lattnere43f40b2002-10-09 00:25:32 +00001254ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001255 if (!ConstantSInt::isValueValidForType($1, $2))
1256 ThrowException("Constant value doesn't fit in type!");
1257 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001258 }
1259 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001260 if (!ConstantUInt::isValueValidForType($1, $2))
1261 ThrowException("Constant value doesn't fit in type!");
1262 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001263 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001264 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001265 $$ = ConstantBool::True;
1266 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001267 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001268 $$ = ConstantBool::False;
1269 }
1270 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001271 if (!ConstantFP::isValueValidForType($1, $2))
1272 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001273 $$ = ConstantFP::get($1, $2);
1274 };
1275
Chris Lattner00950542001-06-06 20:29:01 +00001276
Chris Lattnerd78700d2002-08-16 21:14:40 +00001277ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001278 if (!$3->getType()->isFirstClassType())
1279 ThrowException("cast constant expression from a non-primitive type: '" +
1280 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001281 if (!$5->get()->isFirstClassType())
1282 ThrowException("cast constant expression to a non-primitive type: '" +
1283 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001284 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001285 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001286 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001287 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1288 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001289 ThrowException("GetElementPtr requires a pointer operand!");
1290
Chris Lattner830b6f92004-04-05 01:30:04 +00001291 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1292 // indices to uint struct indices for compatibility.
1293 generic_gep_type_iterator<std::vector<Value*>::iterator>
1294 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1295 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1296 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1297 if (isa<StructType>(*GTI)) // Only change struct indices
1298 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1299 if (CUI->getType() == Type::UByteTy)
1300 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1301
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001302 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001303 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001304 if (!IdxTy)
1305 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001306
Chris Lattner9232b992003-04-22 18:42:41 +00001307 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001308 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1309 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001310 IdxVec.push_back(C);
1311 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001312 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001313
Chris Lattnerd78700d2002-08-16 21:14:40 +00001314 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001315
Chris Lattnerd78700d2002-08-16 21:14:40 +00001316 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001317 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001318 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1319 if ($3->getType() != Type::BoolTy)
1320 ThrowException("Select condition must be of boolean type!");
1321 if ($5->getType() != $7->getType())
1322 ThrowException("Select operand types must match!");
1323 $$ = ConstantExpr::getSelect($3, $5, $7);
1324 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001325 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001326 if ($3->getType() != $5->getType())
1327 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001328 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1329 // To retain backward compatibility with these early compilers, we emit a
1330 // cast to the appropriate integer type automatically if we are in the
1331 // broken case. See PR424 for more information.
1332 if (!isa<PointerType>($3->getType())) {
1333 $$ = ConstantExpr::get($1, $3, $5);
1334 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001335 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001336 switch (CurModule.CurrentModule->getPointerSize()) {
1337 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1338 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1339 default: ThrowException("invalid pointer binary constant expr!");
1340 }
1341 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1342 ConstantExpr::getCast($5, IntPtrTy));
1343 $$ = ConstantExpr::getCast($$, $3->getType());
1344 }
1345 }
1346 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1347 if ($3->getType() != $5->getType())
1348 ThrowException("Logical operator types must match!");
1349 if (!$3->getType()->isIntegral())
1350 ThrowException("Logical operands must have integral types!");
1351 $$ = ConstantExpr::get($1, $3, $5);
1352 }
1353 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1354 if ($3->getType() != $5->getType())
1355 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001356 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001357 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001358 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001359 if ($5->getType() != Type::UByteTy)
1360 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001361 if (!$3->getType()->isInteger())
1362 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001363 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001364 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001365
1366
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001367// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001368ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001369 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001370 }
1371 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001372 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001373 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001374 };
Chris Lattner00950542001-06-06 20:29:01 +00001375
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001376
Chris Lattner1781aca2001-09-18 04:00:54 +00001377// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001378GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001379
Chris Lattner00950542001-06-06 20:29:01 +00001380
Chris Lattner0e73ce62002-05-02 19:11:13 +00001381//===----------------------------------------------------------------------===//
1382// Rules to match Modules
1383//===----------------------------------------------------------------------===//
1384
1385// Module rule: Capture the result of parsing the whole file into a result
1386// variable...
1387//
1388Module : FunctionList {
1389 $$ = ParserResult = $1;
1390 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001391};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001392
Chris Lattner7e708292002-06-25 16:13:24 +00001393// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001394//
1395FunctionList : FunctionList Function {
1396 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001397 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001398 }
1399 | FunctionList FunctionProto {
1400 $$ = $1;
1401 }
1402 | FunctionList IMPLEMENTATION {
1403 $$ = $1;
1404 }
1405 | ConstPool {
1406 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001407 // Emit an error if there are any unresolved types left.
1408 if (!CurModule.LateResolveTypes.empty()) {
1409 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1410 if (DID.Type == ValID::NameVal)
1411 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1412 else
1413 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1414 }
Chris Lattner51727be2002-06-04 21:58:56 +00001415 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001416
Chris Lattnere98dda62001-07-14 06:10:16 +00001417// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001418ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001419 // Eagerly resolve types. This is not an optimization, this is a
1420 // requirement that is due to the fact that we could have this:
1421 //
1422 // %list = type { %list * }
1423 // %list = type { %list * } ; repeated type decl
1424 //
1425 // If types are not resolved eagerly, then the two types will not be
1426 // determined to be the same type!
1427 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001428 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001429
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001430 if (!setTypeName(*$4, $2) && !$2) {
1431 // If this is a named type that is not a redefinition, add it to the slot
1432 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001433 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001434 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001435
1436 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001437 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001438 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001439 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001440 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001441 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1442 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Chris Lattner1781aca2001-09-18 04:00:54 +00001443 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001444 | ConstPool OptAssign EXTERNAL GlobalType Types {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001445 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001446 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001447 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001448 | ConstPool TARGET TargetDefinition {
1449 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001450 | ConstPool DEPLIBS '=' LibrariesDefinition {
1451 }
Chris Lattner00950542001-06-06 20:29:01 +00001452 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001453 };
Chris Lattner00950542001-06-06 20:29:01 +00001454
1455
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001456
1457BigOrLittle : BIG { $$ = Module::BigEndian; };
1458BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1459
1460TargetDefinition : ENDIAN '=' BigOrLittle {
1461 CurModule.CurrentModule->setEndianness($3);
1462 }
1463 | POINTERSIZE '=' EUINT64VAL {
1464 if ($3 == 32)
1465 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1466 else if ($3 == 64)
1467 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1468 else
1469 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001470 }
1471 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001472 CurModule.CurrentModule->setTargetTriple($3);
1473 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001474 };
1475
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001476LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001477
1478LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001479 CurModule.CurrentModule->addLibrary($3);
1480 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001481 }
1482 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001483 CurModule.CurrentModule->addLibrary($1);
1484 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001485 }
1486 | /* empty: end of list */ {
1487 }
1488 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001489
Chris Lattner00950542001-06-06 20:29:01 +00001490//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001491// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001492//===----------------------------------------------------------------------===//
1493
Chris Lattner6c23f572003-08-22 05:42:10 +00001494Name : VAR_ID | STRINGCONSTANT;
1495OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001496
Chris Lattner6c23f572003-08-22 05:42:10 +00001497ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001498 if (*$1 == Type::VoidTy)
1499 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001500 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001501};
Chris Lattner00950542001-06-06 20:29:01 +00001502
Chris Lattner69da5cf2002-10-13 20:57:00 +00001503ArgListH : ArgListH ',' ArgVal {
1504 $$ = $1;
1505 $1->push_back(*$3);
1506 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001507 }
1508 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001509 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001510 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001511 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001512 };
Chris Lattner00950542001-06-06 20:29:01 +00001513
1514ArgList : ArgListH {
1515 $$ = $1;
1516 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001517 | ArgListH ',' DOTDOTDOT {
1518 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001519 $$->push_back(std::pair<PATypeHolder*,
1520 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001521 }
1522 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001523 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1524 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001525 }
Chris Lattner00950542001-06-06 20:29:01 +00001526 | /* empty */ {
1527 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001528 };
Chris Lattner00950542001-06-06 20:29:01 +00001529
Chris Lattnera8e8f162005-05-06 20:27:19 +00001530FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')' {
1531 UnEscapeLexed($3);
1532 std::string FunctionName($3);
1533 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001534
Chris Lattnera8e8f162005-05-06 20:27:19 +00001535 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001536 ThrowException("LLVM functions cannot return aggregate types!");
1537
Chris Lattner9232b992003-04-22 18:42:41 +00001538 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001539 if ($5) { // If there are arguments...
1540 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1541 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001542 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001543 }
Chris Lattner00950542001-06-06 20:29:01 +00001544
Chris Lattner2079fde2001-10-13 06:41:08 +00001545 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1546 if (isVarArg) ParamTypeList.pop_back();
1547
Chris Lattnera8e8f162005-05-06 20:27:19 +00001548 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001549 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001550 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001551
Chris Lattnera09000d2004-07-14 23:03:46 +00001552 ValID ID;
1553 if (!FunctionName.empty()) {
1554 ID = ValID::create((char*)FunctionName.c_str());
1555 } else {
1556 ID = ValID::create((int)CurModule.Values[PFT].size());
1557 }
1558
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001559 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001560 // See if this function was forward referenced. If so, recycle the object.
1561 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1562 // Move the function to the end of the list, from whereever it was
1563 // previously inserted.
1564 Fn = cast<Function>(FWRef);
1565 CurModule.CurrentModule->getFunctionList().remove(Fn);
1566 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1567 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1568 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1569 // If this is the case, either we need to be a forward decl, or it needs
1570 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001571 if (!CurFun.isDeclare && !Fn->isExternal())
1572 ThrowException("Redefinition of function '" + FunctionName + "'!");
1573
Chris Lattnera09000d2004-07-14 23:03:46 +00001574 // Make sure to strip off any argument names so we can't get conflicts.
1575 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001576 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001577 AI != AE; ++AI)
1578 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001579
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001580 } else { // Not already defined?
1581 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1582 CurModule.CurrentModule);
1583 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001584 }
Chris Lattner00950542001-06-06 20:29:01 +00001585
Chris Lattner394f2eb2003-10-16 20:12:13 +00001586 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001587 Fn->setCallingConv($1);
Chris Lattner00950542001-06-06 20:29:01 +00001588
Chris Lattner7e708292002-06-25 16:13:24 +00001589 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001590 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001591 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001592 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001593 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001594 delete $5->back().first;
1595 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001596 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001597 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001598 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1599 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001600 delete I->first; // Delete the typeholder...
1601
Chris Lattner8ab406d2004-07-13 07:59:27 +00001602 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001603 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001604 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001605
Chris Lattnera8e8f162005-05-06 20:27:19 +00001606 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001607 }
Chris Lattner51727be2002-06-04 21:58:56 +00001608};
Chris Lattner00950542001-06-06 20:29:01 +00001609
Chris Lattner9b02cc32002-05-03 18:23:48 +00001610BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1611
Chris Lattner4ad02e72003-04-16 20:28:45 +00001612FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001613 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001614
Chris Lattner4ad02e72003-04-16 20:28:45 +00001615 // Make sure that we keep track of the linkage type even if there was a
1616 // previous "declare".
1617 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001618};
Chris Lattner00950542001-06-06 20:29:01 +00001619
Chris Lattner9b02cc32002-05-03 18:23:48 +00001620END : ENDTOK | '}'; // Allow end of '}' to end a function
1621
Chris Lattner79df7c02002-03-26 18:01:55 +00001622Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001623 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001624};
Chris Lattner00950542001-06-06 20:29:01 +00001625
Chris Lattner394f2eb2003-10-16 20:12:13 +00001626FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1627 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001628 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001629};
Chris Lattner00950542001-06-06 20:29:01 +00001630
1631//===----------------------------------------------------------------------===//
1632// Rules to match Basic Blocks
1633//===----------------------------------------------------------------------===//
1634
1635ConstValueRef : ESINT64VAL { // A reference to a direct constant
1636 $$ = ValID::create($1);
1637 }
1638 | EUINT64VAL {
1639 $$ = ValID::create($1);
1640 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001641 | FPVAL { // Perhaps it's an FP constant?
1642 $$ = ValID::create($1);
1643 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001644 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001645 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001646 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001647 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001648 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001649 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001650 | NULL_TOK {
1651 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001652 }
Chris Lattner16710e92004-10-16 18:17:13 +00001653 | UNDEF {
1654 $$ = ValID::createUndef();
1655 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001656 | '<' ConstVector '>' { // Nonempty unsized packed vector
1657 const Type *ETy = (*$2)[0]->getType();
1658 int NumElements = $2->size();
1659
1660 PackedType* pt = PackedType::get(ETy, NumElements);
1661 PATypeHolder* PTy = new PATypeHolder(
1662 HandleUpRefs(
1663 PackedType::get(
1664 ETy,
1665 NumElements)
1666 )
1667 );
1668
1669 // Verify all elements are correct type!
1670 for (unsigned i = 0; i < $2->size(); i++) {
1671 if (ETy != (*$2)[i]->getType())
1672 ThrowException("Element #" + utostr(i) + " is not of type '" +
1673 ETy->getDescription() +"' as required!\nIt is of type '" +
1674 (*$2)[i]->getType()->getDescription() + "'.");
1675 }
1676
1677 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1678 delete PTy; delete $2;
1679 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001680 | ConstExpr {
1681 $$ = ValID::create($1);
1682 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001683
Chris Lattner2079fde2001-10-13 06:41:08 +00001684// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1685// another value.
1686//
1687SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001688 $$ = ValID::create($1);
1689 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001690 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001691 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001692 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001693
1694// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001695ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001696
Chris Lattner00950542001-06-06 20:29:01 +00001697
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001698// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1699// type immediately preceeds the value reference, and allows complex constant
1700// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001701ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001702 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001703 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001704
Chris Lattner00950542001-06-06 20:29:01 +00001705BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001706 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001707 }
Chris Lattner7e708292002-06-25 16:13:24 +00001708 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001709 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001710 };
Chris Lattner00950542001-06-06 20:29:01 +00001711
1712
1713// Basic blocks are terminated by branching instructions:
1714// br, br/cc, switch, ret
1715//
Chris Lattner2079fde2001-10-13 06:41:08 +00001716BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001717 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001718 InsertValue($3);
1719
1720 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001721 InsertValue($1);
1722 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001723 };
Chris Lattner00950542001-06-06 20:29:01 +00001724
1725InstructionList : InstructionList Inst {
1726 $1->getInstList().push_back($2);
1727 $$ = $1;
1728 }
1729 | /* empty */ {
Chris Lattner1f640252005-05-06 19:49:51 +00001730 $$ = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001731
1732 // Make sure to move the basic block to the correct location in the
1733 // function, instead of leaving it inserted wherever it was first
1734 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001735 Function::BasicBlockListType &BBL =
1736 CurFun.CurrentFunction->getBasicBlockList();
1737 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner22ae2322004-07-13 08:39:15 +00001738 }
1739 | LABELSTR {
Chris Lattner1f640252005-05-06 19:49:51 +00001740 $$ = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001741
1742 // Make sure to move the basic block to the correct location in the
1743 // function, instead of leaving it inserted wherever it was first
1744 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001745 Function::BasicBlockListType &BBL =
1746 CurFun.CurrentFunction->getBasicBlockList();
1747 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner51727be2002-06-04 21:58:56 +00001748 };
Chris Lattner00950542001-06-06 20:29:01 +00001749
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001750BBTerminatorInst : RET ResolvedVal { // Return with a result...
1751 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001752 }
1753 | RET VOID { // Return with no result...
1754 $$ = new ReturnInst();
1755 }
1756 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001757 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001758 } // Conditional Branch...
1759 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001760 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001761 }
1762 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001763 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001764 $$ = S;
1765
Chris Lattner9232b992003-04-22 18:42:41 +00001766 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001767 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001768 for (; I != E; ++I) {
1769 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
1770 S->addCase(CI, I->second);
1771 else
1772 ThrowException("Switch case is constant, but not a simple integer!");
1773 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001774 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001775 }
Chris Lattner196850c2003-05-15 21:30:00 +00001776 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001777 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00001778 $$ = S;
1779 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001780 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1781 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001782 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001783 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001784
Chris Lattnera8e8f162005-05-06 20:27:19 +00001785 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001786 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001787 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001788 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001789 if ($6) {
1790 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00001791 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001792 ParamTypes.push_back((*I)->getType());
1793 }
1794
1795 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1796 if (isVarArg) ParamTypes.pop_back();
1797
Chris Lattnera8e8f162005-05-06 20:27:19 +00001798 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001799 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001800 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001801
Chris Lattnera8e8f162005-05-06 20:27:19 +00001802 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001803
Chris Lattnera8e8f162005-05-06 20:27:19 +00001804 BasicBlock *Normal = getBBVal($10);
1805 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00001806
1807 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001808 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001809 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001810 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001811 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001812 // correctly!
1813 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001814 FunctionType::param_iterator I = Ty->param_begin();
1815 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001816 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001817
1818 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001819 if ((*ArgI)->getType() != *I)
1820 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1821 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001822
1823 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001824 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001825
Chris Lattnera8e8f162005-05-06 20:27:19 +00001826 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00001827 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001828 cast<InvokeInst>($$)->setCallingConv($2);
1829
1830 delete $3;
1831 delete $6;
Chris Lattner36143fc2003-09-08 18:54:55 +00001832 }
1833 | UNWIND {
1834 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00001835 }
1836 | UNREACHABLE {
1837 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001838 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001839
1840
Chris Lattner00950542001-06-06 20:29:01 +00001841
1842JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1843 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001844 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001845 if (V == 0)
1846 ThrowException("May only switch on a constant pool value!");
1847
Chris Lattner64116f92004-07-14 01:33:11 +00001848 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001849 }
1850 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001851 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001852 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001853
1854 if (V == 0)
1855 ThrowException("May only switch on a constant pool value!");
1856
Chris Lattner64116f92004-07-14 01:33:11 +00001857 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001858 };
Chris Lattner00950542001-06-06 20:29:01 +00001859
1860Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001861 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001862 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001863 InsertValue($2);
1864 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001865};
Chris Lattner00950542001-06-06 20:29:01 +00001866
Chris Lattnerc24d2082001-06-11 15:04:20 +00001867PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001868 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001869 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001870 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001871 }
1872 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1873 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001874 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001875 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001876 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001877
1878
Chris Lattner30c89792001-09-07 16:35:17 +00001879ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001880 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001881 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001882 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001883 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001884 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001885 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001886 };
Chris Lattner00950542001-06-06 20:29:01 +00001887
1888// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001889ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001890
Chris Lattnerddb6db42005-05-06 05:51:46 +00001891OptTailCall : TAIL CALL {
1892 $$ = true;
1893 }
1894 | CALL {
1895 $$ = false;
1896 };
1897
1898
1899
Chris Lattner4a6482b2002-09-10 19:57:26 +00001900InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00001901 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
1902 !isa<PackedType>((*$2).get()))
1903 ThrowException(
1904 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00001905 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
1906 ThrowException("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00001907 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1908 if ($$ == 0)
1909 ThrowException("binary operator returned null!");
1910 delete $2;
1911 }
1912 | LogicalOps Types ValueRef ',' ValueRef {
1913 if (!(*$2)->isIntegral())
1914 ThrowException("Logical operator requires integral operands!");
1915 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1916 if ($$ == 0)
1917 ThrowException("binary operator returned null!");
1918 delete $2;
1919 }
1920 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00001921 if(isa<PackedType>((*$2).get())) {
1922 ThrowException(
1923 "PackedTypes currently not supported in setcc instructions!");
1924 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00001925 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001926 if ($$ == 0)
1927 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001928 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001929 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001930 | NOT ResolvedVal {
1931 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1932 << " Replacing with 'xor'.\n";
1933
1934 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1935 if (Ones == 0)
1936 ThrowException("Expected integral type for not instruction!");
1937
1938 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001939 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001940 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001941 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001942 | ShiftOps ResolvedVal ',' ResolvedVal {
1943 if ($4->getType() != Type::UByteTy)
1944 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001945 if (!$2->getType()->isInteger())
1946 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001947 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001948 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001949 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001950 if (!$4->get()->isFirstClassType())
1951 ThrowException("cast instruction to a non-primitive type: '" +
1952 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001953 $$ = new CastInst($2, *$4);
1954 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001955 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001956 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1957 if ($2->getType() != Type::BoolTy)
1958 ThrowException("select condition must be boolean!");
1959 if ($4->getType() != $6->getType())
1960 ThrowException("select value types should match!");
1961 $$ = new SelectInst($2, $4, $6);
1962 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00001963 | VAARG ResolvedVal ',' Types {
1964 $$ = new VAArgInst($2, *$4);
1965 delete $4;
1966 }
1967 | VANEXT ResolvedVal ',' Types {
1968 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001969 delete $4;
1970 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001971 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001972 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001973 if (!Ty->isFirstClassType())
1974 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001975 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001976 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00001977 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001978 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00001979 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001980 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001981 $2->pop_front();
1982 }
1983 delete $2; // Free the list...
Chris Lattnerddb6db42005-05-06 05:51:46 +00001984 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001985 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001986 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001987 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001988
Chris Lattnera8e8f162005-05-06 20:27:19 +00001989 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001990 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001991 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001992 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001993 if ($6) {
1994 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00001995 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001996 ParamTypes.push_back((*I)->getType());
1997 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001998
1999 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2000 if (isVarArg) ParamTypes.pop_back();
2001
Chris Lattnera8e8f162005-05-06 20:27:19 +00002002 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Chris Lattner595f06c2005-02-01 01:47:42 +00002003 ThrowException("LLVM functions cannot return aggregate types!");
2004
Chris Lattnera8e8f162005-05-06 20:27:19 +00002005 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002006 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002007 }
Chris Lattner00950542001-06-06 20:29:01 +00002008
Chris Lattnera8e8f162005-05-06 20:27:19 +00002009 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002010
Chris Lattner8b81bf52001-07-25 22:47:46 +00002011 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002012 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002013 // Make sure no arguments is a good thing!
2014 if (Ty->getNumParams() != 0)
2015 ThrowException("No arguments passed to a function that "
2016 "expects arguments!");
2017
Chris Lattner9232b992003-04-22 18:42:41 +00002018 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002019 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002020 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002021 // correctly!
2022 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002023 FunctionType::param_iterator I = Ty->param_begin();
2024 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002025 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002026
2027 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002028 if ((*ArgI)->getType() != *I)
2029 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2030 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002031
Chris Lattner8b81bf52001-07-25 22:47:46 +00002032 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002033 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002034
Chris Lattnera8e8f162005-05-06 20:27:19 +00002035 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002036 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002037 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002038 cast<CallInst>($$)->setCallingConv($2);
2039 delete $3;
2040 delete $6;
Chris Lattner00950542001-06-06 20:29:01 +00002041 }
2042 | MemoryInst {
2043 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002044 };
Chris Lattner00950542001-06-06 20:29:01 +00002045
Chris Lattner6cdb0112001-11-26 16:54:11 +00002046
2047// IndexList - List of indices for GEP based instructions...
2048IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002049 $$ = $2;
2050 } | /* empty */ {
2051 $$ = new std::vector<Value*>();
2052 };
2053
2054OptVolatile : VOLATILE {
2055 $$ = true;
2056 }
2057 | /* empty */ {
2058 $$ = false;
2059 };
2060
Chris Lattner027dcc52001-07-08 21:10:27 +00002061
Chris Lattnerddb6db42005-05-06 05:51:46 +00002062
Chris Lattner00950542001-06-06 20:29:01 +00002063MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002064 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002065 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002066 }
2067 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002068 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002069 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002070 }
2071 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002072 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002073 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002074 }
2075 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002076 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002077 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002078 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002079 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002080 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002081 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002082 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002083 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002084 }
2085
Chris Lattner15c9c032003-09-08 18:20:29 +00002086 | OptVolatile LOAD Types ValueRef {
2087 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002088 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002089 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002090 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2091 ThrowException("Can't load from pointer of non-first-class type: " +
2092 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002093 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002094 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002095 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002096 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2097 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002098 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002099 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002100 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002101 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002102 if (ElTy != $3->getType())
2103 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002104 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002105
Chris Lattner79ad13802003-09-08 20:29:46 +00002106 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002107 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002108 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002109 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002110 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002111 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002112
2113 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2114 // indices to uint struct indices for compatibility.
2115 generic_gep_type_iterator<std::vector<Value*>::iterator>
2116 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2117 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2118 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2119 if (isa<StructType>(*GTI)) // Only change struct indices
2120 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2121 if (CUI->getType() == Type::UByteTy)
2122 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2123
Chris Lattner30c89792001-09-07 16:35:17 +00002124 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002125 ThrowException("Invalid getelementptr indices for type '" +
2126 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002127 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2128 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002129 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002130
Brian Gaeked0fde302003-11-11 22:41:34 +00002131
Chris Lattner00950542001-06-06 20:29:01 +00002132%%
Chris Lattner09083092001-07-08 04:57:15 +00002133int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002134 std::string where
2135 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002136 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002137 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002138 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002139 errMsg += "end-of-file.";
2140 else
Chris Lattner9232b992003-04-22 18:42:41 +00002141 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002142 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002143 return 0;
2144}