blob: 0e88c0189373b2c111dba1436b36b2f18b259adc [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000016#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000018#include "llvm/SymbolTable.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000019#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/ADT/STLExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000021#include <algorithm>
22#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000023#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000024#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000025
Chris Lattner386a3b72001-10-16 19:54:17 +000026int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000027int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000028int yyparse();
29
Brian Gaeked0fde302003-11-11 22:41:34 +000030namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000031 std::string CurFilename;
32}
33using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000034
Chris Lattner00950542001-06-06 20:29:01 +000035static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattner30c89792001-09-07 16:35:17 +000037// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
38// relating to upreferences in the input stream.
39//
40//#define DEBUG_UPREFS 1
41#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000042#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000043#else
44#define UR_OUT(X)
45#endif
46
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000047#define YYERROR_VERBOSE 1
48
Chris Lattner99e7ab72003-10-18 05:53:13 +000049// HACK ALERT: This variable is used to implement the automatic conversion of
50// variable argument instructions from their old to new forms. When this
51// compatiblity "Feature" is removed, this should be too.
52//
53static BasicBlock *CurBB;
54static bool ObsoleteVarArgs;
55
56
Chris Lattner7e708292002-06-25 16:13:24 +000057// This contains info used when building the body of a function. It is
58// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000059//
Chris Lattner9232b992003-04-22 18:42:41 +000060typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner735f2702004-07-08 22:30:50 +000061static void ResolveDefinitions(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";
Chris Lattner749ce032002-03-11 22:12:39 +000097
98 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...
Chris Lattnera09000d2004-07-14 23:03:46 +0000189 if ((unsigned)D.Num < CurModule.Types.size())
190 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) {
Chris Lattner6e6026b2002-11-20 18:36:02 +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()) {
248 if (Num < VI->second.size())
249 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;
Chris Lattner00950542001-06-06 20:29:01 +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
Chris Lattner2079fde2001-10-13 06:41:08 +0000271 // Check to make sure that "Ty" is an integral type, and that our
272 // 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 '" +
276 itostr(D.ConstPool64) + "' is invalid for type '" +
277 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);
Chris Lattner2079fde2001-10-13 06:41:08 +0000296
297 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));
Chris Lattner2079fde2001-10-13 06:41:08 +0000301
Chris Lattner16710e92004-10-16 18:17:13 +0000302 case ValID::ConstUndefVal: // Is it an undef value?
303 return UndefValue::get(Ty);
304
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
333 // If we reached here, we referenced either a symbol that we don't know about
334 // or an id number that hasn't been read yet. We may be referencing something
335 // forward, so just create an entry to be resolved later and get to it...
336 //
Chris Lattner64116f92004-07-14 01:33:11 +0000337 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000338
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000339 // Remember where this forward reference came from. FIXME, shouldn't we try
340 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000341 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000342 llvmAsmlineno)));
343
Chris Lattner79df7c02002-03-26 18:01:55 +0000344 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000345 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000346 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000347 InsertValue(V, CurModule.LateResolveValues);
348 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000349}
350
Chris Lattner2e2ed292004-07-14 06:28:35 +0000351/// getBBVal - This is used for two purposes:
352/// * If isDefinition is true, a new basic block with the specified ID is being
353/// defined.
354/// * If isDefinition is true, this is a reference to a basic block, which may
355/// or may not be a forward reference.
356///
357static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000358 assert(inFunctionScope() && "Can't get basic block at global scope!");
359
Chris Lattner2e2ed292004-07-14 06:28:35 +0000360 std::string Name;
361 BasicBlock *BB = 0;
362 switch (ID.Type) {
363 default: ThrowException("Illegal label reference " + ID.getName());
364 case ValID::NumberVal: // Is it a numbered definition?
365 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
366 CurFun.NumberedBlocks.resize(ID.Num+1);
367 BB = CurFun.NumberedBlocks[ID.Num];
368 break;
369 case ValID::NameVal: // Is it a named definition?
370 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000371 if (Value *N = CurFun.CurrentFunction->
372 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000373 BB = cast<BasicBlock>(N);
374 break;
375 }
Chris Lattner64116f92004-07-14 01:33:11 +0000376
Chris Lattner2e2ed292004-07-14 06:28:35 +0000377 // See if the block has already been defined.
378 if (BB) {
379 // If this is the definition of the block, make sure the existing value was
380 // just a forward reference. If it was a forward reference, there will be
381 // an entry for it in the PlaceHolderInfo map.
382 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
383 // The existing value was a definition, not a forward reference.
384 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000385
Chris Lattner2e2ed292004-07-14 06:28:35 +0000386 ID.destroy(); // Free strdup'd memory.
387 return BB;
388 }
389
390 // Otherwise this block has not been seen before.
391 BB = new BasicBlock("", CurFun.CurrentFunction);
392 if (ID.Type == ValID::NameVal) {
393 BB->setName(ID.Name);
394 } else {
395 CurFun.NumberedBlocks[ID.Num] = BB;
396 }
397
398 // If this is not a definition, keep track of it so we can use it as a forward
399 // reference.
400 if (!isDefinition) {
401 // Remember where this forward reference came from.
402 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
403 } else {
404 // The forward declaration could have been inserted anywhere in the
405 // function: insert it into the correct place now.
406 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
407 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
408 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000409 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000410 return BB;
411}
412
Chris Lattner00950542001-06-06 20:29:01 +0000413
414//===----------------------------------------------------------------------===//
415// Code to handle forward references in instructions
416//===----------------------------------------------------------------------===//
417//
418// This code handles the late binding needed with statements that reference
419// values not defined yet... for example, a forward branch, or the PHI node for
420// a loop body.
421//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000422// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000423// and back patchs after we are done.
424//
425
426// ResolveDefinitions - If we could not resolve some defs at parsing
427// time (forward branches, phi functions for loops, etc...) resolve the
428// defs now...
429//
Chris Lattner735f2702004-07-08 22:30:50 +0000430static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
431 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000432 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000433 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000434 E = LateResolvers.end(); LRI != E; ++LRI) {
435 ValueList &List = LRI->second;
436 while (!List.empty()) {
437 Value *V = List.back();
438 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000439
440 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
441 CurModule.PlaceHolderInfo.find(V);
442 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
443
444 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000445
Chris Lattner735f2702004-07-08 22:30:50 +0000446 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000447 if (TheRealValue) {
448 V->replaceAllUsesWith(TheRealValue);
449 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000450 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000451 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000452 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000453 // resolver table
454 InsertValue(V, *FutureLateResolvers);
455 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000456 if (DID.Type == ValID::NameVal)
457 ThrowException("Reference to an invalid definition: '" +DID.getName()+
458 "' of type '" + V->getType()->getDescription() + "'",
459 PHI->second.second);
460 else
461 ThrowException("Reference to an invalid definition: #" +
462 itostr(DID.Num) + " of type '" +
463 V->getType()->getDescription() + "'",
464 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000465 }
Chris Lattner00950542001-06-06 20:29:01 +0000466 }
467 }
468
469 LateResolvers.clear();
470}
471
Chris Lattner4a42e902001-10-22 05:56:09 +0000472// ResolveTypeTo - A brand new type was just declared. This means that (if
473// name is not null) things referencing Name can be resolved. Otherwise, things
474// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000475//
Chris Lattner4a42e902001-10-22 05:56:09 +0000476static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000477 ValID D;
478 if (Name) D = ValID::create(Name);
479 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000480
Chris Lattner355ad1f2005-03-21 06:27:42 +0000481 std::map<ValID, PATypeHolder>::iterator I =
482 CurModule.LateResolveTypes.find(D);
483 if (I != CurModule.LateResolveTypes.end()) {
484 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
485 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000486 }
487}
488
Chris Lattner1781aca2001-09-18 04:00:54 +0000489// setValueName - Set the specified value to the name given. The name may be
490// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000491// assumed to be a malloc'd string buffer, and is free'd by this function.
492//
493static void setValueName(Value *V, char *NameStr) {
494 if (NameStr) {
495 std::string Name(NameStr); // Copy string
496 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000497
498 if (V->getType() == Type::VoidTy)
499 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000500
501 assert(inFunctionScope() && "Must be in function scope!");
502 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
503 if (ST.lookup(V->getType(), Name))
504 ThrowException("Redefinition of value named '" + Name + "' in the '" +
505 V->getType()->getDescription() + "' type plane!");
506
Chris Lattnerc9aea522004-07-13 08:12:39 +0000507 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000508 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000509 }
510}
511
Chris Lattnerd88998d2004-07-14 08:23:52 +0000512/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
513/// this is a declaration, otherwise it is a definition.
514static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
515 bool isConstantGlobal, const Type *Ty,
516 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000517 if (isa<FunctionType>(Ty))
518 ThrowException("Cannot declare global vars of function type!");
519
Chris Lattnera09000d2004-07-14 23:03:46 +0000520 const PointerType *PTy = PointerType::get(Ty);
521
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000522 std::string Name;
523 if (NameStr) {
524 Name = NameStr; // Copy string
525 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000526 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000527
Chris Lattner8a327842004-07-14 21:44:00 +0000528 // See if this global value was forward referenced. If so, recycle the
529 // object.
530 ValID ID;
531 if (!Name.empty()) {
532 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000533 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000534 ID = ValID::create((int)CurModule.Values[PTy].size());
535 }
536
537 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
538 // Move the global to the end of the list, from whereever it was
539 // previously inserted.
540 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
541 CurModule.CurrentModule->getGlobalList().remove(GV);
542 CurModule.CurrentModule->getGlobalList().push_back(GV);
543 GV->setInitializer(Initializer);
544 GV->setLinkage(Linkage);
545 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000546 InsertValue(GV, CurModule.Values);
Chris Lattnera09000d2004-07-14 23:03:46 +0000547 return;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000548 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000549
550 // If this global has a name, check to see if there is already a definition
551 // of this global in the module. If so, merge as appropriate. Note that
552 // this is really just a hack around problems in the CFE. :(
553 if (!Name.empty()) {
554 // We are a simple redefinition of a value, check to see if it is defined
555 // the same as the old one.
556 if (GlobalVariable *EGV =
557 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
558 // We are allowed to redefine a global variable in two circumstances:
559 // 1. If at least one of the globals is uninitialized or
560 // 2. If both initializers have the same value.
561 //
562 if (!EGV->hasInitializer() || !Initializer ||
563 EGV->getInitializer() == Initializer) {
564
565 // Make sure the existing global version gets the initializer! Make
566 // sure that it also gets marked const if the new version is.
567 if (Initializer && !EGV->hasInitializer())
568 EGV->setInitializer(Initializer);
569 if (isConstantGlobal)
570 EGV->setConstant(true);
571 EGV->setLinkage(Linkage);
572 return;
573 }
574
575 ThrowException("Redefinition of global variable named '" + Name +
576 "' in the '" + Ty->getDescription() + "' type plane!");
577 }
578 }
579
580 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000581 GlobalVariable *GV =
582 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
583 CurModule.CurrentModule);
584 InsertValue(GV, CurModule.Values);
Chris Lattnerd88998d2004-07-14 08:23:52 +0000585}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000586
Reid Spencer75719bf2004-05-26 21:48:31 +0000587// setTypeName - Set the specified type to the name given. The name may be
588// null potentially, in which case this is a noop. The string passed in is
589// assumed to be a malloc'd string buffer, and is freed by this function.
590//
591// This function returns true if the type has already been defined, but is
592// allowed to be redefined in the specified context. If the name is a new name
593// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000594static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000595 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000596 if (NameStr == 0) return false;
597
598 std::string Name(NameStr); // Copy string
599 free(NameStr); // Free old string
600
601 // We don't allow assigning names to void type
602 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000603 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000604
Chris Lattnera09000d2004-07-14 23:03:46 +0000605 // Set the type name, checking for conflicts as we do so.
606 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000607
Chris Lattnera09000d2004-07-14 23:03:46 +0000608 if (AlreadyExists) { // Inserting a name that is already defined???
609 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
610 assert(Existing && "Conflict but no matching type?");
611
Reid Spencer75719bf2004-05-26 21:48:31 +0000612 // There is only one case where this is allowed: when we are refining an
613 // opaque type. In this case, Existing will be an opaque type.
614 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
615 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000616 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000617 return true;
618 }
619
620 // Otherwise, this is an attempt to redefine a type. That's okay if
621 // the redefinition is identical to the original. This will be so if
622 // Existing and T point to the same Type object. In this one case we
623 // allow the equivalent redefinition.
624 if (Existing == T) return true; // Yes, it's equal.
625
626 // Any other kind of (non-equivalent) redefinition is an error.
627 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000628 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000629 }
630
Reid Spencer75719bf2004-05-26 21:48:31 +0000631 return false;
632}
Chris Lattner8896eda2001-07-09 19:38:36 +0000633
Chris Lattner30c89792001-09-07 16:35:17 +0000634//===----------------------------------------------------------------------===//
635// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000636//
Chris Lattner8896eda2001-07-09 19:38:36 +0000637
Chris Lattner3b073862004-02-09 03:19:29 +0000638// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000639//
640static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000641 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
642 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000643}
644
645namespace {
646 struct UpRefRecord {
647 // NestingLevel - The number of nesting levels that need to be popped before
648 // this type is resolved.
649 unsigned NestingLevel;
650
651 // LastContainedTy - This is the type at the current binding level for the
652 // type. Every time we reduce the nesting level, this gets updated.
653 const Type *LastContainedTy;
654
655 // UpRefTy - This is the actual opaque type that the upreference is
656 // represented with.
657 OpaqueType *UpRefTy;
658
659 UpRefRecord(unsigned NL, OpaqueType *URTy)
660 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
661 };
Chris Lattner30c89792001-09-07 16:35:17 +0000662}
Chris Lattner698b56e2001-07-20 19:15:08 +0000663
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000664// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000665static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000666
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000667/// HandleUpRefs - Every time we finish a new layer of types, this function is
668/// called. It loops through the UpRefs vector, which is a list of the
669/// currently active types. For each type, if the up reference is contained in
670/// the newly completed type, we decrement the level count. When the level
671/// count reaches zero, the upreferenced type is the type that is passed in:
672/// thus we can complete the cycle.
673///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000674static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000675 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000676 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000677 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000678 "' newly formed. Resolving upreferences.\n" <<
679 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000680
681 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
682 // to zero), we resolve them all together before we resolve them to Ty. At
683 // the end of the loop, if there is anything to resolve to Ty, it will be in
684 // this variable.
685 OpaqueType *TypeToResolve = 0;
686
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000687 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000688 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Reid Spencer3271ed52004-07-18 00:08:11 +0000689 << UpRefs[i].second->getDescription() << ") = "
690 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000691 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
692 // Decrement level of upreference
693 unsigned Level = --UpRefs[i].NestingLevel;
694 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000695 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000696 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000697 if (!TypeToResolve) {
698 TypeToResolve = UpRefs[i].UpRefTy;
699 } else {
700 UR_OUT(" * Resolving upreference for "
701 << UpRefs[i].second->getDescription() << "\n";
702 std::string OldName = UpRefs[i].UpRefTy->getDescription());
703 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
704 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
705 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
706 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000707 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000708 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000709 }
710 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000711 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000712
Chris Lattner026a8ce2004-02-09 18:53:54 +0000713 if (TypeToResolve) {
714 UR_OUT(" * Resolving upreference for "
715 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000716 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000717 TypeToResolve->refineAbstractTypeTo(Ty);
718 }
719
Chris Lattner8896eda2001-07-09 19:38:36 +0000720 return Ty;
721}
722
Chris Lattner30c89792001-09-07 16:35:17 +0000723
Chris Lattner00950542001-06-06 20:29:01 +0000724//===----------------------------------------------------------------------===//
725// RunVMAsmParser - Define an interface to this parser
726//===----------------------------------------------------------------------===//
727//
Chris Lattner86427632004-07-14 06:39:48 +0000728Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000729 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000730 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000731 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000732 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000733
Chris Lattner75f20532003-04-22 18:02:52 +0000734 // Allocate a new module to read
735 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000736
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000737 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000738
Chris Lattner00950542001-06-06 20:29:01 +0000739 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000740
741 // Check to see if they called va_start but not va_arg..
742 if (!ObsoleteVarArgs)
743 if (Function *F = Result->getNamedFunction("llvm.va_start"))
Chris Lattnere4d5c442005-03-15 04:54:21 +0000744 if (F->arg_size() == 1) {
Chris Lattner99e7ab72003-10-18 05:53:13 +0000745 std::cerr << "WARNING: this file uses obsolete features. "
746 << "Assemble and disassemble to update it.\n";
747 ObsoleteVarArgs = true;
748 }
749
Chris Lattner99e7ab72003-10-18 05:53:13 +0000750 if (ObsoleteVarArgs) {
751 // If the user is making use of obsolete varargs intrinsics, adjust them for
752 // the user.
753 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
Chris Lattnere4d5c442005-03-15 04:54:21 +0000754 assert(F->arg_size() == 1 && "Obsolete va_start takes 1 argument!");
Chris Lattner99e7ab72003-10-18 05:53:13 +0000755
756 const Type *RetTy = F->getFunctionType()->getParamType(0);
757 RetTy = cast<PointerType>(RetTy)->getElementType();
758 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
759
760 while (!F->use_empty()) {
761 CallInst *CI = cast<CallInst>(F->use_back());
762 Value *V = new CallInst(NF, "", CI);
763 new StoreInst(V, CI->getOperand(1), CI);
764 CI->getParent()->getInstList().erase(CI);
765 }
766 Result->getFunctionList().erase(F);
767 }
768
769 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
Chris Lattnere4d5c442005-03-15 04:54:21 +0000770 assert(F->arg_size() == 1 && "Obsolete va_end takes 1 argument!");
Chris Lattner99e7ab72003-10-18 05:53:13 +0000771 const Type *ArgTy = F->getFunctionType()->getParamType(0);
772 ArgTy = cast<PointerType>(ArgTy)->getElementType();
773 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
774 ArgTy, 0);
775
776 while (!F->use_empty()) {
777 CallInst *CI = cast<CallInst>(F->use_back());
778 Value *V = new LoadInst(CI->getOperand(1), "", CI);
779 new CallInst(NF, V, "", CI);
780 CI->getParent()->getInstList().erase(CI);
781 }
782 Result->getFunctionList().erase(F);
783 }
784
785 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
Chris Lattnere4d5c442005-03-15 04:54:21 +0000786 assert(F->arg_size() == 2 && "Obsolete va_copy takes 2 argument!");
Chris Lattner99e7ab72003-10-18 05:53:13 +0000787 const Type *ArgTy = F->getFunctionType()->getParamType(0);
788 ArgTy = cast<PointerType>(ArgTy)->getElementType();
789 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
790 ArgTy, 0);
791
792 while (!F->use_empty()) {
793 CallInst *CI = cast<CallInst>(F->use_back());
794 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
795 new StoreInst(V, CI->getOperand(1), CI);
796 CI->getParent()->getInstList().erase(CI);
797 }
798 Result->getFunctionList().erase(F);
799 }
800 }
801
Chris Lattner00950542001-06-06 20:29:01 +0000802 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
803 ParserResult = 0;
804
805 return Result;
806}
807
808%}
809
810%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000811 llvm::Module *ModuleVal;
812 llvm::Function *FunctionVal;
813 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
814 llvm::BasicBlock *BasicBlockVal;
815 llvm::TerminatorInst *TermInstVal;
816 llvm::Instruction *InstVal;
817 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000818
Brian Gaeked0fde302003-11-11 22:41:34 +0000819 const llvm::Type *PrimType;
820 llvm::PATypeHolder *TypeVal;
821 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000822
Brian Gaeked0fde302003-11-11 22:41:34 +0000823 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
824 std::vector<llvm::Value*> *ValueList;
825 std::list<llvm::PATypeHolder> *TypeList;
826 std::list<std::pair<llvm::Value*,
827 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
828 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
829 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000830
Brian Gaeked0fde302003-11-11 22:41:34 +0000831 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000832 int64_t SInt64Val;
833 uint64_t UInt64Val;
834 int SIntVal;
835 unsigned UIntVal;
836 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000837 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000838
Chris Lattner30c89792001-09-07 16:35:17 +0000839 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000840 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000841
Brian Gaeked0fde302003-11-11 22:41:34 +0000842 llvm::Instruction::BinaryOps BinaryOpVal;
843 llvm::Instruction::TermOps TermOpVal;
844 llvm::Instruction::MemoryOps MemOpVal;
845 llvm::Instruction::OtherOps OtherOpVal;
846 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000847}
848
Chris Lattner79df7c02002-03-26 18:01:55 +0000849%type <ModuleVal> Module FunctionList
850%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000851%type <BasicBlockVal> BasicBlock InstructionList
852%type <TermInstVal> BBTerminatorInst
853%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000854%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000855%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000856%type <ArgList> ArgList ArgListH
857%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000858%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000859%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000860%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000861%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000862%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000863%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000864%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000865%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000866%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000867
Chris Lattner2079fde2001-10-13 06:41:08 +0000868// ValueRef - Unresolved reference to a definition or BB
869%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000870%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000871// Tokens and types for handling constant integer values
872//
873// ESINT64VAL - A negative number within long long range
874%token <SInt64Val> ESINT64VAL
875
876// EUINT64VAL - A positive number within uns. long long range
877%token <UInt64Val> EUINT64VAL
878%type <SInt64Val> EINT64VAL
879
880%token <SIntVal> SINTVAL // Signed 32 bit ints...
881%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
882%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000883%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000884
885// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000886%type <TypeVal> Types TypesV UpRTypes UpRTypesV
887%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000888%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
889%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000890
Chris Lattner6c23f572003-08-22 05:42:10 +0000891%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
892%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000893
894
Chris Lattner91ef4602004-03-31 03:49:47 +0000895%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000896%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000897%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Reid Spencer0be13e72004-07-25 17:58:28 +0000898%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer1c7b9072004-09-14 05:43:23 +0000899%token DEPLIBS
Chris Lattner00950542001-06-06 20:29:01 +0000900
901// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000902%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000903
Chris Lattner00950542001-06-06 20:29:01 +0000904// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000905%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000906%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000907%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000908
909// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000910%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000911
Chris Lattner027dcc52001-07-08 21:10:27 +0000912// Other Operators
913%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000914%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000915%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000916
Chris Lattner00950542001-06-06 20:29:01 +0000917%start Module
918%%
919
920// Handle constant integer size restriction and conversion...
921//
Chris Lattner51727be2002-06-04 21:58:56 +0000922INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000923INTVAL : UINTVAL {
924 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
925 ThrowException("Value too large for type!");
926 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000927};
Chris Lattner00950542001-06-06 20:29:01 +0000928
929
Chris Lattner51727be2002-06-04 21:58:56 +0000930EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000931EINT64VAL : EUINT64VAL {
932 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
933 ThrowException("Value too large for type!");
934 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000935};
Chris Lattner00950542001-06-06 20:29:01 +0000936
Chris Lattner00950542001-06-06 20:29:01 +0000937// Operations that are notably excluded from this list include:
938// RET, BR, & SWITCH because they end basic blocks and are treated specially.
939//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000940ArithmeticOps: ADD | SUB | MUL | DIV | REM;
941LogicalOps : AND | OR | XOR;
942SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +0000943
Chris Lattner51727be2002-06-04 21:58:56 +0000944ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000945
Chris Lattnere98dda62001-07-14 06:10:16 +0000946// These are some types that allow classification if we only want a particular
947// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000948SIntType : LONG | INT | SHORT | SBYTE;
949UIntType : ULONG | UINT | USHORT | UBYTE;
950IntType : SIntType | UIntType;
951FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000952
Chris Lattnere98dda62001-07-14 06:10:16 +0000953// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000954OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000955 $$ = $1;
956 }
957 | /*empty*/ {
958 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000959 };
Chris Lattner00950542001-06-06 20:29:01 +0000960
Chris Lattner4ad02e72003-04-16 20:28:45 +0000961OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
962 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000963 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000964 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
965 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000966
967//===----------------------------------------------------------------------===//
968// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000969// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000970// access to it, a user must explicitly use TypesV.
971//
972
973// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000974TypesV : Types | VOID { $$ = new PATypeHolder($1); };
975UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000976
977Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000978 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000979 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
980 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000981 };
Chris Lattner30c89792001-09-07 16:35:17 +0000982
983
984// Derived types are added later...
985//
Chris Lattner51727be2002-06-04 21:58:56 +0000986PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
987PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000988UpRTypes : OPAQUE {
989 $$ = new PATypeHolder(OpaqueType::get());
990 }
991 | PrimType {
992 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000993 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000994UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000995 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000996};
Chris Lattner30c89792001-09-07 16:35:17 +0000997
Chris Lattner30c89792001-09-07 16:35:17 +0000998// Include derived types in the Types production.
999//
1000UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001001 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001002 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001003 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001004 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001005 UR_OUT("New Upreference!\n");
1006 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001007 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001008 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001009 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1010 E = $3->end(); I != E; ++I)
1011 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001012 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1013 if (isVarArg) Params.pop_back();
1014
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001015 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001016 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001017 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001018 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001019 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001020 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001021 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001022 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001023 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1024 const llvm::Type* ElemTy = $4->get();
1025 if ((unsigned)$2 != $2) {
1026 ThrowException("Unsigned result not equal to signed result");
1027 }
1028 if(!ElemTy->isPrimitiveType()) {
1029 ThrowException("Elemental type of a PackedType must be primitive");
1030 }
1031 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1032 delete $4;
1033 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001034 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001035 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001036 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1037 E = $2->end(); I != E; ++I)
1038 Elements.push_back(*I);
1039
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001040 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001041 delete $2;
1042 }
1043 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001044 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001045 }
1046 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001047 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001048 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001049 };
Chris Lattner30c89792001-09-07 16:35:17 +00001050
Chris Lattner7e708292002-06-25 16:13:24 +00001051// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001052// declaration type lists
1053//
1054TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001055 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001056 $$->push_back(*$1); delete $1;
1057 }
1058 | TypeListI ',' UpRTypes {
1059 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001060 };
Chris Lattner30c89792001-09-07 16:35:17 +00001061
Chris Lattner7e708292002-06-25 16:13:24 +00001062// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001063ArgTypeListI : TypeListI
1064 | TypeListI ',' DOTDOTDOT {
1065 ($$=$1)->push_back(Type::VoidTy);
1066 }
1067 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001068 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001069 }
1070 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001071 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001072 };
Chris Lattner30c89792001-09-07 16:35:17 +00001073
Chris Lattnere98dda62001-07-14 06:10:16 +00001074// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001075// production is used ONLY to represent constants that show up AFTER a 'const',
1076// 'constant' or 'global' token at global scope. Constants that can be inlined
1077// into other expressions (such as integers and constexprs) are handled by the
1078// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001079//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001080ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001081 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001082 if (ATy == 0)
1083 ThrowException("Cannot make array constant with type: '" +
1084 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001085 const Type *ETy = ATy->getElementType();
1086 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001087
Chris Lattner30c89792001-09-07 16:35:17 +00001088 // Verify that we have the correct size...
1089 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001090 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001091 utostr($3->size()) + " arguments, but has size of " +
1092 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001093
Chris Lattner30c89792001-09-07 16:35:17 +00001094 // Verify all elements are correct type!
1095 for (unsigned i = 0; i < $3->size(); i++) {
1096 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001097 ThrowException("Element #" + utostr(i) + " is not of type '" +
1098 ETy->getDescription() +"' as required!\nIt is of type '"+
1099 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001100 }
1101
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001102 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001103 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001104 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001105 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001106 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001107 if (ATy == 0)
1108 ThrowException("Cannot make array constant with type: '" +
1109 (*$1)->getDescription() + "'!");
1110
1111 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001112 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001113 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001114 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001115 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001116 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001117 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001118 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001119 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001120 if (ATy == 0)
1121 ThrowException("Cannot make array constant with type: '" +
1122 (*$1)->getDescription() + "'!");
1123
Chris Lattner30c89792001-09-07 16:35:17 +00001124 int NumElements = ATy->getNumElements();
1125 const Type *ETy = ATy->getElementType();
1126 char *EndStr = UnEscapeLexed($3, true);
1127 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001128 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001129 itostr((int)(EndStr-$3)) +
1130 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001131 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001132 if (ETy == Type::SByteTy) {
1133 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001134 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001135 } else if (ETy == Type::UByteTy) {
1136 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001137 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001138 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001139 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001140 ThrowException("Cannot build string arrays of non byte sized elements!");
1141 }
Chris Lattner30c89792001-09-07 16:35:17 +00001142 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001143 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001144 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001145 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001146 | Types '<' ConstVector '>' { // Nonempty unsized arr
1147 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1148 if (PTy == 0)
1149 ThrowException("Cannot make packed constant with type: '" +
1150 (*$1)->getDescription() + "'!");
1151 const Type *ETy = PTy->getElementType();
1152 int NumElements = PTy->getNumElements();
1153
1154 // Verify that we have the correct size...
1155 if (NumElements != -1 && NumElements != (int)$3->size())
1156 ThrowException("Type mismatch: constant sized packed initialized with " +
1157 utostr($3->size()) + " arguments, but has size of " +
1158 itostr(NumElements) + "!");
1159
1160 // Verify all elements are correct type!
1161 for (unsigned i = 0; i < $3->size(); i++) {
1162 if (ETy != (*$3)[i]->getType())
1163 ThrowException("Element #" + utostr(i) + " is not of type '" +
1164 ETy->getDescription() +"' as required!\nIt is of type '"+
1165 (*$3)[i]->getType()->getDescription() + "'.");
1166 }
1167
1168 $$ = ConstantPacked::get(PTy, *$3);
1169 delete $1; delete $3;
1170 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001171 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001172 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001173 if (STy == 0)
1174 ThrowException("Cannot make struct constant with type: '" +
1175 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001176
Chris Lattnerc6212f12003-05-21 16:06:56 +00001177 if ($3->size() != STy->getNumContainedTypes())
1178 ThrowException("Illegal number of initializers for structure type!");
1179
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001180 // Check to ensure that constants are compatible with the type initializer!
1181 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001182 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001183 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001184 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001185 "' for element #" + utostr(i) +
1186 " of structure initializer!");
1187
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001188 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001189 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001190 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001191 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001192 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001193 if (STy == 0)
1194 ThrowException("Cannot make struct constant with type: '" +
1195 (*$1)->getDescription() + "'!");
1196
1197 if (STy->getNumContainedTypes() != 0)
1198 ThrowException("Illegal number of initializers for structure type!");
1199
1200 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1201 delete $1;
1202 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001203 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001204 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001205 if (PTy == 0)
1206 ThrowException("Cannot make null pointer constant with type: '" +
1207 (*$1)->getDescription() + "'!");
1208
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001209 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001210 delete $1;
1211 }
Chris Lattner16710e92004-10-16 18:17:13 +00001212 | Types UNDEF {
1213 $$ = UndefValue::get($1->get());
1214 delete $1;
1215 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001216 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001217 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001218 if (Ty == 0)
1219 ThrowException("Global const reference must be a pointer type!");
1220
Chris Lattner3101c252002-08-15 17:58:33 +00001221 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001222 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001223 // the context of a function, getValNonImprovising will search the functions
1224 // symbol table instead of the module symbol table for the global symbol,
1225 // which throws things all off. To get around this, we just tell
1226 // getValNonImprovising that we are at global scope here.
1227 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001228 Function *SavedCurFn = CurFun.CurrentFunction;
1229 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001230
Chris Lattner2079fde2001-10-13 06:41:08 +00001231 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001232
Chris Lattner394f2eb2003-10-16 20:12:13 +00001233 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001234
Chris Lattner2079fde2001-10-13 06:41:08 +00001235 // If this is an initializer for a constant pointer, which is referencing a
1236 // (currently) undefined variable, create a stub now that shall be replaced
1237 // in the future with the right type of variable.
1238 //
1239 if (V == 0) {
1240 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1241 const PointerType *PT = cast<PointerType>(Ty);
1242
1243 // First check to see if the forward references value is already created!
1244 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001245 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001246
1247 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001248 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001249 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001250 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001251 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001252 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001253
Reid Spencer3271ed52004-07-18 00:08:11 +00001254 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001255 GlobalValue *GV;
1256 if (const FunctionType *FTy =
1257 dyn_cast<FunctionType>(PT->getElementType())) {
1258 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1259 CurModule.CurrentModule);
1260 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001261 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001262 GlobalValue::ExternalLinkage, 0,
1263 Name, CurModule.CurrentModule);
1264 }
Chris Lattner8a327842004-07-14 21:44:00 +00001265
Reid Spencer3271ed52004-07-18 00:08:11 +00001266 // Keep track of the fact that we have a forward ref to recycle it
1267 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1268 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001269 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001270 }
1271
Reid Spencer3271ed52004-07-18 00:08:11 +00001272 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001273 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001274 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001275 | Types ConstExpr {
1276 if ($1->get() != $2->getType())
1277 ThrowException("Mismatched types for constant expression!");
1278 $$ = $2;
1279 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001280 }
1281 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001282 const Type *Ty = $1->get();
1283 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1284 ThrowException("Cannot create a null initialized value of this type!");
1285 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001286 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001287 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001288
Chris Lattnere43f40b2002-10-09 00:25:32 +00001289ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001290 if (!ConstantSInt::isValueValidForType($1, $2))
1291 ThrowException("Constant value doesn't fit in type!");
1292 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001293 }
1294 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001295 if (!ConstantUInt::isValueValidForType($1, $2))
1296 ThrowException("Constant value doesn't fit in type!");
1297 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001298 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001299 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001300 $$ = ConstantBool::True;
1301 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001302 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001303 $$ = ConstantBool::False;
1304 }
1305 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001306 if (!ConstantFP::isValueValidForType($1, $2))
1307 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001308 $$ = ConstantFP::get($1, $2);
1309 };
1310
Chris Lattner00950542001-06-06 20:29:01 +00001311
Chris Lattnerd78700d2002-08-16 21:14:40 +00001312ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001313 if (!$3->getType()->isFirstClassType())
1314 ThrowException("cast constant expression from a non-primitive type: '" +
1315 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001316 if (!$5->get()->isFirstClassType())
1317 ThrowException("cast constant expression to a non-primitive type: '" +
1318 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001319 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001320 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001321 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001322 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1323 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001324 ThrowException("GetElementPtr requires a pointer operand!");
1325
Chris Lattner830b6f92004-04-05 01:30:04 +00001326 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1327 // indices to uint struct indices for compatibility.
1328 generic_gep_type_iterator<std::vector<Value*>::iterator>
1329 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1330 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1331 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1332 if (isa<StructType>(*GTI)) // Only change struct indices
1333 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1334 if (CUI->getType() == Type::UByteTy)
1335 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1336
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001337 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001338 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001339 if (!IdxTy)
1340 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001341
Chris Lattner9232b992003-04-22 18:42:41 +00001342 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001343 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1344 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001345 IdxVec.push_back(C);
1346 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001347 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001348
Chris Lattnerd78700d2002-08-16 21:14:40 +00001349 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001350
Chris Lattnerd78700d2002-08-16 21:14:40 +00001351 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001352 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001353 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1354 if ($3->getType() != Type::BoolTy)
1355 ThrowException("Select condition must be of boolean type!");
1356 if ($5->getType() != $7->getType())
1357 ThrowException("Select operand types must match!");
1358 $$ = ConstantExpr::getSelect($3, $5, $7);
1359 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001360 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001361 if ($3->getType() != $5->getType())
1362 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001363 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1364 // To retain backward compatibility with these early compilers, we emit a
1365 // cast to the appropriate integer type automatically if we are in the
1366 // broken case. See PR424 for more information.
1367 if (!isa<PointerType>($3->getType())) {
1368 $$ = ConstantExpr::get($1, $3, $5);
1369 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001370 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001371 switch (CurModule.CurrentModule->getPointerSize()) {
1372 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1373 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1374 default: ThrowException("invalid pointer binary constant expr!");
1375 }
1376 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1377 ConstantExpr::getCast($5, IntPtrTy));
1378 $$ = ConstantExpr::getCast($$, $3->getType());
1379 }
1380 }
1381 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1382 if ($3->getType() != $5->getType())
1383 ThrowException("Logical operator types must match!");
1384 if (!$3->getType()->isIntegral())
1385 ThrowException("Logical operands must have integral types!");
1386 $$ = ConstantExpr::get($1, $3, $5);
1387 }
1388 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1389 if ($3->getType() != $5->getType())
1390 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001391 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001392 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001393 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001394 if ($5->getType() != Type::UByteTy)
1395 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001396 if (!$3->getType()->isInteger())
1397 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001398 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001399 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001400
1401
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001402// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001403ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001404 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001405 }
1406 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001407 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001408 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001409 };
Chris Lattner00950542001-06-06 20:29:01 +00001410
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001411
Chris Lattner1781aca2001-09-18 04:00:54 +00001412// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001413GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001414
Chris Lattner00950542001-06-06 20:29:01 +00001415
Chris Lattner0e73ce62002-05-02 19:11:13 +00001416//===----------------------------------------------------------------------===//
1417// Rules to match Modules
1418//===----------------------------------------------------------------------===//
1419
1420// Module rule: Capture the result of parsing the whole file into a result
1421// variable...
1422//
1423Module : FunctionList {
1424 $$ = ParserResult = $1;
1425 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001426};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001427
Chris Lattner7e708292002-06-25 16:13:24 +00001428// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001429//
1430FunctionList : FunctionList Function {
1431 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001432 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001433 }
1434 | FunctionList FunctionProto {
1435 $$ = $1;
1436 }
1437 | FunctionList IMPLEMENTATION {
1438 $$ = $1;
1439 }
1440 | ConstPool {
1441 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001442 // Emit an error if there are any unresolved types left.
1443 if (!CurModule.LateResolveTypes.empty()) {
1444 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1445 if (DID.Type == ValID::NameVal)
1446 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1447 else
1448 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1449 }
Chris Lattner51727be2002-06-04 21:58:56 +00001450 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001451
Chris Lattnere98dda62001-07-14 06:10:16 +00001452// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001453ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001454 // Eagerly resolve types. This is not an optimization, this is a
1455 // requirement that is due to the fact that we could have this:
1456 //
1457 // %list = type { %list * }
1458 // %list = type { %list * } ; repeated type decl
1459 //
1460 // If types are not resolved eagerly, then the two types will not be
1461 // determined to be the same type!
1462 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001463 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001464
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001465 if (!setTypeName(*$4, $2) && !$2) {
1466 // If this is a named type that is not a redefinition, add it to the slot
1467 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001468 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001469 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001470
1471 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001472 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001473 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001474 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001475 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001476 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1477 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Chris Lattner1781aca2001-09-18 04:00:54 +00001478 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001479 | ConstPool OptAssign EXTERNAL GlobalType Types {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001480 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001481 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001482 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001483 | ConstPool TARGET TargetDefinition {
1484 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001485 | ConstPool DEPLIBS '=' LibrariesDefinition {
1486 }
Chris Lattner00950542001-06-06 20:29:01 +00001487 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001488 };
Chris Lattner00950542001-06-06 20:29:01 +00001489
1490
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001491
1492BigOrLittle : BIG { $$ = Module::BigEndian; };
1493BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1494
1495TargetDefinition : ENDIAN '=' BigOrLittle {
1496 CurModule.CurrentModule->setEndianness($3);
1497 }
1498 | POINTERSIZE '=' EUINT64VAL {
1499 if ($3 == 32)
1500 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1501 else if ($3 == 64)
1502 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1503 else
1504 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001505 }
1506 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001507 CurModule.CurrentModule->setTargetTriple($3);
1508 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001509 };
1510
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001511LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001512
1513LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001514 CurModule.CurrentModule->addLibrary($3);
1515 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001516 }
1517 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001518 CurModule.CurrentModule->addLibrary($1);
1519 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001520 }
1521 | /* empty: end of list */ {
1522 }
1523 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001524
Chris Lattner00950542001-06-06 20:29:01 +00001525//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001526// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001527//===----------------------------------------------------------------------===//
1528
Chris Lattner6c23f572003-08-22 05:42:10 +00001529Name : VAR_ID | STRINGCONSTANT;
1530OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001531
Chris Lattner6c23f572003-08-22 05:42:10 +00001532ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001533 if (*$1 == Type::VoidTy)
1534 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001535 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001536};
Chris Lattner00950542001-06-06 20:29:01 +00001537
Chris Lattner69da5cf2002-10-13 20:57:00 +00001538ArgListH : ArgListH ',' ArgVal {
1539 $$ = $1;
1540 $1->push_back(*$3);
1541 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001542 }
1543 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001544 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001545 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001546 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001547 };
Chris Lattner00950542001-06-06 20:29:01 +00001548
1549ArgList : ArgListH {
1550 $$ = $1;
1551 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001552 | ArgListH ',' DOTDOTDOT {
1553 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001554 $$->push_back(std::pair<PATypeHolder*,
1555 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001556 }
1557 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001558 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1559 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001560 }
Chris Lattner00950542001-06-06 20:29:01 +00001561 | /* empty */ {
1562 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001563 };
Chris Lattner00950542001-06-06 20:29:01 +00001564
Chris Lattner6c23f572003-08-22 05:42:10 +00001565FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001566 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001567 std::string FunctionName($2);
Chris Lattnera09000d2004-07-14 23:03:46 +00001568 free($2); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001569
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001570 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1571 ThrowException("LLVM functions cannot return aggregate types!");
1572
Chris Lattner9232b992003-04-22 18:42:41 +00001573 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001574 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001575 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001576 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001577 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001578 }
Chris Lattner00950542001-06-06 20:29:01 +00001579
Chris Lattner2079fde2001-10-13 06:41:08 +00001580 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1581 if (isVarArg) ParamTypeList.pop_back();
1582
Chris Lattner1f862af2003-04-16 18:13:57 +00001583 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001584 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001585 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001586
Chris Lattnera09000d2004-07-14 23:03:46 +00001587 ValID ID;
1588 if (!FunctionName.empty()) {
1589 ID = ValID::create((char*)FunctionName.c_str());
1590 } else {
1591 ID = ValID::create((int)CurModule.Values[PFT].size());
1592 }
1593
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001594 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001595 // See if this function was forward referenced. If so, recycle the object.
1596 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1597 // Move the function to the end of the list, from whereever it was
1598 // previously inserted.
1599 Fn = cast<Function>(FWRef);
1600 CurModule.CurrentModule->getFunctionList().remove(Fn);
1601 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1602 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1603 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1604 // If this is the case, either we need to be a forward decl, or it needs
1605 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001606 if (!CurFun.isDeclare && !Fn->isExternal())
1607 ThrowException("Redefinition of function '" + FunctionName + "'!");
1608
Chris Lattnera09000d2004-07-14 23:03:46 +00001609 // Make sure to strip off any argument names so we can't get conflicts.
1610 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001611 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001612 AI != AE; ++AI)
1613 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001614
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001615 } else { // Not already defined?
1616 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1617 CurModule.CurrentModule);
1618 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001619 }
Chris Lattner00950542001-06-06 20:29:01 +00001620
Chris Lattner394f2eb2003-10-16 20:12:13 +00001621 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001622
Chris Lattner7e708292002-06-25 16:13:24 +00001623 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001624 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001625 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001626 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001627 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001628 delete $4->back().first;
1629 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001630 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001631 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattner9232b992003-04-22 18:42:41 +00001632 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001633 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001634 delete I->first; // Delete the typeholder...
1635
Chris Lattner8ab406d2004-07-13 07:59:27 +00001636 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001637 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001638 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001639
Chris Lattner1f862af2003-04-16 18:13:57 +00001640 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001641 }
Chris Lattner51727be2002-06-04 21:58:56 +00001642};
Chris Lattner00950542001-06-06 20:29:01 +00001643
Chris Lattner9b02cc32002-05-03 18:23:48 +00001644BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1645
Chris Lattner4ad02e72003-04-16 20:28:45 +00001646FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001647 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001648
Chris Lattner4ad02e72003-04-16 20:28:45 +00001649 // Make sure that we keep track of the linkage type even if there was a
1650 // previous "declare".
1651 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001652};
Chris Lattner00950542001-06-06 20:29:01 +00001653
Chris Lattner9b02cc32002-05-03 18:23:48 +00001654END : ENDTOK | '}'; // Allow end of '}' to end a function
1655
Chris Lattner79df7c02002-03-26 18:01:55 +00001656Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001657 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001658};
Chris Lattner00950542001-06-06 20:29:01 +00001659
Chris Lattner394f2eb2003-10-16 20:12:13 +00001660FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1661 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001662 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001663};
Chris Lattner00950542001-06-06 20:29:01 +00001664
1665//===----------------------------------------------------------------------===//
1666// Rules to match Basic Blocks
1667//===----------------------------------------------------------------------===//
1668
1669ConstValueRef : ESINT64VAL { // A reference to a direct constant
1670 $$ = ValID::create($1);
1671 }
1672 | EUINT64VAL {
1673 $$ = ValID::create($1);
1674 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001675 | FPVAL { // Perhaps it's an FP constant?
1676 $$ = ValID::create($1);
1677 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001678 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001679 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001680 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001681 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001682 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001683 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001684 | NULL_TOK {
1685 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001686 }
Chris Lattner16710e92004-10-16 18:17:13 +00001687 | UNDEF {
1688 $$ = ValID::createUndef();
1689 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001690 | '<' ConstVector '>' { // Nonempty unsized packed vector
1691 const Type *ETy = (*$2)[0]->getType();
1692 int NumElements = $2->size();
1693
1694 PackedType* pt = PackedType::get(ETy, NumElements);
1695 PATypeHolder* PTy = new PATypeHolder(
1696 HandleUpRefs(
1697 PackedType::get(
1698 ETy,
1699 NumElements)
1700 )
1701 );
1702
1703 // Verify all elements are correct type!
1704 for (unsigned i = 0; i < $2->size(); i++) {
1705 if (ETy != (*$2)[i]->getType())
1706 ThrowException("Element #" + utostr(i) + " is not of type '" +
1707 ETy->getDescription() +"' as required!\nIt is of type '" +
1708 (*$2)[i]->getType()->getDescription() + "'.");
1709 }
1710
1711 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1712 delete PTy; delete $2;
1713 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001714 | ConstExpr {
1715 $$ = ValID::create($1);
1716 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001717
Chris Lattner2079fde2001-10-13 06:41:08 +00001718// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1719// another value.
1720//
1721SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001722 $$ = ValID::create($1);
1723 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001724 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001725 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001726 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001727
1728// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001729ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001730
Chris Lattner00950542001-06-06 20:29:01 +00001731
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001732// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1733// type immediately preceeds the value reference, and allows complex constant
1734// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001735ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001736 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001737 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001738
Chris Lattner00950542001-06-06 20:29:01 +00001739BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001740 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001741 }
Chris Lattner7e708292002-06-25 16:13:24 +00001742 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001743 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001744 };
Chris Lattner00950542001-06-06 20:29:01 +00001745
1746
1747// Basic blocks are terminated by branching instructions:
1748// br, br/cc, switch, ret
1749//
Chris Lattner2079fde2001-10-13 06:41:08 +00001750BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001751 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001752 InsertValue($3);
1753
1754 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001755 InsertValue($1);
1756 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001757 };
Chris Lattner00950542001-06-06 20:29:01 +00001758
1759InstructionList : InstructionList Inst {
1760 $1->getInstList().push_back($2);
1761 $$ = $1;
1762 }
1763 | /* empty */ {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001764 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001765
1766 // Make sure to move the basic block to the correct location in the
1767 // function, instead of leaving it inserted wherever it was first
1768 // referenced.
1769 CurFun.CurrentFunction->getBasicBlockList().remove(CurBB);
1770 CurFun.CurrentFunction->getBasicBlockList().push_back(CurBB);
Chris Lattner22ae2322004-07-13 08:39:15 +00001771 }
1772 | LABELSTR {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001773 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001774
1775 // Make sure to move the basic block to the correct location in the
1776 // function, instead of leaving it inserted wherever it was first
1777 // referenced.
1778 CurFun.CurrentFunction->getBasicBlockList().remove(CurBB);
1779 CurFun.CurrentFunction->getBasicBlockList().push_back(CurBB);
Chris Lattner51727be2002-06-04 21:58:56 +00001780 };
Chris Lattner00950542001-06-06 20:29:01 +00001781
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001782BBTerminatorInst : RET ResolvedVal { // Return with a result...
1783 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001784 }
1785 | RET VOID { // Return with no result...
1786 $$ = new ReturnInst();
1787 }
1788 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001789 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001790 } // Conditional Branch...
1791 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001792 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001793 }
1794 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001795 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001796 $$ = S;
1797
Chris Lattner9232b992003-04-22 18:42:41 +00001798 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001799 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001800 for (; I != E; ++I) {
1801 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
1802 S->addCase(CI, I->second);
1803 else
1804 ThrowException("Switch case is constant, but not a simple integer!");
1805 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001806 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001807 }
Chris Lattner196850c2003-05-15 21:30:00 +00001808 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001809 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00001810 $$ = S;
1811 }
Chris Lattner64116f92004-07-14 01:33:11 +00001812 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1813 UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001814 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001815 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001816
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001817 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1818 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001819 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001820 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001821 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001822 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1823 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001824 ParamTypes.push_back((*I)->getType());
1825 }
1826
1827 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1828 if (isVarArg) ParamTypes.pop_back();
1829
Chris Lattner79df7c02002-03-26 18:01:55 +00001830 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001831 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001832 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001833
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001834 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001835
Chris Lattner64116f92004-07-14 01:33:11 +00001836 BasicBlock *Normal = getBBVal($9);
1837 BasicBlock *Except = getBBVal($12);
Chris Lattner2079fde2001-10-13 06:41:08 +00001838
1839 // Create the call node...
1840 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001841 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001842 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001843 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001844 // correctly!
1845 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001846 FunctionType::param_iterator I = Ty->param_begin();
1847 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001848 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001849
1850 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001851 if ((*ArgI)->getType() != *I)
1852 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1853 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001854
1855 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001856 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001857
Chris Lattner6cdb0112001-11-26 16:54:11 +00001858 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001859 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001860 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001861 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001862 }
1863 | UNWIND {
1864 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00001865 }
1866 | UNREACHABLE {
1867 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001868 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001869
1870
Chris Lattner00950542001-06-06 20:29:01 +00001871
1872JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1873 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001874 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001875 if (V == 0)
1876 ThrowException("May only switch on a constant pool value!");
1877
Chris Lattner64116f92004-07-14 01:33:11 +00001878 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001879 }
1880 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001881 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001882 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001883
1884 if (V == 0)
1885 ThrowException("May only switch on a constant pool value!");
1886
Chris Lattner64116f92004-07-14 01:33:11 +00001887 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001888 };
Chris Lattner00950542001-06-06 20:29:01 +00001889
1890Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001891 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001892 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001893 InsertValue($2);
1894 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001895};
Chris Lattner00950542001-06-06 20:29:01 +00001896
Chris Lattnerc24d2082001-06-11 15:04:20 +00001897PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001898 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001899 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001900 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001901 }
1902 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1903 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001904 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001905 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001906 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001907
1908
Chris Lattner30c89792001-09-07 16:35:17 +00001909ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001910 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001911 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001912 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001913 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001914 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001915 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001916 };
Chris Lattner00950542001-06-06 20:29:01 +00001917
1918// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001919ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001920
Chris Lattner4a6482b2002-09-10 19:57:26 +00001921InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00001922 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
1923 !isa<PackedType>((*$2).get()))
1924 ThrowException(
1925 "Arithmetic operator requires integer, FP, or packed operands!");
1926 if(isa<PackedType>((*$2).get()) && $1 == Instruction::Rem) {
1927 ThrowException(
1928 "Rem not supported on packed types!");
1929 }
Chris Lattner4a6482b2002-09-10 19:57:26 +00001930 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1931 if ($$ == 0)
1932 ThrowException("binary operator returned null!");
1933 delete $2;
1934 }
1935 | LogicalOps Types ValueRef ',' ValueRef {
1936 if (!(*$2)->isIntegral())
1937 ThrowException("Logical operator requires integral operands!");
1938 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1939 if ($$ == 0)
1940 ThrowException("binary operator returned null!");
1941 delete $2;
1942 }
1943 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00001944 if(isa<PackedType>((*$2).get())) {
1945 ThrowException(
1946 "PackedTypes currently not supported in setcc instructions!");
1947 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00001948 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001949 if ($$ == 0)
1950 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001951 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001952 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001953 | NOT ResolvedVal {
1954 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1955 << " Replacing with 'xor'.\n";
1956
1957 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1958 if (Ones == 0)
1959 ThrowException("Expected integral type for not instruction!");
1960
1961 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001962 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001963 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001964 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001965 | ShiftOps ResolvedVal ',' ResolvedVal {
1966 if ($4->getType() != Type::UByteTy)
1967 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001968 if (!$2->getType()->isInteger())
1969 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001970 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001971 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001972 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001973 if (!$4->get()->isFirstClassType())
1974 ThrowException("cast instruction to a non-primitive type: '" +
1975 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001976 $$ = new CastInst($2, *$4);
1977 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001978 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001979 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1980 if ($2->getType() != Type::BoolTy)
1981 ThrowException("select condition must be boolean!");
1982 if ($4->getType() != $6->getType())
1983 ThrowException("select value types should match!");
1984 $$ = new SelectInst($2, $4, $6);
1985 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001986 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001987 // FIXME: This is emulation code for an obsolete syntax. This should be
1988 // removed at some point.
1989 if (!ObsoleteVarArgs) {
1990 std::cerr << "WARNING: this file uses obsolete features. "
1991 << "Assemble and disassemble to update it.\n";
1992 ObsoleteVarArgs = true;
1993 }
1994
1995 // First, load the valist...
1996 Instruction *CurVAList = new LoadInst($2, "");
1997 CurBB->getInstList().push_back(CurVAList);
1998
1999 // Emit the vaarg instruction.
2000 $$ = new VAArgInst(CurVAList, *$4);
2001
2002 // Now we must advance the pointer and update it in memory.
2003 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
2004 CurBB->getInstList().push_back(TheVANext);
2005
2006 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
2007 delete $4;
2008 }
2009 | VAARG ResolvedVal ',' Types {
2010 $$ = new VAArgInst($2, *$4);
2011 delete $4;
2012 }
2013 | VANEXT ResolvedVal ',' Types {
2014 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002015 delete $4;
2016 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002017 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002018 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002019 if (!Ty->isFirstClassType())
2020 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002021 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002022 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002023 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002024 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00002025 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002026 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002027 $2->pop_front();
2028 }
2029 delete $2; // Free the list...
2030 }
Chris Lattner93750fa2001-07-28 17:48:55 +00002031 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002032 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002033 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002034
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002035 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
2036 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002037 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002038 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00002039 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00002040 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
2041 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002042 ParamTypes.push_back((*I)->getType());
2043 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002044
2045 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2046 if (isVarArg) ParamTypes.pop_back();
2047
Chris Lattner595f06c2005-02-01 01:47:42 +00002048 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
2049 ThrowException("LLVM functions cannot return aggregate types!");
2050
Chris Lattner79df7c02002-03-26 18:01:55 +00002051 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002052 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002053 }
Chris Lattner00950542001-06-06 20:29:01 +00002054
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002055 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002056
Chris Lattner8b81bf52001-07-25 22:47:46 +00002057 // Create the call node...
2058 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002059 // Make sure no arguments is a good thing!
2060 if (Ty->getNumParams() != 0)
2061 ThrowException("No arguments passed to a function that "
2062 "expects arguments!");
2063
Chris Lattner9232b992003-04-22 18:42:41 +00002064 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002065 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002066 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002067 // correctly!
2068 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002069 FunctionType::param_iterator I = Ty->param_begin();
2070 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00002071 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002072
2073 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002074 if ((*ArgI)->getType() != *I)
2075 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2076 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002077
Chris Lattner8b81bf52001-07-25 22:47:46 +00002078 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002079 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002080
Chris Lattner6cdb0112001-11-26 16:54:11 +00002081 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002082 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00002083 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002084 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00002085 }
2086 | MemoryInst {
2087 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002088 };
Chris Lattner00950542001-06-06 20:29:01 +00002089
Chris Lattner6cdb0112001-11-26 16:54:11 +00002090
2091// IndexList - List of indices for GEP based instructions...
2092IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002093 $$ = $2;
2094 } | /* empty */ {
2095 $$ = new std::vector<Value*>();
2096 };
2097
2098OptVolatile : VOLATILE {
2099 $$ = true;
2100 }
2101 | /* empty */ {
2102 $$ = false;
2103 };
2104
Chris Lattner027dcc52001-07-08 21:10:27 +00002105
Chris Lattner00950542001-06-06 20:29:01 +00002106MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002107 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002108 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002109 }
2110 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002111 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002112 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002113 }
2114 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002115 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002116 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002117 }
2118 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002119 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002120 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002121 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002122 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002123 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002124 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002125 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002126 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002127 }
2128
Chris Lattner15c9c032003-09-08 18:20:29 +00002129 | OptVolatile LOAD Types ValueRef {
2130 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002131 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002132 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002133 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2134 ThrowException("Can't load from pointer of non-first-class type: " +
2135 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002136 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002137 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002138 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002139 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2140 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002141 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002142 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002143 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002144 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002145 if (ElTy != $3->getType())
2146 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002147 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002148
Chris Lattner79ad13802003-09-08 20:29:46 +00002149 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002150 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002151 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002152 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002153 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002154 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002155
2156 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2157 // indices to uint struct indices for compatibility.
2158 generic_gep_type_iterator<std::vector<Value*>::iterator>
2159 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2160 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2161 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2162 if (isa<StructType>(*GTI)) // Only change struct indices
2163 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2164 if (CUI->getType() == Type::UByteTy)
2165 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2166
Chris Lattner30c89792001-09-07 16:35:17 +00002167 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002168 ThrowException("Invalid getelementptr indices for type '" +
2169 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002170 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2171 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002172 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002173
Brian Gaeked0fde302003-11-11 22:41:34 +00002174
Chris Lattner00950542001-06-06 20:29:01 +00002175%%
Chris Lattner09083092001-07-08 04:57:15 +00002176int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002177 std::string where
2178 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002179 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002180 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002181 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002182 errMsg += "end-of-file.";
2183 else
Chris Lattner9232b992003-04-22 18:42:41 +00002184 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002185 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002186 return 0;
2187}