blob: f59475cd13011c146412103de4b3ad5714bf0f9a [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
Andrew Lenharth558bc882005-06-18 18:34:52 +000050static bool ObsoleteVarArgs;
51static BasicBlock* CurBB;
52
53
Chris Lattner7e708292002-06-25 16:13:24 +000054// This contains info used when building the body of a function. It is
55// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000056//
Chris Lattner9232b992003-04-22 18:42:41 +000057typedef std::vector<Value *> ValueList; // Numbered defs
Misha Brukman5a2a3822005-05-10 22:02:28 +000058static void
59ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
60 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000061
62static struct PerModuleInfo {
63 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000064 std::map<const Type *, ValueList> Values; // Module level numbered definitions
65 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000066 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000067 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000068
Chris Lattnerbc721ed2004-07-13 08:28:21 +000069 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
70 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000071 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000072 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
73
Chris Lattner2079fde2001-10-13 06:41:08 +000074 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
75 // references to global values. Global values may be referenced before they
76 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000077 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000078 //
Chris Lattner9232b992003-04-22 18:42:41 +000079 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000080 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000081 GlobalRefsType GlobalRefs;
82
Chris Lattner00950542001-06-06 20:29:01 +000083 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000084 // If we could not resolve some functions at function compilation time
85 // (calls to functions before they are defined), resolve them now... Types
86 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000087 //
Chris Lattner00950542001-06-06 20:29:01 +000088 ResolveDefinitions(LateResolveValues);
89
Chris Lattner2079fde2001-10-13 06:41:08 +000090 // Check to make sure that all global value forward references have been
91 // resolved!
92 //
93 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000094 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +000095
Chris Lattner749ce032002-03-11 22:12:39 +000096 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
97 I != E; ++I) {
98 UndefinedReferences += " " + I->first.first->getDescription() + " " +
99 I->first.second.getName() + "\n";
100 }
101 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000102 }
103
Chris Lattner7e708292002-06-25 16:13:24 +0000104 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000105 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000106 CurrentModule = 0;
107 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000108
109
Chris Lattner8a327842004-07-14 21:44:00 +0000110 // GetForwardRefForGlobal - Check to see if there is a forward reference
111 // for this global. If so, remove it from the GlobalRefs map and return it.
112 // If not, just return null.
113 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
114 // Check to see if there is a forward reference to this global variable...
115 // if there is, eliminate it and patch the reference to use the new def'n.
116 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
117 GlobalValue *Ret = 0;
118 if (I != GlobalRefs.end()) {
119 Ret = I->second;
120 GlobalRefs.erase(I);
121 }
122 return Ret;
123 }
Chris Lattner00950542001-06-06 20:29:01 +0000124} CurModule;
125
Chris Lattner79df7c02002-03-26 18:01:55 +0000126static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000127 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000128
Chris Lattner735f2702004-07-08 22:30:50 +0000129 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
130 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000131 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000132
Chris Lattner2e2ed292004-07-14 06:28:35 +0000133 /// BBForwardRefs - When we see forward references to basic blocks, keep
134 /// track of them here.
135 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
136 std::vector<BasicBlock*> NumberedBlocks;
137 unsigned NextBBNum;
138
Chris Lattner79df7c02002-03-26 18:01:55 +0000139 inline PerFunctionInfo() {
140 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000141 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000142 }
143
Chris Lattner79df7c02002-03-26 18:01:55 +0000144 inline void FunctionStart(Function *M) {
145 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000146 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000147 }
148
Chris Lattner79df7c02002-03-26 18:01:55 +0000149 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000150 NumberedBlocks.clear();
151
152 // Any forward referenced blocks left?
153 if (!BBForwardRefs.empty())
154 ThrowException("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000155 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000156
157 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000158 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000159
Chris Lattner7e708292002-06-25 16:13:24 +0000160 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000161 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000162 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000163 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000164} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000165
Chris Lattner394f2eb2003-10-16 20:12:13 +0000166static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000167
Chris Lattner00950542001-06-06 20:29:01 +0000168
169//===----------------------------------------------------------------------===//
170// Code to handle definitions of all the types
171//===----------------------------------------------------------------------===//
172
Chris Lattner735f2702004-07-08 22:30:50 +0000173static int InsertValue(Value *V,
174 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
175 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000176
177 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000178 ValueList &List = ValueTab[V->getType()];
179 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000180 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000181}
182
Chris Lattner30c89792001-09-07 16:35:17 +0000183static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000184 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000185 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000186 // Module constants occupy the lowest numbered slots...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000187 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000188 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000189 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000190 case ValID::NameVal: // Is it a named definition?
191 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
192 D.destroy(); // Free old strdup'd memory...
193 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000194 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000195 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000196 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000197 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000198 }
199
200 // If we reached here, we referenced either a symbol that we don't know about
201 // or an id number that hasn't been read yet. We may be referencing something
202 // forward, so just create an entry to be resolved later and get to it...
203 //
204 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
205
Chris Lattner355ad1f2005-03-21 06:27:42 +0000206
207 if (inFunctionScope()) {
208 if (D.Type == ValID::NameVal)
209 ThrowException("Reference to an undefined type: '" + D.getName() + "'");
210 else
211 ThrowException("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000212 }
Chris Lattner30c89792001-09-07 16:35:17 +0000213
Chris Lattner355ad1f2005-03-21 06:27:42 +0000214 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
215 if (I != CurModule.LateResolveTypes.end())
216 return I->second;
217
Chris Lattner82269592001-10-22 06:01:08 +0000218 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000219 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000220 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000221 }
Chris Lattner30c89792001-09-07 16:35:17 +0000222
Chris Lattner9232b992003-04-22 18:42:41 +0000223static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000224 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000225 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000226 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000227 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000228}
229
Chris Lattner2079fde2001-10-13 06:41:08 +0000230// getValNonImprovising - Look up the value specified by the provided type and
231// the provided ValID. If the value exists and has already been defined, return
232// it. Otherwise return null.
233//
234static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000235 if (isa<FunctionType>(Ty))
236 ThrowException("Functions are not values and "
237 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000238
Chris Lattner30c89792001-09-07 16:35:17 +0000239 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000240 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000241 unsigned Num = (unsigned)D.Num;
242
243 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000244 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000245 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000246 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000247 return VI->second[Num];
248 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000249 }
250
251 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000252 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000253 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000254
255 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000256 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000257
Chris Lattner16af11d2004-02-09 21:03:38 +0000258 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000259 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000260
Chris Lattner1a1cb112001-09-30 22:46:54 +0000261 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000262 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000263 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000264
265 D.destroy(); // Free old strdup'd memory...
266 return N;
267 }
268
Misha Brukman5a2a3822005-05-10 22:02:28 +0000269 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000270 // value will fit into the specified type...
271 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000272 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
273 ThrowException("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000274 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000275 Ty->getDescription() + "'!");
276 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000277
278 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000279 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
280 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000281 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000282 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000283 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000284 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000285 }
286 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000287 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000288 }
289
Chris Lattner2079fde2001-10-13 06:41:08 +0000290 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000291 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000292 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000293 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000294
Chris Lattner2079fde2001-10-13 06:41:08 +0000295 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000296 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000297 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000298 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000299
Chris Lattner16710e92004-10-16 18:17:13 +0000300 case ValID::ConstUndefVal: // Is it an undef value?
301 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000302
Chris Lattnerd78700d2002-08-16 21:14:40 +0000303 case ValID::ConstantVal: // Fully resolved constant?
304 if (D.ConstantValue->getType() != Ty)
305 ThrowException("Constant expression type different from required type!");
306 return D.ConstantValue;
307
Chris Lattner30c89792001-09-07 16:35:17 +0000308 default:
309 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000310 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000311 } // End of switch
312
Chris Lattner2079fde2001-10-13 06:41:08 +0000313 assert(0 && "Unhandled case!");
314 return 0;
315}
316
Chris Lattner2079fde2001-10-13 06:41:08 +0000317// getVal - This function is identical to getValNonImprovising, except that if a
318// value is not already defined, it "improvises" by creating a placeholder var
319// that looks and acts just like the requested variable. When the value is
320// defined later, all uses of the placeholder variable are replaced with the
321// real thing.
322//
Chris Lattner64116f92004-07-14 01:33:11 +0000323static Value *getVal(const Type *Ty, const ValID &ID) {
324 if (Ty == Type::LabelTy)
325 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000326
Chris Lattner64116f92004-07-14 01:33:11 +0000327 // See if the value has already been defined.
328 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000329 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000330
Chris Lattner60cd9552005-03-23 01:29:26 +0000331 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
332 ThrowException("Invalid use of a composite type!");
333
Chris Lattner00950542001-06-06 20:29:01 +0000334 // If we reached here, we referenced either a symbol that we don't know about
335 // or an id number that hasn't been read yet. We may be referencing something
336 // forward, so just create an entry to be resolved later and get to it...
337 //
Chris Lattner64116f92004-07-14 01:33:11 +0000338 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000339
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000340 // Remember where this forward reference came from. FIXME, shouldn't we try
341 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000342 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000343 llvmAsmlineno)));
344
Chris Lattner79df7c02002-03-26 18:01:55 +0000345 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000346 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000347 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000348 InsertValue(V, CurModule.LateResolveValues);
349 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000350}
351
Chris Lattner2e2ed292004-07-14 06:28:35 +0000352/// getBBVal - This is used for two purposes:
353/// * If isDefinition is true, a new basic block with the specified ID is being
354/// defined.
355/// * If isDefinition is true, this is a reference to a basic block, which may
356/// or may not be a forward reference.
357///
358static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000359 assert(inFunctionScope() && "Can't get basic block at global scope!");
360
Chris Lattner2e2ed292004-07-14 06:28:35 +0000361 std::string Name;
362 BasicBlock *BB = 0;
363 switch (ID.Type) {
364 default: ThrowException("Illegal label reference " + ID.getName());
365 case ValID::NumberVal: // Is it a numbered definition?
366 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
367 CurFun.NumberedBlocks.resize(ID.Num+1);
368 BB = CurFun.NumberedBlocks[ID.Num];
369 break;
370 case ValID::NameVal: // Is it a named definition?
371 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000372 if (Value *N = CurFun.CurrentFunction->
373 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000374 BB = cast<BasicBlock>(N);
375 break;
376 }
Chris Lattner64116f92004-07-14 01:33:11 +0000377
Chris Lattner2e2ed292004-07-14 06:28:35 +0000378 // See if the block has already been defined.
379 if (BB) {
380 // If this is the definition of the block, make sure the existing value was
381 // just a forward reference. If it was a forward reference, there will be
382 // an entry for it in the PlaceHolderInfo map.
383 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
384 // The existing value was a definition, not a forward reference.
385 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000386
Chris Lattner2e2ed292004-07-14 06:28:35 +0000387 ID.destroy(); // Free strdup'd memory.
388 return BB;
389 }
390
391 // Otherwise this block has not been seen before.
392 BB = new BasicBlock("", CurFun.CurrentFunction);
393 if (ID.Type == ValID::NameVal) {
394 BB->setName(ID.Name);
395 } else {
396 CurFun.NumberedBlocks[ID.Num] = BB;
397 }
398
399 // If this is not a definition, keep track of it so we can use it as a forward
400 // reference.
401 if (!isDefinition) {
402 // Remember where this forward reference came from.
403 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
404 } else {
405 // The forward declaration could have been inserted anywhere in the
406 // function: insert it into the correct place now.
407 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
408 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
409 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000410 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000411 return BB;
412}
413
Chris Lattner00950542001-06-06 20:29:01 +0000414
415//===----------------------------------------------------------------------===//
416// Code to handle forward references in instructions
417//===----------------------------------------------------------------------===//
418//
419// This code handles the late binding needed with statements that reference
420// values not defined yet... for example, a forward branch, or the PHI node for
421// a loop body.
422//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000423// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000424// and back patchs after we are done.
425//
426
Misha Brukman5a2a3822005-05-10 22:02:28 +0000427// ResolveDefinitions - If we could not resolve some defs at parsing
428// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000429// defs now...
430//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000431static void
432ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
433 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000434 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000435 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000436 E = LateResolvers.end(); LRI != E; ++LRI) {
437 ValueList &List = LRI->second;
438 while (!List.empty()) {
439 Value *V = List.back();
440 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000441
442 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
443 CurModule.PlaceHolderInfo.find(V);
444 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
445
446 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000447
Chris Lattner735f2702004-07-08 22:30:50 +0000448 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000449 if (TheRealValue) {
450 V->replaceAllUsesWith(TheRealValue);
451 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000452 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000453 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000454 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000455 // resolver table
456 InsertValue(V, *FutureLateResolvers);
457 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000458 if (DID.Type == ValID::NameVal)
459 ThrowException("Reference to an invalid definition: '" +DID.getName()+
460 "' of type '" + V->getType()->getDescription() + "'",
461 PHI->second.second);
462 else
463 ThrowException("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000464 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000465 V->getType()->getDescription() + "'",
466 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000467 }
Chris Lattner00950542001-06-06 20:29:01 +0000468 }
469 }
470
471 LateResolvers.clear();
472}
473
Chris Lattner4a42e902001-10-22 05:56:09 +0000474// ResolveTypeTo - A brand new type was just declared. This means that (if
475// name is not null) things referencing Name can be resolved. Otherwise, things
476// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000477//
Chris Lattner4a42e902001-10-22 05:56:09 +0000478static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000479 ValID D;
480 if (Name) D = ValID::create(Name);
481 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000482
Chris Lattner355ad1f2005-03-21 06:27:42 +0000483 std::map<ValID, PATypeHolder>::iterator I =
484 CurModule.LateResolveTypes.find(D);
485 if (I != CurModule.LateResolveTypes.end()) {
486 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
487 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000488 }
489}
490
Chris Lattner1781aca2001-09-18 04:00:54 +0000491// setValueName - Set the specified value to the name given. The name may be
492// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000493// assumed to be a malloc'd string buffer, and is free'd by this function.
494//
495static void setValueName(Value *V, char *NameStr) {
496 if (NameStr) {
497 std::string Name(NameStr); // Copy string
498 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000499
Misha Brukman5a2a3822005-05-10 22:02:28 +0000500 if (V->getType() == Type::VoidTy)
Chris Lattnerc9aea522004-07-13 08:12:39 +0000501 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000502
Chris Lattner8ab406d2004-07-13 07:59:27 +0000503 assert(inFunctionScope() && "Must be in function scope!");
504 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
505 if (ST.lookup(V->getType(), Name))
506 ThrowException("Redefinition of value named '" + Name + "' in the '" +
507 V->getType()->getDescription() + "' type plane!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000508
Chris Lattnerc9aea522004-07-13 08:12:39 +0000509 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000510 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000511 }
512}
513
Chris Lattnerd88998d2004-07-14 08:23:52 +0000514/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
515/// this is a declaration, otherwise it is a definition.
516static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
517 bool isConstantGlobal, const Type *Ty,
518 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000519 if (isa<FunctionType>(Ty))
520 ThrowException("Cannot declare global vars of function type!");
521
Misha Brukman5a2a3822005-05-10 22:02:28 +0000522 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000523
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000524 std::string Name;
525 if (NameStr) {
526 Name = NameStr; // Copy string
527 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000528 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000529
Chris Lattner8a327842004-07-14 21:44:00 +0000530 // See if this global value was forward referenced. If so, recycle the
531 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000532 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000533 if (!Name.empty()) {
534 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000535 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000536 ID = ValID::create((int)CurModule.Values[PTy].size());
537 }
538
539 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000540 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000541 // previously inserted.
542 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
543 CurModule.CurrentModule->getGlobalList().remove(GV);
544 CurModule.CurrentModule->getGlobalList().push_back(GV);
545 GV->setInitializer(Initializer);
546 GV->setLinkage(Linkage);
547 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000548 InsertValue(GV, CurModule.Values);
Chris Lattnera09000d2004-07-14 23:03:46 +0000549 return;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000550 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000551
552 // If this global has a name, check to see if there is already a definition
553 // of this global in the module. If so, merge as appropriate. Note that
554 // this is really just a hack around problems in the CFE. :(
555 if (!Name.empty()) {
556 // We are a simple redefinition of a value, check to see if it is defined
557 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000558 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000559 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
560 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000561 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000562 // 2. If both initializers have the same value.
563 //
564 if (!EGV->hasInitializer() || !Initializer ||
565 EGV->getInitializer() == Initializer) {
566
567 // Make sure the existing global version gets the initializer! Make
568 // sure that it also gets marked const if the new version is.
569 if (Initializer && !EGV->hasInitializer())
570 EGV->setInitializer(Initializer);
571 if (isConstantGlobal)
572 EGV->setConstant(true);
573 EGV->setLinkage(Linkage);
574 return;
575 }
576
Misha Brukman5a2a3822005-05-10 22:02:28 +0000577 ThrowException("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000578 "' in the '" + Ty->getDescription() + "' type plane!");
579 }
580 }
581
582 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000583 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000584 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000585 CurModule.CurrentModule);
586 InsertValue(GV, CurModule.Values);
Chris Lattnerd88998d2004-07-14 08:23:52 +0000587}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000588
Reid Spencer75719bf2004-05-26 21:48:31 +0000589// setTypeName - Set the specified type to the name given. The name may be
590// null potentially, in which case this is a noop. The string passed in is
591// assumed to be a malloc'd string buffer, and is freed by this function.
592//
593// This function returns true if the type has already been defined, but is
594// allowed to be redefined in the specified context. If the name is a new name
595// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000596static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000597 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000598 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000599
Reid Spencer75719bf2004-05-26 21:48:31 +0000600 std::string Name(NameStr); // Copy string
601 free(NameStr); // Free old string
602
603 // We don't allow assigning names to void type
Misha Brukman5a2a3822005-05-10 22:02:28 +0000604 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000605 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000606
Chris Lattnera09000d2004-07-14 23:03:46 +0000607 // Set the type name, checking for conflicts as we do so.
608 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000609
Chris Lattnera09000d2004-07-14 23:03:46 +0000610 if (AlreadyExists) { // Inserting a name that is already defined???
611 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
612 assert(Existing && "Conflict but no matching type?");
613
Reid Spencer75719bf2004-05-26 21:48:31 +0000614 // There is only one case where this is allowed: when we are refining an
615 // opaque type. In this case, Existing will be an opaque type.
616 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
617 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000618 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000619 return true;
620 }
621
622 // Otherwise, this is an attempt to redefine a type. That's okay if
623 // the redefinition is identical to the original. This will be so if
624 // Existing and T point to the same Type object. In this one case we
625 // allow the equivalent redefinition.
626 if (Existing == T) return true; // Yes, it's equal.
627
628 // Any other kind of (non-equivalent) redefinition is an error.
629 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000630 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000631 }
632
Reid Spencer75719bf2004-05-26 21:48:31 +0000633 return false;
634}
Chris Lattner8896eda2001-07-09 19:38:36 +0000635
Chris Lattner30c89792001-09-07 16:35:17 +0000636//===----------------------------------------------------------------------===//
637// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000638//
Chris Lattner8896eda2001-07-09 19:38:36 +0000639
Chris Lattner3b073862004-02-09 03:19:29 +0000640// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000641//
642static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000643 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000644 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000645}
646
647namespace {
648 struct UpRefRecord {
649 // NestingLevel - The number of nesting levels that need to be popped before
650 // this type is resolved.
651 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000652
Chris Lattner3b073862004-02-09 03:19:29 +0000653 // LastContainedTy - This is the type at the current binding level for the
654 // type. Every time we reduce the nesting level, this gets updated.
655 const Type *LastContainedTy;
656
657 // UpRefTy - This is the actual opaque type that the upreference is
658 // represented with.
659 OpaqueType *UpRefTy;
660
661 UpRefRecord(unsigned NL, OpaqueType *URTy)
662 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
663 };
Chris Lattner30c89792001-09-07 16:35:17 +0000664}
Chris Lattner698b56e2001-07-20 19:15:08 +0000665
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000666// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000667static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000668
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000669/// HandleUpRefs - Every time we finish a new layer of types, this function is
670/// called. It loops through the UpRefs vector, which is a list of the
671/// currently active types. For each type, if the up reference is contained in
672/// the newly completed type, we decrement the level count. When the level
673/// count reaches zero, the upreferenced type is the type that is passed in:
674/// thus we can complete the cycle.
675///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000676static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000677 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000678 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000679 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000680 "' newly formed. Resolving upreferences.\n" <<
681 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000682
683 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
684 // to zero), we resolve them all together before we resolve them to Ty. At
685 // the end of the loop, if there is anything to resolve to Ty, it will be in
686 // this variable.
687 OpaqueType *TypeToResolve = 0;
688
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000689 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000690 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
691 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000692 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000693 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
694 // Decrement level of upreference
695 unsigned Level = --UpRefs[i].NestingLevel;
696 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000697 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000698 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000699 if (!TypeToResolve) {
700 TypeToResolve = UpRefs[i].UpRefTy;
701 } else {
702 UR_OUT(" * Resolving upreference for "
703 << UpRefs[i].second->getDescription() << "\n";
704 std::string OldName = UpRefs[i].UpRefTy->getDescription());
705 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
706 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
707 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
708 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000709 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000710 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000711 }
712 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000713 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000714
Chris Lattner026a8ce2004-02-09 18:53:54 +0000715 if (TypeToResolve) {
716 UR_OUT(" * Resolving upreference for "
717 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000718 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000719 TypeToResolve->refineAbstractTypeTo(Ty);
720 }
721
Chris Lattner8896eda2001-07-09 19:38:36 +0000722 return Ty;
723}
724
Chris Lattner30c89792001-09-07 16:35:17 +0000725
Chris Lattner6184feb2005-05-20 03:25:47 +0000726// common code from the two 'RunVMAsmParser' functions
727 static Module * RunParser(Module * M) {
728
729 llvmAsmlineno = 1; // Reset the current line number...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000730 ObsoleteVarArgs = false;
Chris Lattner6184feb2005-05-20 03:25:47 +0000731
732 CurModule.CurrentModule = M;
733 yyparse(); // Parse the file, potentially throwing exception
734
735 Module *Result = ParserResult;
736 ParserResult = 0;
737
Andrew Lenharth558bc882005-06-18 18:34:52 +0000738 if(ObsoleteVarArgs) {
739 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
740 assert(F->arg_size() == 0 && "Obsolete va_start takes 0 argument!");
741
742 //foo = va_start()
743 // ->
744 //bar = alloca typeof(foo)
745 //va_start(bar)
746 //foo = load bar
747
748 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
749 const Type* ArgTy = F->getFunctionType()->getReturnType();
750 const Type* ArgTyPtr = PointerType::get(ArgTy);
751 Function* NF = Result->getOrInsertFunction("llvm.va_start",
752 RetTy, ArgTyPtr, 0);
753
754 while (!F->use_empty()) {
755 CallInst* CI = cast<CallInst>(F->use_back());
756 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
757 new CallInst(NF, bar, "", CI);
758 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
759 CI->replaceAllUsesWith(foo);
760 CI->getParent()->getInstList().erase(CI);
761 }
762 Result->getFunctionList().erase(F);
763 }
764
765 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
766 assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
767 //vaend foo
768 // ->
769 //bar = alloca 1 of typeof(foo)
770 //vaend bar
771 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
772 const Type* ArgTy = F->getFunctionType()->getParamType(0);
773 const Type* ArgTyPtr = PointerType::get(ArgTy);
774 Function* NF = Result->getOrInsertFunction("llvm.va_end",
775 RetTy, ArgTyPtr, 0);
776
777 while (!F->use_empty()) {
778 CallInst* CI = cast<CallInst>(F->use_back());
779 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
780 new CallInst(NF, bar, "", CI);
781 CI->getParent()->getInstList().erase(CI);
782 }
783 Result->getFunctionList().erase(F);
784 }
785
786 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
787 assert(F->arg_size() == 1 && "Obsolete va_copy takes 1 argument!");
788 //foo = vacopy(bar)
789 // ->
790 //a = alloca 1 of typeof(foo)
791 //vacopy(a, bar)
792 //foo = load a
793
794 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
795 const Type* ArgTy = F->getFunctionType()->getReturnType();
796 const Type* ArgTyPtr = PointerType::get(ArgTy);
797 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
798 RetTy, ArgTyPtr, ArgTy, 0);
799
800 while (!F->use_empty()) {
801 CallInst* CI = cast<CallInst>(F->use_back());
802 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
803 new CallInst(NF, a, CI->getOperand(1), "", CI);
804 Value* foo = new LoadInst(a, "vacopy.fix.2", CI);
805 CI->replaceAllUsesWith(foo);
806 CI->getParent()->getInstList().erase(CI);
807 }
808 Result->getFunctionList().erase(F);
809 }
810 }
811
Chris Lattner6184feb2005-05-20 03:25:47 +0000812 return Result;
813
814 }
815
Chris Lattner00950542001-06-06 20:29:01 +0000816//===----------------------------------------------------------------------===//
817// RunVMAsmParser - Define an interface to this parser
818//===----------------------------------------------------------------------===//
819//
Chris Lattner86427632004-07-14 06:39:48 +0000820Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000821 set_scan_file(F);
822
Chris Lattnera2850432001-07-22 18:36:00 +0000823 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000824 return RunParser(new Module(CurFilename));
825}
Chris Lattner00950542001-06-06 20:29:01 +0000826
Chris Lattner6184feb2005-05-20 03:25:47 +0000827Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
828 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000829
Chris Lattner6184feb2005-05-20 03:25:47 +0000830 CurFilename = "from_memory";
831 if (M == NULL) {
832 return RunParser(new Module (CurFilename));
833 } else {
834 return RunParser(M);
835 }
Chris Lattner00950542001-06-06 20:29:01 +0000836}
837
838%}
839
840%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000841 llvm::Module *ModuleVal;
842 llvm::Function *FunctionVal;
843 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
844 llvm::BasicBlock *BasicBlockVal;
845 llvm::TerminatorInst *TermInstVal;
846 llvm::Instruction *InstVal;
847 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000848
Brian Gaeked0fde302003-11-11 22:41:34 +0000849 const llvm::Type *PrimType;
850 llvm::PATypeHolder *TypeVal;
851 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000852
Brian Gaeked0fde302003-11-11 22:41:34 +0000853 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
854 std::vector<llvm::Value*> *ValueList;
855 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000856 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000857 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000858 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000859 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
860 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000861
Brian Gaeked0fde302003-11-11 22:41:34 +0000862 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000863 int64_t SInt64Val;
864 uint64_t UInt64Val;
865 int SIntVal;
866 unsigned UIntVal;
867 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000868 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000869
Chris Lattner30c89792001-09-07 16:35:17 +0000870 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000871 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000872
Brian Gaeked0fde302003-11-11 22:41:34 +0000873 llvm::Instruction::BinaryOps BinaryOpVal;
874 llvm::Instruction::TermOps TermOpVal;
875 llvm::Instruction::MemoryOps MemOpVal;
876 llvm::Instruction::OtherOps OtherOpVal;
877 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000878}
879
Chris Lattner79df7c02002-03-26 18:01:55 +0000880%type <ModuleVal> Module FunctionList
881%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000882%type <BasicBlockVal> BasicBlock InstructionList
883%type <TermInstVal> BBTerminatorInst
884%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000885%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000886%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000887%type <ArgList> ArgList ArgListH
888%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000889%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000890%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000891%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000892%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000893%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000894%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000895%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000896%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000897%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000898%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000899
Chris Lattner2079fde2001-10-13 06:41:08 +0000900// ValueRef - Unresolved reference to a definition or BB
901%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000902%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000903// Tokens and types for handling constant integer values
904//
905// ESINT64VAL - A negative number within long long range
906%token <SInt64Val> ESINT64VAL
907
908// EUINT64VAL - A positive number within uns. long long range
909%token <UInt64Val> EUINT64VAL
910%type <SInt64Val> EINT64VAL
911
912%token <SIntVal> SINTVAL // Signed 32 bit ints...
913%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
914%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000915%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000916
917// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000918%type <TypeVal> Types TypesV UpRTypes UpRTypesV
919%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000920%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
921%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000922
Chris Lattner6c23f572003-08-22 05:42:10 +0000923%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
924%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000925
926
Chris Lattner91ef4602004-03-31 03:49:47 +0000927%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000928%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000929%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Reid Spencer0be13e72004-07-25 17:58:28 +0000930%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Chris Lattnerddb6db42005-05-06 05:51:46 +0000931%token DEPLIBS CALL TAIL
Chris Lattnera8e8f162005-05-06 20:27:19 +0000932%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK
933%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +0000934
Misha Brukman5a2a3822005-05-10 22:02:28 +0000935// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000936%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000937
Misha Brukman5a2a3822005-05-10 22:02:28 +0000938// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000939%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000940%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000941%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000942
943// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000944%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000945
Chris Lattner027dcc52001-07-08 21:10:27 +0000946// Other Operators
947%type <OtherOpVal> ShiftOps
Andrew Lenharth558bc882005-06-18 18:34:52 +0000948%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG
949%token VAARG_old VANEXT_old //OBSOLETE
Chris Lattnerddb6db42005-05-06 05:51:46 +0000950
Chris Lattner027dcc52001-07-08 21:10:27 +0000951
Chris Lattner00950542001-06-06 20:29:01 +0000952%start Module
953%%
954
955// Handle constant integer size restriction and conversion...
956//
Chris Lattner51727be2002-06-04 21:58:56 +0000957INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000958INTVAL : UINTVAL {
959 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
960 ThrowException("Value too large for type!");
961 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000962};
Chris Lattner00950542001-06-06 20:29:01 +0000963
964
Chris Lattner51727be2002-06-04 21:58:56 +0000965EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000966EINT64VAL : EUINT64VAL {
967 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
968 ThrowException("Value too large for type!");
969 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000970};
Chris Lattner00950542001-06-06 20:29:01 +0000971
Misha Brukman5a2a3822005-05-10 22:02:28 +0000972// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +0000973// RET, BR, & SWITCH because they end basic blocks and are treated specially.
974//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000975ArithmeticOps: ADD | SUB | MUL | DIV | REM;
976LogicalOps : AND | OR | XOR;
977SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +0000978
Chris Lattner51727be2002-06-04 21:58:56 +0000979ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000980
Chris Lattnere98dda62001-07-14 06:10:16 +0000981// These are some types that allow classification if we only want a particular
982// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000983SIntType : LONG | INT | SHORT | SBYTE;
984UIntType : ULONG | UINT | USHORT | UBYTE;
985IntType : SIntType | UIntType;
986FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000987
Chris Lattnere98dda62001-07-14 06:10:16 +0000988// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000989OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000990 $$ = $1;
991 }
Misha Brukman5a2a3822005-05-10 22:02:28 +0000992 | /*empty*/ {
993 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000994 };
Chris Lattner00950542001-06-06 20:29:01 +0000995
Chris Lattner4ad02e72003-04-16 20:28:45 +0000996OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
997 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000998 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000999 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1000 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001001
Chris Lattnera8e8f162005-05-06 20:27:19 +00001002OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1003 CCC_TOK { $$ = CallingConv::C; } |
1004 FASTCC_TOK { $$ = CallingConv::Fast; } |
1005 COLDCC_TOK { $$ = CallingConv::Cold; } |
1006 CC_TOK EUINT64VAL {
1007 if ((unsigned)$2 != $2)
1008 ThrowException("Calling conv too large!");
1009 $$ = $2;
1010 };
1011
Chris Lattner30c89792001-09-07 16:35:17 +00001012//===----------------------------------------------------------------------===//
1013// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001014// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001015// access to it, a user must explicitly use TypesV.
1016//
1017
1018// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001019TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1020UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001021
1022Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001023 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001024 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1025 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001026 };
Chris Lattner30c89792001-09-07 16:35:17 +00001027
1028
1029// Derived types are added later...
1030//
Chris Lattner51727be2002-06-04 21:58:56 +00001031PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1032PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001033UpRTypes : OPAQUE {
1034 $$ = new PATypeHolder(OpaqueType::get());
1035 }
1036 | PrimType {
1037 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001038 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001039UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001040 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001041};
Chris Lattner30c89792001-09-07 16:35:17 +00001042
Chris Lattner30c89792001-09-07 16:35:17 +00001043// Include derived types in the Types production.
1044//
1045UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001046 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001047 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001048 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001049 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001050 UR_OUT("New Upreference!\n");
1051 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001052 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001053 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001054 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1055 E = $3->end(); I != E; ++I)
1056 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001057 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1058 if (isVarArg) Params.pop_back();
1059
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001060 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001061 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001062 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001063 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001064 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001065 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001066 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001067 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001068 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1069 const llvm::Type* ElemTy = $4->get();
1070 if ((unsigned)$2 != $2) {
1071 ThrowException("Unsigned result not equal to signed result");
1072 }
1073 if(!ElemTy->isPrimitiveType()) {
1074 ThrowException("Elemental type of a PackedType must be primitive");
1075 }
1076 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1077 delete $4;
1078 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001079 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001080 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001081 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1082 E = $2->end(); I != E; ++I)
1083 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001084
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001085 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001086 delete $2;
1087 }
1088 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001089 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001090 }
1091 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001092 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001093 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001094 };
Chris Lattner30c89792001-09-07 16:35:17 +00001095
Chris Lattner7e708292002-06-25 16:13:24 +00001096// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001097// declaration type lists
1098//
1099TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001100 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001101 $$->push_back(*$1); delete $1;
1102 }
1103 | TypeListI ',' UpRTypes {
1104 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001105 };
Chris Lattner30c89792001-09-07 16:35:17 +00001106
Chris Lattner7e708292002-06-25 16:13:24 +00001107// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001108ArgTypeListI : TypeListI
1109 | TypeListI ',' DOTDOTDOT {
1110 ($$=$1)->push_back(Type::VoidTy);
1111 }
1112 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001113 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001114 }
1115 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001116 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001117 };
Chris Lattner30c89792001-09-07 16:35:17 +00001118
Chris Lattnere98dda62001-07-14 06:10:16 +00001119// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001120// production is used ONLY to represent constants that show up AFTER a 'const',
1121// 'constant' or 'global' token at global scope. Constants that can be inlined
1122// into other expressions (such as integers and constexprs) are handled by the
1123// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001124//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001125ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001126 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001127 if (ATy == 0)
1128 ThrowException("Cannot make array constant with type: '" +
1129 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001130 const Type *ETy = ATy->getElementType();
1131 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001132
Chris Lattner30c89792001-09-07 16:35:17 +00001133 // Verify that we have the correct size...
1134 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001135 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001136 utostr($3->size()) + " arguments, but has size of " +
1137 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001138
Chris Lattner30c89792001-09-07 16:35:17 +00001139 // Verify all elements are correct type!
1140 for (unsigned i = 0; i < $3->size(); i++) {
1141 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001142 ThrowException("Element #" + utostr(i) + " is not of type '" +
1143 ETy->getDescription() +"' as required!\nIt is of type '"+
1144 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001145 }
1146
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001147 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001148 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001149 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001150 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001151 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001152 if (ATy == 0)
1153 ThrowException("Cannot make array constant with type: '" +
1154 (*$1)->getDescription() + "'!");
1155
1156 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001157 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001158 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001159 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001160 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001161 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001162 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001163 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001164 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001165 if (ATy == 0)
1166 ThrowException("Cannot make array constant with type: '" +
1167 (*$1)->getDescription() + "'!");
1168
Chris Lattner30c89792001-09-07 16:35:17 +00001169 int NumElements = ATy->getNumElements();
1170 const Type *ETy = ATy->getElementType();
1171 char *EndStr = UnEscapeLexed($3, true);
1172 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001173 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001174 itostr((int)(EndStr-$3)) +
1175 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001176 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001177 if (ETy == Type::SByteTy) {
1178 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001179 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001180 } else if (ETy == Type::UByteTy) {
1181 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001182 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001183 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001184 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001185 ThrowException("Cannot build string arrays of non byte sized elements!");
1186 }
Chris Lattner30c89792001-09-07 16:35:17 +00001187 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001188 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001189 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001190 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001191 | Types '<' ConstVector '>' { // Nonempty unsized arr
1192 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1193 if (PTy == 0)
1194 ThrowException("Cannot make packed constant with type: '" +
1195 (*$1)->getDescription() + "'!");
1196 const Type *ETy = PTy->getElementType();
1197 int NumElements = PTy->getNumElements();
1198
1199 // Verify that we have the correct size...
1200 if (NumElements != -1 && NumElements != (int)$3->size())
1201 ThrowException("Type mismatch: constant sized packed initialized with " +
1202 utostr($3->size()) + " arguments, but has size of " +
1203 itostr(NumElements) + "!");
1204
1205 // Verify all elements are correct type!
1206 for (unsigned i = 0; i < $3->size(); i++) {
1207 if (ETy != (*$3)[i]->getType())
1208 ThrowException("Element #" + utostr(i) + " is not of type '" +
1209 ETy->getDescription() +"' as required!\nIt is of type '"+
1210 (*$3)[i]->getType()->getDescription() + "'.");
1211 }
1212
1213 $$ = ConstantPacked::get(PTy, *$3);
1214 delete $1; delete $3;
1215 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001216 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001217 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001218 if (STy == 0)
1219 ThrowException("Cannot make struct constant with type: '" +
1220 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001221
Chris Lattnerc6212f12003-05-21 16:06:56 +00001222 if ($3->size() != STy->getNumContainedTypes())
1223 ThrowException("Illegal number of initializers for structure type!");
1224
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001225 // Check to ensure that constants are compatible with the type initializer!
1226 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001227 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001228 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001229 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001230 "' for element #" + utostr(i) +
1231 " of structure initializer!");
1232
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001233 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001234 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001235 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001236 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001237 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001238 if (STy == 0)
1239 ThrowException("Cannot make struct constant with type: '" +
1240 (*$1)->getDescription() + "'!");
1241
1242 if (STy->getNumContainedTypes() != 0)
1243 ThrowException("Illegal number of initializers for structure type!");
1244
1245 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1246 delete $1;
1247 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001248 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001249 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001250 if (PTy == 0)
1251 ThrowException("Cannot make null pointer constant with type: '" +
1252 (*$1)->getDescription() + "'!");
1253
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001254 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001255 delete $1;
1256 }
Chris Lattner16710e92004-10-16 18:17:13 +00001257 | Types UNDEF {
1258 $$ = UndefValue::get($1->get());
1259 delete $1;
1260 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001261 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001262 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001263 if (Ty == 0)
1264 ThrowException("Global const reference must be a pointer type!");
1265
Chris Lattner3101c252002-08-15 17:58:33 +00001266 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001267 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001268 // the context of a function, getValNonImprovising will search the functions
1269 // symbol table instead of the module symbol table for the global symbol,
1270 // which throws things all off. To get around this, we just tell
1271 // getValNonImprovising that we are at global scope here.
1272 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001273 Function *SavedCurFn = CurFun.CurrentFunction;
1274 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001275
Chris Lattner2079fde2001-10-13 06:41:08 +00001276 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001277
Chris Lattner394f2eb2003-10-16 20:12:13 +00001278 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001279
Chris Lattner2079fde2001-10-13 06:41:08 +00001280 // If this is an initializer for a constant pointer, which is referencing a
1281 // (currently) undefined variable, create a stub now that shall be replaced
1282 // in the future with the right type of variable.
1283 //
1284 if (V == 0) {
1285 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1286 const PointerType *PT = cast<PointerType>(Ty);
1287
1288 // First check to see if the forward references value is already created!
1289 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001290 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001291
1292 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001293 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001294 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001295 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001296 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001297 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001298
Reid Spencer3271ed52004-07-18 00:08:11 +00001299 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001300 GlobalValue *GV;
1301 if (const FunctionType *FTy =
1302 dyn_cast<FunctionType>(PT->getElementType())) {
1303 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1304 CurModule.CurrentModule);
1305 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001306 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001307 GlobalValue::ExternalLinkage, 0,
1308 Name, CurModule.CurrentModule);
1309 }
Chris Lattner8a327842004-07-14 21:44:00 +00001310
Reid Spencer3271ed52004-07-18 00:08:11 +00001311 // Keep track of the fact that we have a forward ref to recycle it
1312 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1313 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001314 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001315 }
1316
Reid Spencer3271ed52004-07-18 00:08:11 +00001317 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001318 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001319 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001320 | Types ConstExpr {
1321 if ($1->get() != $2->getType())
1322 ThrowException("Mismatched types for constant expression!");
1323 $$ = $2;
1324 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001325 }
1326 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001327 const Type *Ty = $1->get();
1328 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1329 ThrowException("Cannot create a null initialized value of this type!");
1330 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001331 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001332 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001333
Chris Lattnere43f40b2002-10-09 00:25:32 +00001334ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001335 if (!ConstantSInt::isValueValidForType($1, $2))
1336 ThrowException("Constant value doesn't fit in type!");
1337 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001338 }
1339 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001340 if (!ConstantUInt::isValueValidForType($1, $2))
1341 ThrowException("Constant value doesn't fit in type!");
1342 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001343 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001344 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001345 $$ = ConstantBool::True;
1346 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001347 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001348 $$ = ConstantBool::False;
1349 }
1350 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001351 if (!ConstantFP::isValueValidForType($1, $2))
1352 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001353 $$ = ConstantFP::get($1, $2);
1354 };
1355
Chris Lattner00950542001-06-06 20:29:01 +00001356
Chris Lattnerd78700d2002-08-16 21:14:40 +00001357ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001358 if (!$3->getType()->isFirstClassType())
1359 ThrowException("cast constant expression from a non-primitive type: '" +
1360 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001361 if (!$5->get()->isFirstClassType())
1362 ThrowException("cast constant expression to a non-primitive type: '" +
1363 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001364 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001365 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001366 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001367 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1368 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001369 ThrowException("GetElementPtr requires a pointer operand!");
1370
Chris Lattner830b6f92004-04-05 01:30:04 +00001371 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1372 // indices to uint struct indices for compatibility.
1373 generic_gep_type_iterator<std::vector<Value*>::iterator>
1374 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1375 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1376 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1377 if (isa<StructType>(*GTI)) // Only change struct indices
1378 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1379 if (CUI->getType() == Type::UByteTy)
1380 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1381
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001382 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001383 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001384 if (!IdxTy)
1385 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001386
Chris Lattner9232b992003-04-22 18:42:41 +00001387 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001388 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1389 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001390 IdxVec.push_back(C);
1391 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001392 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001393
Chris Lattnerd78700d2002-08-16 21:14:40 +00001394 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001395
Chris Lattnerd78700d2002-08-16 21:14:40 +00001396 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001397 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001398 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1399 if ($3->getType() != Type::BoolTy)
1400 ThrowException("Select condition must be of boolean type!");
1401 if ($5->getType() != $7->getType())
1402 ThrowException("Select operand types must match!");
1403 $$ = ConstantExpr::getSelect($3, $5, $7);
1404 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001405 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001406 if ($3->getType() != $5->getType())
1407 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001408 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1409 // To retain backward compatibility with these early compilers, we emit a
1410 // cast to the appropriate integer type automatically if we are in the
1411 // broken case. See PR424 for more information.
1412 if (!isa<PointerType>($3->getType())) {
1413 $$ = ConstantExpr::get($1, $3, $5);
1414 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001415 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001416 switch (CurModule.CurrentModule->getPointerSize()) {
1417 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1418 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1419 default: ThrowException("invalid pointer binary constant expr!");
1420 }
1421 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1422 ConstantExpr::getCast($5, IntPtrTy));
1423 $$ = ConstantExpr::getCast($$, $3->getType());
1424 }
1425 }
1426 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1427 if ($3->getType() != $5->getType())
1428 ThrowException("Logical operator types must match!");
1429 if (!$3->getType()->isIntegral())
1430 ThrowException("Logical operands must have integral types!");
1431 $$ = ConstantExpr::get($1, $3, $5);
1432 }
1433 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1434 if ($3->getType() != $5->getType())
1435 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001436 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001437 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001438 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001439 if ($5->getType() != Type::UByteTy)
1440 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001441 if (!$3->getType()->isInteger())
1442 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001443 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001444 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001445
1446
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001447// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001448ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001449 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001450 }
1451 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001452 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001453 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001454 };
Chris Lattner00950542001-06-06 20:29:01 +00001455
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001456
Chris Lattner1781aca2001-09-18 04:00:54 +00001457// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001458GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001459
Chris Lattner00950542001-06-06 20:29:01 +00001460
Chris Lattner0e73ce62002-05-02 19:11:13 +00001461//===----------------------------------------------------------------------===//
1462// Rules to match Modules
1463//===----------------------------------------------------------------------===//
1464
1465// Module rule: Capture the result of parsing the whole file into a result
1466// variable...
1467//
1468Module : FunctionList {
1469 $$ = ParserResult = $1;
1470 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001471};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001472
Chris Lattner7e708292002-06-25 16:13:24 +00001473// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001474//
1475FunctionList : FunctionList Function {
1476 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001477 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001478 }
1479 | FunctionList FunctionProto {
1480 $$ = $1;
1481 }
1482 | FunctionList IMPLEMENTATION {
1483 $$ = $1;
1484 }
1485 | ConstPool {
1486 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001487 // Emit an error if there are any unresolved types left.
1488 if (!CurModule.LateResolveTypes.empty()) {
1489 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1490 if (DID.Type == ValID::NameVal)
1491 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1492 else
1493 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1494 }
Chris Lattner51727be2002-06-04 21:58:56 +00001495 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001496
Chris Lattnere98dda62001-07-14 06:10:16 +00001497// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001498ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001499 // Eagerly resolve types. This is not an optimization, this is a
1500 // requirement that is due to the fact that we could have this:
1501 //
1502 // %list = type { %list * }
1503 // %list = type { %list * } ; repeated type decl
1504 //
1505 // If types are not resolved eagerly, then the two types will not be
1506 // determined to be the same type!
1507 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001508 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001509
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001510 if (!setTypeName(*$4, $2) && !$2) {
1511 // If this is a named type that is not a redefinition, add it to the slot
1512 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001513 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001514 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001515
1516 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001517 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001518 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001519 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001520 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001521 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1522 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Chris Lattner1781aca2001-09-18 04:00:54 +00001523 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001524 | ConstPool OptAssign EXTERNAL GlobalType Types {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001525 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001526 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001527 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001528 | ConstPool TARGET TargetDefinition {
1529 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001530 | ConstPool DEPLIBS '=' LibrariesDefinition {
1531 }
Chris Lattner00950542001-06-06 20:29:01 +00001532 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001533 };
Chris Lattner00950542001-06-06 20:29:01 +00001534
1535
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001536
1537BigOrLittle : BIG { $$ = Module::BigEndian; };
1538BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1539
1540TargetDefinition : ENDIAN '=' BigOrLittle {
1541 CurModule.CurrentModule->setEndianness($3);
1542 }
1543 | POINTERSIZE '=' EUINT64VAL {
1544 if ($3 == 32)
1545 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1546 else if ($3 == 64)
1547 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1548 else
1549 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001550 }
1551 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001552 CurModule.CurrentModule->setTargetTriple($3);
1553 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001554 };
1555
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001556LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001557
1558LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001559 CurModule.CurrentModule->addLibrary($3);
1560 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001561 }
1562 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001563 CurModule.CurrentModule->addLibrary($1);
1564 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001565 }
1566 | /* empty: end of list */ {
1567 }
1568 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001569
Chris Lattner00950542001-06-06 20:29:01 +00001570//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001571// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001572//===----------------------------------------------------------------------===//
1573
Chris Lattner6c23f572003-08-22 05:42:10 +00001574Name : VAR_ID | STRINGCONSTANT;
1575OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001576
Chris Lattner6c23f572003-08-22 05:42:10 +00001577ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001578 if (*$1 == Type::VoidTy)
1579 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001580 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001581};
Chris Lattner00950542001-06-06 20:29:01 +00001582
Chris Lattner69da5cf2002-10-13 20:57:00 +00001583ArgListH : ArgListH ',' ArgVal {
1584 $$ = $1;
1585 $1->push_back(*$3);
1586 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001587 }
1588 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001589 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001590 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001591 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001592 };
Chris Lattner00950542001-06-06 20:29:01 +00001593
1594ArgList : ArgListH {
1595 $$ = $1;
1596 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001597 | ArgListH ',' DOTDOTDOT {
1598 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001599 $$->push_back(std::pair<PATypeHolder*,
1600 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001601 }
1602 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001603 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1604 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001605 }
Chris Lattner00950542001-06-06 20:29:01 +00001606 | /* empty */ {
1607 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001608 };
Chris Lattner00950542001-06-06 20:29:01 +00001609
Chris Lattnera8e8f162005-05-06 20:27:19 +00001610FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')' {
1611 UnEscapeLexed($3);
1612 std::string FunctionName($3);
1613 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001614
Chris Lattnera8e8f162005-05-06 20:27:19 +00001615 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001616 ThrowException("LLVM functions cannot return aggregate types!");
1617
Chris Lattner9232b992003-04-22 18:42:41 +00001618 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001619 if ($5) { // If there are arguments...
1620 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1621 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001622 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001623 }
Chris Lattner00950542001-06-06 20:29:01 +00001624
Chris Lattner2079fde2001-10-13 06:41:08 +00001625 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1626 if (isVarArg) ParamTypeList.pop_back();
1627
Chris Lattnera8e8f162005-05-06 20:27:19 +00001628 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001629 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001630 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001631
Chris Lattnera09000d2004-07-14 23:03:46 +00001632 ValID ID;
1633 if (!FunctionName.empty()) {
1634 ID = ValID::create((char*)FunctionName.c_str());
1635 } else {
1636 ID = ValID::create((int)CurModule.Values[PFT].size());
1637 }
1638
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001639 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001640 // See if this function was forward referenced. If so, recycle the object.
1641 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1642 // Move the function to the end of the list, from whereever it was
1643 // previously inserted.
1644 Fn = cast<Function>(FWRef);
1645 CurModule.CurrentModule->getFunctionList().remove(Fn);
1646 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1647 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1648 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1649 // If this is the case, either we need to be a forward decl, or it needs
1650 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001651 if (!CurFun.isDeclare && !Fn->isExternal())
1652 ThrowException("Redefinition of function '" + FunctionName + "'!");
1653
Chris Lattnera09000d2004-07-14 23:03:46 +00001654 // Make sure to strip off any argument names so we can't get conflicts.
1655 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001656 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001657 AI != AE; ++AI)
1658 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001659
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001660 } else { // Not already defined?
1661 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1662 CurModule.CurrentModule);
1663 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001664 }
Chris Lattner00950542001-06-06 20:29:01 +00001665
Chris Lattner394f2eb2003-10-16 20:12:13 +00001666 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001667 Fn->setCallingConv($1);
Chris Lattner00950542001-06-06 20:29:01 +00001668
Chris Lattner7e708292002-06-25 16:13:24 +00001669 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001670 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001671 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001672 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001673 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001674 delete $5->back().first;
1675 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001676 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001677 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001678 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1679 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001680 delete I->first; // Delete the typeholder...
1681
Chris Lattner8ab406d2004-07-13 07:59:27 +00001682 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001683 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001684 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001685
Chris Lattnera8e8f162005-05-06 20:27:19 +00001686 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001687 }
Chris Lattner51727be2002-06-04 21:58:56 +00001688};
Chris Lattner00950542001-06-06 20:29:01 +00001689
Chris Lattner9b02cc32002-05-03 18:23:48 +00001690BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1691
Chris Lattner4ad02e72003-04-16 20:28:45 +00001692FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001693 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001694
Chris Lattner4ad02e72003-04-16 20:28:45 +00001695 // Make sure that we keep track of the linkage type even if there was a
1696 // previous "declare".
1697 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001698};
Chris Lattner00950542001-06-06 20:29:01 +00001699
Chris Lattner9b02cc32002-05-03 18:23:48 +00001700END : ENDTOK | '}'; // Allow end of '}' to end a function
1701
Chris Lattner79df7c02002-03-26 18:01:55 +00001702Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001703 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001704};
Chris Lattner00950542001-06-06 20:29:01 +00001705
Chris Lattner394f2eb2003-10-16 20:12:13 +00001706FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1707 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001708 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001709};
Chris Lattner00950542001-06-06 20:29:01 +00001710
1711//===----------------------------------------------------------------------===//
1712// Rules to match Basic Blocks
1713//===----------------------------------------------------------------------===//
1714
1715ConstValueRef : ESINT64VAL { // A reference to a direct constant
1716 $$ = ValID::create($1);
1717 }
1718 | EUINT64VAL {
1719 $$ = ValID::create($1);
1720 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001721 | FPVAL { // Perhaps it's an FP constant?
1722 $$ = ValID::create($1);
1723 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001724 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001725 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001726 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001727 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001728 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001729 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001730 | NULL_TOK {
1731 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001732 }
Chris Lattner16710e92004-10-16 18:17:13 +00001733 | UNDEF {
1734 $$ = ValID::createUndef();
1735 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001736 | '<' ConstVector '>' { // Nonempty unsized packed vector
1737 const Type *ETy = (*$2)[0]->getType();
1738 int NumElements = $2->size();
1739
1740 PackedType* pt = PackedType::get(ETy, NumElements);
1741 PATypeHolder* PTy = new PATypeHolder(
1742 HandleUpRefs(
1743 PackedType::get(
1744 ETy,
1745 NumElements)
1746 )
1747 );
1748
1749 // Verify all elements are correct type!
1750 for (unsigned i = 0; i < $2->size(); i++) {
1751 if (ETy != (*$2)[i]->getType())
1752 ThrowException("Element #" + utostr(i) + " is not of type '" +
1753 ETy->getDescription() +"' as required!\nIt is of type '" +
1754 (*$2)[i]->getType()->getDescription() + "'.");
1755 }
1756
1757 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1758 delete PTy; delete $2;
1759 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001760 | ConstExpr {
1761 $$ = ValID::create($1);
1762 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001763
Chris Lattner2079fde2001-10-13 06:41:08 +00001764// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1765// another value.
1766//
1767SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001768 $$ = ValID::create($1);
1769 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001770 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001771 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001772 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001773
1774// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001775ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001776
Chris Lattner00950542001-06-06 20:29:01 +00001777
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001778// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1779// type immediately preceeds the value reference, and allows complex constant
1780// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001781ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001782 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001783 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001784
Chris Lattner00950542001-06-06 20:29:01 +00001785BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001786 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001787 }
Chris Lattner7e708292002-06-25 16:13:24 +00001788 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001789 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001790 };
Chris Lattner00950542001-06-06 20:29:01 +00001791
1792
1793// Basic blocks are terminated by branching instructions:
1794// br, br/cc, switch, ret
1795//
Chris Lattner2079fde2001-10-13 06:41:08 +00001796BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001797 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001798 InsertValue($3);
1799
1800 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001801 InsertValue($1);
1802 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001803 };
Chris Lattner00950542001-06-06 20:29:01 +00001804
1805InstructionList : InstructionList Inst {
1806 $1->getInstList().push_back($2);
1807 $$ = $1;
1808 }
1809 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001810 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001811
1812 // Make sure to move the basic block to the correct location in the
1813 // function, instead of leaving it inserted wherever it was first
1814 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001815 Function::BasicBlockListType &BBL =
1816 CurFun.CurrentFunction->getBasicBlockList();
1817 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner22ae2322004-07-13 08:39:15 +00001818 }
1819 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001820 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001821
1822 // Make sure to move the basic block to the correct location in the
1823 // function, instead of leaving it inserted wherever it was first
1824 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001825 Function::BasicBlockListType &BBL =
1826 CurFun.CurrentFunction->getBasicBlockList();
1827 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner51727be2002-06-04 21:58:56 +00001828 };
Chris Lattner00950542001-06-06 20:29:01 +00001829
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001830BBTerminatorInst : RET ResolvedVal { // Return with a result...
1831 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001832 }
1833 | RET VOID { // Return with no result...
1834 $$ = new ReturnInst();
1835 }
1836 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001837 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001838 } // Conditional Branch...
1839 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001840 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001841 }
1842 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001843 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001844 $$ = S;
1845
Chris Lattner9232b992003-04-22 18:42:41 +00001846 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001847 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001848 for (; I != E; ++I) {
1849 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
1850 S->addCase(CI, I->second);
1851 else
1852 ThrowException("Switch case is constant, but not a simple integer!");
1853 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001854 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001855 }
Chris Lattner196850c2003-05-15 21:30:00 +00001856 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001857 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00001858 $$ = S;
1859 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001860 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1861 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001862 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001863 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001864
Chris Lattnera8e8f162005-05-06 20:27:19 +00001865 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001866 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001867 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001868 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001869 if ($6) {
1870 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00001871 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001872 ParamTypes.push_back((*I)->getType());
1873 }
1874
1875 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1876 if (isVarArg) ParamTypes.pop_back();
1877
Chris Lattnera8e8f162005-05-06 20:27:19 +00001878 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001879 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001880 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001881
Chris Lattnera8e8f162005-05-06 20:27:19 +00001882 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001883
Chris Lattnera8e8f162005-05-06 20:27:19 +00001884 BasicBlock *Normal = getBBVal($10);
1885 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00001886
1887 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001888 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001889 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001890 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001891 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001892 // correctly!
1893 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001894 FunctionType::param_iterator I = Ty->param_begin();
1895 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001896 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001897
1898 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001899 if ((*ArgI)->getType() != *I)
1900 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1901 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001902
1903 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001904 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001905
Chris Lattnera8e8f162005-05-06 20:27:19 +00001906 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00001907 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001908 cast<InvokeInst>($$)->setCallingConv($2);
1909
1910 delete $3;
1911 delete $6;
Chris Lattner36143fc2003-09-08 18:54:55 +00001912 }
1913 | UNWIND {
1914 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00001915 }
1916 | UNREACHABLE {
1917 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001918 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001919
1920
Chris Lattner00950542001-06-06 20:29:01 +00001921
1922JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1923 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001924 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001925 if (V == 0)
1926 ThrowException("May only switch on a constant pool value!");
1927
Chris Lattner64116f92004-07-14 01:33:11 +00001928 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001929 }
1930 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001931 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001932 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001933
1934 if (V == 0)
1935 ThrowException("May only switch on a constant pool value!");
1936
Chris Lattner64116f92004-07-14 01:33:11 +00001937 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001938 };
Chris Lattner00950542001-06-06 20:29:01 +00001939
1940Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001941 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001942 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001943 InsertValue($2);
1944 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001945};
Chris Lattner00950542001-06-06 20:29:01 +00001946
Chris Lattnerc24d2082001-06-11 15:04:20 +00001947PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001948 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001949 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001950 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001951 }
1952 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1953 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001954 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001955 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001956 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001957
1958
Chris Lattner30c89792001-09-07 16:35:17 +00001959ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001960 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001961 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001962 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001963 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001964 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001965 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001966 };
Chris Lattner00950542001-06-06 20:29:01 +00001967
1968// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001969ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001970
Chris Lattnerddb6db42005-05-06 05:51:46 +00001971OptTailCall : TAIL CALL {
1972 $$ = true;
1973 }
1974 | CALL {
1975 $$ = false;
1976 };
1977
1978
1979
Chris Lattner4a6482b2002-09-10 19:57:26 +00001980InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00001981 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
1982 !isa<PackedType>((*$2).get()))
1983 ThrowException(
1984 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00001985 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
1986 ThrowException("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00001987 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1988 if ($$ == 0)
1989 ThrowException("binary operator returned null!");
1990 delete $2;
1991 }
1992 | LogicalOps Types ValueRef ',' ValueRef {
1993 if (!(*$2)->isIntegral())
1994 ThrowException("Logical operator requires integral operands!");
1995 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1996 if ($$ == 0)
1997 ThrowException("binary operator returned null!");
1998 delete $2;
1999 }
2000 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002001 if(isa<PackedType>((*$2).get())) {
2002 ThrowException(
2003 "PackedTypes currently not supported in setcc instructions!");
2004 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00002005 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00002006 if ($$ == 0)
2007 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002008 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002009 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002010 | NOT ResolvedVal {
2011 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2012 << " Replacing with 'xor'.\n";
2013
2014 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2015 if (Ones == 0)
2016 ThrowException("Expected integral type for not instruction!");
2017
2018 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002019 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00002020 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00002021 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002022 | ShiftOps ResolvedVal ',' ResolvedVal {
2023 if ($4->getType() != Type::UByteTy)
2024 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002025 if (!$2->getType()->isInteger())
2026 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002027 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00002028 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002029 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002030 if (!$4->get()->isFirstClassType())
2031 ThrowException("cast instruction to a non-primitive type: '" +
2032 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002033 $$ = new CastInst($2, *$4);
2034 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002035 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002036 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2037 if ($2->getType() != Type::BoolTy)
2038 ThrowException("select condition must be boolean!");
2039 if ($4->getType() != $6->getType())
2040 ThrowException("select value types should match!");
2041 $$ = new SelectInst($2, $4, $6);
2042 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002043 | VAARG ResolvedVal ',' Types {
2044 $$ = new VAArgInst($2, *$4);
2045 delete $4;
2046 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002047 | VAARG_old ResolvedVal ',' Types {
2048 ObsoleteVarArgs = true;
2049 const Type* ArgTy = $2->getType();
2050 Function* NF = CurModule.CurrentModule->
2051 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, 0);
2052
2053 //b = vaarg a, t ->
2054 //foo = alloca 1 of t
2055 //bar = vacopy a
2056 //store bar -> foo
2057 //b = vaarg foo, t
2058 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2059 CurBB->getInstList().push_back(foo);
2060 CallInst* bar = new CallInst(NF, $2);
2061 CurBB->getInstList().push_back(bar);
2062 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2063 $$ = new VAArgInst(foo, *$4);
2064 delete $4;
2065 }
2066 | VANEXT_old ResolvedVal ',' Types {
2067 ObsoleteVarArgs = true;
2068 const Type* ArgTy = $2->getType();
2069 Function* NF = CurModule.CurrentModule->
2070 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, 0);
2071
2072 //b = vanext a, t ->
2073 //foo = alloca 1 of t
2074 //bar = vacopy a
2075 //store bar -> foo
2076 //tmp = vaarg foo, t
2077 //b = load foo
2078 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2079 CurBB->getInstList().push_back(foo);
2080 CallInst* bar = new CallInst(NF, $2);
2081 CurBB->getInstList().push_back(bar);
2082 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2083 Instruction* tmp = new VAArgInst(foo, *$4);
2084 CurBB->getInstList().push_back(tmp);
2085 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002086 delete $4;
2087 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002088 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002089 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002090 if (!Ty->isFirstClassType())
2091 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002092 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002093 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002094 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002095 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00002096 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002097 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002098 $2->pop_front();
2099 }
2100 delete $2; // Free the list...
Chris Lattnerddb6db42005-05-06 05:51:46 +00002101 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002102 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002103 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002104 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002105
Chris Lattnera8e8f162005-05-06 20:27:19 +00002106 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002107 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002108 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002109 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002110 if ($6) {
2111 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002112 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002113 ParamTypes.push_back((*I)->getType());
2114 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002115
2116 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2117 if (isVarArg) ParamTypes.pop_back();
2118
Chris Lattnera8e8f162005-05-06 20:27:19 +00002119 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Chris Lattner595f06c2005-02-01 01:47:42 +00002120 ThrowException("LLVM functions cannot return aggregate types!");
2121
Chris Lattnera8e8f162005-05-06 20:27:19 +00002122 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002123 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002124 }
Chris Lattner00950542001-06-06 20:29:01 +00002125
Chris Lattnera8e8f162005-05-06 20:27:19 +00002126 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002127
Chris Lattner8b81bf52001-07-25 22:47:46 +00002128 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002129 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002130 // Make sure no arguments is a good thing!
2131 if (Ty->getNumParams() != 0)
2132 ThrowException("No arguments passed to a function that "
2133 "expects arguments!");
2134
Chris Lattner9232b992003-04-22 18:42:41 +00002135 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002136 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002137 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002138 // correctly!
2139 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002140 FunctionType::param_iterator I = Ty->param_begin();
2141 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002142 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002143
2144 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002145 if ((*ArgI)->getType() != *I)
2146 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2147 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002148
Chris Lattner8b81bf52001-07-25 22:47:46 +00002149 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002150 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002151
Chris Lattnera8e8f162005-05-06 20:27:19 +00002152 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002153 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002154 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002155 cast<CallInst>($$)->setCallingConv($2);
2156 delete $3;
2157 delete $6;
Chris Lattner00950542001-06-06 20:29:01 +00002158 }
2159 | MemoryInst {
2160 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002161 };
Chris Lattner00950542001-06-06 20:29:01 +00002162
Chris Lattner6cdb0112001-11-26 16:54:11 +00002163
2164// IndexList - List of indices for GEP based instructions...
2165IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002166 $$ = $2;
2167 } | /* empty */ {
2168 $$ = new std::vector<Value*>();
2169 };
2170
2171OptVolatile : VOLATILE {
2172 $$ = true;
2173 }
2174 | /* empty */ {
2175 $$ = false;
2176 };
2177
Chris Lattner027dcc52001-07-08 21:10:27 +00002178
Chris Lattnerddb6db42005-05-06 05:51:46 +00002179
Chris Lattner00950542001-06-06 20:29:01 +00002180MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002181 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002182 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002183 }
2184 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002185 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002186 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002187 }
2188 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002189 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002190 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002191 }
2192 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002193 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002194 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002195 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002196 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002197 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002198 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002199 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002200 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002201 }
2202
Chris Lattner15c9c032003-09-08 18:20:29 +00002203 | OptVolatile LOAD Types ValueRef {
2204 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002205 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002206 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002207 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2208 ThrowException("Can't load from pointer of non-first-class type: " +
2209 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002210 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002211 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002212 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002213 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2214 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002215 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002216 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002217 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002218 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002219 if (ElTy != $3->getType())
2220 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002221 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002222
Chris Lattner79ad13802003-09-08 20:29:46 +00002223 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002224 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002225 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002226 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002227 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002228 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002229
2230 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2231 // indices to uint struct indices for compatibility.
2232 generic_gep_type_iterator<std::vector<Value*>::iterator>
2233 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2234 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2235 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2236 if (isa<StructType>(*GTI)) // Only change struct indices
2237 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2238 if (CUI->getType() == Type::UByteTy)
2239 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2240
Chris Lattner30c89792001-09-07 16:35:17 +00002241 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002242 ThrowException("Invalid getelementptr indices for type '" +
2243 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002244 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2245 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002246 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002247
Brian Gaeked0fde302003-11-11 22:41:34 +00002248
Chris Lattner00950542001-06-06 20:29:01 +00002249%%
Chris Lattner09083092001-07-08 04:57:15 +00002250int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002251 std::string where
2252 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002253 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002254 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002255 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002256 errMsg += "end-of-file.";
2257 else
Chris Lattner9232b992003-04-22 18:42:41 +00002258 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002259 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002260 return 0;
2261}