blob: 6bfc3bb8e83f2d02afbbe44841dc14655c0faff4 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000017#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/SymbolTable.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/STLExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000022#include <algorithm>
23#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000024#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000025#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000026
Chris Lattner386a3b72001-10-16 19:54:17 +000027int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000028int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000029int yyparse();
30
Brian Gaeked0fde302003-11-11 22:41:34 +000031namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000032 std::string CurFilename;
33}
34using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chris Lattner00950542001-06-06 20:29:01 +000036static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000043#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000044#else
45#define UR_OUT(X)
46#endif
47
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000048#define YYERROR_VERBOSE 1
49
Andrew Lenharth558bc882005-06-18 18:34:52 +000050static bool ObsoleteVarArgs;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +000051static bool NewVarArgs;
Andrew Lenharth558bc882005-06-18 18:34:52 +000052static BasicBlock* CurBB;
53
54
Chris Lattner7e708292002-06-25 16:13:24 +000055// This contains info used when building the body of a function. It is
56// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000057//
Chris Lattner9232b992003-04-22 18:42:41 +000058typedef std::vector<Value *> ValueList; // Numbered defs
Misha Brukman5a2a3822005-05-10 22:02:28 +000059static void
60ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
61 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000062
63static struct PerModuleInfo {
64 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000065 std::map<const Type *, ValueList> Values; // Module level numbered definitions
66 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000067 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000068 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000069
Chris Lattnerbc721ed2004-07-13 08:28:21 +000070 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
71 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000072 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000073 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
74
Chris Lattner2079fde2001-10-13 06:41:08 +000075 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
76 // references to global values. Global values may be referenced before they
77 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000078 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000079 //
Chris Lattner9232b992003-04-22 18:42:41 +000080 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000081 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000082 GlobalRefsType GlobalRefs;
83
Chris Lattner00950542001-06-06 20:29:01 +000084 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000085 // If we could not resolve some functions at function compilation time
86 // (calls to functions before they are defined), resolve them now... Types
87 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000088 //
Chris Lattner00950542001-06-06 20:29:01 +000089 ResolveDefinitions(LateResolveValues);
90
Chris Lattner2079fde2001-10-13 06:41:08 +000091 // Check to make sure that all global value forward references have been
92 // resolved!
93 //
94 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000095 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +000096
Chris Lattner749ce032002-03-11 22:12:39 +000097 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
98 I != E; ++I) {
99 UndefinedReferences += " " + I->first.first->getDescription() + " " +
100 I->first.second.getName() + "\n";
101 }
102 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000103 }
104
Chris Lattner7e708292002-06-25 16:13:24 +0000105 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000106 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000107 CurrentModule = 0;
108 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000109
110
Chris Lattner8a327842004-07-14 21:44:00 +0000111 // GetForwardRefForGlobal - Check to see if there is a forward reference
112 // for this global. If so, remove it from the GlobalRefs map and return it.
113 // If not, just return null.
114 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
115 // Check to see if there is a forward reference to this global variable...
116 // if there is, eliminate it and patch the reference to use the new def'n.
117 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
118 GlobalValue *Ret = 0;
119 if (I != GlobalRefs.end()) {
120 Ret = I->second;
121 GlobalRefs.erase(I);
122 }
123 return Ret;
124 }
Chris Lattner00950542001-06-06 20:29:01 +0000125} CurModule;
126
Chris Lattner79df7c02002-03-26 18:01:55 +0000127static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000128 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000129
Chris Lattner735f2702004-07-08 22:30:50 +0000130 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
131 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000132 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000133
Chris Lattner2e2ed292004-07-14 06:28:35 +0000134 /// BBForwardRefs - When we see forward references to basic blocks, keep
135 /// track of them here.
136 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
137 std::vector<BasicBlock*> NumberedBlocks;
138 unsigned NextBBNum;
139
Chris Lattner79df7c02002-03-26 18:01:55 +0000140 inline PerFunctionInfo() {
141 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000142 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000143 }
144
Chris Lattner79df7c02002-03-26 18:01:55 +0000145 inline void FunctionStart(Function *M) {
146 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000147 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000148 }
149
Chris Lattner79df7c02002-03-26 18:01:55 +0000150 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000151 NumberedBlocks.clear();
152
153 // Any forward referenced blocks left?
154 if (!BBForwardRefs.empty())
155 ThrowException("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000156 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000157
158 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000159 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000160
Chris Lattner7e708292002-06-25 16:13:24 +0000161 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000162 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000163 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000164 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000165} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000166
Chris Lattner394f2eb2003-10-16 20:12:13 +0000167static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000168
Chris Lattner00950542001-06-06 20:29:01 +0000169
170//===----------------------------------------------------------------------===//
171// Code to handle definitions of all the types
172//===----------------------------------------------------------------------===//
173
Chris Lattner735f2702004-07-08 22:30:50 +0000174static int InsertValue(Value *V,
175 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
176 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000177
178 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000179 ValueList &List = ValueTab[V->getType()];
180 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000181 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000182}
183
Chris Lattner30c89792001-09-07 16:35:17 +0000184static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000185 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000186 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000187 // Module constants occupy the lowest numbered slots...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000188 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000189 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000190 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000191 case ValID::NameVal: // Is it a named definition?
192 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
193 D.destroy(); // Free old strdup'd memory...
194 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000195 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000196 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000197 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000198 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000199 }
200
201 // If we reached here, we referenced either a symbol that we don't know about
202 // or an id number that hasn't been read yet. We may be referencing something
203 // forward, so just create an entry to be resolved later and get to it...
204 //
205 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
206
Chris Lattner355ad1f2005-03-21 06:27:42 +0000207
208 if (inFunctionScope()) {
209 if (D.Type == ValID::NameVal)
210 ThrowException("Reference to an undefined type: '" + D.getName() + "'");
211 else
212 ThrowException("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000213 }
Chris Lattner30c89792001-09-07 16:35:17 +0000214
Chris Lattner355ad1f2005-03-21 06:27:42 +0000215 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
216 if (I != CurModule.LateResolveTypes.end())
217 return I->second;
218
Chris Lattner82269592001-10-22 06:01:08 +0000219 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000220 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000221 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000222 }
Chris Lattner30c89792001-09-07 16:35:17 +0000223
Chris Lattner9232b992003-04-22 18:42:41 +0000224static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000225 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000226 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000227 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000228 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000229}
230
Chris Lattner2079fde2001-10-13 06:41:08 +0000231// getValNonImprovising - Look up the value specified by the provided type and
232// the provided ValID. If the value exists and has already been defined, return
233// it. Otherwise return null.
234//
235static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000236 if (isa<FunctionType>(Ty))
237 ThrowException("Functions are not values and "
238 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000239
Chris Lattner30c89792001-09-07 16:35:17 +0000240 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000241 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000242 unsigned Num = (unsigned)D.Num;
243
244 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000245 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000246 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000247 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000248 return VI->second[Num];
249 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000250 }
251
252 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000253 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000254 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000255
256 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000257 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000258
Chris Lattner16af11d2004-02-09 21:03:38 +0000259 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000260 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000261
Chris Lattner1a1cb112001-09-30 22:46:54 +0000262 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000263 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000264 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000265
266 D.destroy(); // Free old strdup'd memory...
267 return N;
268 }
269
Misha Brukman5a2a3822005-05-10 22:02:28 +0000270 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000271 // value will fit into the specified type...
272 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000273 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
274 ThrowException("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000275 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000276 Ty->getDescription() + "'!");
277 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000278
279 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000280 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
281 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000282 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000283 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000284 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000285 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000286 }
287 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000288 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000289 }
290
Chris Lattner2079fde2001-10-13 06:41:08 +0000291 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000292 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000293 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000294 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000295
Chris Lattner2079fde2001-10-13 06:41:08 +0000296 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000297 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000298 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000299 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000300
Chris Lattner16710e92004-10-16 18:17:13 +0000301 case ValID::ConstUndefVal: // Is it an undef value?
302 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000303
Chris Lattnerd78700d2002-08-16 21:14:40 +0000304 case ValID::ConstantVal: // Fully resolved constant?
305 if (D.ConstantValue->getType() != Ty)
306 ThrowException("Constant expression type different from required type!");
307 return D.ConstantValue;
308
Chris Lattner30c89792001-09-07 16:35:17 +0000309 default:
310 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000311 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000312 } // End of switch
313
Chris Lattner2079fde2001-10-13 06:41:08 +0000314 assert(0 && "Unhandled case!");
315 return 0;
316}
317
Chris Lattner2079fde2001-10-13 06:41:08 +0000318// getVal - This function is identical to getValNonImprovising, except that if a
319// value is not already defined, it "improvises" by creating a placeholder var
320// that looks and acts just like the requested variable. When the value is
321// defined later, all uses of the placeholder variable are replaced with the
322// real thing.
323//
Chris Lattner64116f92004-07-14 01:33:11 +0000324static Value *getVal(const Type *Ty, const ValID &ID) {
325 if (Ty == Type::LabelTy)
326 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000327
Chris Lattner64116f92004-07-14 01:33:11 +0000328 // See if the value has already been defined.
329 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000330 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000331
Chris Lattner60cd9552005-03-23 01:29:26 +0000332 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
333 ThrowException("Invalid use of a composite type!");
334
Chris Lattner00950542001-06-06 20:29:01 +0000335 // If we reached here, we referenced either a symbol that we don't know about
336 // or an id number that hasn't been read yet. We may be referencing something
337 // forward, so just create an entry to be resolved later and get to it...
338 //
Chris Lattner64116f92004-07-14 01:33:11 +0000339 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000340
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000341 // Remember where this forward reference came from. FIXME, shouldn't we try
342 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000343 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000344 llvmAsmlineno)));
345
Chris Lattner79df7c02002-03-26 18:01:55 +0000346 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000347 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000348 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000349 InsertValue(V, CurModule.LateResolveValues);
350 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000351}
352
Chris Lattner2e2ed292004-07-14 06:28:35 +0000353/// getBBVal - This is used for two purposes:
354/// * If isDefinition is true, a new basic block with the specified ID is being
355/// defined.
356/// * If isDefinition is true, this is a reference to a basic block, which may
357/// or may not be a forward reference.
358///
359static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000360 assert(inFunctionScope() && "Can't get basic block at global scope!");
361
Chris Lattner2e2ed292004-07-14 06:28:35 +0000362 std::string Name;
363 BasicBlock *BB = 0;
364 switch (ID.Type) {
365 default: ThrowException("Illegal label reference " + ID.getName());
366 case ValID::NumberVal: // Is it a numbered definition?
367 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
368 CurFun.NumberedBlocks.resize(ID.Num+1);
369 BB = CurFun.NumberedBlocks[ID.Num];
370 break;
371 case ValID::NameVal: // Is it a named definition?
372 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000373 if (Value *N = CurFun.CurrentFunction->
374 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000375 BB = cast<BasicBlock>(N);
376 break;
377 }
Chris Lattner64116f92004-07-14 01:33:11 +0000378
Chris Lattner2e2ed292004-07-14 06:28:35 +0000379 // See if the block has already been defined.
380 if (BB) {
381 // If this is the definition of the block, make sure the existing value was
382 // just a forward reference. If it was a forward reference, there will be
383 // an entry for it in the PlaceHolderInfo map.
384 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
385 // The existing value was a definition, not a forward reference.
386 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000387
Chris Lattner2e2ed292004-07-14 06:28:35 +0000388 ID.destroy(); // Free strdup'd memory.
389 return BB;
390 }
391
392 // Otherwise this block has not been seen before.
393 BB = new BasicBlock("", CurFun.CurrentFunction);
394 if (ID.Type == ValID::NameVal) {
395 BB->setName(ID.Name);
396 } else {
397 CurFun.NumberedBlocks[ID.Num] = BB;
398 }
399
400 // If this is not a definition, keep track of it so we can use it as a forward
401 // reference.
402 if (!isDefinition) {
403 // Remember where this forward reference came from.
404 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
405 } else {
406 // The forward declaration could have been inserted anywhere in the
407 // function: insert it into the correct place now.
408 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
409 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
410 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000411 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000412 return BB;
413}
414
Chris Lattner00950542001-06-06 20:29:01 +0000415
416//===----------------------------------------------------------------------===//
417// Code to handle forward references in instructions
418//===----------------------------------------------------------------------===//
419//
420// This code handles the late binding needed with statements that reference
421// values not defined yet... for example, a forward branch, or the PHI node for
422// a loop body.
423//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000424// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000425// and back patchs after we are done.
426//
427
Misha Brukman5a2a3822005-05-10 22:02:28 +0000428// ResolveDefinitions - If we could not resolve some defs at parsing
429// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000430// defs now...
431//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000432static void
433ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
434 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000435 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000436 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000437 E = LateResolvers.end(); LRI != E; ++LRI) {
438 ValueList &List = LRI->second;
439 while (!List.empty()) {
440 Value *V = List.back();
441 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000442
443 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
444 CurModule.PlaceHolderInfo.find(V);
445 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
446
447 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000448
Chris Lattner735f2702004-07-08 22:30:50 +0000449 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000450 if (TheRealValue) {
451 V->replaceAllUsesWith(TheRealValue);
452 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000453 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000454 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000455 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000456 // resolver table
457 InsertValue(V, *FutureLateResolvers);
458 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000459 if (DID.Type == ValID::NameVal)
460 ThrowException("Reference to an invalid definition: '" +DID.getName()+
461 "' of type '" + V->getType()->getDescription() + "'",
462 PHI->second.second);
463 else
464 ThrowException("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000465 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000466 V->getType()->getDescription() + "'",
467 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000468 }
Chris Lattner00950542001-06-06 20:29:01 +0000469 }
470 }
471
472 LateResolvers.clear();
473}
474
Chris Lattner4a42e902001-10-22 05:56:09 +0000475// ResolveTypeTo - A brand new type was just declared. This means that (if
476// name is not null) things referencing Name can be resolved. Otherwise, things
477// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000478//
Chris Lattner4a42e902001-10-22 05:56:09 +0000479static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000480 ValID D;
481 if (Name) D = ValID::create(Name);
482 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000483
Chris Lattner355ad1f2005-03-21 06:27:42 +0000484 std::map<ValID, PATypeHolder>::iterator I =
485 CurModule.LateResolveTypes.find(D);
486 if (I != CurModule.LateResolveTypes.end()) {
487 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
488 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000489 }
490}
491
Chris Lattner1781aca2001-09-18 04:00:54 +0000492// setValueName - Set the specified value to the name given. The name may be
493// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000494// assumed to be a malloc'd string buffer, and is free'd by this function.
495//
496static void setValueName(Value *V, char *NameStr) {
497 if (NameStr) {
498 std::string Name(NameStr); // Copy string
499 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000500
Misha Brukman5a2a3822005-05-10 22:02:28 +0000501 if (V->getType() == Type::VoidTy)
Chris Lattnerc9aea522004-07-13 08:12:39 +0000502 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000503
Chris Lattner8ab406d2004-07-13 07:59:27 +0000504 assert(inFunctionScope() && "Must be in function scope!");
505 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
506 if (ST.lookup(V->getType(), Name))
507 ThrowException("Redefinition of value named '" + Name + "' in the '" +
508 V->getType()->getDescription() + "' type plane!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000509
Chris Lattnerc9aea522004-07-13 08:12:39 +0000510 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000511 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000512 }
513}
514
Chris Lattnerd88998d2004-07-14 08:23:52 +0000515/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
516/// this is a declaration, otherwise it is a definition.
517static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
518 bool isConstantGlobal, const Type *Ty,
519 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000520 if (isa<FunctionType>(Ty))
521 ThrowException("Cannot declare global vars of function type!");
522
Misha Brukman5a2a3822005-05-10 22:02:28 +0000523 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000524
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000525 std::string Name;
526 if (NameStr) {
527 Name = NameStr; // Copy string
528 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000529 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000530
Chris Lattner8a327842004-07-14 21:44:00 +0000531 // See if this global value was forward referenced. If so, recycle the
532 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000533 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000534 if (!Name.empty()) {
535 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000536 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000537 ID = ValID::create((int)CurModule.Values[PTy].size());
538 }
539
540 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000541 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000542 // previously inserted.
543 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
544 CurModule.CurrentModule->getGlobalList().remove(GV);
545 CurModule.CurrentModule->getGlobalList().push_back(GV);
546 GV->setInitializer(Initializer);
547 GV->setLinkage(Linkage);
548 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000549 InsertValue(GV, CurModule.Values);
Chris Lattnera09000d2004-07-14 23:03:46 +0000550 return;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000551 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000552
553 // If this global has a name, check to see if there is already a definition
554 // of this global in the module. If so, merge as appropriate. Note that
555 // this is really just a hack around problems in the CFE. :(
556 if (!Name.empty()) {
557 // We are a simple redefinition of a value, check to see if it is defined
558 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000559 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000560 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
561 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000562 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000563 // 2. If both initializers have the same value.
564 //
565 if (!EGV->hasInitializer() || !Initializer ||
566 EGV->getInitializer() == Initializer) {
567
568 // Make sure the existing global version gets the initializer! Make
569 // sure that it also gets marked const if the new version is.
570 if (Initializer && !EGV->hasInitializer())
571 EGV->setInitializer(Initializer);
572 if (isConstantGlobal)
573 EGV->setConstant(true);
574 EGV->setLinkage(Linkage);
575 return;
576 }
577
Misha Brukman5a2a3822005-05-10 22:02:28 +0000578 ThrowException("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000579 "' in the '" + Ty->getDescription() + "' type plane!");
580 }
581 }
582
583 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000584 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000585 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000586 CurModule.CurrentModule);
587 InsertValue(GV, CurModule.Values);
Chris Lattnerd88998d2004-07-14 08:23:52 +0000588}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000589
Reid Spencer75719bf2004-05-26 21:48:31 +0000590// setTypeName - Set the specified type to the name given. The name may be
591// null potentially, in which case this is a noop. The string passed in is
592// assumed to be a malloc'd string buffer, and is freed by this function.
593//
594// This function returns true if the type has already been defined, but is
595// allowed to be redefined in the specified context. If the name is a new name
596// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000597static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000598 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000599 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000600
Reid Spencer75719bf2004-05-26 21:48:31 +0000601 std::string Name(NameStr); // Copy string
602 free(NameStr); // Free old string
603
604 // We don't allow assigning names to void type
Misha Brukman5a2a3822005-05-10 22:02:28 +0000605 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000606 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000607
Chris Lattnera09000d2004-07-14 23:03:46 +0000608 // Set the type name, checking for conflicts as we do so.
609 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000610
Chris Lattnera09000d2004-07-14 23:03:46 +0000611 if (AlreadyExists) { // Inserting a name that is already defined???
612 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
613 assert(Existing && "Conflict but no matching type?");
614
Reid Spencer75719bf2004-05-26 21:48:31 +0000615 // There is only one case where this is allowed: when we are refining an
616 // opaque type. In this case, Existing will be an opaque type.
617 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
618 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000619 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000620 return true;
621 }
622
623 // Otherwise, this is an attempt to redefine a type. That's okay if
624 // the redefinition is identical to the original. This will be so if
625 // Existing and T point to the same Type object. In this one case we
626 // allow the equivalent redefinition.
627 if (Existing == T) return true; // Yes, it's equal.
628
629 // Any other kind of (non-equivalent) redefinition is an error.
630 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000631 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000632 }
633
Reid Spencer75719bf2004-05-26 21:48:31 +0000634 return false;
635}
Chris Lattner8896eda2001-07-09 19:38:36 +0000636
Chris Lattner30c89792001-09-07 16:35:17 +0000637//===----------------------------------------------------------------------===//
638// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000639//
Chris Lattner8896eda2001-07-09 19:38:36 +0000640
Chris Lattner3b073862004-02-09 03:19:29 +0000641// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000642//
643static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000644 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000645 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000646}
647
648namespace {
649 struct UpRefRecord {
650 // NestingLevel - The number of nesting levels that need to be popped before
651 // this type is resolved.
652 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000653
Chris Lattner3b073862004-02-09 03:19:29 +0000654 // LastContainedTy - This is the type at the current binding level for the
655 // type. Every time we reduce the nesting level, this gets updated.
656 const Type *LastContainedTy;
657
658 // UpRefTy - This is the actual opaque type that the upreference is
659 // represented with.
660 OpaqueType *UpRefTy;
661
662 UpRefRecord(unsigned NL, OpaqueType *URTy)
663 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
664 };
Chris Lattner30c89792001-09-07 16:35:17 +0000665}
Chris Lattner698b56e2001-07-20 19:15:08 +0000666
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000667// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000668static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000669
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000670/// HandleUpRefs - Every time we finish a new layer of types, this function is
671/// called. It loops through the UpRefs vector, which is a list of the
672/// currently active types. For each type, if the up reference is contained in
673/// the newly completed type, we decrement the level count. When the level
674/// count reaches zero, the upreferenced type is the type that is passed in:
675/// thus we can complete the cycle.
676///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000677static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000678 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000679 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000680 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000681 "' newly formed. Resolving upreferences.\n" <<
682 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000683
684 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
685 // to zero), we resolve them all together before we resolve them to Ty. At
686 // the end of the loop, if there is anything to resolve to Ty, it will be in
687 // this variable.
688 OpaqueType *TypeToResolve = 0;
689
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000690 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000691 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
692 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000693 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000694 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
695 // Decrement level of upreference
696 unsigned Level = --UpRefs[i].NestingLevel;
697 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000698 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000699 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000700 if (!TypeToResolve) {
701 TypeToResolve = UpRefs[i].UpRefTy;
702 } else {
703 UR_OUT(" * Resolving upreference for "
704 << UpRefs[i].second->getDescription() << "\n";
705 std::string OldName = UpRefs[i].UpRefTy->getDescription());
706 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
707 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
708 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
709 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000710 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000711 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000712 }
713 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000714 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000715
Chris Lattner026a8ce2004-02-09 18:53:54 +0000716 if (TypeToResolve) {
717 UR_OUT(" * Resolving upreference for "
718 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000719 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000720 TypeToResolve->refineAbstractTypeTo(Ty);
721 }
722
Chris Lattner8896eda2001-07-09 19:38:36 +0000723 return Ty;
724}
725
Chris Lattner30c89792001-09-07 16:35:17 +0000726
Chris Lattner6184feb2005-05-20 03:25:47 +0000727// common code from the two 'RunVMAsmParser' functions
728 static Module * RunParser(Module * M) {
729
730 llvmAsmlineno = 1; // Reset the current line number...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000731 ObsoleteVarArgs = false;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000732 NewVarArgs = false;
Chris Lattner6184feb2005-05-20 03:25:47 +0000733
734 CurModule.CurrentModule = M;
735 yyparse(); // Parse the file, potentially throwing exception
736
737 Module *Result = ParserResult;
738 ParserResult = 0;
739
Andrew Lenharth31c98bf2005-06-20 15:41:37 +0000740 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
741 {
742 Function* F;
743 if ((F = Result->getNamedFunction("llvm.va_start"))
744 && F->getFunctionType()->getNumParams() == 0)
745 ObsoleteVarArgs = true;
746 if((F = Result->getNamedFunction("llvm.va_copy"))
747 && F->getFunctionType()->getNumParams() == 1)
748 ObsoleteVarArgs = true;
749 }
750
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000751 if (ObsoleteVarArgs && NewVarArgs)
Chris Lattner548021f2005-06-24 18:00:40 +0000752 ThrowException("This file is corrupt: it uses both new and old style varargs");
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000753
Andrew Lenharth558bc882005-06-18 18:34:52 +0000754 if(ObsoleteVarArgs) {
755 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000756 if (F->arg_size() != 0)
757 ThrowException("Obsolete va_start takes 0 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000758
759 //foo = va_start()
760 // ->
761 //bar = alloca typeof(foo)
762 //va_start(bar)
763 //foo = load bar
764
765 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
766 const Type* ArgTy = F->getFunctionType()->getReturnType();
767 const Type* ArgTyPtr = PointerType::get(ArgTy);
768 Function* NF = Result->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000769 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000770
771 while (!F->use_empty()) {
772 CallInst* CI = cast<CallInst>(F->use_back());
773 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
774 new CallInst(NF, bar, "", CI);
775 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
776 CI->replaceAllUsesWith(foo);
777 CI->getParent()->getInstList().erase(CI);
778 }
779 Result->getFunctionList().erase(F);
780 }
781
782 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000783 if(F->arg_size() != 1)
784 ThrowException("Obsolete va_end takes 1 argument!");
785
Andrew Lenharth558bc882005-06-18 18:34:52 +0000786 //vaend foo
787 // ->
788 //bar = alloca 1 of typeof(foo)
789 //vaend bar
790 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
791 const Type* ArgTy = F->getFunctionType()->getParamType(0);
792 const Type* ArgTyPtr = PointerType::get(ArgTy);
793 Function* NF = Result->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000794 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000795
796 while (!F->use_empty()) {
797 CallInst* CI = cast<CallInst>(F->use_back());
798 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000799 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000800 new CallInst(NF, bar, "", CI);
801 CI->getParent()->getInstList().erase(CI);
802 }
803 Result->getFunctionList().erase(F);
804 }
805
806 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000807 if(F->arg_size() != 1)
808 ThrowException("Obsolete va_copy takes 1 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000809 //foo = vacopy(bar)
810 // ->
811 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000812 //b = alloca 1 of typeof(foo)
813 //store bar -> b
814 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000815 //foo = load a
816
817 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
818 const Type* ArgTy = F->getFunctionType()->getReturnType();
819 const Type* ArgTyPtr = PointerType::get(ArgTy);
820 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000821 RetTy, ArgTyPtr, ArgTyPtr,
822 (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000823
824 while (!F->use_empty()) {
825 CallInst* CI = cast<CallInst>(F->use_back());
826 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000827 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
828 new StoreInst(CI->getOperand(1), b, CI);
829 new CallInst(NF, a, b, "", CI);
830 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000831 CI->replaceAllUsesWith(foo);
832 CI->getParent()->getInstList().erase(CI);
833 }
834 Result->getFunctionList().erase(F);
835 }
836 }
837
Chris Lattner6184feb2005-05-20 03:25:47 +0000838 return Result;
839
840 }
841
Chris Lattner00950542001-06-06 20:29:01 +0000842//===----------------------------------------------------------------------===//
843// RunVMAsmParser - Define an interface to this parser
844//===----------------------------------------------------------------------===//
845//
Chris Lattner86427632004-07-14 06:39:48 +0000846Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000847 set_scan_file(F);
848
Chris Lattnera2850432001-07-22 18:36:00 +0000849 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000850 return RunParser(new Module(CurFilename));
851}
Chris Lattner00950542001-06-06 20:29:01 +0000852
Chris Lattner6184feb2005-05-20 03:25:47 +0000853Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
854 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000855
Chris Lattner6184feb2005-05-20 03:25:47 +0000856 CurFilename = "from_memory";
857 if (M == NULL) {
858 return RunParser(new Module (CurFilename));
859 } else {
860 return RunParser(M);
861 }
Chris Lattner00950542001-06-06 20:29:01 +0000862}
863
864%}
865
866%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000867 llvm::Module *ModuleVal;
868 llvm::Function *FunctionVal;
869 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
870 llvm::BasicBlock *BasicBlockVal;
871 llvm::TerminatorInst *TermInstVal;
872 llvm::Instruction *InstVal;
873 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000874
Brian Gaeked0fde302003-11-11 22:41:34 +0000875 const llvm::Type *PrimType;
876 llvm::PATypeHolder *TypeVal;
877 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000878
Brian Gaeked0fde302003-11-11 22:41:34 +0000879 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
880 std::vector<llvm::Value*> *ValueList;
881 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000882 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000883 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000884 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000885 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
886 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000887
Brian Gaeked0fde302003-11-11 22:41:34 +0000888 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000889 int64_t SInt64Val;
890 uint64_t UInt64Val;
891 int SIntVal;
892 unsigned UIntVal;
893 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000894 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000895
Chris Lattner30c89792001-09-07 16:35:17 +0000896 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000897 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000898
Brian Gaeked0fde302003-11-11 22:41:34 +0000899 llvm::Instruction::BinaryOps BinaryOpVal;
900 llvm::Instruction::TermOps TermOpVal;
901 llvm::Instruction::MemoryOps MemOpVal;
902 llvm::Instruction::OtherOps OtherOpVal;
903 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000904}
905
Chris Lattner79df7c02002-03-26 18:01:55 +0000906%type <ModuleVal> Module FunctionList
907%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000908%type <BasicBlockVal> BasicBlock InstructionList
909%type <TermInstVal> BBTerminatorInst
910%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000911%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000912%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000913%type <ArgList> ArgList ArgListH
914%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000915%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000916%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000917%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000918%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000919%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000920%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000921%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000922%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000923%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000924%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000925
Chris Lattner2079fde2001-10-13 06:41:08 +0000926// ValueRef - Unresolved reference to a definition or BB
927%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000928%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000929// Tokens and types for handling constant integer values
930//
931// ESINT64VAL - A negative number within long long range
932%token <SInt64Val> ESINT64VAL
933
934// EUINT64VAL - A positive number within uns. long long range
935%token <UInt64Val> EUINT64VAL
936%type <SInt64Val> EINT64VAL
937
938%token <SIntVal> SINTVAL // Signed 32 bit ints...
939%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
940%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000941%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000942
943// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000944%type <TypeVal> Types TypesV UpRTypes UpRTypesV
945%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000946%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
947%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000948
Chris Lattner6c23f572003-08-22 05:42:10 +0000949%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
950%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000951
952
Chris Lattner91ef4602004-03-31 03:49:47 +0000953%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000954%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000955%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Nate Begeman14b05292005-11-05 09:21:28 +0000956%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
Chris Lattnerddb6db42005-05-06 05:51:46 +0000957%token DEPLIBS CALL TAIL
Chris Lattnera8e8f162005-05-06 20:27:19 +0000958%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK
959%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +0000960
Misha Brukman5a2a3822005-05-10 22:02:28 +0000961// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000962%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000963
Misha Brukman5a2a3822005-05-10 22:02:28 +0000964// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000965%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000966%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000967%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000968
969// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000970%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000971
Chris Lattner027dcc52001-07-08 21:10:27 +0000972// Other Operators
973%type <OtherOpVal> ShiftOps
Andrew Lenharth558bc882005-06-18 18:34:52 +0000974%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG
975%token VAARG_old VANEXT_old //OBSOLETE
Chris Lattnerddb6db42005-05-06 05:51:46 +0000976
Chris Lattner027dcc52001-07-08 21:10:27 +0000977
Chris Lattner00950542001-06-06 20:29:01 +0000978%start Module
979%%
980
981// Handle constant integer size restriction and conversion...
982//
Chris Lattner51727be2002-06-04 21:58:56 +0000983INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000984INTVAL : UINTVAL {
985 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
986 ThrowException("Value too large for type!");
987 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000988};
Chris Lattner00950542001-06-06 20:29:01 +0000989
990
Chris Lattner51727be2002-06-04 21:58:56 +0000991EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000992EINT64VAL : EUINT64VAL {
993 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
994 ThrowException("Value too large for type!");
995 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000996};
Chris Lattner00950542001-06-06 20:29:01 +0000997
Misha Brukman5a2a3822005-05-10 22:02:28 +0000998// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +0000999// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1000//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001001ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1002LogicalOps : AND | OR | XOR;
1003SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +00001004
Chris Lattner51727be2002-06-04 21:58:56 +00001005ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001006
Chris Lattnere98dda62001-07-14 06:10:16 +00001007// These are some types that allow classification if we only want a particular
1008// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001009SIntType : LONG | INT | SHORT | SBYTE;
1010UIntType : ULONG | UINT | USHORT | UBYTE;
1011IntType : SIntType | UIntType;
1012FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001013
Chris Lattnere98dda62001-07-14 06:10:16 +00001014// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001015OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001016 $$ = $1;
1017 }
Misha Brukman5a2a3822005-05-10 22:02:28 +00001018 | /*empty*/ {
1019 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001020 };
Chris Lattner00950542001-06-06 20:29:01 +00001021
Chris Lattner4ad02e72003-04-16 20:28:45 +00001022OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1023 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001024 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001025 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1026 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001027
Chris Lattnera8e8f162005-05-06 20:27:19 +00001028OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1029 CCC_TOK { $$ = CallingConv::C; } |
1030 FASTCC_TOK { $$ = CallingConv::Fast; } |
1031 COLDCC_TOK { $$ = CallingConv::Cold; } |
1032 CC_TOK EUINT64VAL {
1033 if ((unsigned)$2 != $2)
1034 ThrowException("Calling conv too large!");
1035 $$ = $2;
1036 };
1037
Chris Lattner30c89792001-09-07 16:35:17 +00001038//===----------------------------------------------------------------------===//
1039// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001040// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001041// access to it, a user must explicitly use TypesV.
1042//
1043
1044// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001045TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1046UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001047
1048Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001049 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001050 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1051 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001052 };
Chris Lattner30c89792001-09-07 16:35:17 +00001053
1054
1055// Derived types are added later...
1056//
Chris Lattner51727be2002-06-04 21:58:56 +00001057PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1058PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001059UpRTypes : OPAQUE {
1060 $$ = new PATypeHolder(OpaqueType::get());
1061 }
1062 | PrimType {
1063 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001064 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001065UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001066 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001067};
Chris Lattner30c89792001-09-07 16:35:17 +00001068
Chris Lattner30c89792001-09-07 16:35:17 +00001069// Include derived types in the Types production.
1070//
1071UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001072 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001073 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001074 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001075 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001076 UR_OUT("New Upreference!\n");
1077 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001078 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001079 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001080 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1081 E = $3->end(); I != E; ++I)
1082 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001083 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1084 if (isVarArg) Params.pop_back();
1085
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001086 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001087 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001088 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001089 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001090 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001091 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001092 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001093 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001094 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1095 const llvm::Type* ElemTy = $4->get();
1096 if ((unsigned)$2 != $2) {
1097 ThrowException("Unsigned result not equal to signed result");
1098 }
1099 if(!ElemTy->isPrimitiveType()) {
1100 ThrowException("Elemental type of a PackedType must be primitive");
1101 }
1102 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1103 delete $4;
1104 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001105 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001106 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001107 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1108 E = $2->end(); I != E; ++I)
1109 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001110
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001111 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001112 delete $2;
1113 }
1114 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001115 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001116 }
1117 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001118 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001119 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001120 };
Chris Lattner30c89792001-09-07 16:35:17 +00001121
Chris Lattner7e708292002-06-25 16:13:24 +00001122// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001123// declaration type lists
1124//
1125TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001126 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001127 $$->push_back(*$1); delete $1;
1128 }
1129 | TypeListI ',' UpRTypes {
1130 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001131 };
Chris Lattner30c89792001-09-07 16:35:17 +00001132
Chris Lattner7e708292002-06-25 16:13:24 +00001133// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001134ArgTypeListI : TypeListI
1135 | TypeListI ',' DOTDOTDOT {
1136 ($$=$1)->push_back(Type::VoidTy);
1137 }
1138 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001139 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001140 }
1141 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001142 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001143 };
Chris Lattner30c89792001-09-07 16:35:17 +00001144
Chris Lattnere98dda62001-07-14 06:10:16 +00001145// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001146// production is used ONLY to represent constants that show up AFTER a 'const',
1147// 'constant' or 'global' token at global scope. Constants that can be inlined
1148// into other expressions (such as integers and constexprs) are handled by the
1149// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001150//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001151ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001152 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001153 if (ATy == 0)
1154 ThrowException("Cannot make array constant with type: '" +
1155 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001156 const Type *ETy = ATy->getElementType();
1157 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001158
Chris Lattner30c89792001-09-07 16:35:17 +00001159 // Verify that we have the correct size...
1160 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001161 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001162 utostr($3->size()) + " arguments, but has size of " +
1163 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001164
Chris Lattner30c89792001-09-07 16:35:17 +00001165 // Verify all elements are correct type!
1166 for (unsigned i = 0; i < $3->size(); i++) {
1167 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001168 ThrowException("Element #" + utostr(i) + " is not of type '" +
1169 ETy->getDescription() +"' as required!\nIt is of type '"+
1170 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001171 }
1172
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001173 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001174 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001175 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001176 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001177 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001178 if (ATy == 0)
1179 ThrowException("Cannot make array constant with type: '" +
1180 (*$1)->getDescription() + "'!");
1181
1182 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001183 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001184 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001185 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001186 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001187 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001188 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001189 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001190 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001191 if (ATy == 0)
1192 ThrowException("Cannot make array constant with type: '" +
1193 (*$1)->getDescription() + "'!");
1194
Chris Lattner30c89792001-09-07 16:35:17 +00001195 int NumElements = ATy->getNumElements();
1196 const Type *ETy = ATy->getElementType();
1197 char *EndStr = UnEscapeLexed($3, true);
1198 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001199 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001200 itostr((int)(EndStr-$3)) +
1201 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001202 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001203 if (ETy == Type::SByteTy) {
1204 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001205 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001206 } else if (ETy == Type::UByteTy) {
1207 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001208 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001209 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001210 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001211 ThrowException("Cannot build string arrays of non byte sized elements!");
1212 }
Chris Lattner30c89792001-09-07 16:35:17 +00001213 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001214 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001215 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001216 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001217 | Types '<' ConstVector '>' { // Nonempty unsized arr
1218 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1219 if (PTy == 0)
1220 ThrowException("Cannot make packed constant with type: '" +
1221 (*$1)->getDescription() + "'!");
1222 const Type *ETy = PTy->getElementType();
1223 int NumElements = PTy->getNumElements();
1224
1225 // Verify that we have the correct size...
1226 if (NumElements != -1 && NumElements != (int)$3->size())
1227 ThrowException("Type mismatch: constant sized packed initialized with " +
1228 utostr($3->size()) + " arguments, but has size of " +
1229 itostr(NumElements) + "!");
1230
1231 // Verify all elements are correct type!
1232 for (unsigned i = 0; i < $3->size(); i++) {
1233 if (ETy != (*$3)[i]->getType())
1234 ThrowException("Element #" + utostr(i) + " is not of type '" +
1235 ETy->getDescription() +"' as required!\nIt is of type '"+
1236 (*$3)[i]->getType()->getDescription() + "'.");
1237 }
1238
1239 $$ = ConstantPacked::get(PTy, *$3);
1240 delete $1; delete $3;
1241 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001242 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001243 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001244 if (STy == 0)
1245 ThrowException("Cannot make struct constant with type: '" +
1246 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001247
Chris Lattnerc6212f12003-05-21 16:06:56 +00001248 if ($3->size() != STy->getNumContainedTypes())
1249 ThrowException("Illegal number of initializers for structure type!");
1250
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001251 // Check to ensure that constants are compatible with the type initializer!
1252 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001253 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001254 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001255 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001256 "' for element #" + utostr(i) +
1257 " of structure initializer!");
1258
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001259 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001260 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001261 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001262 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001263 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001264 if (STy == 0)
1265 ThrowException("Cannot make struct constant with type: '" +
1266 (*$1)->getDescription() + "'!");
1267
1268 if (STy->getNumContainedTypes() != 0)
1269 ThrowException("Illegal number of initializers for structure type!");
1270
1271 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1272 delete $1;
1273 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001274 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001275 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001276 if (PTy == 0)
1277 ThrowException("Cannot make null pointer constant with type: '" +
1278 (*$1)->getDescription() + "'!");
1279
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001280 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001281 delete $1;
1282 }
Chris Lattner16710e92004-10-16 18:17:13 +00001283 | Types UNDEF {
1284 $$ = UndefValue::get($1->get());
1285 delete $1;
1286 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001287 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001288 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001289 if (Ty == 0)
1290 ThrowException("Global const reference must be a pointer type!");
1291
Chris Lattner3101c252002-08-15 17:58:33 +00001292 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001293 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001294 // the context of a function, getValNonImprovising will search the functions
1295 // symbol table instead of the module symbol table for the global symbol,
1296 // which throws things all off. To get around this, we just tell
1297 // getValNonImprovising that we are at global scope here.
1298 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001299 Function *SavedCurFn = CurFun.CurrentFunction;
1300 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001301
Chris Lattner2079fde2001-10-13 06:41:08 +00001302 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001303
Chris Lattner394f2eb2003-10-16 20:12:13 +00001304 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001305
Chris Lattner2079fde2001-10-13 06:41:08 +00001306 // If this is an initializer for a constant pointer, which is referencing a
1307 // (currently) undefined variable, create a stub now that shall be replaced
1308 // in the future with the right type of variable.
1309 //
1310 if (V == 0) {
1311 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1312 const PointerType *PT = cast<PointerType>(Ty);
1313
1314 // First check to see if the forward references value is already created!
1315 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001316 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001317
1318 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001319 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001320 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001321 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001322 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001323 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001324
Reid Spencer3271ed52004-07-18 00:08:11 +00001325 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001326 GlobalValue *GV;
1327 if (const FunctionType *FTy =
1328 dyn_cast<FunctionType>(PT->getElementType())) {
1329 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1330 CurModule.CurrentModule);
1331 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001332 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001333 GlobalValue::ExternalLinkage, 0,
1334 Name, CurModule.CurrentModule);
1335 }
Chris Lattner8a327842004-07-14 21:44:00 +00001336
Reid Spencer3271ed52004-07-18 00:08:11 +00001337 // Keep track of the fact that we have a forward ref to recycle it
1338 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1339 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001340 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001341 }
1342
Reid Spencer3271ed52004-07-18 00:08:11 +00001343 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001344 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001345 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001346 | Types ConstExpr {
1347 if ($1->get() != $2->getType())
1348 ThrowException("Mismatched types for constant expression!");
1349 $$ = $2;
1350 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001351 }
1352 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001353 const Type *Ty = $1->get();
1354 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1355 ThrowException("Cannot create a null initialized value of this type!");
1356 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001357 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001358 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001359
Chris Lattnere43f40b2002-10-09 00:25:32 +00001360ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001361 if (!ConstantSInt::isValueValidForType($1, $2))
1362 ThrowException("Constant value doesn't fit in type!");
1363 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001364 }
1365 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001366 if (!ConstantUInt::isValueValidForType($1, $2))
1367 ThrowException("Constant value doesn't fit in type!");
1368 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001369 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001370 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001371 $$ = ConstantBool::True;
1372 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001373 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001374 $$ = ConstantBool::False;
1375 }
1376 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001377 if (!ConstantFP::isValueValidForType($1, $2))
1378 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001379 $$ = ConstantFP::get($1, $2);
1380 };
1381
Chris Lattner00950542001-06-06 20:29:01 +00001382
Chris Lattnerd78700d2002-08-16 21:14:40 +00001383ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001384 if (!$3->getType()->isFirstClassType())
1385 ThrowException("cast constant expression from a non-primitive type: '" +
1386 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001387 if (!$5->get()->isFirstClassType())
1388 ThrowException("cast constant expression to a non-primitive type: '" +
1389 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001390 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001391 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001392 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001393 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1394 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001395 ThrowException("GetElementPtr requires a pointer operand!");
1396
Chris Lattner830b6f92004-04-05 01:30:04 +00001397 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1398 // indices to uint struct indices for compatibility.
1399 generic_gep_type_iterator<std::vector<Value*>::iterator>
1400 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1401 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1402 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1403 if (isa<StructType>(*GTI)) // Only change struct indices
1404 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1405 if (CUI->getType() == Type::UByteTy)
1406 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1407
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001408 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001409 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001410 if (!IdxTy)
1411 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001412
Chris Lattner9232b992003-04-22 18:42:41 +00001413 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001414 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1415 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001416 IdxVec.push_back(C);
1417 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001418 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001419
Chris Lattnerd78700d2002-08-16 21:14:40 +00001420 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001421
Chris Lattnerd78700d2002-08-16 21:14:40 +00001422 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001423 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001424 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1425 if ($3->getType() != Type::BoolTy)
1426 ThrowException("Select condition must be of boolean type!");
1427 if ($5->getType() != $7->getType())
1428 ThrowException("Select operand types must match!");
1429 $$ = ConstantExpr::getSelect($3, $5, $7);
1430 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001431 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001432 if ($3->getType() != $5->getType())
1433 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001434 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1435 // To retain backward compatibility with these early compilers, we emit a
1436 // cast to the appropriate integer type automatically if we are in the
1437 // broken case. See PR424 for more information.
1438 if (!isa<PointerType>($3->getType())) {
1439 $$ = ConstantExpr::get($1, $3, $5);
1440 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001441 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001442 switch (CurModule.CurrentModule->getPointerSize()) {
1443 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1444 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1445 default: ThrowException("invalid pointer binary constant expr!");
1446 }
1447 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1448 ConstantExpr::getCast($5, IntPtrTy));
1449 $$ = ConstantExpr::getCast($$, $3->getType());
1450 }
1451 }
1452 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1453 if ($3->getType() != $5->getType())
1454 ThrowException("Logical operator types must match!");
1455 if (!$3->getType()->isIntegral())
1456 ThrowException("Logical operands must have integral types!");
1457 $$ = ConstantExpr::get($1, $3, $5);
1458 }
1459 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1460 if ($3->getType() != $5->getType())
1461 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001462 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001463 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001464 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001465 if ($5->getType() != Type::UByteTy)
1466 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001467 if (!$3->getType()->isInteger())
1468 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001469 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001470 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001471
1472
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001473// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001474ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001475 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001476 }
1477 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001478 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001479 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001480 };
Chris Lattner00950542001-06-06 20:29:01 +00001481
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001482
Chris Lattner1781aca2001-09-18 04:00:54 +00001483// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001484GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001485
Chris Lattner00950542001-06-06 20:29:01 +00001486
Chris Lattner0e73ce62002-05-02 19:11:13 +00001487//===----------------------------------------------------------------------===//
1488// Rules to match Modules
1489//===----------------------------------------------------------------------===//
1490
1491// Module rule: Capture the result of parsing the whole file into a result
1492// variable...
1493//
1494Module : FunctionList {
1495 $$ = ParserResult = $1;
1496 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001497};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001498
Chris Lattner7e708292002-06-25 16:13:24 +00001499// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001500//
1501FunctionList : FunctionList Function {
1502 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001503 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001504 }
1505 | FunctionList FunctionProto {
1506 $$ = $1;
1507 }
1508 | FunctionList IMPLEMENTATION {
1509 $$ = $1;
1510 }
1511 | ConstPool {
1512 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001513 // Emit an error if there are any unresolved types left.
1514 if (!CurModule.LateResolveTypes.empty()) {
1515 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1516 if (DID.Type == ValID::NameVal)
1517 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1518 else
1519 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1520 }
Chris Lattner51727be2002-06-04 21:58:56 +00001521 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001522
Chris Lattnere98dda62001-07-14 06:10:16 +00001523// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001524ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001525 // Eagerly resolve types. This is not an optimization, this is a
1526 // requirement that is due to the fact that we could have this:
1527 //
1528 // %list = type { %list * }
1529 // %list = type { %list * } ; repeated type decl
1530 //
1531 // If types are not resolved eagerly, then the two types will not be
1532 // determined to be the same type!
1533 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001534 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001535
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001536 if (!setTypeName(*$4, $2) && !$2) {
1537 // If this is a named type that is not a redefinition, add it to the slot
1538 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001539 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001540 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001541
1542 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001543 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001544 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001545 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001546 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001547 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1548 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Chris Lattner1781aca2001-09-18 04:00:54 +00001549 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001550 | ConstPool OptAssign EXTERNAL GlobalType Types {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001551 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001552 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001553 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001554 | ConstPool TARGET TargetDefinition {
1555 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001556 | ConstPool DEPLIBS '=' LibrariesDefinition {
1557 }
Chris Lattner00950542001-06-06 20:29:01 +00001558 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001559 };
Chris Lattner00950542001-06-06 20:29:01 +00001560
1561
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001562
1563BigOrLittle : BIG { $$ = Module::BigEndian; };
1564BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1565
1566TargetDefinition : ENDIAN '=' BigOrLittle {
1567 CurModule.CurrentModule->setEndianness($3);
1568 }
1569 | POINTERSIZE '=' EUINT64VAL {
1570 if ($3 == 32)
1571 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1572 else if ($3 == 64)
1573 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1574 else
1575 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001576 }
1577 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001578 CurModule.CurrentModule->setTargetTriple($3);
1579 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001580 };
1581
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001582LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001583
1584LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001585 CurModule.CurrentModule->addLibrary($3);
1586 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001587 }
1588 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001589 CurModule.CurrentModule->addLibrary($1);
1590 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001591 }
1592 | /* empty: end of list */ {
1593 }
1594 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001595
Chris Lattner00950542001-06-06 20:29:01 +00001596//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001597// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001598//===----------------------------------------------------------------------===//
1599
Chris Lattner6c23f572003-08-22 05:42:10 +00001600Name : VAR_ID | STRINGCONSTANT;
1601OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001602
Chris Lattner6c23f572003-08-22 05:42:10 +00001603ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001604 if (*$1 == Type::VoidTy)
1605 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001606 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001607};
Chris Lattner00950542001-06-06 20:29:01 +00001608
Chris Lattner69da5cf2002-10-13 20:57:00 +00001609ArgListH : ArgListH ',' ArgVal {
1610 $$ = $1;
1611 $1->push_back(*$3);
1612 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001613 }
1614 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001615 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001616 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001617 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001618 };
Chris Lattner00950542001-06-06 20:29:01 +00001619
1620ArgList : ArgListH {
1621 $$ = $1;
1622 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001623 | ArgListH ',' DOTDOTDOT {
1624 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001625 $$->push_back(std::pair<PATypeHolder*,
1626 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001627 }
1628 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001629 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1630 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001631 }
Chris Lattner00950542001-06-06 20:29:01 +00001632 | /* empty */ {
1633 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001634 };
Chris Lattner00950542001-06-06 20:29:01 +00001635
Chris Lattnera8e8f162005-05-06 20:27:19 +00001636FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')' {
1637 UnEscapeLexed($3);
1638 std::string FunctionName($3);
1639 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001640
Chris Lattnera8e8f162005-05-06 20:27:19 +00001641 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001642 ThrowException("LLVM functions cannot return aggregate types!");
1643
Chris Lattner9232b992003-04-22 18:42:41 +00001644 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001645 if ($5) { // If there are arguments...
1646 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1647 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001648 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001649 }
Chris Lattner00950542001-06-06 20:29:01 +00001650
Chris Lattner2079fde2001-10-13 06:41:08 +00001651 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1652 if (isVarArg) ParamTypeList.pop_back();
1653
Chris Lattnera8e8f162005-05-06 20:27:19 +00001654 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001655 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001656 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001657
Chris Lattnera09000d2004-07-14 23:03:46 +00001658 ValID ID;
1659 if (!FunctionName.empty()) {
1660 ID = ValID::create((char*)FunctionName.c_str());
1661 } else {
1662 ID = ValID::create((int)CurModule.Values[PFT].size());
1663 }
1664
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001665 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001666 // See if this function was forward referenced. If so, recycle the object.
1667 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1668 // Move the function to the end of the list, from whereever it was
1669 // previously inserted.
1670 Fn = cast<Function>(FWRef);
1671 CurModule.CurrentModule->getFunctionList().remove(Fn);
1672 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1673 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1674 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1675 // If this is the case, either we need to be a forward decl, or it needs
1676 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001677 if (!CurFun.isDeclare && !Fn->isExternal())
1678 ThrowException("Redefinition of function '" + FunctionName + "'!");
1679
Chris Lattnera09000d2004-07-14 23:03:46 +00001680 // Make sure to strip off any argument names so we can't get conflicts.
1681 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001682 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001683 AI != AE; ++AI)
1684 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001685
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001686 } else { // Not already defined?
1687 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1688 CurModule.CurrentModule);
1689 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001690 }
Chris Lattner00950542001-06-06 20:29:01 +00001691
Chris Lattner394f2eb2003-10-16 20:12:13 +00001692 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001693 Fn->setCallingConv($1);
Chris Lattner00950542001-06-06 20:29:01 +00001694
Chris Lattner7e708292002-06-25 16:13:24 +00001695 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001696 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001697 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001698 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001699 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001700 delete $5->back().first;
1701 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001702 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001703 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001704 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1705 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001706 delete I->first; // Delete the typeholder...
1707
Chris Lattner8ab406d2004-07-13 07:59:27 +00001708 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001709 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001710 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001711
Chris Lattnera8e8f162005-05-06 20:27:19 +00001712 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001713 }
Chris Lattner51727be2002-06-04 21:58:56 +00001714};
Chris Lattner00950542001-06-06 20:29:01 +00001715
Chris Lattner9b02cc32002-05-03 18:23:48 +00001716BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1717
Chris Lattner4ad02e72003-04-16 20:28:45 +00001718FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001719 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001720
Chris Lattner4ad02e72003-04-16 20:28:45 +00001721 // Make sure that we keep track of the linkage type even if there was a
1722 // previous "declare".
1723 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001724};
Chris Lattner00950542001-06-06 20:29:01 +00001725
Chris Lattner9b02cc32002-05-03 18:23:48 +00001726END : ENDTOK | '}'; // Allow end of '}' to end a function
1727
Chris Lattner79df7c02002-03-26 18:01:55 +00001728Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001729 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001730};
Chris Lattner00950542001-06-06 20:29:01 +00001731
Chris Lattner394f2eb2003-10-16 20:12:13 +00001732FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1733 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001734 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001735};
Chris Lattner00950542001-06-06 20:29:01 +00001736
1737//===----------------------------------------------------------------------===//
1738// Rules to match Basic Blocks
1739//===----------------------------------------------------------------------===//
1740
1741ConstValueRef : ESINT64VAL { // A reference to a direct constant
1742 $$ = ValID::create($1);
1743 }
1744 | EUINT64VAL {
1745 $$ = ValID::create($1);
1746 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001747 | FPVAL { // Perhaps it's an FP constant?
1748 $$ = ValID::create($1);
1749 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001750 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001751 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001752 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001753 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001754 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001755 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001756 | NULL_TOK {
1757 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001758 }
Chris Lattner16710e92004-10-16 18:17:13 +00001759 | UNDEF {
1760 $$ = ValID::createUndef();
1761 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001762 | '<' ConstVector '>' { // Nonempty unsized packed vector
1763 const Type *ETy = (*$2)[0]->getType();
1764 int NumElements = $2->size();
1765
1766 PackedType* pt = PackedType::get(ETy, NumElements);
1767 PATypeHolder* PTy = new PATypeHolder(
1768 HandleUpRefs(
1769 PackedType::get(
1770 ETy,
1771 NumElements)
1772 )
1773 );
1774
1775 // Verify all elements are correct type!
1776 for (unsigned i = 0; i < $2->size(); i++) {
1777 if (ETy != (*$2)[i]->getType())
1778 ThrowException("Element #" + utostr(i) + " is not of type '" +
1779 ETy->getDescription() +"' as required!\nIt is of type '" +
1780 (*$2)[i]->getType()->getDescription() + "'.");
1781 }
1782
1783 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1784 delete PTy; delete $2;
1785 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001786 | ConstExpr {
1787 $$ = ValID::create($1);
1788 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001789
Chris Lattner2079fde2001-10-13 06:41:08 +00001790// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1791// another value.
1792//
1793SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001794 $$ = ValID::create($1);
1795 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001796 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001797 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001798 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001799
1800// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001801ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001802
Chris Lattner00950542001-06-06 20:29:01 +00001803
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001804// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1805// type immediately preceeds the value reference, and allows complex constant
1806// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001807ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001808 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001809 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001810
Chris Lattner00950542001-06-06 20:29:01 +00001811BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001812 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001813 }
Chris Lattner7e708292002-06-25 16:13:24 +00001814 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001815 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001816 };
Chris Lattner00950542001-06-06 20:29:01 +00001817
1818
1819// Basic blocks are terminated by branching instructions:
1820// br, br/cc, switch, ret
1821//
Chris Lattner2079fde2001-10-13 06:41:08 +00001822BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001823 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001824 InsertValue($3);
1825
1826 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001827 InsertValue($1);
1828 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001829 };
Chris Lattner00950542001-06-06 20:29:01 +00001830
1831InstructionList : InstructionList Inst {
1832 $1->getInstList().push_back($2);
1833 $$ = $1;
1834 }
1835 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001836 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001837
1838 // Make sure to move the basic block to the correct location in the
1839 // function, instead of leaving it inserted wherever it was first
1840 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001841 Function::BasicBlockListType &BBL =
1842 CurFun.CurrentFunction->getBasicBlockList();
1843 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner22ae2322004-07-13 08:39:15 +00001844 }
1845 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001846 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001847
1848 // Make sure to move the basic block to the correct location in the
1849 // function, instead of leaving it inserted wherever it was first
1850 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001851 Function::BasicBlockListType &BBL =
1852 CurFun.CurrentFunction->getBasicBlockList();
1853 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner51727be2002-06-04 21:58:56 +00001854 };
Chris Lattner00950542001-06-06 20:29:01 +00001855
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001856BBTerminatorInst : RET ResolvedVal { // Return with a result...
1857 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001858 }
1859 | RET VOID { // Return with no result...
1860 $$ = new ReturnInst();
1861 }
1862 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001863 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001864 } // Conditional Branch...
1865 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001866 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001867 }
1868 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001869 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001870 $$ = S;
1871
Chris Lattner9232b992003-04-22 18:42:41 +00001872 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001873 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001874 for (; I != E; ++I) {
1875 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
1876 S->addCase(CI, I->second);
1877 else
1878 ThrowException("Switch case is constant, but not a simple integer!");
1879 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001880 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001881 }
Chris Lattner196850c2003-05-15 21:30:00 +00001882 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001883 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00001884 $$ = S;
1885 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001886 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1887 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001888 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001889 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001890
Chris Lattnera8e8f162005-05-06 20:27:19 +00001891 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001892 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001893 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001894 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001895 if ($6) {
1896 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00001897 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001898 ParamTypes.push_back((*I)->getType());
1899 }
1900
1901 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1902 if (isVarArg) ParamTypes.pop_back();
1903
Chris Lattnera8e8f162005-05-06 20:27:19 +00001904 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001905 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001906 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001907
Chris Lattnera8e8f162005-05-06 20:27:19 +00001908 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001909
Chris Lattnera8e8f162005-05-06 20:27:19 +00001910 BasicBlock *Normal = getBBVal($10);
1911 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00001912
1913 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001914 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001915 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001916 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001917 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001918 // correctly!
1919 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001920 FunctionType::param_iterator I = Ty->param_begin();
1921 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001922 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001923
1924 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001925 if ((*ArgI)->getType() != *I)
1926 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1927 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001928
1929 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001930 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001931
Chris Lattnera8e8f162005-05-06 20:27:19 +00001932 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00001933 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001934 cast<InvokeInst>($$)->setCallingConv($2);
1935
1936 delete $3;
1937 delete $6;
Chris Lattner36143fc2003-09-08 18:54:55 +00001938 }
1939 | UNWIND {
1940 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00001941 }
1942 | UNREACHABLE {
1943 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001944 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001945
1946
Chris Lattner00950542001-06-06 20:29:01 +00001947
1948JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1949 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001950 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001951 if (V == 0)
1952 ThrowException("May only switch on a constant pool value!");
1953
Chris Lattner64116f92004-07-14 01:33:11 +00001954 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001955 }
1956 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001957 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001958 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001959
1960 if (V == 0)
1961 ThrowException("May only switch on a constant pool value!");
1962
Chris Lattner64116f92004-07-14 01:33:11 +00001963 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001964 };
Chris Lattner00950542001-06-06 20:29:01 +00001965
1966Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001967 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001968 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001969 InsertValue($2);
1970 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001971};
Chris Lattner00950542001-06-06 20:29:01 +00001972
Chris Lattnerc24d2082001-06-11 15:04:20 +00001973PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001974 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001975 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001976 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001977 }
1978 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1979 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001980 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001981 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001982 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001983
1984
Chris Lattner30c89792001-09-07 16:35:17 +00001985ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001986 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001987 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001988 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001989 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001990 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001991 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001992 };
Chris Lattner00950542001-06-06 20:29:01 +00001993
1994// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001995ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001996
Chris Lattnerddb6db42005-05-06 05:51:46 +00001997OptTailCall : TAIL CALL {
1998 $$ = true;
1999 }
2000 | CALL {
2001 $$ = false;
2002 };
2003
2004
2005
Chris Lattner4a6482b2002-09-10 19:57:26 +00002006InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00002007 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2008 !isa<PackedType>((*$2).get()))
2009 ThrowException(
2010 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00002011 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
2012 ThrowException("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002013 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2014 if ($$ == 0)
2015 ThrowException("binary operator returned null!");
2016 delete $2;
2017 }
2018 | LogicalOps Types ValueRef ',' ValueRef {
2019 if (!(*$2)->isIntegral())
2020 ThrowException("Logical operator requires integral operands!");
2021 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2022 if ($$ == 0)
2023 ThrowException("binary operator returned null!");
2024 delete $2;
2025 }
2026 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002027 if(isa<PackedType>((*$2).get())) {
2028 ThrowException(
2029 "PackedTypes currently not supported in setcc instructions!");
2030 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00002031 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00002032 if ($$ == 0)
2033 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002034 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002035 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002036 | NOT ResolvedVal {
2037 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2038 << " Replacing with 'xor'.\n";
2039
2040 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2041 if (Ones == 0)
2042 ThrowException("Expected integral type for not instruction!");
2043
2044 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002045 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00002046 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00002047 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002048 | ShiftOps ResolvedVal ',' ResolvedVal {
2049 if ($4->getType() != Type::UByteTy)
2050 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002051 if (!$2->getType()->isInteger())
2052 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002053 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00002054 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002055 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002056 if (!$4->get()->isFirstClassType())
2057 ThrowException("cast instruction to a non-primitive type: '" +
2058 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002059 $$ = new CastInst($2, *$4);
2060 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002061 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002062 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2063 if ($2->getType() != Type::BoolTy)
2064 ThrowException("select condition must be boolean!");
2065 if ($4->getType() != $6->getType())
2066 ThrowException("select value types should match!");
2067 $$ = new SelectInst($2, $4, $6);
2068 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002069 | VAARG ResolvedVal ',' Types {
Andrew Lenharthe78f50d2005-06-19 03:53:56 +00002070 NewVarArgs = true;
Chris Lattner99e7ab72003-10-18 05:53:13 +00002071 $$ = new VAArgInst($2, *$4);
2072 delete $4;
2073 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002074 | VAARG_old ResolvedVal ',' Types {
2075 ObsoleteVarArgs = true;
2076 const Type* ArgTy = $2->getType();
2077 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002078 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002079
2080 //b = vaarg a, t ->
2081 //foo = alloca 1 of t
2082 //bar = vacopy a
2083 //store bar -> foo
2084 //b = vaarg foo, t
2085 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2086 CurBB->getInstList().push_back(foo);
2087 CallInst* bar = new CallInst(NF, $2);
2088 CurBB->getInstList().push_back(bar);
2089 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2090 $$ = new VAArgInst(foo, *$4);
2091 delete $4;
2092 }
2093 | VANEXT_old ResolvedVal ',' Types {
2094 ObsoleteVarArgs = true;
2095 const Type* ArgTy = $2->getType();
2096 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002097 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002098
2099 //b = vanext a, t ->
2100 //foo = alloca 1 of t
2101 //bar = vacopy a
2102 //store bar -> foo
2103 //tmp = vaarg foo, t
2104 //b = load foo
2105 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2106 CurBB->getInstList().push_back(foo);
2107 CallInst* bar = new CallInst(NF, $2);
2108 CurBB->getInstList().push_back(bar);
2109 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2110 Instruction* tmp = new VAArgInst(foo, *$4);
2111 CurBB->getInstList().push_back(tmp);
2112 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002113 delete $4;
2114 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002115 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002116 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002117 if (!Ty->isFirstClassType())
2118 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002119 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002120 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002121 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002122 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00002123 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002124 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002125 $2->pop_front();
2126 }
2127 delete $2; // Free the list...
Chris Lattnerddb6db42005-05-06 05:51:46 +00002128 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002129 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002130 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002131 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002132
Chris Lattnera8e8f162005-05-06 20:27:19 +00002133 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002134 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002135 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002136 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002137 if ($6) {
2138 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002139 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002140 ParamTypes.push_back((*I)->getType());
2141 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002142
2143 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2144 if (isVarArg) ParamTypes.pop_back();
2145
Chris Lattnera8e8f162005-05-06 20:27:19 +00002146 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Chris Lattner595f06c2005-02-01 01:47:42 +00002147 ThrowException("LLVM functions cannot return aggregate types!");
2148
Chris Lattnera8e8f162005-05-06 20:27:19 +00002149 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002150 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002151 }
Chris Lattner00950542001-06-06 20:29:01 +00002152
Chris Lattnera8e8f162005-05-06 20:27:19 +00002153 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002154
Chris Lattner8b81bf52001-07-25 22:47:46 +00002155 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002156 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002157 // Make sure no arguments is a good thing!
2158 if (Ty->getNumParams() != 0)
2159 ThrowException("No arguments passed to a function that "
2160 "expects arguments!");
2161
Chris Lattner9232b992003-04-22 18:42:41 +00002162 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002163 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002164 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002165 // correctly!
2166 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002167 FunctionType::param_iterator I = Ty->param_begin();
2168 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002169 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002170
2171 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002172 if ((*ArgI)->getType() != *I)
2173 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2174 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002175
Chris Lattner8b81bf52001-07-25 22:47:46 +00002176 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002177 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002178
Chris Lattnera8e8f162005-05-06 20:27:19 +00002179 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002180 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002181 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002182 cast<CallInst>($$)->setCallingConv($2);
2183 delete $3;
2184 delete $6;
Chris Lattner00950542001-06-06 20:29:01 +00002185 }
2186 | MemoryInst {
2187 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002188 };
Chris Lattner00950542001-06-06 20:29:01 +00002189
Chris Lattner6cdb0112001-11-26 16:54:11 +00002190
2191// IndexList - List of indices for GEP based instructions...
2192IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002193 $$ = $2;
2194 } | /* empty */ {
2195 $$ = new std::vector<Value*>();
2196 };
2197
2198OptVolatile : VOLATILE {
2199 $$ = true;
2200 }
2201 | /* empty */ {
2202 $$ = false;
2203 };
2204
Chris Lattner027dcc52001-07-08 21:10:27 +00002205
Chris Lattnerddb6db42005-05-06 05:51:46 +00002206
Chris Lattner00950542001-06-06 20:29:01 +00002207MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002208 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002209 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002210 }
Nate Begeman14b05292005-11-05 09:21:28 +00002211 | MALLOC Types ',' ALIGN EUINT64VAL {
Chris Lattnerac6e5c12005-11-05 21:54:03 +00002212 if ($5 & ($5-1))
2213 ThrowException("Alignment amount '" + utostr($5) +
2214 "' is not a power of 2!");
Nate Begeman14b05292005-11-05 09:21:28 +00002215 $$ = new MallocInst(*$2, 0, $5);
2216 delete $2;
2217 }
Chris Lattner00950542001-06-06 20:29:01 +00002218 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002219 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002220 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002221 }
Nate Begeman14b05292005-11-05 09:21:28 +00002222 | MALLOC Types ',' UINT ValueRef ',' ALIGN EUINT64VAL {
Chris Lattnerac6e5c12005-11-05 21:54:03 +00002223 if ($8 & ($8-1))
2224 ThrowException("Alignment amount '" + utostr($8) +
2225 "' is not a power of 2!");
Nate Begeman14b05292005-11-05 09:21:28 +00002226 $$ = new MallocInst(*$2, getVal($4, $5), $8);
2227 delete $2;
2228 }
Chris Lattner00950542001-06-06 20:29:01 +00002229 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002230 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002231 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002232 }
Nate Begeman14b05292005-11-05 09:21:28 +00002233 | ALLOCA Types ',' ALIGN EUINT64VAL {
Chris Lattnerac6e5c12005-11-05 21:54:03 +00002234 if ($5 & ($5-1))
2235 ThrowException("Alignment amount '" + utostr($5) +
2236 "' is not a power of 2!");
Nate Begeman14b05292005-11-05 09:21:28 +00002237 $$ = new AllocaInst(*$2, 0, $5);
2238 delete $2;
2239 }
Chris Lattner00950542001-06-06 20:29:01 +00002240 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002241 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002242 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002243 }
Nate Begeman14b05292005-11-05 09:21:28 +00002244 | ALLOCA Types ',' UINT ValueRef ',' ALIGN EUINT64VAL {
Chris Lattnerac6e5c12005-11-05 21:54:03 +00002245 if ($8 & ($8-1))
2246 ThrowException("Alignment amount '" + utostr($8) +
2247 "' is not a power of 2!");
Nate Begeman14b05292005-11-05 09:21:28 +00002248 $$ = new AllocaInst(*$2, getVal($4, $5), $8);
2249 delete $2;
2250 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002251 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002252 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002253 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002254 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002255 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002256 }
2257
Chris Lattner15c9c032003-09-08 18:20:29 +00002258 | OptVolatile LOAD Types ValueRef {
2259 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002260 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002261 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002262 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2263 ThrowException("Can't load from pointer of non-first-class type: " +
2264 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002265 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002266 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002267 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002268 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2269 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002270 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002271 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002272 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002273 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002274 if (ElTy != $3->getType())
2275 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002276 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002277
Chris Lattner79ad13802003-09-08 20:29:46 +00002278 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002279 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002280 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002281 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002282 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002283 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002284
2285 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2286 // indices to uint struct indices for compatibility.
2287 generic_gep_type_iterator<std::vector<Value*>::iterator>
2288 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2289 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2290 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2291 if (isa<StructType>(*GTI)) // Only change struct indices
2292 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2293 if (CUI->getType() == Type::UByteTy)
2294 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2295
Chris Lattner30c89792001-09-07 16:35:17 +00002296 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002297 ThrowException("Invalid getelementptr indices for type '" +
2298 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002299 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2300 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002301 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002302
Brian Gaeked0fde302003-11-11 22:41:34 +00002303
Chris Lattner00950542001-06-06 20:29:01 +00002304%%
Chris Lattner09083092001-07-08 04:57:15 +00002305int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002306 std::string where
2307 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002308 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002309 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002310 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002311 errMsg += "end-of-file.";
2312 else
Chris Lattner9232b992003-04-22 18:42:41 +00002313 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002314 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002315 return 0;
2316}