blob: 549a96d9c61795e72d44b269a74ac543ef7f945c [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"
Chris Lattner87ac9722005-11-06 06:46:28 +000022#include "llvm/Support/MathExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000023#include <algorithm>
24#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000025#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000026#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner386a3b72001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000030int yyparse();
31
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000033 std::string CurFilename;
34}
35using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattner00950542001-06-06 20:29:01 +000037static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000038
Chris Lattner30c89792001-09-07 16:35:17 +000039// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
40// relating to upreferences in the input stream.
41//
42//#define DEBUG_UPREFS 1
43#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000044#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000045#else
46#define UR_OUT(X)
47#endif
48
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000049#define YYERROR_VERBOSE 1
50
Andrew Lenharth558bc882005-06-18 18:34:52 +000051static bool ObsoleteVarArgs;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +000052static bool NewVarArgs;
Chris Lattnerb2b96672005-11-12 18:21:21 +000053static BasicBlock *CurBB;
54static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000055
56
Chris Lattner7e708292002-06-25 16:13:24 +000057// This contains info used when building the body of a function. It is
58// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000059//
Chris Lattner9232b992003-04-22 18:42:41 +000060typedef std::vector<Value *> ValueList; // Numbered defs
Misha Brukman5a2a3822005-05-10 22:02:28 +000061static void
62ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
63 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000064
65static struct PerModuleInfo {
66 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000067 std::map<const Type *, ValueList> Values; // Module level numbered definitions
68 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000069 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000070 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000071
Chris Lattnerbc721ed2004-07-13 08:28:21 +000072 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
73 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000074 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000075 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
76
Chris Lattner2079fde2001-10-13 06:41:08 +000077 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
78 // references to global values. Global values may be referenced before they
79 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000080 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000081 //
Chris Lattner9232b992003-04-22 18:42:41 +000082 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000083 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000084 GlobalRefsType GlobalRefs;
85
Chris Lattner00950542001-06-06 20:29:01 +000086 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000087 // If we could not resolve some functions at function compilation time
88 // (calls to functions before they are defined), resolve them now... Types
89 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000090 //
Chris Lattner00950542001-06-06 20:29:01 +000091 ResolveDefinitions(LateResolveValues);
92
Chris Lattner2079fde2001-10-13 06:41:08 +000093 // Check to make sure that all global value forward references have been
94 // resolved!
95 //
96 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000097 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +000098
Chris Lattner749ce032002-03-11 22:12:39 +000099 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
100 I != E; ++I) {
101 UndefinedReferences += " " + I->first.first->getDescription() + " " +
102 I->first.second.getName() + "\n";
103 }
104 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000105 }
106
Chris Lattner7e708292002-06-25 16:13:24 +0000107 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000108 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000109 CurrentModule = 0;
110 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000111
112
Chris Lattner8a327842004-07-14 21:44:00 +0000113 // GetForwardRefForGlobal - Check to see if there is a forward reference
114 // for this global. If so, remove it from the GlobalRefs map and return it.
115 // If not, just return null.
116 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
117 // Check to see if there is a forward reference to this global variable...
118 // if there is, eliminate it and patch the reference to use the new def'n.
119 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
120 GlobalValue *Ret = 0;
121 if (I != GlobalRefs.end()) {
122 Ret = I->second;
123 GlobalRefs.erase(I);
124 }
125 return Ret;
126 }
Chris Lattner00950542001-06-06 20:29:01 +0000127} CurModule;
128
Chris Lattner79df7c02002-03-26 18:01:55 +0000129static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000130 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000131
Chris Lattner735f2702004-07-08 22:30:50 +0000132 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
133 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000134 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000135
Chris Lattner2e2ed292004-07-14 06:28:35 +0000136 /// BBForwardRefs - When we see forward references to basic blocks, keep
137 /// track of them here.
138 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
139 std::vector<BasicBlock*> NumberedBlocks;
140 unsigned NextBBNum;
141
Chris Lattner79df7c02002-03-26 18:01:55 +0000142 inline PerFunctionInfo() {
143 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000144 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000145 }
146
Chris Lattner79df7c02002-03-26 18:01:55 +0000147 inline void FunctionStart(Function *M) {
148 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000149 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000150 }
151
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000153 NumberedBlocks.clear();
154
155 // Any forward referenced blocks left?
156 if (!BBForwardRefs.empty())
157 ThrowException("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000158 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000159
160 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000161 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000162
Chris Lattner7e708292002-06-25 16:13:24 +0000163 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000164 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000165 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000166 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000167} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000168
Chris Lattner394f2eb2003-10-16 20:12:13 +0000169static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000170
Chris Lattner00950542001-06-06 20:29:01 +0000171
172//===----------------------------------------------------------------------===//
173// Code to handle definitions of all the types
174//===----------------------------------------------------------------------===//
175
Chris Lattner735f2702004-07-08 22:30:50 +0000176static int InsertValue(Value *V,
177 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
178 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000179
180 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000181 ValueList &List = ValueTab[V->getType()];
182 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000183 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000184}
185
Chris Lattner30c89792001-09-07 16:35:17 +0000186static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000187 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000188 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000189 // Module constants occupy the lowest numbered slots...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000190 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000191 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000192 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000193 case ValID::NameVal: // Is it a named definition?
194 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
195 D.destroy(); // Free old strdup'd memory...
196 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000197 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000198 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000199 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000200 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000201 }
202
203 // If we reached here, we referenced either a symbol that we don't know about
204 // or an id number that hasn't been read yet. We may be referencing something
205 // forward, so just create an entry to be resolved later and get to it...
206 //
207 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
208
Chris Lattner355ad1f2005-03-21 06:27:42 +0000209
210 if (inFunctionScope()) {
211 if (D.Type == ValID::NameVal)
212 ThrowException("Reference to an undefined type: '" + D.getName() + "'");
213 else
214 ThrowException("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000215 }
Chris Lattner30c89792001-09-07 16:35:17 +0000216
Chris Lattner355ad1f2005-03-21 06:27:42 +0000217 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
218 if (I != CurModule.LateResolveTypes.end())
219 return I->second;
220
Chris Lattner82269592001-10-22 06:01:08 +0000221 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000222 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000223 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000224 }
Chris Lattner30c89792001-09-07 16:35:17 +0000225
Chris Lattner9232b992003-04-22 18:42:41 +0000226static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000227 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000228 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000229 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000230 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000231}
232
Chris Lattner2079fde2001-10-13 06:41:08 +0000233// getValNonImprovising - Look up the value specified by the provided type and
234// the provided ValID. If the value exists and has already been defined, return
235// it. Otherwise return null.
236//
237static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000238 if (isa<FunctionType>(Ty))
239 ThrowException("Functions are not values and "
240 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000241
Chris Lattner30c89792001-09-07 16:35:17 +0000242 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000243 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000244 unsigned Num = (unsigned)D.Num;
245
246 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000247 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000248 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000249 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000250 return VI->second[Num];
251 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000252 }
253
254 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000255 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000256 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000257
258 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000259 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000260
Chris Lattner16af11d2004-02-09 21:03:38 +0000261 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000262 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000263
Chris Lattner1a1cb112001-09-30 22:46:54 +0000264 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000265 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000266 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000267
268 D.destroy(); // Free old strdup'd memory...
269 return N;
270 }
271
Misha Brukman5a2a3822005-05-10 22:02:28 +0000272 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000273 // value will fit into the specified type...
274 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000275 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
276 ThrowException("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000277 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000278 Ty->getDescription() + "'!");
279 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000280
281 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000282 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
283 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000284 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000285 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000286 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000287 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000288 }
289 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000290 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000291 }
292
Chris Lattner2079fde2001-10-13 06:41:08 +0000293 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000294 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000295 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000296 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000297
Chris Lattner2079fde2001-10-13 06:41:08 +0000298 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000299 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000300 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000301 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000302
Chris Lattner16710e92004-10-16 18:17:13 +0000303 case ValID::ConstUndefVal: // Is it an undef value?
304 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000305
Chris Lattnerd78700d2002-08-16 21:14:40 +0000306 case ValID::ConstantVal: // Fully resolved constant?
307 if (D.ConstantValue->getType() != Ty)
308 ThrowException("Constant expression type different from required type!");
309 return D.ConstantValue;
310
Chris Lattner30c89792001-09-07 16:35:17 +0000311 default:
312 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000313 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000314 } // End of switch
315
Chris Lattner2079fde2001-10-13 06:41:08 +0000316 assert(0 && "Unhandled case!");
317 return 0;
318}
319
Chris Lattner2079fde2001-10-13 06:41:08 +0000320// getVal - This function is identical to getValNonImprovising, except that if a
321// value is not already defined, it "improvises" by creating a placeholder var
322// that looks and acts just like the requested variable. When the value is
323// defined later, all uses of the placeholder variable are replaced with the
324// real thing.
325//
Chris Lattner64116f92004-07-14 01:33:11 +0000326static Value *getVal(const Type *Ty, const ValID &ID) {
327 if (Ty == Type::LabelTy)
328 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000329
Chris Lattner64116f92004-07-14 01:33:11 +0000330 // See if the value has already been defined.
331 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000332 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000333
Chris Lattner60cd9552005-03-23 01:29:26 +0000334 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
335 ThrowException("Invalid use of a composite type!");
336
Chris Lattner00950542001-06-06 20:29:01 +0000337 // If we reached here, we referenced either a symbol that we don't know about
338 // or an id number that hasn't been read yet. We may be referencing something
339 // forward, so just create an entry to be resolved later and get to it...
340 //
Chris Lattner64116f92004-07-14 01:33:11 +0000341 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000342
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000343 // Remember where this forward reference came from. FIXME, shouldn't we try
344 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000345 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000346 llvmAsmlineno)));
347
Chris Lattner79df7c02002-03-26 18:01:55 +0000348 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000349 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000350 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000351 InsertValue(V, CurModule.LateResolveValues);
352 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000353}
354
Chris Lattner2e2ed292004-07-14 06:28:35 +0000355/// getBBVal - This is used for two purposes:
356/// * If isDefinition is true, a new basic block with the specified ID is being
357/// defined.
358/// * If isDefinition is true, this is a reference to a basic block, which may
359/// or may not be a forward reference.
360///
361static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000362 assert(inFunctionScope() && "Can't get basic block at global scope!");
363
Chris Lattner2e2ed292004-07-14 06:28:35 +0000364 std::string Name;
365 BasicBlock *BB = 0;
366 switch (ID.Type) {
367 default: ThrowException("Illegal label reference " + ID.getName());
368 case ValID::NumberVal: // Is it a numbered definition?
369 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
370 CurFun.NumberedBlocks.resize(ID.Num+1);
371 BB = CurFun.NumberedBlocks[ID.Num];
372 break;
373 case ValID::NameVal: // Is it a named definition?
374 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000375 if (Value *N = CurFun.CurrentFunction->
376 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000377 BB = cast<BasicBlock>(N);
378 break;
379 }
Chris Lattner64116f92004-07-14 01:33:11 +0000380
Chris Lattner2e2ed292004-07-14 06:28:35 +0000381 // See if the block has already been defined.
382 if (BB) {
383 // If this is the definition of the block, make sure the existing value was
384 // just a forward reference. If it was a forward reference, there will be
385 // an entry for it in the PlaceHolderInfo map.
386 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
387 // The existing value was a definition, not a forward reference.
388 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000389
Chris Lattner2e2ed292004-07-14 06:28:35 +0000390 ID.destroy(); // Free strdup'd memory.
391 return BB;
392 }
393
394 // Otherwise this block has not been seen before.
395 BB = new BasicBlock("", CurFun.CurrentFunction);
396 if (ID.Type == ValID::NameVal) {
397 BB->setName(ID.Name);
398 } else {
399 CurFun.NumberedBlocks[ID.Num] = BB;
400 }
401
402 // If this is not a definition, keep track of it so we can use it as a forward
403 // reference.
404 if (!isDefinition) {
405 // Remember where this forward reference came from.
406 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
407 } else {
408 // The forward declaration could have been inserted anywhere in the
409 // function: insert it into the correct place now.
410 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
411 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
412 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000413 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000414 return BB;
415}
416
Chris Lattner00950542001-06-06 20:29:01 +0000417
418//===----------------------------------------------------------------------===//
419// Code to handle forward references in instructions
420//===----------------------------------------------------------------------===//
421//
422// This code handles the late binding needed with statements that reference
423// values not defined yet... for example, a forward branch, or the PHI node for
424// a loop body.
425//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000426// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000427// and back patchs after we are done.
428//
429
Misha Brukman5a2a3822005-05-10 22:02:28 +0000430// ResolveDefinitions - If we could not resolve some defs at parsing
431// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000432// defs now...
433//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000434static void
435ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
436 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000437 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000438 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000439 E = LateResolvers.end(); LRI != E; ++LRI) {
440 ValueList &List = LRI->second;
441 while (!List.empty()) {
442 Value *V = List.back();
443 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000444
445 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
446 CurModule.PlaceHolderInfo.find(V);
447 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
448
449 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000450
Chris Lattner735f2702004-07-08 22:30:50 +0000451 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000452 if (TheRealValue) {
453 V->replaceAllUsesWith(TheRealValue);
454 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000455 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000456 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000457 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000458 // resolver table
459 InsertValue(V, *FutureLateResolvers);
460 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000461 if (DID.Type == ValID::NameVal)
462 ThrowException("Reference to an invalid definition: '" +DID.getName()+
463 "' of type '" + V->getType()->getDescription() + "'",
464 PHI->second.second);
465 else
466 ThrowException("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000467 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000468 V->getType()->getDescription() + "'",
469 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000470 }
Chris Lattner00950542001-06-06 20:29:01 +0000471 }
472 }
473
474 LateResolvers.clear();
475}
476
Chris Lattner4a42e902001-10-22 05:56:09 +0000477// ResolveTypeTo - A brand new type was just declared. This means that (if
478// name is not null) things referencing Name can be resolved. Otherwise, things
479// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000480//
Chris Lattner4a42e902001-10-22 05:56:09 +0000481static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000482 ValID D;
483 if (Name) D = ValID::create(Name);
484 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000485
Chris Lattner355ad1f2005-03-21 06:27:42 +0000486 std::map<ValID, PATypeHolder>::iterator I =
487 CurModule.LateResolveTypes.find(D);
488 if (I != CurModule.LateResolveTypes.end()) {
489 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
490 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000491 }
492}
493
Chris Lattner1781aca2001-09-18 04:00:54 +0000494// setValueName - Set the specified value to the name given. The name may be
495// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000496// assumed to be a malloc'd string buffer, and is free'd by this function.
497//
498static void setValueName(Value *V, char *NameStr) {
499 if (NameStr) {
500 std::string Name(NameStr); // Copy string
501 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000502
Misha Brukman5a2a3822005-05-10 22:02:28 +0000503 if (V->getType() == Type::VoidTy)
Chris Lattnerc9aea522004-07-13 08:12:39 +0000504 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000505
Chris Lattner8ab406d2004-07-13 07:59:27 +0000506 assert(inFunctionScope() && "Must be in function scope!");
507 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
508 if (ST.lookup(V->getType(), Name))
509 ThrowException("Redefinition of value named '" + Name + "' in the '" +
510 V->getType()->getDescription() + "' type plane!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000511
Chris Lattnerc9aea522004-07-13 08:12:39 +0000512 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000513 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000514 }
515}
516
Chris Lattnerd88998d2004-07-14 08:23:52 +0000517/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
518/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000519static GlobalVariable *
520ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
521 bool isConstantGlobal, const Type *Ty,
522 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000523 if (isa<FunctionType>(Ty))
524 ThrowException("Cannot declare global vars of function type!");
525
Misha Brukman5a2a3822005-05-10 22:02:28 +0000526 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000527
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000528 std::string Name;
529 if (NameStr) {
530 Name = NameStr; // Copy string
531 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000532 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000533
Chris Lattner8a327842004-07-14 21:44:00 +0000534 // See if this global value was forward referenced. If so, recycle the
535 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000536 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000537 if (!Name.empty()) {
538 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000539 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000540 ID = ValID::create((int)CurModule.Values[PTy].size());
541 }
542
543 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000544 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000545 // previously inserted.
546 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
547 CurModule.CurrentModule->getGlobalList().remove(GV);
548 CurModule.CurrentModule->getGlobalList().push_back(GV);
549 GV->setInitializer(Initializer);
550 GV->setLinkage(Linkage);
551 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000552 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000553 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000554 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000555
556 // If this global has a name, check to see if there is already a definition
557 // of this global in the module. If so, merge as appropriate. Note that
558 // this is really just a hack around problems in the CFE. :(
559 if (!Name.empty()) {
560 // We are a simple redefinition of a value, check to see if it is defined
561 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000562 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000563 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
564 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000565 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000566 // 2. If both initializers have the same value.
567 //
568 if (!EGV->hasInitializer() || !Initializer ||
569 EGV->getInitializer() == Initializer) {
570
571 // Make sure the existing global version gets the initializer! Make
572 // sure that it also gets marked const if the new version is.
573 if (Initializer && !EGV->hasInitializer())
574 EGV->setInitializer(Initializer);
575 if (isConstantGlobal)
576 EGV->setConstant(true);
577 EGV->setLinkage(Linkage);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000578 return EGV;
Chris Lattnera09000d2004-07-14 23:03:46 +0000579 }
580
Misha Brukman5a2a3822005-05-10 22:02:28 +0000581 ThrowException("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000582 "' in the '" + Ty->getDescription() + "' type plane!");
583 }
584 }
585
586 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000587 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000588 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000589 CurModule.CurrentModule);
590 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000591 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000592}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000593
Reid Spencer75719bf2004-05-26 21:48:31 +0000594// setTypeName - Set the specified type to the name given. The name may be
595// null potentially, in which case this is a noop. The string passed in is
596// assumed to be a malloc'd string buffer, and is freed by this function.
597//
598// This function returns true if the type has already been defined, but is
599// allowed to be redefined in the specified context. If the name is a new name
600// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000601static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000602 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000603 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000604
Reid Spencer75719bf2004-05-26 21:48:31 +0000605 std::string Name(NameStr); // Copy string
606 free(NameStr); // Free old string
607
608 // We don't allow assigning names to void type
Misha Brukman5a2a3822005-05-10 22:02:28 +0000609 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000610 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000611
Chris Lattnera09000d2004-07-14 23:03:46 +0000612 // Set the type name, checking for conflicts as we do so.
613 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000614
Chris Lattnera09000d2004-07-14 23:03:46 +0000615 if (AlreadyExists) { // Inserting a name that is already defined???
616 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
617 assert(Existing && "Conflict but no matching type?");
618
Reid Spencer75719bf2004-05-26 21:48:31 +0000619 // There is only one case where this is allowed: when we are refining an
620 // opaque type. In this case, Existing will be an opaque type.
621 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
622 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000623 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000624 return true;
625 }
626
627 // Otherwise, this is an attempt to redefine a type. That's okay if
628 // the redefinition is identical to the original. This will be so if
629 // Existing and T point to the same Type object. In this one case we
630 // allow the equivalent redefinition.
631 if (Existing == T) return true; // Yes, it's equal.
632
633 // Any other kind of (non-equivalent) redefinition is an error.
634 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000635 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000636 }
637
Reid Spencer75719bf2004-05-26 21:48:31 +0000638 return false;
639}
Chris Lattner8896eda2001-07-09 19:38:36 +0000640
Chris Lattner30c89792001-09-07 16:35:17 +0000641//===----------------------------------------------------------------------===//
642// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000643//
Chris Lattner8896eda2001-07-09 19:38:36 +0000644
Chris Lattner3b073862004-02-09 03:19:29 +0000645// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000646//
647static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000648 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000649 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000650}
651
652namespace {
653 struct UpRefRecord {
654 // NestingLevel - The number of nesting levels that need to be popped before
655 // this type is resolved.
656 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000657
Chris Lattner3b073862004-02-09 03:19:29 +0000658 // LastContainedTy - This is the type at the current binding level for the
659 // type. Every time we reduce the nesting level, this gets updated.
660 const Type *LastContainedTy;
661
662 // UpRefTy - This is the actual opaque type that the upreference is
663 // represented with.
664 OpaqueType *UpRefTy;
665
666 UpRefRecord(unsigned NL, OpaqueType *URTy)
667 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
668 };
Chris Lattner30c89792001-09-07 16:35:17 +0000669}
Chris Lattner698b56e2001-07-20 19:15:08 +0000670
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000671// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000672static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000673
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000674/// HandleUpRefs - Every time we finish a new layer of types, this function is
675/// called. It loops through the UpRefs vector, which is a list of the
676/// currently active types. For each type, if the up reference is contained in
677/// the newly completed type, we decrement the level count. When the level
678/// count reaches zero, the upreferenced type is the type that is passed in:
679/// thus we can complete the cycle.
680///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000681static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000682 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000683 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000684 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000685 "' newly formed. Resolving upreferences.\n" <<
686 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000687
688 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
689 // to zero), we resolve them all together before we resolve them to Ty. At
690 // the end of the loop, if there is anything to resolve to Ty, it will be in
691 // this variable.
692 OpaqueType *TypeToResolve = 0;
693
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000694 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000695 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
696 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000697 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000698 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
699 // Decrement level of upreference
700 unsigned Level = --UpRefs[i].NestingLevel;
701 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000702 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000703 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000704 if (!TypeToResolve) {
705 TypeToResolve = UpRefs[i].UpRefTy;
706 } else {
707 UR_OUT(" * Resolving upreference for "
708 << UpRefs[i].second->getDescription() << "\n";
709 std::string OldName = UpRefs[i].UpRefTy->getDescription());
710 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
711 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
712 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
713 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000714 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000715 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000716 }
717 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000718 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000719
Chris Lattner026a8ce2004-02-09 18:53:54 +0000720 if (TypeToResolve) {
721 UR_OUT(" * Resolving upreference for "
722 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000723 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000724 TypeToResolve->refineAbstractTypeTo(Ty);
725 }
726
Chris Lattner8896eda2001-07-09 19:38:36 +0000727 return Ty;
728}
729
Chris Lattner30c89792001-09-07 16:35:17 +0000730
Chris Lattner6184feb2005-05-20 03:25:47 +0000731// common code from the two 'RunVMAsmParser' functions
732 static Module * RunParser(Module * M) {
733
734 llvmAsmlineno = 1; // Reset the current line number...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000735 ObsoleteVarArgs = false;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000736 NewVarArgs = false;
Chris Lattner6184feb2005-05-20 03:25:47 +0000737
738 CurModule.CurrentModule = M;
739 yyparse(); // Parse the file, potentially throwing exception
740
741 Module *Result = ParserResult;
742 ParserResult = 0;
743
Andrew Lenharth31c98bf2005-06-20 15:41:37 +0000744 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
745 {
746 Function* F;
747 if ((F = Result->getNamedFunction("llvm.va_start"))
748 && F->getFunctionType()->getNumParams() == 0)
749 ObsoleteVarArgs = true;
750 if((F = Result->getNamedFunction("llvm.va_copy"))
751 && F->getFunctionType()->getNumParams() == 1)
752 ObsoleteVarArgs = true;
753 }
754
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000755 if (ObsoleteVarArgs && NewVarArgs)
Chris Lattner548021f2005-06-24 18:00:40 +0000756 ThrowException("This file is corrupt: it uses both new and old style varargs");
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000757
Andrew Lenharth558bc882005-06-18 18:34:52 +0000758 if(ObsoleteVarArgs) {
759 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000760 if (F->arg_size() != 0)
761 ThrowException("Obsolete va_start takes 0 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000762
763 //foo = va_start()
764 // ->
765 //bar = alloca typeof(foo)
766 //va_start(bar)
767 //foo = load bar
768
769 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
770 const Type* ArgTy = F->getFunctionType()->getReturnType();
771 const Type* ArgTyPtr = PointerType::get(ArgTy);
772 Function* NF = Result->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000773 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000774
775 while (!F->use_empty()) {
776 CallInst* CI = cast<CallInst>(F->use_back());
777 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
778 new CallInst(NF, bar, "", CI);
779 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
780 CI->replaceAllUsesWith(foo);
781 CI->getParent()->getInstList().erase(CI);
782 }
783 Result->getFunctionList().erase(F);
784 }
785
786 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000787 if(F->arg_size() != 1)
788 ThrowException("Obsolete va_end takes 1 argument!");
789
Andrew Lenharth558bc882005-06-18 18:34:52 +0000790 //vaend foo
791 // ->
792 //bar = alloca 1 of typeof(foo)
793 //vaend bar
794 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
795 const Type* ArgTy = F->getFunctionType()->getParamType(0);
796 const Type* ArgTyPtr = PointerType::get(ArgTy);
797 Function* NF = Result->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000798 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000799
800 while (!F->use_empty()) {
801 CallInst* CI = cast<CallInst>(F->use_back());
802 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000803 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000804 new CallInst(NF, bar, "", CI);
805 CI->getParent()->getInstList().erase(CI);
806 }
807 Result->getFunctionList().erase(F);
808 }
809
810 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000811 if(F->arg_size() != 1)
812 ThrowException("Obsolete va_copy takes 1 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000813 //foo = vacopy(bar)
814 // ->
815 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000816 //b = alloca 1 of typeof(foo)
817 //store bar -> b
818 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000819 //foo = load a
820
821 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
822 const Type* ArgTy = F->getFunctionType()->getReturnType();
823 const Type* ArgTyPtr = PointerType::get(ArgTy);
824 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000825 RetTy, ArgTyPtr, ArgTyPtr,
826 (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000827
828 while (!F->use_empty()) {
829 CallInst* CI = cast<CallInst>(F->use_back());
830 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000831 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
832 new StoreInst(CI->getOperand(1), b, CI);
833 new CallInst(NF, a, b, "", CI);
834 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000835 CI->replaceAllUsesWith(foo);
836 CI->getParent()->getInstList().erase(CI);
837 }
838 Result->getFunctionList().erase(F);
839 }
840 }
841
Chris Lattner6184feb2005-05-20 03:25:47 +0000842 return Result;
843
844 }
845
Chris Lattner00950542001-06-06 20:29:01 +0000846//===----------------------------------------------------------------------===//
847// RunVMAsmParser - Define an interface to this parser
848//===----------------------------------------------------------------------===//
849//
Chris Lattner86427632004-07-14 06:39:48 +0000850Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000851 set_scan_file(F);
852
Chris Lattnera2850432001-07-22 18:36:00 +0000853 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000854 return RunParser(new Module(CurFilename));
855}
Chris Lattner00950542001-06-06 20:29:01 +0000856
Chris Lattner6184feb2005-05-20 03:25:47 +0000857Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
858 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000859
Chris Lattner6184feb2005-05-20 03:25:47 +0000860 CurFilename = "from_memory";
861 if (M == NULL) {
862 return RunParser(new Module (CurFilename));
863 } else {
864 return RunParser(M);
865 }
Chris Lattner00950542001-06-06 20:29:01 +0000866}
867
868%}
869
870%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000871 llvm::Module *ModuleVal;
872 llvm::Function *FunctionVal;
873 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
874 llvm::BasicBlock *BasicBlockVal;
875 llvm::TerminatorInst *TermInstVal;
876 llvm::Instruction *InstVal;
877 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000878
Brian Gaeked0fde302003-11-11 22:41:34 +0000879 const llvm::Type *PrimType;
880 llvm::PATypeHolder *TypeVal;
881 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000882
Brian Gaeked0fde302003-11-11 22:41:34 +0000883 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
884 std::vector<llvm::Value*> *ValueList;
885 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000886 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000887 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000888 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000889 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
890 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000891
Brian Gaeked0fde302003-11-11 22:41:34 +0000892 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000893 int64_t SInt64Val;
894 uint64_t UInt64Val;
895 int SIntVal;
896 unsigned UIntVal;
897 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000898 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000899
Chris Lattner30c89792001-09-07 16:35:17 +0000900 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000901 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000902
Brian Gaeked0fde302003-11-11 22:41:34 +0000903 llvm::Instruction::BinaryOps BinaryOpVal;
904 llvm::Instruction::TermOps TermOpVal;
905 llvm::Instruction::MemoryOps MemOpVal;
906 llvm::Instruction::OtherOps OtherOpVal;
907 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000908}
909
Chris Lattner79df7c02002-03-26 18:01:55 +0000910%type <ModuleVal> Module FunctionList
911%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000912%type <BasicBlockVal> BasicBlock InstructionList
913%type <TermInstVal> BBTerminatorInst
914%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000915%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000916%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000917%type <ArgList> ArgList ArgListH
918%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000919%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000920%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000921%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000922%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000923%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000924%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000925%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000926%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000927%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000928%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000929
Chris Lattner2079fde2001-10-13 06:41:08 +0000930// ValueRef - Unresolved reference to a definition or BB
931%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000932%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000933// Tokens and types for handling constant integer values
934//
935// ESINT64VAL - A negative number within long long range
936%token <SInt64Val> ESINT64VAL
937
938// EUINT64VAL - A positive number within uns. long long range
939%token <UInt64Val> EUINT64VAL
940%type <SInt64Val> EINT64VAL
941
942%token <SIntVal> SINTVAL // Signed 32 bit ints...
943%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
944%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000945%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000946
947// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000948%type <TypeVal> Types TypesV UpRTypes UpRTypesV
949%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000950%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
951%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000952
Chris Lattner6c23f572003-08-22 05:42:10 +0000953%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
954%type <StrVal> Name OptName OptAssign
Chris Lattner87ac9722005-11-06 06:46:28 +0000955%type <UIntVal> OptAlign OptCAlign
Chris Lattnerb2b96672005-11-12 18:21:21 +0000956%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +0000957
Chris Lattner91ef4602004-03-31 03:49:47 +0000958%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner164c3782005-11-12 00:11:10 +0000959%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000960%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Nate Begeman14b05292005-11-05 09:21:28 +0000961%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
Chris Lattnerddb6db42005-05-06 05:51:46 +0000962%token DEPLIBS CALL TAIL
Chris Lattnera8e8f162005-05-06 20:27:19 +0000963%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK
964%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +0000965
Misha Brukman5a2a3822005-05-10 22:02:28 +0000966// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000967%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000968
Misha Brukman5a2a3822005-05-10 22:02:28 +0000969// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000970%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000971%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000972%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000973
974// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000975%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000976
Chris Lattner027dcc52001-07-08 21:10:27 +0000977// Other Operators
978%type <OtherOpVal> ShiftOps
Andrew Lenharth558bc882005-06-18 18:34:52 +0000979%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG
980%token VAARG_old VANEXT_old //OBSOLETE
Chris Lattnerddb6db42005-05-06 05:51:46 +0000981
Chris Lattner027dcc52001-07-08 21:10:27 +0000982
Chris Lattner00950542001-06-06 20:29:01 +0000983%start Module
984%%
985
986// Handle constant integer size restriction and conversion...
987//
Chris Lattner51727be2002-06-04 21:58:56 +0000988INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000989INTVAL : UINTVAL {
990 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
991 ThrowException("Value too large for type!");
992 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000993};
Chris Lattner00950542001-06-06 20:29:01 +0000994
995
Chris Lattner51727be2002-06-04 21:58:56 +0000996EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000997EINT64VAL : EUINT64VAL {
998 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
999 ThrowException("Value too large for type!");
1000 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +00001001};
Chris Lattner00950542001-06-06 20:29:01 +00001002
Misha Brukman5a2a3822005-05-10 22:02:28 +00001003// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001004// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1005//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001006ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1007LogicalOps : AND | OR | XOR;
1008SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +00001009
Chris Lattner51727be2002-06-04 21:58:56 +00001010ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001011
Chris Lattnere98dda62001-07-14 06:10:16 +00001012// These are some types that allow classification if we only want a particular
1013// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001014SIntType : LONG | INT | SHORT | SBYTE;
1015UIntType : ULONG | UINT | USHORT | UBYTE;
1016IntType : SIntType | UIntType;
1017FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001018
Chris Lattnere98dda62001-07-14 06:10:16 +00001019// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001020OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001021 $$ = $1;
1022 }
Misha Brukman5a2a3822005-05-10 22:02:28 +00001023 | /*empty*/ {
1024 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001025 };
Chris Lattner00950542001-06-06 20:29:01 +00001026
Chris Lattner4ad02e72003-04-16 20:28:45 +00001027OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1028 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001029 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001030 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1031 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001032
Chris Lattnera8e8f162005-05-06 20:27:19 +00001033OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1034 CCC_TOK { $$ = CallingConv::C; } |
1035 FASTCC_TOK { $$ = CallingConv::Fast; } |
1036 COLDCC_TOK { $$ = CallingConv::Cold; } |
1037 CC_TOK EUINT64VAL {
1038 if ((unsigned)$2 != $2)
1039 ThrowException("Calling conv too large!");
1040 $$ = $2;
1041 };
1042
Chris Lattner66db8e42005-11-06 06:34:12 +00001043// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1044// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001045OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001046 ALIGN EUINT64VAL {
1047 $$ = $2;
1048 if ($$ != 0 && !isPowerOf2_32($$))
1049 ThrowException("Alignment must be a power of two!");
1050};
Chris Lattner66db8e42005-11-06 06:34:12 +00001051OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001052 ',' ALIGN EUINT64VAL {
1053 $$ = $3;
1054 if ($$ != 0 && !isPowerOf2_32($$))
1055 ThrowException("Alignment must be a power of two!");
1056};
1057
Chris Lattner66db8e42005-11-06 06:34:12 +00001058
Chris Lattner164c3782005-11-12 00:11:10 +00001059SectionString : SECTION STRINGCONSTANT {
1060 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1061 if ($2[i] == '"' || $2[i] == '\\')
1062 ThrowException("Invalid character in section name!");
1063 $$ = $2;
1064};
1065
1066OptSection : /*empty*/ { $$ = 0; } |
1067 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001068
Chris Lattnerb2b96672005-11-12 18:21:21 +00001069// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1070// is set to be the global we are processing.
1071//
1072GlobalVarAttributes : /* empty */ {} |
1073 ',' GlobalVarAttribute GlobalVarAttributes {};
1074GlobalVarAttribute : SectionString {
1075 CurGV->setSection($1);
1076 free($1);
1077 }
1078 | ALIGN EUINT64VAL {
1079 if ($2 != 0 && !isPowerOf2_32($2))
1080 ThrowException("Alignment must be a power of two!");
1081 CurGV->setAlignment($2);
1082 };
Chris Lattner164c3782005-11-12 00:11:10 +00001083
Chris Lattner30c89792001-09-07 16:35:17 +00001084//===----------------------------------------------------------------------===//
1085// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001086// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001087// access to it, a user must explicitly use TypesV.
1088//
1089
1090// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001091TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1092UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001093
1094Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001095 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001096 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1097 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001098 };
Chris Lattner30c89792001-09-07 16:35:17 +00001099
1100
1101// Derived types are added later...
1102//
Chris Lattner51727be2002-06-04 21:58:56 +00001103PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1104PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001105UpRTypes : OPAQUE {
1106 $$ = new PATypeHolder(OpaqueType::get());
1107 }
1108 | PrimType {
1109 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001110 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001111UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001112 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001113};
Chris Lattner30c89792001-09-07 16:35:17 +00001114
Chris Lattner30c89792001-09-07 16:35:17 +00001115// Include derived types in the Types production.
1116//
1117UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001118 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001119 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001120 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001121 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001122 UR_OUT("New Upreference!\n");
1123 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001124 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001125 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001126 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1127 E = $3->end(); I != E; ++I)
1128 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001129 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1130 if (isVarArg) Params.pop_back();
1131
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001132 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001133 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001134 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001135 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001136 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001137 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001138 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001139 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001140 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1141 const llvm::Type* ElemTy = $4->get();
Chris Lattner9547d7f2005-11-10 01:42:43 +00001142 if ((unsigned)$2 != $2)
Brian Gaeke715c90b2004-08-20 06:00:58 +00001143 ThrowException("Unsigned result not equal to signed result");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001144 if (!ElemTy->isPrimitiveType())
Brian Gaeke715c90b2004-08-20 06:00:58 +00001145 ThrowException("Elemental type of a PackedType must be primitive");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001146 if (!isPowerOf2_32($2))
1147 ThrowException("Vector length should be a power of 2!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001148 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1149 delete $4;
1150 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001151 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001152 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001153 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1154 E = $2->end(); I != E; ++I)
1155 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001156
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001157 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001158 delete $2;
1159 }
1160 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001161 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001162 }
1163 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001164 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001165 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001166 };
Chris Lattner30c89792001-09-07 16:35:17 +00001167
Chris Lattner7e708292002-06-25 16:13:24 +00001168// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001169// declaration type lists
1170//
1171TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001172 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001173 $$->push_back(*$1); delete $1;
1174 }
1175 | TypeListI ',' UpRTypes {
1176 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001177 };
Chris Lattner30c89792001-09-07 16:35:17 +00001178
Chris Lattner7e708292002-06-25 16:13:24 +00001179// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001180ArgTypeListI : TypeListI
1181 | TypeListI ',' DOTDOTDOT {
1182 ($$=$1)->push_back(Type::VoidTy);
1183 }
1184 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001185 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001186 }
1187 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001188 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001189 };
Chris Lattner30c89792001-09-07 16:35:17 +00001190
Chris Lattnere98dda62001-07-14 06:10:16 +00001191// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001192// production is used ONLY to represent constants that show up AFTER a 'const',
1193// 'constant' or 'global' token at global scope. Constants that can be inlined
1194// into other expressions (such as integers and constexprs) are handled by the
1195// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001196//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001197ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001198 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001199 if (ATy == 0)
1200 ThrowException("Cannot make array constant with type: '" +
1201 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001202 const Type *ETy = ATy->getElementType();
1203 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001204
Chris Lattner30c89792001-09-07 16:35:17 +00001205 // Verify that we have the correct size...
1206 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001207 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001208 utostr($3->size()) + " arguments, but has size of " +
1209 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001210
Chris Lattner30c89792001-09-07 16:35:17 +00001211 // Verify all elements are correct type!
1212 for (unsigned i = 0; i < $3->size(); i++) {
1213 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001214 ThrowException("Element #" + utostr(i) + " is not of type '" +
1215 ETy->getDescription() +"' as required!\nIt is of type '"+
1216 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001217 }
1218
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001219 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001220 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001221 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001222 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001223 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001224 if (ATy == 0)
1225 ThrowException("Cannot make array constant with type: '" +
1226 (*$1)->getDescription() + "'!");
1227
1228 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001229 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001230 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001231 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001232 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001233 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001234 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001235 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001236 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001237 if (ATy == 0)
1238 ThrowException("Cannot make array constant with type: '" +
1239 (*$1)->getDescription() + "'!");
1240
Chris Lattner30c89792001-09-07 16:35:17 +00001241 int NumElements = ATy->getNumElements();
1242 const Type *ETy = ATy->getElementType();
1243 char *EndStr = UnEscapeLexed($3, true);
1244 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001245 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001246 itostr((int)(EndStr-$3)) +
1247 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001248 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001249 if (ETy == Type::SByteTy) {
1250 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001251 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001252 } else if (ETy == Type::UByteTy) {
1253 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001254 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001255 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001256 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001257 ThrowException("Cannot build string arrays of non byte sized elements!");
1258 }
Chris Lattner30c89792001-09-07 16:35:17 +00001259 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001260 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001261 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001262 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001263 | Types '<' ConstVector '>' { // Nonempty unsized arr
1264 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1265 if (PTy == 0)
1266 ThrowException("Cannot make packed constant with type: '" +
1267 (*$1)->getDescription() + "'!");
1268 const Type *ETy = PTy->getElementType();
1269 int NumElements = PTy->getNumElements();
1270
1271 // Verify that we have the correct size...
1272 if (NumElements != -1 && NumElements != (int)$3->size())
1273 ThrowException("Type mismatch: constant sized packed initialized with " +
1274 utostr($3->size()) + " arguments, but has size of " +
1275 itostr(NumElements) + "!");
1276
1277 // Verify all elements are correct type!
1278 for (unsigned i = 0; i < $3->size(); i++) {
1279 if (ETy != (*$3)[i]->getType())
1280 ThrowException("Element #" + utostr(i) + " is not of type '" +
1281 ETy->getDescription() +"' as required!\nIt is of type '"+
1282 (*$3)[i]->getType()->getDescription() + "'.");
1283 }
1284
1285 $$ = ConstantPacked::get(PTy, *$3);
1286 delete $1; delete $3;
1287 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001288 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001289 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001290 if (STy == 0)
1291 ThrowException("Cannot make struct constant with type: '" +
1292 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001293
Chris Lattnerc6212f12003-05-21 16:06:56 +00001294 if ($3->size() != STy->getNumContainedTypes())
1295 ThrowException("Illegal number of initializers for structure type!");
1296
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001297 // Check to ensure that constants are compatible with the type initializer!
1298 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001299 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001300 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001301 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001302 "' for element #" + utostr(i) +
1303 " of structure initializer!");
1304
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001305 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001306 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001307 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001308 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001309 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001310 if (STy == 0)
1311 ThrowException("Cannot make struct constant with type: '" +
1312 (*$1)->getDescription() + "'!");
1313
1314 if (STy->getNumContainedTypes() != 0)
1315 ThrowException("Illegal number of initializers for structure type!");
1316
1317 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1318 delete $1;
1319 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001320 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001321 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001322 if (PTy == 0)
1323 ThrowException("Cannot make null pointer constant with type: '" +
1324 (*$1)->getDescription() + "'!");
1325
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001326 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001327 delete $1;
1328 }
Chris Lattner16710e92004-10-16 18:17:13 +00001329 | Types UNDEF {
1330 $$ = UndefValue::get($1->get());
1331 delete $1;
1332 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001333 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001334 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001335 if (Ty == 0)
1336 ThrowException("Global const reference must be a pointer type!");
1337
Chris Lattner3101c252002-08-15 17:58:33 +00001338 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001339 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001340 // the context of a function, getValNonImprovising will search the functions
1341 // symbol table instead of the module symbol table for the global symbol,
1342 // which throws things all off. To get around this, we just tell
1343 // getValNonImprovising that we are at global scope here.
1344 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001345 Function *SavedCurFn = CurFun.CurrentFunction;
1346 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001347
Chris Lattner2079fde2001-10-13 06:41:08 +00001348 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001349
Chris Lattner394f2eb2003-10-16 20:12:13 +00001350 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001351
Chris Lattner2079fde2001-10-13 06:41:08 +00001352 // If this is an initializer for a constant pointer, which is referencing a
1353 // (currently) undefined variable, create a stub now that shall be replaced
1354 // in the future with the right type of variable.
1355 //
1356 if (V == 0) {
1357 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1358 const PointerType *PT = cast<PointerType>(Ty);
1359
1360 // First check to see if the forward references value is already created!
1361 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001362 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001363
1364 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001365 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001366 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001367 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001368 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001369 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001370
Reid Spencer3271ed52004-07-18 00:08:11 +00001371 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001372 GlobalValue *GV;
1373 if (const FunctionType *FTy =
1374 dyn_cast<FunctionType>(PT->getElementType())) {
1375 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1376 CurModule.CurrentModule);
1377 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001378 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001379 GlobalValue::ExternalLinkage, 0,
1380 Name, CurModule.CurrentModule);
1381 }
Chris Lattner8a327842004-07-14 21:44:00 +00001382
Reid Spencer3271ed52004-07-18 00:08:11 +00001383 // Keep track of the fact that we have a forward ref to recycle it
1384 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1385 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001386 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001387 }
1388
Reid Spencer3271ed52004-07-18 00:08:11 +00001389 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001390 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001391 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001392 | Types ConstExpr {
1393 if ($1->get() != $2->getType())
1394 ThrowException("Mismatched types for constant expression!");
1395 $$ = $2;
1396 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001397 }
1398 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001399 const Type *Ty = $1->get();
1400 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1401 ThrowException("Cannot create a null initialized value of this type!");
1402 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001403 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001404 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001405
Chris Lattnere43f40b2002-10-09 00:25:32 +00001406ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001407 if (!ConstantSInt::isValueValidForType($1, $2))
1408 ThrowException("Constant value doesn't fit in type!");
1409 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001410 }
1411 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001412 if (!ConstantUInt::isValueValidForType($1, $2))
1413 ThrowException("Constant value doesn't fit in type!");
1414 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001415 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001416 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001417 $$ = ConstantBool::True;
1418 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001419 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001420 $$ = ConstantBool::False;
1421 }
1422 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001423 if (!ConstantFP::isValueValidForType($1, $2))
1424 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001425 $$ = ConstantFP::get($1, $2);
1426 };
1427
Chris Lattner00950542001-06-06 20:29:01 +00001428
Chris Lattnerd78700d2002-08-16 21:14:40 +00001429ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001430 if (!$3->getType()->isFirstClassType())
1431 ThrowException("cast constant expression from a non-primitive type: '" +
1432 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001433 if (!$5->get()->isFirstClassType())
1434 ThrowException("cast constant expression to a non-primitive type: '" +
1435 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001436 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001437 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001438 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001439 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1440 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001441 ThrowException("GetElementPtr requires a pointer operand!");
1442
Chris Lattner830b6f92004-04-05 01:30:04 +00001443 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1444 // indices to uint struct indices for compatibility.
1445 generic_gep_type_iterator<std::vector<Value*>::iterator>
1446 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1447 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1448 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1449 if (isa<StructType>(*GTI)) // Only change struct indices
1450 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1451 if (CUI->getType() == Type::UByteTy)
1452 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1453
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001454 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001455 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001456 if (!IdxTy)
1457 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001458
Chris Lattner9232b992003-04-22 18:42:41 +00001459 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001460 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1461 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001462 IdxVec.push_back(C);
1463 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001464 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001465
Chris Lattnerd78700d2002-08-16 21:14:40 +00001466 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001467
Chris Lattnerd78700d2002-08-16 21:14:40 +00001468 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001469 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001470 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1471 if ($3->getType() != Type::BoolTy)
1472 ThrowException("Select condition must be of boolean type!");
1473 if ($5->getType() != $7->getType())
1474 ThrowException("Select operand types must match!");
1475 $$ = ConstantExpr::getSelect($3, $5, $7);
1476 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001477 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001478 if ($3->getType() != $5->getType())
1479 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001480 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1481 // To retain backward compatibility with these early compilers, we emit a
1482 // cast to the appropriate integer type automatically if we are in the
1483 // broken case. See PR424 for more information.
1484 if (!isa<PointerType>($3->getType())) {
1485 $$ = ConstantExpr::get($1, $3, $5);
1486 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001487 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001488 switch (CurModule.CurrentModule->getPointerSize()) {
1489 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1490 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1491 default: ThrowException("invalid pointer binary constant expr!");
1492 }
1493 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1494 ConstantExpr::getCast($5, IntPtrTy));
1495 $$ = ConstantExpr::getCast($$, $3->getType());
1496 }
1497 }
1498 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1499 if ($3->getType() != $5->getType())
1500 ThrowException("Logical operator types must match!");
1501 if (!$3->getType()->isIntegral())
1502 ThrowException("Logical operands must have integral types!");
1503 $$ = ConstantExpr::get($1, $3, $5);
1504 }
1505 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1506 if ($3->getType() != $5->getType())
1507 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001508 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001509 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001510 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001511 if ($5->getType() != Type::UByteTy)
1512 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001513 if (!$3->getType()->isInteger())
1514 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001515 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001516 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001517
1518
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001519// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001520ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001521 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001522 }
1523 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001524 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001525 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001526 };
Chris Lattner00950542001-06-06 20:29:01 +00001527
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001528
Chris Lattner1781aca2001-09-18 04:00:54 +00001529// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001530GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001531
Chris Lattner00950542001-06-06 20:29:01 +00001532
Chris Lattner0e73ce62002-05-02 19:11:13 +00001533//===----------------------------------------------------------------------===//
1534// Rules to match Modules
1535//===----------------------------------------------------------------------===//
1536
1537// Module rule: Capture the result of parsing the whole file into a result
1538// variable...
1539//
1540Module : FunctionList {
1541 $$ = ParserResult = $1;
1542 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001543};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001544
Chris Lattner7e708292002-06-25 16:13:24 +00001545// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001546//
1547FunctionList : FunctionList Function {
1548 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001549 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001550 }
1551 | FunctionList FunctionProto {
1552 $$ = $1;
1553 }
1554 | FunctionList IMPLEMENTATION {
1555 $$ = $1;
1556 }
1557 | ConstPool {
1558 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001559 // Emit an error if there are any unresolved types left.
1560 if (!CurModule.LateResolveTypes.empty()) {
1561 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1562 if (DID.Type == ValID::NameVal)
1563 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1564 else
1565 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1566 }
Chris Lattner51727be2002-06-04 21:58:56 +00001567 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001568
Chris Lattnere98dda62001-07-14 06:10:16 +00001569// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001570ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001571 // Eagerly resolve types. This is not an optimization, this is a
1572 // requirement that is due to the fact that we could have this:
1573 //
1574 // %list = type { %list * }
1575 // %list = type { %list * } ; repeated type decl
1576 //
1577 // If types are not resolved eagerly, then the two types will not be
1578 // determined to be the same type!
1579 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001580 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001581
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001582 if (!setTypeName(*$4, $2) && !$2) {
1583 // If this is a named type that is not a redefinition, add it to the slot
1584 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001585 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001586 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001587
1588 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001589 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001590 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001591 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001592 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001593 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001594 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
1595 } GlobalVarAttributes {
1596 CurGV = 0;
Chris Lattner1781aca2001-09-18 04:00:54 +00001597 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001598 | ConstPool OptAssign EXTERNAL GlobalType Types {
1599 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage,
1600 $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001601 delete $5;
Chris Lattnerb2b96672005-11-12 18:21:21 +00001602 } GlobalVarAttributes {
1603 CurGV = 0;
Chris Lattnere98dda62001-07-14 06:10:16 +00001604 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001605 | ConstPool TARGET TargetDefinition {
1606 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001607 | ConstPool DEPLIBS '=' LibrariesDefinition {
1608 }
Chris Lattner00950542001-06-06 20:29:01 +00001609 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001610 };
Chris Lattner00950542001-06-06 20:29:01 +00001611
1612
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001613
1614BigOrLittle : BIG { $$ = Module::BigEndian; };
1615BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1616
1617TargetDefinition : ENDIAN '=' BigOrLittle {
1618 CurModule.CurrentModule->setEndianness($3);
1619 }
1620 | POINTERSIZE '=' EUINT64VAL {
1621 if ($3 == 32)
1622 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1623 else if ($3 == 64)
1624 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1625 else
1626 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001627 }
1628 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001629 CurModule.CurrentModule->setTargetTriple($3);
1630 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001631 };
1632
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001633LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001634
1635LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001636 CurModule.CurrentModule->addLibrary($3);
1637 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001638 }
1639 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001640 CurModule.CurrentModule->addLibrary($1);
1641 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001642 }
1643 | /* empty: end of list */ {
1644 }
1645 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001646
Chris Lattner00950542001-06-06 20:29:01 +00001647//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001648// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001649//===----------------------------------------------------------------------===//
1650
Chris Lattner6c23f572003-08-22 05:42:10 +00001651Name : VAR_ID | STRINGCONSTANT;
1652OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001653
Chris Lattner6c23f572003-08-22 05:42:10 +00001654ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001655 if (*$1 == Type::VoidTy)
1656 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001657 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001658};
Chris Lattner00950542001-06-06 20:29:01 +00001659
Chris Lattner69da5cf2002-10-13 20:57:00 +00001660ArgListH : ArgListH ',' ArgVal {
1661 $$ = $1;
1662 $1->push_back(*$3);
1663 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001664 }
1665 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001666 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001667 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001668 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001669 };
Chris Lattner00950542001-06-06 20:29:01 +00001670
1671ArgList : ArgListH {
1672 $$ = $1;
1673 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001674 | ArgListH ',' DOTDOTDOT {
1675 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001676 $$->push_back(std::pair<PATypeHolder*,
1677 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001678 }
1679 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001680 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1681 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001682 }
Chris Lattner00950542001-06-06 20:29:01 +00001683 | /* empty */ {
1684 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001685 };
Chris Lattner00950542001-06-06 20:29:01 +00001686
Chris Lattner164c3782005-11-12 00:11:10 +00001687FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1688 OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001689 UnEscapeLexed($3);
1690 std::string FunctionName($3);
1691 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001692
Chris Lattnera8e8f162005-05-06 20:27:19 +00001693 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001694 ThrowException("LLVM functions cannot return aggregate types!");
1695
Chris Lattner9232b992003-04-22 18:42:41 +00001696 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001697 if ($5) { // If there are arguments...
1698 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1699 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001700 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001701 }
Chris Lattner00950542001-06-06 20:29:01 +00001702
Chris Lattner2079fde2001-10-13 06:41:08 +00001703 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1704 if (isVarArg) ParamTypeList.pop_back();
1705
Chris Lattnera8e8f162005-05-06 20:27:19 +00001706 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001707 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001708 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001709
Chris Lattnera09000d2004-07-14 23:03:46 +00001710 ValID ID;
1711 if (!FunctionName.empty()) {
1712 ID = ValID::create((char*)FunctionName.c_str());
1713 } else {
1714 ID = ValID::create((int)CurModule.Values[PFT].size());
1715 }
1716
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001717 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001718 // See if this function was forward referenced. If so, recycle the object.
1719 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1720 // Move the function to the end of the list, from whereever it was
1721 // previously inserted.
1722 Fn = cast<Function>(FWRef);
1723 CurModule.CurrentModule->getFunctionList().remove(Fn);
1724 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1725 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1726 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1727 // If this is the case, either we need to be a forward decl, or it needs
1728 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001729 if (!CurFun.isDeclare && !Fn->isExternal())
1730 ThrowException("Redefinition of function '" + FunctionName + "'!");
1731
Chris Lattnera09000d2004-07-14 23:03:46 +00001732 // Make sure to strip off any argument names so we can't get conflicts.
1733 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001734 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001735 AI != AE; ++AI)
1736 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001737
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001738 } else { // Not already defined?
1739 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1740 CurModule.CurrentModule);
1741 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001742 }
Chris Lattner00950542001-06-06 20:29:01 +00001743
Chris Lattner394f2eb2003-10-16 20:12:13 +00001744 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001745 Fn->setCallingConv($1);
Chris Lattner164c3782005-11-12 00:11:10 +00001746 Fn->setAlignment($8);
1747 if ($7) {
1748 Fn->setSection($7);
1749 free($7);
1750 }
Chris Lattner00950542001-06-06 20:29:01 +00001751
Chris Lattner7e708292002-06-25 16:13:24 +00001752 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001753 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001754 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001755 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001756 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001757 delete $5->back().first;
1758 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001759 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001760 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001761 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1762 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001763 delete I->first; // Delete the typeholder...
1764
Chris Lattner8ab406d2004-07-13 07:59:27 +00001765 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001766 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001767 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001768
Chris Lattnera8e8f162005-05-06 20:27:19 +00001769 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001770 }
Chris Lattner51727be2002-06-04 21:58:56 +00001771};
Chris Lattner00950542001-06-06 20:29:01 +00001772
Chris Lattner9b02cc32002-05-03 18:23:48 +00001773BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1774
Chris Lattner4ad02e72003-04-16 20:28:45 +00001775FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001776 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001777
Chris Lattner4ad02e72003-04-16 20:28:45 +00001778 // Make sure that we keep track of the linkage type even if there was a
1779 // previous "declare".
1780 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001781};
Chris Lattner00950542001-06-06 20:29:01 +00001782
Chris Lattner9b02cc32002-05-03 18:23:48 +00001783END : ENDTOK | '}'; // Allow end of '}' to end a function
1784
Chris Lattner79df7c02002-03-26 18:01:55 +00001785Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001786 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001787};
Chris Lattner00950542001-06-06 20:29:01 +00001788
Chris Lattner394f2eb2003-10-16 20:12:13 +00001789FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1790 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001791 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001792};
Chris Lattner00950542001-06-06 20:29:01 +00001793
1794//===----------------------------------------------------------------------===//
1795// Rules to match Basic Blocks
1796//===----------------------------------------------------------------------===//
1797
1798ConstValueRef : ESINT64VAL { // A reference to a direct constant
1799 $$ = ValID::create($1);
1800 }
1801 | EUINT64VAL {
1802 $$ = ValID::create($1);
1803 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001804 | FPVAL { // Perhaps it's an FP constant?
1805 $$ = ValID::create($1);
1806 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001807 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001808 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001809 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001810 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001811 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001812 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001813 | NULL_TOK {
1814 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001815 }
Chris Lattner16710e92004-10-16 18:17:13 +00001816 | UNDEF {
1817 $$ = ValID::createUndef();
1818 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001819 | '<' ConstVector '>' { // Nonempty unsized packed vector
1820 const Type *ETy = (*$2)[0]->getType();
1821 int NumElements = $2->size();
1822
1823 PackedType* pt = PackedType::get(ETy, NumElements);
1824 PATypeHolder* PTy = new PATypeHolder(
1825 HandleUpRefs(
1826 PackedType::get(
1827 ETy,
1828 NumElements)
1829 )
1830 );
1831
1832 // Verify all elements are correct type!
1833 for (unsigned i = 0; i < $2->size(); i++) {
1834 if (ETy != (*$2)[i]->getType())
1835 ThrowException("Element #" + utostr(i) + " is not of type '" +
1836 ETy->getDescription() +"' as required!\nIt is of type '" +
1837 (*$2)[i]->getType()->getDescription() + "'.");
1838 }
1839
1840 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1841 delete PTy; delete $2;
1842 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001843 | ConstExpr {
1844 $$ = ValID::create($1);
1845 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001846
Chris Lattner2079fde2001-10-13 06:41:08 +00001847// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1848// another value.
1849//
1850SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001851 $$ = ValID::create($1);
1852 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001853 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001854 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001855 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001856
1857// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001858ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001859
Chris Lattner00950542001-06-06 20:29:01 +00001860
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001861// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1862// type immediately preceeds the value reference, and allows complex constant
1863// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001864ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001865 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001866 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001867
Chris Lattner00950542001-06-06 20:29:01 +00001868BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001869 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001870 }
Chris Lattner7e708292002-06-25 16:13:24 +00001871 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001872 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001873 };
Chris Lattner00950542001-06-06 20:29:01 +00001874
1875
1876// Basic blocks are terminated by branching instructions:
1877// br, br/cc, switch, ret
1878//
Chris Lattner2079fde2001-10-13 06:41:08 +00001879BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001880 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001881 InsertValue($3);
1882
1883 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001884 InsertValue($1);
1885 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001886 };
Chris Lattner00950542001-06-06 20:29:01 +00001887
1888InstructionList : InstructionList Inst {
1889 $1->getInstList().push_back($2);
1890 $$ = $1;
1891 }
1892 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001893 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001894
1895 // Make sure to move the basic block to the correct location in the
1896 // function, instead of leaving it inserted wherever it was first
1897 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001898 Function::BasicBlockListType &BBL =
1899 CurFun.CurrentFunction->getBasicBlockList();
1900 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner22ae2322004-07-13 08:39:15 +00001901 }
1902 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001903 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001904
1905 // Make sure to move the basic block to the correct location in the
1906 // function, instead of leaving it inserted wherever it was first
1907 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001908 Function::BasicBlockListType &BBL =
1909 CurFun.CurrentFunction->getBasicBlockList();
1910 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner51727be2002-06-04 21:58:56 +00001911 };
Chris Lattner00950542001-06-06 20:29:01 +00001912
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001913BBTerminatorInst : RET ResolvedVal { // Return with a result...
1914 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001915 }
1916 | RET VOID { // Return with no result...
1917 $$ = new ReturnInst();
1918 }
1919 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001920 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001921 } // Conditional Branch...
1922 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001923 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001924 }
1925 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001926 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001927 $$ = S;
1928
Chris Lattner9232b992003-04-22 18:42:41 +00001929 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001930 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001931 for (; I != E; ++I) {
1932 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
1933 S->addCase(CI, I->second);
1934 else
1935 ThrowException("Switch case is constant, but not a simple integer!");
1936 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001937 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001938 }
Chris Lattner196850c2003-05-15 21:30:00 +00001939 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001940 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00001941 $$ = S;
1942 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001943 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1944 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001945 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001946 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001947
Chris Lattnera8e8f162005-05-06 20:27:19 +00001948 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001949 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001950 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001951 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001952 if ($6) {
1953 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00001954 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001955 ParamTypes.push_back((*I)->getType());
1956 }
1957
1958 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1959 if (isVarArg) ParamTypes.pop_back();
1960
Chris Lattnera8e8f162005-05-06 20:27:19 +00001961 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001962 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001963 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001964
Chris Lattnera8e8f162005-05-06 20:27:19 +00001965 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001966
Chris Lattnera8e8f162005-05-06 20:27:19 +00001967 BasicBlock *Normal = getBBVal($10);
1968 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00001969
1970 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001971 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001972 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001973 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001974 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001975 // correctly!
1976 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001977 FunctionType::param_iterator I = Ty->param_begin();
1978 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001979 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001980
1981 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001982 if ((*ArgI)->getType() != *I)
1983 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1984 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001985
1986 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001987 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001988
Chris Lattnera8e8f162005-05-06 20:27:19 +00001989 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00001990 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001991 cast<InvokeInst>($$)->setCallingConv($2);
1992
1993 delete $3;
1994 delete $6;
Chris Lattner36143fc2003-09-08 18:54:55 +00001995 }
1996 | UNWIND {
1997 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00001998 }
1999 | UNREACHABLE {
2000 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00002001 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002002
2003
Chris Lattner00950542001-06-06 20:29:01 +00002004
2005JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2006 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002007 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00002008 if (V == 0)
2009 ThrowException("May only switch on a constant pool value!");
2010
Chris Lattner64116f92004-07-14 01:33:11 +00002011 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00002012 }
2013 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002014 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002015 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00002016
2017 if (V == 0)
2018 ThrowException("May only switch on a constant pool value!");
2019
Chris Lattner64116f92004-07-14 01:33:11 +00002020 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00002021 };
Chris Lattner00950542001-06-06 20:29:01 +00002022
2023Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00002024 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00002025 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00002026 InsertValue($2);
2027 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00002028};
Chris Lattner00950542001-06-06 20:29:01 +00002029
Chris Lattnerc24d2082001-06-11 15:04:20 +00002030PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00002031 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00002032 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00002033 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002034 }
2035 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2036 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00002037 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00002038 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00002039 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002040
2041
Chris Lattner30c89792001-09-07 16:35:17 +00002042ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00002043 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002044 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00002045 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002046 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00002047 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002048 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00002049 };
Chris Lattner00950542001-06-06 20:29:01 +00002050
2051// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00002052ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00002053
Chris Lattnerddb6db42005-05-06 05:51:46 +00002054OptTailCall : TAIL CALL {
2055 $$ = true;
2056 }
2057 | CALL {
2058 $$ = false;
2059 };
2060
2061
2062
Chris Lattner4a6482b2002-09-10 19:57:26 +00002063InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00002064 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2065 !isa<PackedType>((*$2).get()))
2066 ThrowException(
2067 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00002068 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
2069 ThrowException("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002070 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2071 if ($$ == 0)
2072 ThrowException("binary operator returned null!");
2073 delete $2;
2074 }
2075 | LogicalOps Types ValueRef ',' ValueRef {
2076 if (!(*$2)->isIntegral())
2077 ThrowException("Logical operator requires integral operands!");
2078 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2079 if ($$ == 0)
2080 ThrowException("binary operator returned null!");
2081 delete $2;
2082 }
2083 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002084 if(isa<PackedType>((*$2).get())) {
2085 ThrowException(
2086 "PackedTypes currently not supported in setcc instructions!");
2087 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00002088 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00002089 if ($$ == 0)
2090 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002091 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002092 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002093 | NOT ResolvedVal {
2094 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2095 << " Replacing with 'xor'.\n";
2096
2097 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2098 if (Ones == 0)
2099 ThrowException("Expected integral type for not instruction!");
2100
2101 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002102 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00002103 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00002104 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002105 | ShiftOps ResolvedVal ',' ResolvedVal {
2106 if ($4->getType() != Type::UByteTy)
2107 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002108 if (!$2->getType()->isInteger())
2109 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002110 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00002111 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002112 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002113 if (!$4->get()->isFirstClassType())
2114 ThrowException("cast instruction to a non-primitive type: '" +
2115 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002116 $$ = new CastInst($2, *$4);
2117 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002118 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002119 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2120 if ($2->getType() != Type::BoolTy)
2121 ThrowException("select condition must be boolean!");
2122 if ($4->getType() != $6->getType())
2123 ThrowException("select value types should match!");
2124 $$ = new SelectInst($2, $4, $6);
2125 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002126 | VAARG ResolvedVal ',' Types {
Andrew Lenharthe78f50d2005-06-19 03:53:56 +00002127 NewVarArgs = true;
Chris Lattner99e7ab72003-10-18 05:53:13 +00002128 $$ = new VAArgInst($2, *$4);
2129 delete $4;
2130 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002131 | VAARG_old ResolvedVal ',' Types {
2132 ObsoleteVarArgs = true;
2133 const Type* ArgTy = $2->getType();
2134 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002135 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002136
2137 //b = vaarg a, t ->
2138 //foo = alloca 1 of t
2139 //bar = vacopy a
2140 //store bar -> foo
2141 //b = vaarg foo, t
2142 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2143 CurBB->getInstList().push_back(foo);
2144 CallInst* bar = new CallInst(NF, $2);
2145 CurBB->getInstList().push_back(bar);
2146 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2147 $$ = new VAArgInst(foo, *$4);
2148 delete $4;
2149 }
2150 | VANEXT_old ResolvedVal ',' Types {
2151 ObsoleteVarArgs = true;
2152 const Type* ArgTy = $2->getType();
2153 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002154 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002155
2156 //b = vanext a, t ->
2157 //foo = alloca 1 of t
2158 //bar = vacopy a
2159 //store bar -> foo
2160 //tmp = vaarg foo, t
2161 //b = load foo
2162 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2163 CurBB->getInstList().push_back(foo);
2164 CallInst* bar = new CallInst(NF, $2);
2165 CurBB->getInstList().push_back(bar);
2166 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2167 Instruction* tmp = new VAArgInst(foo, *$4);
2168 CurBB->getInstList().push_back(tmp);
2169 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002170 delete $4;
2171 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002172 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002173 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002174 if (!Ty->isFirstClassType())
2175 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002176 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002177 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002178 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002179 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00002180 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002181 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002182 $2->pop_front();
2183 }
2184 delete $2; // Free the list...
Chris Lattnerddb6db42005-05-06 05:51:46 +00002185 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002186 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002187 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002188 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002189
Chris Lattnera8e8f162005-05-06 20:27:19 +00002190 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002191 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002192 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002193 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002194 if ($6) {
2195 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002196 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002197 ParamTypes.push_back((*I)->getType());
2198 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002199
2200 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2201 if (isVarArg) ParamTypes.pop_back();
2202
Chris Lattnera8e8f162005-05-06 20:27:19 +00002203 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Chris Lattner595f06c2005-02-01 01:47:42 +00002204 ThrowException("LLVM functions cannot return aggregate types!");
2205
Chris Lattnera8e8f162005-05-06 20:27:19 +00002206 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002207 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002208 }
Chris Lattner00950542001-06-06 20:29:01 +00002209
Chris Lattnera8e8f162005-05-06 20:27:19 +00002210 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002211
Chris Lattner8b81bf52001-07-25 22:47:46 +00002212 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002213 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002214 // Make sure no arguments is a good thing!
2215 if (Ty->getNumParams() != 0)
2216 ThrowException("No arguments passed to a function that "
2217 "expects arguments!");
2218
Chris Lattner9232b992003-04-22 18:42:41 +00002219 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002220 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002221 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002222 // correctly!
2223 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002224 FunctionType::param_iterator I = Ty->param_begin();
2225 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002226 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002227
2228 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002229 if ((*ArgI)->getType() != *I)
2230 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2231 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002232
Chris Lattner8b81bf52001-07-25 22:47:46 +00002233 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002234 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002235
Chris Lattnera8e8f162005-05-06 20:27:19 +00002236 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002237 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002238 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002239 cast<CallInst>($$)->setCallingConv($2);
2240 delete $3;
2241 delete $6;
Chris Lattner00950542001-06-06 20:29:01 +00002242 }
2243 | MemoryInst {
2244 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002245 };
Chris Lattner00950542001-06-06 20:29:01 +00002246
Chris Lattner6cdb0112001-11-26 16:54:11 +00002247
2248// IndexList - List of indices for GEP based instructions...
2249IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002250 $$ = $2;
2251 } | /* empty */ {
2252 $$ = new std::vector<Value*>();
2253 };
2254
2255OptVolatile : VOLATILE {
2256 $$ = true;
2257 }
2258 | /* empty */ {
2259 $$ = false;
2260 };
2261
Chris Lattner027dcc52001-07-08 21:10:27 +00002262
Chris Lattnerddb6db42005-05-06 05:51:46 +00002263
Chris Lattner66db8e42005-11-06 06:34:12 +00002264MemoryInst : MALLOC Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002265 $$ = new MallocInst(*$2, 0, $3);
Chris Lattner30c89792001-09-07 16:35:17 +00002266 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002267 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002268 | MALLOC Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002269 $$ = new MallocInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002270 delete $2;
2271 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002272 | ALLOCA Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002273 $$ = new AllocaInst(*$2, 0, $3);
Nate Begeman14b05292005-11-05 09:21:28 +00002274 delete $2;
2275 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002276 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002277 $$ = new AllocaInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002278 delete $2;
2279 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002280 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002281 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002282 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002283 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002284 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002285 }
2286
Chris Lattner15c9c032003-09-08 18:20:29 +00002287 | OptVolatile LOAD Types ValueRef {
2288 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002289 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002290 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002291 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2292 ThrowException("Can't load from pointer of non-first-class type: " +
2293 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002294 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002295 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002296 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002297 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2298 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002299 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002300 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002301 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002302 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002303 if (ElTy != $3->getType())
2304 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002305 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002306
Chris Lattner79ad13802003-09-08 20:29:46 +00002307 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002308 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002309 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002310 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002311 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002312 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002313
2314 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2315 // indices to uint struct indices for compatibility.
2316 generic_gep_type_iterator<std::vector<Value*>::iterator>
2317 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2318 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2319 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2320 if (isa<StructType>(*GTI)) // Only change struct indices
2321 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2322 if (CUI->getType() == Type::UByteTy)
2323 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2324
Chris Lattner30c89792001-09-07 16:35:17 +00002325 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002326 ThrowException("Invalid getelementptr indices for type '" +
2327 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002328 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2329 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002330 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002331
Brian Gaeked0fde302003-11-11 22:41:34 +00002332
Chris Lattner00950542001-06-06 20:29:01 +00002333%%
Chris Lattner09083092001-07-08 04:57:15 +00002334int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002335 std::string where
2336 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002337 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002338 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002339 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002340 errMsg += "end-of-file.";
2341 else
Chris Lattner9232b992003-04-22 18:42:41 +00002342 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002343 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002344 return 0;
2345}