blob: 84e064ea5c7a6bfdd1ff99cfc5d1b4d2fb75f70b [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000017#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/SymbolTable.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/STLExtras.h"
Chris Lattner87ac9722005-11-06 06:46:28 +000022#include "llvm/Support/MathExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000023#include <algorithm>
24#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000025#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000026#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner386a3b72001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000030int yyparse();
31
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000033 std::string CurFilename;
34}
35using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattner00950542001-06-06 20:29:01 +000037static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000038
Chris Lattner30c89792001-09-07 16:35:17 +000039// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
40// relating to upreferences in the input stream.
41//
42//#define DEBUG_UPREFS 1
43#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000044#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000045#else
46#define UR_OUT(X)
47#endif
48
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000049#define YYERROR_VERBOSE 1
50
Andrew Lenharth558bc882005-06-18 18:34:52 +000051static bool ObsoleteVarArgs;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +000052static bool NewVarArgs;
Andrew Lenharth558bc882005-06-18 18:34:52 +000053static BasicBlock* CurBB;
54
55
Chris Lattner7e708292002-06-25 16:13:24 +000056// This contains info used when building the body of a function. It is
57// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000058//
Chris Lattner9232b992003-04-22 18:42:41 +000059typedef std::vector<Value *> ValueList; // Numbered defs
Misha Brukman5a2a3822005-05-10 22:02:28 +000060static void
61ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
62 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000063
64static struct PerModuleInfo {
65 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000066 std::map<const Type *, ValueList> Values; // Module level numbered definitions
67 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000068 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000069 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000070
Chris Lattnerbc721ed2004-07-13 08:28:21 +000071 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
72 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000073 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000074 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
75
Chris Lattner2079fde2001-10-13 06:41:08 +000076 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
77 // references to global values. Global values may be referenced before they
78 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000079 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000080 //
Chris Lattner9232b992003-04-22 18:42:41 +000081 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000082 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000083 GlobalRefsType GlobalRefs;
84
Chris Lattner00950542001-06-06 20:29:01 +000085 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000086 // If we could not resolve some functions at function compilation time
87 // (calls to functions before they are defined), resolve them now... Types
88 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000089 //
Chris Lattner00950542001-06-06 20:29:01 +000090 ResolveDefinitions(LateResolveValues);
91
Chris Lattner2079fde2001-10-13 06:41:08 +000092 // Check to make sure that all global value forward references have been
93 // resolved!
94 //
95 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000096 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +000097
Chris Lattner749ce032002-03-11 22:12:39 +000098 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
99 I != E; ++I) {
100 UndefinedReferences += " " + I->first.first->getDescription() + " " +
101 I->first.second.getName() + "\n";
102 }
103 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000104 }
105
Chris Lattner7e708292002-06-25 16:13:24 +0000106 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000107 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000108 CurrentModule = 0;
109 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000110
111
Chris Lattner8a327842004-07-14 21:44:00 +0000112 // GetForwardRefForGlobal - Check to see if there is a forward reference
113 // for this global. If so, remove it from the GlobalRefs map and return it.
114 // If not, just return null.
115 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
116 // Check to see if there is a forward reference to this global variable...
117 // if there is, eliminate it and patch the reference to use the new def'n.
118 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
119 GlobalValue *Ret = 0;
120 if (I != GlobalRefs.end()) {
121 Ret = I->second;
122 GlobalRefs.erase(I);
123 }
124 return Ret;
125 }
Chris Lattner00950542001-06-06 20:29:01 +0000126} CurModule;
127
Chris Lattner79df7c02002-03-26 18:01:55 +0000128static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000129 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000130
Chris Lattner735f2702004-07-08 22:30:50 +0000131 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
132 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000133 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000134
Chris Lattner2e2ed292004-07-14 06:28:35 +0000135 /// BBForwardRefs - When we see forward references to basic blocks, keep
136 /// track of them here.
137 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
138 std::vector<BasicBlock*> NumberedBlocks;
139 unsigned NextBBNum;
140
Chris Lattner79df7c02002-03-26 18:01:55 +0000141 inline PerFunctionInfo() {
142 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000143 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000144 }
145
Chris Lattner79df7c02002-03-26 18:01:55 +0000146 inline void FunctionStart(Function *M) {
147 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000148 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000149 }
150
Chris Lattner79df7c02002-03-26 18:01:55 +0000151 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000152 NumberedBlocks.clear();
153
154 // Any forward referenced blocks left?
155 if (!BBForwardRefs.empty())
156 ThrowException("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000157 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000158
159 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000160 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000161
Chris Lattner7e708292002-06-25 16:13:24 +0000162 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000163 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000164 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000165 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000166} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000167
Chris Lattner394f2eb2003-10-16 20:12:13 +0000168static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000169
Chris Lattner00950542001-06-06 20:29:01 +0000170
171//===----------------------------------------------------------------------===//
172// Code to handle definitions of all the types
173//===----------------------------------------------------------------------===//
174
Chris Lattner735f2702004-07-08 22:30:50 +0000175static int InsertValue(Value *V,
176 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
177 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000178
179 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000180 ValueList &List = ValueTab[V->getType()];
181 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000182 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000183}
184
Chris Lattner30c89792001-09-07 16:35:17 +0000185static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000186 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000187 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000188 // Module constants occupy the lowest numbered slots...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000189 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000190 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000191 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000192 case ValID::NameVal: // Is it a named definition?
193 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
194 D.destroy(); // Free old strdup'd memory...
195 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000196 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000197 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000198 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000199 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000200 }
201
202 // If we reached here, we referenced either a symbol that we don't know about
203 // or an id number that hasn't been read yet. We may be referencing something
204 // forward, so just create an entry to be resolved later and get to it...
205 //
206 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
207
Chris Lattner355ad1f2005-03-21 06:27:42 +0000208
209 if (inFunctionScope()) {
210 if (D.Type == ValID::NameVal)
211 ThrowException("Reference to an undefined type: '" + D.getName() + "'");
212 else
213 ThrowException("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000214 }
Chris Lattner30c89792001-09-07 16:35:17 +0000215
Chris Lattner355ad1f2005-03-21 06:27:42 +0000216 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
217 if (I != CurModule.LateResolveTypes.end())
218 return I->second;
219
Chris Lattner82269592001-10-22 06:01:08 +0000220 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000221 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000222 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000223 }
Chris Lattner30c89792001-09-07 16:35:17 +0000224
Chris Lattner9232b992003-04-22 18:42:41 +0000225static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000226 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000227 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000228 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000229 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000230}
231
Chris Lattner2079fde2001-10-13 06:41:08 +0000232// getValNonImprovising - Look up the value specified by the provided type and
233// the provided ValID. If the value exists and has already been defined, return
234// it. Otherwise return null.
235//
236static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000237 if (isa<FunctionType>(Ty))
238 ThrowException("Functions are not values and "
239 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000240
Chris Lattner30c89792001-09-07 16:35:17 +0000241 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000242 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000243 unsigned Num = (unsigned)D.Num;
244
245 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000246 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000247 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000248 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000249 return VI->second[Num];
250 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000251 }
252
253 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000254 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000255 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000256
257 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000258 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000259
Chris Lattner16af11d2004-02-09 21:03:38 +0000260 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000261 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000262
Chris Lattner1a1cb112001-09-30 22:46:54 +0000263 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000264 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000265 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000266
267 D.destroy(); // Free old strdup'd memory...
268 return N;
269 }
270
Misha Brukman5a2a3822005-05-10 22:02:28 +0000271 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000272 // value will fit into the specified type...
273 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000274 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
275 ThrowException("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000276 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000277 Ty->getDescription() + "'!");
278 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000279
280 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000281 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
282 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000283 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000284 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000285 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000286 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000287 }
288 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000289 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000290 }
291
Chris Lattner2079fde2001-10-13 06:41:08 +0000292 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000293 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000294 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000295 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000296
Chris Lattner2079fde2001-10-13 06:41:08 +0000297 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000298 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000299 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000300 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000301
Chris Lattner16710e92004-10-16 18:17:13 +0000302 case ValID::ConstUndefVal: // Is it an undef value?
303 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000304
Chris Lattnerd78700d2002-08-16 21:14:40 +0000305 case ValID::ConstantVal: // Fully resolved constant?
306 if (D.ConstantValue->getType() != Ty)
307 ThrowException("Constant expression type different from required type!");
308 return D.ConstantValue;
309
Chris Lattner30c89792001-09-07 16:35:17 +0000310 default:
311 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000312 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000313 } // End of switch
314
Chris Lattner2079fde2001-10-13 06:41:08 +0000315 assert(0 && "Unhandled case!");
316 return 0;
317}
318
Chris Lattner2079fde2001-10-13 06:41:08 +0000319// getVal - This function is identical to getValNonImprovising, except that if a
320// value is not already defined, it "improvises" by creating a placeholder var
321// that looks and acts just like the requested variable. When the value is
322// defined later, all uses of the placeholder variable are replaced with the
323// real thing.
324//
Chris Lattner64116f92004-07-14 01:33:11 +0000325static Value *getVal(const Type *Ty, const ValID &ID) {
326 if (Ty == Type::LabelTy)
327 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000328
Chris Lattner64116f92004-07-14 01:33:11 +0000329 // See if the value has already been defined.
330 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000331 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000332
Chris Lattner60cd9552005-03-23 01:29:26 +0000333 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
334 ThrowException("Invalid use of a composite type!");
335
Chris Lattner00950542001-06-06 20:29:01 +0000336 // If we reached here, we referenced either a symbol that we don't know about
337 // or an id number that hasn't been read yet. We may be referencing something
338 // forward, so just create an entry to be resolved later and get to it...
339 //
Chris Lattner64116f92004-07-14 01:33:11 +0000340 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000341
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000342 // Remember where this forward reference came from. FIXME, shouldn't we try
343 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000344 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000345 llvmAsmlineno)));
346
Chris Lattner79df7c02002-03-26 18:01:55 +0000347 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000348 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000349 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000350 InsertValue(V, CurModule.LateResolveValues);
351 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000352}
353
Chris Lattner2e2ed292004-07-14 06:28:35 +0000354/// getBBVal - This is used for two purposes:
355/// * If isDefinition is true, a new basic block with the specified ID is being
356/// defined.
357/// * If isDefinition is true, this is a reference to a basic block, which may
358/// or may not be a forward reference.
359///
360static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000361 assert(inFunctionScope() && "Can't get basic block at global scope!");
362
Chris Lattner2e2ed292004-07-14 06:28:35 +0000363 std::string Name;
364 BasicBlock *BB = 0;
365 switch (ID.Type) {
366 default: ThrowException("Illegal label reference " + ID.getName());
367 case ValID::NumberVal: // Is it a numbered definition?
368 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
369 CurFun.NumberedBlocks.resize(ID.Num+1);
370 BB = CurFun.NumberedBlocks[ID.Num];
371 break;
372 case ValID::NameVal: // Is it a named definition?
373 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000374 if (Value *N = CurFun.CurrentFunction->
375 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000376 BB = cast<BasicBlock>(N);
377 break;
378 }
Chris Lattner64116f92004-07-14 01:33:11 +0000379
Chris Lattner2e2ed292004-07-14 06:28:35 +0000380 // See if the block has already been defined.
381 if (BB) {
382 // If this is the definition of the block, make sure the existing value was
383 // just a forward reference. If it was a forward reference, there will be
384 // an entry for it in the PlaceHolderInfo map.
385 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
386 // The existing value was a definition, not a forward reference.
387 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000388
Chris Lattner2e2ed292004-07-14 06:28:35 +0000389 ID.destroy(); // Free strdup'd memory.
390 return BB;
391 }
392
393 // Otherwise this block has not been seen before.
394 BB = new BasicBlock("", CurFun.CurrentFunction);
395 if (ID.Type == ValID::NameVal) {
396 BB->setName(ID.Name);
397 } else {
398 CurFun.NumberedBlocks[ID.Num] = BB;
399 }
400
401 // If this is not a definition, keep track of it so we can use it as a forward
402 // reference.
403 if (!isDefinition) {
404 // Remember where this forward reference came from.
405 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
406 } else {
407 // The forward declaration could have been inserted anywhere in the
408 // function: insert it into the correct place now.
409 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
410 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
411 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000412 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000413 return BB;
414}
415
Chris Lattner00950542001-06-06 20:29:01 +0000416
417//===----------------------------------------------------------------------===//
418// Code to handle forward references in instructions
419//===----------------------------------------------------------------------===//
420//
421// This code handles the late binding needed with statements that reference
422// values not defined yet... for example, a forward branch, or the PHI node for
423// a loop body.
424//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000425// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000426// and back patchs after we are done.
427//
428
Misha Brukman5a2a3822005-05-10 22:02:28 +0000429// ResolveDefinitions - If we could not resolve some defs at parsing
430// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000431// defs now...
432//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000433static void
434ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
435 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000436 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000437 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000438 E = LateResolvers.end(); LRI != E; ++LRI) {
439 ValueList &List = LRI->second;
440 while (!List.empty()) {
441 Value *V = List.back();
442 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000443
444 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
445 CurModule.PlaceHolderInfo.find(V);
446 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
447
448 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000449
Chris Lattner735f2702004-07-08 22:30:50 +0000450 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000451 if (TheRealValue) {
452 V->replaceAllUsesWith(TheRealValue);
453 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000454 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000455 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000456 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000457 // resolver table
458 InsertValue(V, *FutureLateResolvers);
459 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000460 if (DID.Type == ValID::NameVal)
461 ThrowException("Reference to an invalid definition: '" +DID.getName()+
462 "' of type '" + V->getType()->getDescription() + "'",
463 PHI->second.second);
464 else
465 ThrowException("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000466 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000467 V->getType()->getDescription() + "'",
468 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000469 }
Chris Lattner00950542001-06-06 20:29:01 +0000470 }
471 }
472
473 LateResolvers.clear();
474}
475
Chris Lattner4a42e902001-10-22 05:56:09 +0000476// ResolveTypeTo - A brand new type was just declared. This means that (if
477// name is not null) things referencing Name can be resolved. Otherwise, things
478// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000479//
Chris Lattner4a42e902001-10-22 05:56:09 +0000480static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000481 ValID D;
482 if (Name) D = ValID::create(Name);
483 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000484
Chris Lattner355ad1f2005-03-21 06:27:42 +0000485 std::map<ValID, PATypeHolder>::iterator I =
486 CurModule.LateResolveTypes.find(D);
487 if (I != CurModule.LateResolveTypes.end()) {
488 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
489 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000490 }
491}
492
Chris Lattner1781aca2001-09-18 04:00:54 +0000493// setValueName - Set the specified value to the name given. The name may be
494// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000495// assumed to be a malloc'd string buffer, and is free'd by this function.
496//
497static void setValueName(Value *V, char *NameStr) {
498 if (NameStr) {
499 std::string Name(NameStr); // Copy string
500 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000501
Misha Brukman5a2a3822005-05-10 22:02:28 +0000502 if (V->getType() == Type::VoidTy)
Chris Lattnerc9aea522004-07-13 08:12:39 +0000503 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000504
Chris Lattner8ab406d2004-07-13 07:59:27 +0000505 assert(inFunctionScope() && "Must be in function scope!");
506 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
507 if (ST.lookup(V->getType(), Name))
508 ThrowException("Redefinition of value named '" + Name + "' in the '" +
509 V->getType()->getDescription() + "' type plane!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000510
Chris Lattnerc9aea522004-07-13 08:12:39 +0000511 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000512 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000513 }
514}
515
Chris Lattnerd88998d2004-07-14 08:23:52 +0000516/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
517/// this is a declaration, otherwise it is a definition.
518static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
519 bool isConstantGlobal, const Type *Ty,
Chris Lattner87ac9722005-11-06 06:46:28 +0000520 Constant *Initializer, unsigned Align) {
521 if (Align != 0 && !isPowerOf2_32(Align))
522 ThrowException("Global alignment must be a power of two!");
523
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000524 if (isa<FunctionType>(Ty))
525 ThrowException("Cannot declare global vars of function type!");
526
Misha Brukman5a2a3822005-05-10 22:02:28 +0000527 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000528
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000529 std::string Name;
530 if (NameStr) {
531 Name = NameStr; // Copy string
532 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000533 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000534
Chris Lattner8a327842004-07-14 21:44:00 +0000535 // See if this global value was forward referenced. If so, recycle the
536 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000537 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000538 if (!Name.empty()) {
539 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000540 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000541 ID = ValID::create((int)CurModule.Values[PTy].size());
542 }
543
544 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000545 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000546 // previously inserted.
547 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
548 CurModule.CurrentModule->getGlobalList().remove(GV);
549 CurModule.CurrentModule->getGlobalList().push_back(GV);
550 GV->setInitializer(Initializer);
551 GV->setLinkage(Linkage);
552 GV->setConstant(isConstantGlobal);
Chris Lattner87ac9722005-11-06 06:46:28 +0000553 GV->setAlignment(Align);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000554 InsertValue(GV, CurModule.Values);
Chris Lattnera09000d2004-07-14 23:03:46 +0000555 return;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000556 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000557
558 // If this global has a name, check to see if there is already a definition
559 // of this global in the module. If so, merge as appropriate. Note that
560 // this is really just a hack around problems in the CFE. :(
561 if (!Name.empty()) {
562 // We are a simple redefinition of a value, check to see if it is defined
563 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000564 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000565 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
566 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000567 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000568 // 2. If both initializers have the same value.
569 //
570 if (!EGV->hasInitializer() || !Initializer ||
571 EGV->getInitializer() == Initializer) {
572
573 // Make sure the existing global version gets the initializer! Make
574 // sure that it also gets marked const if the new version is.
575 if (Initializer && !EGV->hasInitializer())
576 EGV->setInitializer(Initializer);
577 if (isConstantGlobal)
578 EGV->setConstant(true);
579 EGV->setLinkage(Linkage);
Chris Lattner87ac9722005-11-06 06:46:28 +0000580 EGV->setAlignment(Align);
Chris Lattnera09000d2004-07-14 23:03:46 +0000581 return;
582 }
583
Misha Brukman5a2a3822005-05-10 22:02:28 +0000584 ThrowException("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000585 "' in the '" + Ty->getDescription() + "' type plane!");
586 }
587 }
588
589 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000590 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000591 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000592 CurModule.CurrentModule);
Chris Lattner87ac9722005-11-06 06:46:28 +0000593 GV->setAlignment(Align);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000594 InsertValue(GV, CurModule.Values);
Chris Lattnerd88998d2004-07-14 08:23:52 +0000595}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000596
Reid Spencer75719bf2004-05-26 21:48:31 +0000597// setTypeName - Set the specified type to the name given. The name may be
598// null potentially, in which case this is a noop. The string passed in is
599// assumed to be a malloc'd string buffer, and is freed by this function.
600//
601// This function returns true if the type has already been defined, but is
602// allowed to be redefined in the specified context. If the name is a new name
603// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000604static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000605 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000606 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000607
Reid Spencer75719bf2004-05-26 21:48:31 +0000608 std::string Name(NameStr); // Copy string
609 free(NameStr); // Free old string
610
611 // We don't allow assigning names to void type
Misha Brukman5a2a3822005-05-10 22:02:28 +0000612 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000613 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000614
Chris Lattnera09000d2004-07-14 23:03:46 +0000615 // Set the type name, checking for conflicts as we do so.
616 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000617
Chris Lattnera09000d2004-07-14 23:03:46 +0000618 if (AlreadyExists) { // Inserting a name that is already defined???
619 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
620 assert(Existing && "Conflict but no matching type?");
621
Reid Spencer75719bf2004-05-26 21:48:31 +0000622 // There is only one case where this is allowed: when we are refining an
623 // opaque type. In this case, Existing will be an opaque type.
624 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
625 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000626 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000627 return true;
628 }
629
630 // Otherwise, this is an attempt to redefine a type. That's okay if
631 // the redefinition is identical to the original. This will be so if
632 // Existing and T point to the same Type object. In this one case we
633 // allow the equivalent redefinition.
634 if (Existing == T) return true; // Yes, it's equal.
635
636 // Any other kind of (non-equivalent) redefinition is an error.
637 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000638 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000639 }
640
Reid Spencer75719bf2004-05-26 21:48:31 +0000641 return false;
642}
Chris Lattner8896eda2001-07-09 19:38:36 +0000643
Chris Lattner30c89792001-09-07 16:35:17 +0000644//===----------------------------------------------------------------------===//
645// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000646//
Chris Lattner8896eda2001-07-09 19:38:36 +0000647
Chris Lattner3b073862004-02-09 03:19:29 +0000648// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000649//
650static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000651 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000652 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000653}
654
655namespace {
656 struct UpRefRecord {
657 // NestingLevel - The number of nesting levels that need to be popped before
658 // this type is resolved.
659 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000660
Chris Lattner3b073862004-02-09 03:19:29 +0000661 // LastContainedTy - This is the type at the current binding level for the
662 // type. Every time we reduce the nesting level, this gets updated.
663 const Type *LastContainedTy;
664
665 // UpRefTy - This is the actual opaque type that the upreference is
666 // represented with.
667 OpaqueType *UpRefTy;
668
669 UpRefRecord(unsigned NL, OpaqueType *URTy)
670 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
671 };
Chris Lattner30c89792001-09-07 16:35:17 +0000672}
Chris Lattner698b56e2001-07-20 19:15:08 +0000673
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000674// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000675static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000676
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000677/// HandleUpRefs - Every time we finish a new layer of types, this function is
678/// called. It loops through the UpRefs vector, which is a list of the
679/// currently active types. For each type, if the up reference is contained in
680/// the newly completed type, we decrement the level count. When the level
681/// count reaches zero, the upreferenced type is the type that is passed in:
682/// thus we can complete the cycle.
683///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000684static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000685 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000686 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000687 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000688 "' newly formed. Resolving upreferences.\n" <<
689 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000690
691 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
692 // to zero), we resolve them all together before we resolve them to Ty. At
693 // the end of the loop, if there is anything to resolve to Ty, it will be in
694 // this variable.
695 OpaqueType *TypeToResolve = 0;
696
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000697 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000698 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
699 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000700 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000701 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
702 // Decrement level of upreference
703 unsigned Level = --UpRefs[i].NestingLevel;
704 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000705 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000706 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000707 if (!TypeToResolve) {
708 TypeToResolve = UpRefs[i].UpRefTy;
709 } else {
710 UR_OUT(" * Resolving upreference for "
711 << UpRefs[i].second->getDescription() << "\n";
712 std::string OldName = UpRefs[i].UpRefTy->getDescription());
713 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
714 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
715 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
716 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000717 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000718 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000719 }
720 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000721 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000722
Chris Lattner026a8ce2004-02-09 18:53:54 +0000723 if (TypeToResolve) {
724 UR_OUT(" * Resolving upreference for "
725 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000726 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000727 TypeToResolve->refineAbstractTypeTo(Ty);
728 }
729
Chris Lattner8896eda2001-07-09 19:38:36 +0000730 return Ty;
731}
732
Chris Lattner30c89792001-09-07 16:35:17 +0000733
Chris Lattner6184feb2005-05-20 03:25:47 +0000734// common code from the two 'RunVMAsmParser' functions
735 static Module * RunParser(Module * M) {
736
737 llvmAsmlineno = 1; // Reset the current line number...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000738 ObsoleteVarArgs = false;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000739 NewVarArgs = false;
Chris Lattner6184feb2005-05-20 03:25:47 +0000740
741 CurModule.CurrentModule = M;
742 yyparse(); // Parse the file, potentially throwing exception
743
744 Module *Result = ParserResult;
745 ParserResult = 0;
746
Andrew Lenharth31c98bf2005-06-20 15:41:37 +0000747 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
748 {
749 Function* F;
750 if ((F = Result->getNamedFunction("llvm.va_start"))
751 && F->getFunctionType()->getNumParams() == 0)
752 ObsoleteVarArgs = true;
753 if((F = Result->getNamedFunction("llvm.va_copy"))
754 && F->getFunctionType()->getNumParams() == 1)
755 ObsoleteVarArgs = true;
756 }
757
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000758 if (ObsoleteVarArgs && NewVarArgs)
Chris Lattner548021f2005-06-24 18:00:40 +0000759 ThrowException("This file is corrupt: it uses both new and old style varargs");
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000760
Andrew Lenharth558bc882005-06-18 18:34:52 +0000761 if(ObsoleteVarArgs) {
762 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000763 if (F->arg_size() != 0)
764 ThrowException("Obsolete va_start takes 0 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000765
766 //foo = va_start()
767 // ->
768 //bar = alloca typeof(foo)
769 //va_start(bar)
770 //foo = load bar
771
772 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
773 const Type* ArgTy = F->getFunctionType()->getReturnType();
774 const Type* ArgTyPtr = PointerType::get(ArgTy);
775 Function* NF = Result->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000776 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000777
778 while (!F->use_empty()) {
779 CallInst* CI = cast<CallInst>(F->use_back());
780 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
781 new CallInst(NF, bar, "", CI);
782 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
783 CI->replaceAllUsesWith(foo);
784 CI->getParent()->getInstList().erase(CI);
785 }
786 Result->getFunctionList().erase(F);
787 }
788
789 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000790 if(F->arg_size() != 1)
791 ThrowException("Obsolete va_end takes 1 argument!");
792
Andrew Lenharth558bc882005-06-18 18:34:52 +0000793 //vaend foo
794 // ->
795 //bar = alloca 1 of typeof(foo)
796 //vaend bar
797 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
798 const Type* ArgTy = F->getFunctionType()->getParamType(0);
799 const Type* ArgTyPtr = PointerType::get(ArgTy);
800 Function* NF = Result->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000801 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000802
803 while (!F->use_empty()) {
804 CallInst* CI = cast<CallInst>(F->use_back());
805 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000806 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000807 new CallInst(NF, bar, "", CI);
808 CI->getParent()->getInstList().erase(CI);
809 }
810 Result->getFunctionList().erase(F);
811 }
812
813 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000814 if(F->arg_size() != 1)
815 ThrowException("Obsolete va_copy takes 1 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000816 //foo = vacopy(bar)
817 // ->
818 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000819 //b = alloca 1 of typeof(foo)
820 //store bar -> b
821 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000822 //foo = load a
823
824 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
825 const Type* ArgTy = F->getFunctionType()->getReturnType();
826 const Type* ArgTyPtr = PointerType::get(ArgTy);
827 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000828 RetTy, ArgTyPtr, ArgTyPtr,
829 (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000830
831 while (!F->use_empty()) {
832 CallInst* CI = cast<CallInst>(F->use_back());
833 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000834 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
835 new StoreInst(CI->getOperand(1), b, CI);
836 new CallInst(NF, a, b, "", CI);
837 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000838 CI->replaceAllUsesWith(foo);
839 CI->getParent()->getInstList().erase(CI);
840 }
841 Result->getFunctionList().erase(F);
842 }
843 }
844
Chris Lattner6184feb2005-05-20 03:25:47 +0000845 return Result;
846
847 }
848
Chris Lattner00950542001-06-06 20:29:01 +0000849//===----------------------------------------------------------------------===//
850// RunVMAsmParser - Define an interface to this parser
851//===----------------------------------------------------------------------===//
852//
Chris Lattner86427632004-07-14 06:39:48 +0000853Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000854 set_scan_file(F);
855
Chris Lattnera2850432001-07-22 18:36:00 +0000856 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000857 return RunParser(new Module(CurFilename));
858}
Chris Lattner00950542001-06-06 20:29:01 +0000859
Chris Lattner6184feb2005-05-20 03:25:47 +0000860Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
861 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000862
Chris Lattner6184feb2005-05-20 03:25:47 +0000863 CurFilename = "from_memory";
864 if (M == NULL) {
865 return RunParser(new Module (CurFilename));
866 } else {
867 return RunParser(M);
868 }
Chris Lattner00950542001-06-06 20:29:01 +0000869}
870
871%}
872
873%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000874 llvm::Module *ModuleVal;
875 llvm::Function *FunctionVal;
876 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
877 llvm::BasicBlock *BasicBlockVal;
878 llvm::TerminatorInst *TermInstVal;
879 llvm::Instruction *InstVal;
880 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000881
Brian Gaeked0fde302003-11-11 22:41:34 +0000882 const llvm::Type *PrimType;
883 llvm::PATypeHolder *TypeVal;
884 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000885
Brian Gaeked0fde302003-11-11 22:41:34 +0000886 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
887 std::vector<llvm::Value*> *ValueList;
888 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000889 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000890 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000891 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000892 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
893 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000894
Brian Gaeked0fde302003-11-11 22:41:34 +0000895 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000896 int64_t SInt64Val;
897 uint64_t UInt64Val;
898 int SIntVal;
899 unsigned UIntVal;
900 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000901 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000902
Chris Lattner30c89792001-09-07 16:35:17 +0000903 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000904 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000905
Brian Gaeked0fde302003-11-11 22:41:34 +0000906 llvm::Instruction::BinaryOps BinaryOpVal;
907 llvm::Instruction::TermOps TermOpVal;
908 llvm::Instruction::MemoryOps MemOpVal;
909 llvm::Instruction::OtherOps OtherOpVal;
910 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000911}
912
Chris Lattner79df7c02002-03-26 18:01:55 +0000913%type <ModuleVal> Module FunctionList
914%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000915%type <BasicBlockVal> BasicBlock InstructionList
916%type <TermInstVal> BBTerminatorInst
917%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000918%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000919%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000920%type <ArgList> ArgList ArgListH
921%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000922%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000923%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000924%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000925%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000926%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000927%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000928%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000929%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000930%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000931%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000932
Chris Lattner2079fde2001-10-13 06:41:08 +0000933// ValueRef - Unresolved reference to a definition or BB
934%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000935%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000936// Tokens and types for handling constant integer values
937//
938// ESINT64VAL - A negative number within long long range
939%token <SInt64Val> ESINT64VAL
940
941// EUINT64VAL - A positive number within uns. long long range
942%token <UInt64Val> EUINT64VAL
943%type <SInt64Val> EINT64VAL
944
945%token <SIntVal> SINTVAL // Signed 32 bit ints...
946%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
947%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000948%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000949
950// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000951%type <TypeVal> Types TypesV UpRTypes UpRTypesV
952%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000953%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
954%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000955
Chris Lattner6c23f572003-08-22 05:42:10 +0000956%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
957%type <StrVal> Name OptName OptAssign
Chris Lattner87ac9722005-11-06 06:46:28 +0000958%type <UIntVal> OptAlign OptCAlign
Chris Lattner00950542001-06-06 20:29:01 +0000959
Chris Lattner91ef4602004-03-31 03:49:47 +0000960%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000961%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000962%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Nate Begeman14b05292005-11-05 09:21:28 +0000963%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
Chris Lattnerddb6db42005-05-06 05:51:46 +0000964%token DEPLIBS CALL TAIL
Chris Lattnera8e8f162005-05-06 20:27:19 +0000965%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK
966%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +0000967
Misha Brukman5a2a3822005-05-10 22:02:28 +0000968// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000969%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000970
Misha Brukman5a2a3822005-05-10 22:02:28 +0000971// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000972%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000973%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000974%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000975
976// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000977%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000978
Chris Lattner027dcc52001-07-08 21:10:27 +0000979// Other Operators
980%type <OtherOpVal> ShiftOps
Andrew Lenharth558bc882005-06-18 18:34:52 +0000981%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG
982%token VAARG_old VANEXT_old //OBSOLETE
Chris Lattnerddb6db42005-05-06 05:51:46 +0000983
Chris Lattner027dcc52001-07-08 21:10:27 +0000984
Chris Lattner00950542001-06-06 20:29:01 +0000985%start Module
986%%
987
988// Handle constant integer size restriction and conversion...
989//
Chris Lattner51727be2002-06-04 21:58:56 +0000990INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000991INTVAL : UINTVAL {
992 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
993 ThrowException("Value too large for type!");
994 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000995};
Chris Lattner00950542001-06-06 20:29:01 +0000996
997
Chris Lattner51727be2002-06-04 21:58:56 +0000998EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000999EINT64VAL : EUINT64VAL {
1000 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1001 ThrowException("Value too large for type!");
1002 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +00001003};
Chris Lattner00950542001-06-06 20:29:01 +00001004
Misha Brukman5a2a3822005-05-10 22:02:28 +00001005// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001006// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1007//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001008ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1009LogicalOps : AND | OR | XOR;
1010SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +00001011
Chris Lattner51727be2002-06-04 21:58:56 +00001012ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001013
Chris Lattnere98dda62001-07-14 06:10:16 +00001014// These are some types that allow classification if we only want a particular
1015// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001016SIntType : LONG | INT | SHORT | SBYTE;
1017UIntType : ULONG | UINT | USHORT | UBYTE;
1018IntType : SIntType | UIntType;
1019FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001020
Chris Lattnere98dda62001-07-14 06:10:16 +00001021// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001022OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001023 $$ = $1;
1024 }
Misha Brukman5a2a3822005-05-10 22:02:28 +00001025 | /*empty*/ {
1026 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001027 };
Chris Lattner00950542001-06-06 20:29:01 +00001028
Chris Lattner4ad02e72003-04-16 20:28:45 +00001029OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1030 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001031 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001032 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1033 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001034
Chris Lattnera8e8f162005-05-06 20:27:19 +00001035OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1036 CCC_TOK { $$ = CallingConv::C; } |
1037 FASTCC_TOK { $$ = CallingConv::Fast; } |
1038 COLDCC_TOK { $$ = CallingConv::Cold; } |
1039 CC_TOK EUINT64VAL {
1040 if ((unsigned)$2 != $2)
1041 ThrowException("Calling conv too large!");
1042 $$ = $2;
1043 };
1044
Chris Lattner66db8e42005-11-06 06:34:12 +00001045// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1046// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001047OptAlign : /*empty*/ { $$ = 0; } |
1048 ALIGN EUINT64VAL { $$ = $2; };
Chris Lattner66db8e42005-11-06 06:34:12 +00001049OptCAlign : /*empty*/ { $$ = 0; } |
1050 ',' ALIGN EUINT64VAL { $$ = $3; };
1051
Chris Lattner30c89792001-09-07 16:35:17 +00001052//===----------------------------------------------------------------------===//
1053// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001054// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001055// access to it, a user must explicitly use TypesV.
1056//
1057
1058// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001059TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1060UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001061
1062Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001063 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001064 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1065 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001066 };
Chris Lattner30c89792001-09-07 16:35:17 +00001067
1068
1069// Derived types are added later...
1070//
Chris Lattner51727be2002-06-04 21:58:56 +00001071PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1072PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001073UpRTypes : OPAQUE {
1074 $$ = new PATypeHolder(OpaqueType::get());
1075 }
1076 | PrimType {
1077 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001078 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001079UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001080 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001081};
Chris Lattner30c89792001-09-07 16:35:17 +00001082
Chris Lattner30c89792001-09-07 16:35:17 +00001083// Include derived types in the Types production.
1084//
1085UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001086 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001087 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001088 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001089 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001090 UR_OUT("New Upreference!\n");
1091 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001092 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001093 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001094 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1095 E = $3->end(); I != E; ++I)
1096 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001097 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1098 if (isVarArg) Params.pop_back();
1099
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001100 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001101 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001102 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001103 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001104 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001105 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001106 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001107 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001108 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1109 const llvm::Type* ElemTy = $4->get();
Chris Lattner9547d7f2005-11-10 01:42:43 +00001110 if ((unsigned)$2 != $2)
Brian Gaeke715c90b2004-08-20 06:00:58 +00001111 ThrowException("Unsigned result not equal to signed result");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001112 if (!ElemTy->isPrimitiveType())
Brian Gaeke715c90b2004-08-20 06:00:58 +00001113 ThrowException("Elemental type of a PackedType must be primitive");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001114 if (!isPowerOf2_32($2))
1115 ThrowException("Vector length should be a power of 2!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001116 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1117 delete $4;
1118 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001119 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001120 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001121 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1122 E = $2->end(); I != E; ++I)
1123 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001124
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001125 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001126 delete $2;
1127 }
1128 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001129 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001130 }
1131 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001132 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001133 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001134 };
Chris Lattner30c89792001-09-07 16:35:17 +00001135
Chris Lattner7e708292002-06-25 16:13:24 +00001136// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001137// declaration type lists
1138//
1139TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001140 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001141 $$->push_back(*$1); delete $1;
1142 }
1143 | TypeListI ',' UpRTypes {
1144 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001145 };
Chris Lattner30c89792001-09-07 16:35:17 +00001146
Chris Lattner7e708292002-06-25 16:13:24 +00001147// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001148ArgTypeListI : TypeListI
1149 | TypeListI ',' DOTDOTDOT {
1150 ($$=$1)->push_back(Type::VoidTy);
1151 }
1152 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001153 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001154 }
1155 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001156 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001157 };
Chris Lattner30c89792001-09-07 16:35:17 +00001158
Chris Lattnere98dda62001-07-14 06:10:16 +00001159// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001160// production is used ONLY to represent constants that show up AFTER a 'const',
1161// 'constant' or 'global' token at global scope. Constants that can be inlined
1162// into other expressions (such as integers and constexprs) are handled by the
1163// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001164//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001165ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001166 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001167 if (ATy == 0)
1168 ThrowException("Cannot make array constant with type: '" +
1169 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001170 const Type *ETy = ATy->getElementType();
1171 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001172
Chris Lattner30c89792001-09-07 16:35:17 +00001173 // Verify that we have the correct size...
1174 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001175 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001176 utostr($3->size()) + " arguments, but has size of " +
1177 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001178
Chris Lattner30c89792001-09-07 16:35:17 +00001179 // Verify all elements are correct type!
1180 for (unsigned i = 0; i < $3->size(); i++) {
1181 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001182 ThrowException("Element #" + utostr(i) + " is not of type '" +
1183 ETy->getDescription() +"' as required!\nIt is of type '"+
1184 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001185 }
1186
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001187 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001188 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001189 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001190 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001191 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001192 if (ATy == 0)
1193 ThrowException("Cannot make array constant with type: '" +
1194 (*$1)->getDescription() + "'!");
1195
1196 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001197 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001198 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001199 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001200 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001201 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001202 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001203 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001204 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001205 if (ATy == 0)
1206 ThrowException("Cannot make array constant with type: '" +
1207 (*$1)->getDescription() + "'!");
1208
Chris Lattner30c89792001-09-07 16:35:17 +00001209 int NumElements = ATy->getNumElements();
1210 const Type *ETy = ATy->getElementType();
1211 char *EndStr = UnEscapeLexed($3, true);
1212 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001213 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001214 itostr((int)(EndStr-$3)) +
1215 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001216 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001217 if (ETy == Type::SByteTy) {
1218 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001219 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001220 } else if (ETy == Type::UByteTy) {
1221 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001222 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001223 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001224 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001225 ThrowException("Cannot build string arrays of non byte sized elements!");
1226 }
Chris Lattner30c89792001-09-07 16:35:17 +00001227 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001228 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001229 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001230 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001231 | Types '<' ConstVector '>' { // Nonempty unsized arr
1232 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1233 if (PTy == 0)
1234 ThrowException("Cannot make packed constant with type: '" +
1235 (*$1)->getDescription() + "'!");
1236 const Type *ETy = PTy->getElementType();
1237 int NumElements = PTy->getNumElements();
1238
1239 // Verify that we have the correct size...
1240 if (NumElements != -1 && NumElements != (int)$3->size())
1241 ThrowException("Type mismatch: constant sized packed initialized with " +
1242 utostr($3->size()) + " arguments, but has size of " +
1243 itostr(NumElements) + "!");
1244
1245 // Verify all elements are correct type!
1246 for (unsigned i = 0; i < $3->size(); i++) {
1247 if (ETy != (*$3)[i]->getType())
1248 ThrowException("Element #" + utostr(i) + " is not of type '" +
1249 ETy->getDescription() +"' as required!\nIt is of type '"+
1250 (*$3)[i]->getType()->getDescription() + "'.");
1251 }
1252
1253 $$ = ConstantPacked::get(PTy, *$3);
1254 delete $1; delete $3;
1255 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001256 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001257 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001258 if (STy == 0)
1259 ThrowException("Cannot make struct constant with type: '" +
1260 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001261
Chris Lattnerc6212f12003-05-21 16:06:56 +00001262 if ($3->size() != STy->getNumContainedTypes())
1263 ThrowException("Illegal number of initializers for structure type!");
1264
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001265 // Check to ensure that constants are compatible with the type initializer!
1266 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001267 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001268 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001269 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001270 "' for element #" + utostr(i) +
1271 " of structure initializer!");
1272
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001273 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001274 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001275 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001276 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001277 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001278 if (STy == 0)
1279 ThrowException("Cannot make struct constant with type: '" +
1280 (*$1)->getDescription() + "'!");
1281
1282 if (STy->getNumContainedTypes() != 0)
1283 ThrowException("Illegal number of initializers for structure type!");
1284
1285 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1286 delete $1;
1287 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001288 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001289 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001290 if (PTy == 0)
1291 ThrowException("Cannot make null pointer constant with type: '" +
1292 (*$1)->getDescription() + "'!");
1293
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001294 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001295 delete $1;
1296 }
Chris Lattner16710e92004-10-16 18:17:13 +00001297 | Types UNDEF {
1298 $$ = UndefValue::get($1->get());
1299 delete $1;
1300 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001301 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001302 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001303 if (Ty == 0)
1304 ThrowException("Global const reference must be a pointer type!");
1305
Chris Lattner3101c252002-08-15 17:58:33 +00001306 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001307 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001308 // the context of a function, getValNonImprovising will search the functions
1309 // symbol table instead of the module symbol table for the global symbol,
1310 // which throws things all off. To get around this, we just tell
1311 // getValNonImprovising that we are at global scope here.
1312 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001313 Function *SavedCurFn = CurFun.CurrentFunction;
1314 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001315
Chris Lattner2079fde2001-10-13 06:41:08 +00001316 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001317
Chris Lattner394f2eb2003-10-16 20:12:13 +00001318 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001319
Chris Lattner2079fde2001-10-13 06:41:08 +00001320 // If this is an initializer for a constant pointer, which is referencing a
1321 // (currently) undefined variable, create a stub now that shall be replaced
1322 // in the future with the right type of variable.
1323 //
1324 if (V == 0) {
1325 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1326 const PointerType *PT = cast<PointerType>(Ty);
1327
1328 // First check to see if the forward references value is already created!
1329 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001330 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001331
1332 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001333 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001334 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001335 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001336 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001337 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001338
Reid Spencer3271ed52004-07-18 00:08:11 +00001339 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001340 GlobalValue *GV;
1341 if (const FunctionType *FTy =
1342 dyn_cast<FunctionType>(PT->getElementType())) {
1343 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1344 CurModule.CurrentModule);
1345 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001346 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001347 GlobalValue::ExternalLinkage, 0,
1348 Name, CurModule.CurrentModule);
1349 }
Chris Lattner8a327842004-07-14 21:44:00 +00001350
Reid Spencer3271ed52004-07-18 00:08:11 +00001351 // Keep track of the fact that we have a forward ref to recycle it
1352 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1353 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001354 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001355 }
1356
Reid Spencer3271ed52004-07-18 00:08:11 +00001357 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001358 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001359 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001360 | Types ConstExpr {
1361 if ($1->get() != $2->getType())
1362 ThrowException("Mismatched types for constant expression!");
1363 $$ = $2;
1364 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001365 }
1366 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001367 const Type *Ty = $1->get();
1368 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1369 ThrowException("Cannot create a null initialized value of this type!");
1370 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001371 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001372 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001373
Chris Lattnere43f40b2002-10-09 00:25:32 +00001374ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001375 if (!ConstantSInt::isValueValidForType($1, $2))
1376 ThrowException("Constant value doesn't fit in type!");
1377 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001378 }
1379 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001380 if (!ConstantUInt::isValueValidForType($1, $2))
1381 ThrowException("Constant value doesn't fit in type!");
1382 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001383 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001384 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001385 $$ = ConstantBool::True;
1386 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001387 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001388 $$ = ConstantBool::False;
1389 }
1390 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001391 if (!ConstantFP::isValueValidForType($1, $2))
1392 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001393 $$ = ConstantFP::get($1, $2);
1394 };
1395
Chris Lattner00950542001-06-06 20:29:01 +00001396
Chris Lattnerd78700d2002-08-16 21:14:40 +00001397ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001398 if (!$3->getType()->isFirstClassType())
1399 ThrowException("cast constant expression from a non-primitive type: '" +
1400 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001401 if (!$5->get()->isFirstClassType())
1402 ThrowException("cast constant expression to a non-primitive type: '" +
1403 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001404 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001405 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001406 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001407 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1408 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001409 ThrowException("GetElementPtr requires a pointer operand!");
1410
Chris Lattner830b6f92004-04-05 01:30:04 +00001411 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1412 // indices to uint struct indices for compatibility.
1413 generic_gep_type_iterator<std::vector<Value*>::iterator>
1414 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1415 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1416 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1417 if (isa<StructType>(*GTI)) // Only change struct indices
1418 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1419 if (CUI->getType() == Type::UByteTy)
1420 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1421
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001422 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001423 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001424 if (!IdxTy)
1425 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001426
Chris Lattner9232b992003-04-22 18:42:41 +00001427 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001428 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1429 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001430 IdxVec.push_back(C);
1431 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001432 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001433
Chris Lattnerd78700d2002-08-16 21:14:40 +00001434 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001435
Chris Lattnerd78700d2002-08-16 21:14:40 +00001436 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001437 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001438 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1439 if ($3->getType() != Type::BoolTy)
1440 ThrowException("Select condition must be of boolean type!");
1441 if ($5->getType() != $7->getType())
1442 ThrowException("Select operand types must match!");
1443 $$ = ConstantExpr::getSelect($3, $5, $7);
1444 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001445 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001446 if ($3->getType() != $5->getType())
1447 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001448 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1449 // To retain backward compatibility with these early compilers, we emit a
1450 // cast to the appropriate integer type automatically if we are in the
1451 // broken case. See PR424 for more information.
1452 if (!isa<PointerType>($3->getType())) {
1453 $$ = ConstantExpr::get($1, $3, $5);
1454 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001455 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001456 switch (CurModule.CurrentModule->getPointerSize()) {
1457 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1458 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1459 default: ThrowException("invalid pointer binary constant expr!");
1460 }
1461 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1462 ConstantExpr::getCast($5, IntPtrTy));
1463 $$ = ConstantExpr::getCast($$, $3->getType());
1464 }
1465 }
1466 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1467 if ($3->getType() != $5->getType())
1468 ThrowException("Logical operator types must match!");
1469 if (!$3->getType()->isIntegral())
1470 ThrowException("Logical operands must have integral types!");
1471 $$ = ConstantExpr::get($1, $3, $5);
1472 }
1473 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1474 if ($3->getType() != $5->getType())
1475 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001476 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001477 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001478 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001479 if ($5->getType() != Type::UByteTy)
1480 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001481 if (!$3->getType()->isInteger())
1482 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001483 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001484 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001485
1486
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001487// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001488ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001489 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001490 }
1491 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001492 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001493 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001494 };
Chris Lattner00950542001-06-06 20:29:01 +00001495
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001496
Chris Lattner1781aca2001-09-18 04:00:54 +00001497// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001498GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001499
Chris Lattner00950542001-06-06 20:29:01 +00001500
Chris Lattner0e73ce62002-05-02 19:11:13 +00001501//===----------------------------------------------------------------------===//
1502// Rules to match Modules
1503//===----------------------------------------------------------------------===//
1504
1505// Module rule: Capture the result of parsing the whole file into a result
1506// variable...
1507//
1508Module : FunctionList {
1509 $$ = ParserResult = $1;
1510 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001511};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001512
Chris Lattner7e708292002-06-25 16:13:24 +00001513// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001514//
1515FunctionList : FunctionList Function {
1516 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001517 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001518 }
1519 | FunctionList FunctionProto {
1520 $$ = $1;
1521 }
1522 | FunctionList IMPLEMENTATION {
1523 $$ = $1;
1524 }
1525 | ConstPool {
1526 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001527 // Emit an error if there are any unresolved types left.
1528 if (!CurModule.LateResolveTypes.empty()) {
1529 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1530 if (DID.Type == ValID::NameVal)
1531 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1532 else
1533 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1534 }
Chris Lattner51727be2002-06-04 21:58:56 +00001535 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001536
Chris Lattnere98dda62001-07-14 06:10:16 +00001537// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001538ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001539 // Eagerly resolve types. This is not an optimization, this is a
1540 // requirement that is due to the fact that we could have this:
1541 //
1542 // %list = type { %list * }
1543 // %list = type { %list * } ; repeated type decl
1544 //
1545 // If types are not resolved eagerly, then the two types will not be
1546 // determined to be the same type!
1547 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001548 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001549
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001550 if (!setTypeName(*$4, $2) && !$2) {
1551 // If this is a named type that is not a redefinition, add it to the slot
1552 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001553 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001554 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001555
1556 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001557 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001558 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001559 }
Chris Lattner87ac9722005-11-06 06:46:28 +00001560 | ConstPool OptAssign OptLinkage GlobalType ConstVal OptCAlign {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001561 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
Chris Lattner87ac9722005-11-06 06:46:28 +00001562 ParseGlobalVariable($2, $3, $4, $5->getType(), $5, $6);
Chris Lattner1781aca2001-09-18 04:00:54 +00001563 }
Chris Lattner87ac9722005-11-06 06:46:28 +00001564 | ConstPool OptAssign EXTERNAL GlobalType Types OptCAlign {
1565 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0, $6);
Chris Lattner1f862af2003-04-16 18:13:57 +00001566 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001567 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001568 | ConstPool TARGET TargetDefinition {
1569 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001570 | ConstPool DEPLIBS '=' LibrariesDefinition {
1571 }
Chris Lattner00950542001-06-06 20:29:01 +00001572 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001573 };
Chris Lattner00950542001-06-06 20:29:01 +00001574
1575
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001576
1577BigOrLittle : BIG { $$ = Module::BigEndian; };
1578BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1579
1580TargetDefinition : ENDIAN '=' BigOrLittle {
1581 CurModule.CurrentModule->setEndianness($3);
1582 }
1583 | POINTERSIZE '=' EUINT64VAL {
1584 if ($3 == 32)
1585 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1586 else if ($3 == 64)
1587 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1588 else
1589 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001590 }
1591 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001592 CurModule.CurrentModule->setTargetTriple($3);
1593 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001594 };
1595
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001596LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001597
1598LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001599 CurModule.CurrentModule->addLibrary($3);
1600 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001601 }
1602 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001603 CurModule.CurrentModule->addLibrary($1);
1604 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001605 }
1606 | /* empty: end of list */ {
1607 }
1608 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001609
Chris Lattner00950542001-06-06 20:29:01 +00001610//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001611// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001612//===----------------------------------------------------------------------===//
1613
Chris Lattner6c23f572003-08-22 05:42:10 +00001614Name : VAR_ID | STRINGCONSTANT;
1615OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001616
Chris Lattner6c23f572003-08-22 05:42:10 +00001617ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001618 if (*$1 == Type::VoidTy)
1619 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001620 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001621};
Chris Lattner00950542001-06-06 20:29:01 +00001622
Chris Lattner69da5cf2002-10-13 20:57:00 +00001623ArgListH : ArgListH ',' ArgVal {
1624 $$ = $1;
1625 $1->push_back(*$3);
1626 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001627 }
1628 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001629 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001630 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001631 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001632 };
Chris Lattner00950542001-06-06 20:29:01 +00001633
1634ArgList : ArgListH {
1635 $$ = $1;
1636 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001637 | ArgListH ',' DOTDOTDOT {
1638 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001639 $$->push_back(std::pair<PATypeHolder*,
1640 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001641 }
1642 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001643 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1644 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001645 }
Chris Lattner00950542001-06-06 20:29:01 +00001646 | /* empty */ {
1647 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001648 };
Chris Lattner00950542001-06-06 20:29:01 +00001649
Chris Lattner87ac9722005-11-06 06:46:28 +00001650FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')' OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001651 UnEscapeLexed($3);
1652 std::string FunctionName($3);
1653 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001654
Chris Lattnera8e8f162005-05-06 20:27:19 +00001655 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001656 ThrowException("LLVM functions cannot return aggregate types!");
Chris Lattner87ac9722005-11-06 06:46:28 +00001657 if ($7 != 0 && !isPowerOf2_32($7))
1658 ThrowException("Function alignment must be a power of two!");
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001659
Chris Lattner9232b992003-04-22 18:42:41 +00001660 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001661 if ($5) { // If there are arguments...
1662 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1663 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001664 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001665 }
Chris Lattner00950542001-06-06 20:29:01 +00001666
Chris Lattner2079fde2001-10-13 06:41:08 +00001667 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1668 if (isVarArg) ParamTypeList.pop_back();
1669
Chris Lattnera8e8f162005-05-06 20:27:19 +00001670 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001671 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001672 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001673
Chris Lattnera09000d2004-07-14 23:03:46 +00001674 ValID ID;
1675 if (!FunctionName.empty()) {
1676 ID = ValID::create((char*)FunctionName.c_str());
1677 } else {
1678 ID = ValID::create((int)CurModule.Values[PFT].size());
1679 }
1680
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001681 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001682 // See if this function was forward referenced. If so, recycle the object.
1683 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1684 // Move the function to the end of the list, from whereever it was
1685 // previously inserted.
1686 Fn = cast<Function>(FWRef);
1687 CurModule.CurrentModule->getFunctionList().remove(Fn);
1688 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1689 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1690 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1691 // If this is the case, either we need to be a forward decl, or it needs
1692 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001693 if (!CurFun.isDeclare && !Fn->isExternal())
1694 ThrowException("Redefinition of function '" + FunctionName + "'!");
1695
Chris Lattnera09000d2004-07-14 23:03:46 +00001696 // Make sure to strip off any argument names so we can't get conflicts.
1697 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001698 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001699 AI != AE; ++AI)
1700 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001701
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001702 } else { // Not already defined?
1703 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1704 CurModule.CurrentModule);
1705 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001706 }
Chris Lattner00950542001-06-06 20:29:01 +00001707
Chris Lattner394f2eb2003-10-16 20:12:13 +00001708 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001709 Fn->setCallingConv($1);
Chris Lattner87ac9722005-11-06 06:46:28 +00001710 Fn->setAlignment($7);
Chris Lattner00950542001-06-06 20:29:01 +00001711
Chris Lattner7e708292002-06-25 16:13:24 +00001712 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001713 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001714 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001715 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001716 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001717 delete $5->back().first;
1718 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001719 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001720 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001721 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1722 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001723 delete I->first; // Delete the typeholder...
1724
Chris Lattner8ab406d2004-07-13 07:59:27 +00001725 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001726 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001727 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001728
Chris Lattnera8e8f162005-05-06 20:27:19 +00001729 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001730 }
Chris Lattner51727be2002-06-04 21:58:56 +00001731};
Chris Lattner00950542001-06-06 20:29:01 +00001732
Chris Lattner9b02cc32002-05-03 18:23:48 +00001733BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1734
Chris Lattner4ad02e72003-04-16 20:28:45 +00001735FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001736 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001737
Chris Lattner4ad02e72003-04-16 20:28:45 +00001738 // Make sure that we keep track of the linkage type even if there was a
1739 // previous "declare".
1740 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001741};
Chris Lattner00950542001-06-06 20:29:01 +00001742
Chris Lattner9b02cc32002-05-03 18:23:48 +00001743END : ENDTOK | '}'; // Allow end of '}' to end a function
1744
Chris Lattner79df7c02002-03-26 18:01:55 +00001745Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001746 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001747};
Chris Lattner00950542001-06-06 20:29:01 +00001748
Chris Lattner394f2eb2003-10-16 20:12:13 +00001749FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1750 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001751 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001752};
Chris Lattner00950542001-06-06 20:29:01 +00001753
1754//===----------------------------------------------------------------------===//
1755// Rules to match Basic Blocks
1756//===----------------------------------------------------------------------===//
1757
1758ConstValueRef : ESINT64VAL { // A reference to a direct constant
1759 $$ = ValID::create($1);
1760 }
1761 | EUINT64VAL {
1762 $$ = ValID::create($1);
1763 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001764 | FPVAL { // Perhaps it's an FP constant?
1765 $$ = ValID::create($1);
1766 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001767 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001768 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001769 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001770 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001771 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001772 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001773 | NULL_TOK {
1774 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001775 }
Chris Lattner16710e92004-10-16 18:17:13 +00001776 | UNDEF {
1777 $$ = ValID::createUndef();
1778 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001779 | '<' ConstVector '>' { // Nonempty unsized packed vector
1780 const Type *ETy = (*$2)[0]->getType();
1781 int NumElements = $2->size();
1782
1783 PackedType* pt = PackedType::get(ETy, NumElements);
1784 PATypeHolder* PTy = new PATypeHolder(
1785 HandleUpRefs(
1786 PackedType::get(
1787 ETy,
1788 NumElements)
1789 )
1790 );
1791
1792 // Verify all elements are correct type!
1793 for (unsigned i = 0; i < $2->size(); i++) {
1794 if (ETy != (*$2)[i]->getType())
1795 ThrowException("Element #" + utostr(i) + " is not of type '" +
1796 ETy->getDescription() +"' as required!\nIt is of type '" +
1797 (*$2)[i]->getType()->getDescription() + "'.");
1798 }
1799
1800 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1801 delete PTy; delete $2;
1802 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001803 | ConstExpr {
1804 $$ = ValID::create($1);
1805 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001806
Chris Lattner2079fde2001-10-13 06:41:08 +00001807// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1808// another value.
1809//
1810SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001811 $$ = ValID::create($1);
1812 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001813 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001814 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001815 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001816
1817// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001818ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001819
Chris Lattner00950542001-06-06 20:29:01 +00001820
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001821// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1822// type immediately preceeds the value reference, and allows complex constant
1823// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001824ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001825 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001826 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001827
Chris Lattner00950542001-06-06 20:29:01 +00001828BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001829 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001830 }
Chris Lattner7e708292002-06-25 16:13:24 +00001831 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001832 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001833 };
Chris Lattner00950542001-06-06 20:29:01 +00001834
1835
1836// Basic blocks are terminated by branching instructions:
1837// br, br/cc, switch, ret
1838//
Chris Lattner2079fde2001-10-13 06:41:08 +00001839BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001840 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001841 InsertValue($3);
1842
1843 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001844 InsertValue($1);
1845 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001846 };
Chris Lattner00950542001-06-06 20:29:01 +00001847
1848InstructionList : InstructionList Inst {
1849 $1->getInstList().push_back($2);
1850 $$ = $1;
1851 }
1852 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001853 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001854
1855 // Make sure to move the basic block to the correct location in the
1856 // function, instead of leaving it inserted wherever it was first
1857 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001858 Function::BasicBlockListType &BBL =
1859 CurFun.CurrentFunction->getBasicBlockList();
1860 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner22ae2322004-07-13 08:39:15 +00001861 }
1862 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001863 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001864
1865 // Make sure to move the basic block to the correct location in the
1866 // function, instead of leaving it inserted wherever it was first
1867 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001868 Function::BasicBlockListType &BBL =
1869 CurFun.CurrentFunction->getBasicBlockList();
1870 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner51727be2002-06-04 21:58:56 +00001871 };
Chris Lattner00950542001-06-06 20:29:01 +00001872
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001873BBTerminatorInst : RET ResolvedVal { // Return with a result...
1874 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001875 }
1876 | RET VOID { // Return with no result...
1877 $$ = new ReturnInst();
1878 }
1879 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001880 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001881 } // Conditional Branch...
1882 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001883 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001884 }
1885 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001886 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001887 $$ = S;
1888
Chris Lattner9232b992003-04-22 18:42:41 +00001889 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001890 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001891 for (; I != E; ++I) {
1892 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
1893 S->addCase(CI, I->second);
1894 else
1895 ThrowException("Switch case is constant, but not a simple integer!");
1896 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001897 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001898 }
Chris Lattner196850c2003-05-15 21:30:00 +00001899 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001900 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00001901 $$ = S;
1902 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001903 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1904 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001905 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001906 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001907
Chris Lattnera8e8f162005-05-06 20:27:19 +00001908 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001909 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001910 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001911 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001912 if ($6) {
1913 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00001914 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001915 ParamTypes.push_back((*I)->getType());
1916 }
1917
1918 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1919 if (isVarArg) ParamTypes.pop_back();
1920
Chris Lattnera8e8f162005-05-06 20:27:19 +00001921 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001922 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001923 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001924
Chris Lattnera8e8f162005-05-06 20:27:19 +00001925 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001926
Chris Lattnera8e8f162005-05-06 20:27:19 +00001927 BasicBlock *Normal = getBBVal($10);
1928 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00001929
1930 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001931 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001932 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001933 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001934 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001935 // correctly!
1936 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001937 FunctionType::param_iterator I = Ty->param_begin();
1938 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001939 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001940
1941 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001942 if ((*ArgI)->getType() != *I)
1943 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1944 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001945
1946 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001947 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001948
Chris Lattnera8e8f162005-05-06 20:27:19 +00001949 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00001950 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001951 cast<InvokeInst>($$)->setCallingConv($2);
1952
1953 delete $3;
1954 delete $6;
Chris Lattner36143fc2003-09-08 18:54:55 +00001955 }
1956 | UNWIND {
1957 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00001958 }
1959 | UNREACHABLE {
1960 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001961 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001962
1963
Chris Lattner00950542001-06-06 20:29:01 +00001964
1965JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1966 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001967 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001968 if (V == 0)
1969 ThrowException("May only switch on a constant pool value!");
1970
Chris Lattner64116f92004-07-14 01:33:11 +00001971 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001972 }
1973 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001974 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001975 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001976
1977 if (V == 0)
1978 ThrowException("May only switch on a constant pool value!");
1979
Chris Lattner64116f92004-07-14 01:33:11 +00001980 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001981 };
Chris Lattner00950542001-06-06 20:29:01 +00001982
1983Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001984 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001985 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001986 InsertValue($2);
1987 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001988};
Chris Lattner00950542001-06-06 20:29:01 +00001989
Chris Lattnerc24d2082001-06-11 15:04:20 +00001990PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001991 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001992 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001993 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001994 }
1995 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1996 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001997 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001998 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001999 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002000
2001
Chris Lattner30c89792001-09-07 16:35:17 +00002002ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00002003 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002004 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00002005 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002006 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00002007 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002008 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00002009 };
Chris Lattner00950542001-06-06 20:29:01 +00002010
2011// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00002012ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00002013
Chris Lattnerddb6db42005-05-06 05:51:46 +00002014OptTailCall : TAIL CALL {
2015 $$ = true;
2016 }
2017 | CALL {
2018 $$ = false;
2019 };
2020
2021
2022
Chris Lattner4a6482b2002-09-10 19:57:26 +00002023InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00002024 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2025 !isa<PackedType>((*$2).get()))
2026 ThrowException(
2027 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00002028 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
2029 ThrowException("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002030 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2031 if ($$ == 0)
2032 ThrowException("binary operator returned null!");
2033 delete $2;
2034 }
2035 | LogicalOps Types ValueRef ',' ValueRef {
2036 if (!(*$2)->isIntegral())
2037 ThrowException("Logical operator requires integral operands!");
2038 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2039 if ($$ == 0)
2040 ThrowException("binary operator returned null!");
2041 delete $2;
2042 }
2043 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002044 if(isa<PackedType>((*$2).get())) {
2045 ThrowException(
2046 "PackedTypes currently not supported in setcc instructions!");
2047 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00002048 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00002049 if ($$ == 0)
2050 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002051 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002052 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002053 | NOT ResolvedVal {
2054 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2055 << " Replacing with 'xor'.\n";
2056
2057 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2058 if (Ones == 0)
2059 ThrowException("Expected integral type for not instruction!");
2060
2061 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002062 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00002063 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00002064 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002065 | ShiftOps ResolvedVal ',' ResolvedVal {
2066 if ($4->getType() != Type::UByteTy)
2067 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002068 if (!$2->getType()->isInteger())
2069 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002070 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00002071 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002072 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002073 if (!$4->get()->isFirstClassType())
2074 ThrowException("cast instruction to a non-primitive type: '" +
2075 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002076 $$ = new CastInst($2, *$4);
2077 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002078 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002079 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2080 if ($2->getType() != Type::BoolTy)
2081 ThrowException("select condition must be boolean!");
2082 if ($4->getType() != $6->getType())
2083 ThrowException("select value types should match!");
2084 $$ = new SelectInst($2, $4, $6);
2085 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002086 | VAARG ResolvedVal ',' Types {
Andrew Lenharthe78f50d2005-06-19 03:53:56 +00002087 NewVarArgs = true;
Chris Lattner99e7ab72003-10-18 05:53:13 +00002088 $$ = new VAArgInst($2, *$4);
2089 delete $4;
2090 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002091 | VAARG_old ResolvedVal ',' Types {
2092 ObsoleteVarArgs = true;
2093 const Type* ArgTy = $2->getType();
2094 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002095 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002096
2097 //b = vaarg a, t ->
2098 //foo = alloca 1 of t
2099 //bar = vacopy a
2100 //store bar -> foo
2101 //b = vaarg foo, t
2102 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2103 CurBB->getInstList().push_back(foo);
2104 CallInst* bar = new CallInst(NF, $2);
2105 CurBB->getInstList().push_back(bar);
2106 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2107 $$ = new VAArgInst(foo, *$4);
2108 delete $4;
2109 }
2110 | VANEXT_old ResolvedVal ',' Types {
2111 ObsoleteVarArgs = true;
2112 const Type* ArgTy = $2->getType();
2113 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002114 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002115
2116 //b = vanext a, t ->
2117 //foo = alloca 1 of t
2118 //bar = vacopy a
2119 //store bar -> foo
2120 //tmp = vaarg foo, t
2121 //b = load foo
2122 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2123 CurBB->getInstList().push_back(foo);
2124 CallInst* bar = new CallInst(NF, $2);
2125 CurBB->getInstList().push_back(bar);
2126 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2127 Instruction* tmp = new VAArgInst(foo, *$4);
2128 CurBB->getInstList().push_back(tmp);
2129 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002130 delete $4;
2131 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002132 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002133 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002134 if (!Ty->isFirstClassType())
2135 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002136 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002137 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002138 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002139 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00002140 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002141 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002142 $2->pop_front();
2143 }
2144 delete $2; // Free the list...
Chris Lattnerddb6db42005-05-06 05:51:46 +00002145 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002146 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002147 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002148 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002149
Chris Lattnera8e8f162005-05-06 20:27:19 +00002150 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002151 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002152 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002153 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002154 if ($6) {
2155 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002156 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002157 ParamTypes.push_back((*I)->getType());
2158 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002159
2160 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2161 if (isVarArg) ParamTypes.pop_back();
2162
Chris Lattnera8e8f162005-05-06 20:27:19 +00002163 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Chris Lattner595f06c2005-02-01 01:47:42 +00002164 ThrowException("LLVM functions cannot return aggregate types!");
2165
Chris Lattnera8e8f162005-05-06 20:27:19 +00002166 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002167 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002168 }
Chris Lattner00950542001-06-06 20:29:01 +00002169
Chris Lattnera8e8f162005-05-06 20:27:19 +00002170 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002171
Chris Lattner8b81bf52001-07-25 22:47:46 +00002172 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002173 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002174 // Make sure no arguments is a good thing!
2175 if (Ty->getNumParams() != 0)
2176 ThrowException("No arguments passed to a function that "
2177 "expects arguments!");
2178
Chris Lattner9232b992003-04-22 18:42:41 +00002179 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002180 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002181 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002182 // correctly!
2183 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002184 FunctionType::param_iterator I = Ty->param_begin();
2185 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002186 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002187
2188 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002189 if ((*ArgI)->getType() != *I)
2190 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2191 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002192
Chris Lattner8b81bf52001-07-25 22:47:46 +00002193 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002194 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002195
Chris Lattnera8e8f162005-05-06 20:27:19 +00002196 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002197 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002198 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002199 cast<CallInst>($$)->setCallingConv($2);
2200 delete $3;
2201 delete $6;
Chris Lattner00950542001-06-06 20:29:01 +00002202 }
2203 | MemoryInst {
2204 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002205 };
Chris Lattner00950542001-06-06 20:29:01 +00002206
Chris Lattner6cdb0112001-11-26 16:54:11 +00002207
2208// IndexList - List of indices for GEP based instructions...
2209IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002210 $$ = $2;
2211 } | /* empty */ {
2212 $$ = new std::vector<Value*>();
2213 };
2214
2215OptVolatile : VOLATILE {
2216 $$ = true;
2217 }
2218 | /* empty */ {
2219 $$ = false;
2220 };
2221
Chris Lattner027dcc52001-07-08 21:10:27 +00002222
Chris Lattnerddb6db42005-05-06 05:51:46 +00002223
Chris Lattner66db8e42005-11-06 06:34:12 +00002224MemoryInst : MALLOC Types OptCAlign {
Chris Lattner87ac9722005-11-06 06:46:28 +00002225 if ($3 != 0 && !isPowerOf2_32($3))
2226 ThrowException("Alignment amount '" + utostr($3) +
2227 "' is not a power of 2!");
Chris Lattner66db8e42005-11-06 06:34:12 +00002228 $$ = new MallocInst(*$2, 0, $3);
Chris Lattner30c89792001-09-07 16:35:17 +00002229 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002230 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002231 | MALLOC Types ',' UINT ValueRef OptCAlign {
Chris Lattner87ac9722005-11-06 06:46:28 +00002232 if ($6 != 0 && !isPowerOf2_32($6))
Chris Lattner66db8e42005-11-06 06:34:12 +00002233 ThrowException("Alignment amount '" + utostr($6) +
Chris Lattnerac6e5c12005-11-05 21:54:03 +00002234 "' is not a power of 2!");
Chris Lattner66db8e42005-11-06 06:34:12 +00002235 $$ = new MallocInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002236 delete $2;
2237 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002238 | ALLOCA Types OptCAlign {
Chris Lattner87ac9722005-11-06 06:46:28 +00002239 if ($3 != 0 && !isPowerOf2_32($3))
Chris Lattner66db8e42005-11-06 06:34:12 +00002240 ThrowException("Alignment amount '" + utostr($3) +
Chris Lattnerac6e5c12005-11-05 21:54:03 +00002241 "' is not a power of 2!");
Chris Lattner66db8e42005-11-06 06:34:12 +00002242 $$ = new AllocaInst(*$2, 0, $3);
Nate Begeman14b05292005-11-05 09:21:28 +00002243 delete $2;
2244 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002245 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Chris Lattner87ac9722005-11-06 06:46:28 +00002246 if ($6 != 0 && !isPowerOf2_32($6))
Chris Lattner66db8e42005-11-06 06:34:12 +00002247 ThrowException("Alignment amount '" + utostr($6) +
Chris Lattnerac6e5c12005-11-05 21:54:03 +00002248 "' is not a power of 2!");
Chris Lattner66db8e42005-11-06 06:34:12 +00002249 $$ = new AllocaInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002250 delete $2;
2251 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002252 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002253 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002254 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002255 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002256 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002257 }
2258
Chris Lattner15c9c032003-09-08 18:20:29 +00002259 | OptVolatile LOAD Types ValueRef {
2260 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002261 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002262 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002263 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2264 ThrowException("Can't load from pointer of non-first-class type: " +
2265 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002266 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002267 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002268 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002269 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2270 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002271 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002272 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002273 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002274 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002275 if (ElTy != $3->getType())
2276 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002277 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002278
Chris Lattner79ad13802003-09-08 20:29:46 +00002279 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002280 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002281 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002282 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002283 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002284 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002285
2286 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2287 // indices to uint struct indices for compatibility.
2288 generic_gep_type_iterator<std::vector<Value*>::iterator>
2289 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2290 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2291 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2292 if (isa<StructType>(*GTI)) // Only change struct indices
2293 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2294 if (CUI->getType() == Type::UByteTy)
2295 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2296
Chris Lattner30c89792001-09-07 16:35:17 +00002297 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002298 ThrowException("Invalid getelementptr indices for type '" +
2299 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002300 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2301 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002302 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002303
Brian Gaeked0fde302003-11-11 22:41:34 +00002304
Chris Lattner00950542001-06-06 20:29:01 +00002305%%
Chris Lattner09083092001-07-08 04:57:15 +00002306int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002307 std::string where
2308 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002309 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002310 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002311 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002312 errMsg += "end-of-file.";
2313 else
Chris Lattner9232b992003-04-22 18:42:41 +00002314 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002315 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002316 return 0;
2317}