blob: 2b04412bd5f3866902454a22f1890ed6e7d3d9f9 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000017#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/SymbolTable.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/STLExtras.h"
Chris Lattner87ac9722005-11-06 06:46:28 +000022#include "llvm/Support/MathExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000023#include <algorithm>
24#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000025#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000026#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner386a3b72001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000030int yyparse();
31
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000033 std::string CurFilename;
34}
35using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattner00950542001-06-06 20:29:01 +000037static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000038
Chris Lattner30c89792001-09-07 16:35:17 +000039// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
40// relating to upreferences in the input stream.
41//
42//#define DEBUG_UPREFS 1
43#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000044#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000045#else
46#define UR_OUT(X)
47#endif
48
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000049#define YYERROR_VERBOSE 1
50
Andrew Lenharth558bc882005-06-18 18:34:52 +000051static bool ObsoleteVarArgs;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +000052static bool NewVarArgs;
Chris Lattnerb2b96672005-11-12 18:21:21 +000053static BasicBlock *CurBB;
54static GlobalVariable *CurGV;
Andrew Lenharth558bc882005-06-18 18:34:52 +000055
56
Chris Lattner7e708292002-06-25 16:13:24 +000057// This contains info used when building the body of a function. It is
58// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000059//
Chris Lattner9232b992003-04-22 18:42:41 +000060typedef std::vector<Value *> ValueList; // Numbered defs
Misha Brukman5a2a3822005-05-10 22:02:28 +000061static void
62ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
63 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000064
65static struct PerModuleInfo {
66 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000067 std::map<const Type *, ValueList> Values; // Module level numbered definitions
68 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000069 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000070 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000071
Chris Lattnerbc721ed2004-07-13 08:28:21 +000072 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
73 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000074 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000075 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
76
Chris Lattner2079fde2001-10-13 06:41:08 +000077 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
78 // references to global values. Global values may be referenced before they
79 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000080 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000081 //
Chris Lattner9232b992003-04-22 18:42:41 +000082 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000083 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000084 GlobalRefsType GlobalRefs;
85
Chris Lattner00950542001-06-06 20:29:01 +000086 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000087 // If we could not resolve some functions at function compilation time
88 // (calls to functions before they are defined), resolve them now... Types
89 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000090 //
Chris Lattner00950542001-06-06 20:29:01 +000091 ResolveDefinitions(LateResolveValues);
92
Chris Lattner2079fde2001-10-13 06:41:08 +000093 // Check to make sure that all global value forward references have been
94 // resolved!
95 //
96 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000097 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +000098
Chris Lattner749ce032002-03-11 22:12:39 +000099 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
100 I != E; ++I) {
101 UndefinedReferences += " " + I->first.first->getDescription() + " " +
102 I->first.second.getName() + "\n";
103 }
104 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000105 }
106
Chris Lattner7e708292002-06-25 16:13:24 +0000107 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000108 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000109 CurrentModule = 0;
110 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000111
112
Chris Lattner8a327842004-07-14 21:44:00 +0000113 // GetForwardRefForGlobal - Check to see if there is a forward reference
114 // for this global. If so, remove it from the GlobalRefs map and return it.
115 // If not, just return null.
116 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
117 // Check to see if there is a forward reference to this global variable...
118 // if there is, eliminate it and patch the reference to use the new def'n.
119 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
120 GlobalValue *Ret = 0;
121 if (I != GlobalRefs.end()) {
122 Ret = I->second;
123 GlobalRefs.erase(I);
124 }
125 return Ret;
126 }
Chris Lattner00950542001-06-06 20:29:01 +0000127} CurModule;
128
Chris Lattner79df7c02002-03-26 18:01:55 +0000129static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000130 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000131
Chris Lattner735f2702004-07-08 22:30:50 +0000132 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
133 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000134 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000135
Chris Lattner2e2ed292004-07-14 06:28:35 +0000136 /// BBForwardRefs - When we see forward references to basic blocks, keep
137 /// track of them here.
138 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
139 std::vector<BasicBlock*> NumberedBlocks;
140 unsigned NextBBNum;
141
Chris Lattner79df7c02002-03-26 18:01:55 +0000142 inline PerFunctionInfo() {
143 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000144 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000145 }
146
Chris Lattner79df7c02002-03-26 18:01:55 +0000147 inline void FunctionStart(Function *M) {
148 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000149 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000150 }
151
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000153 NumberedBlocks.clear();
154
155 // Any forward referenced blocks left?
156 if (!BBForwardRefs.empty())
157 ThrowException("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000158 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000159
160 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000161 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000162
Chris Lattner7e708292002-06-25 16:13:24 +0000163 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000164 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000165 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000166 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000167} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000168
Chris Lattner394f2eb2003-10-16 20:12:13 +0000169static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000170
Chris Lattner00950542001-06-06 20:29:01 +0000171
172//===----------------------------------------------------------------------===//
173// Code to handle definitions of all the types
174//===----------------------------------------------------------------------===//
175
Chris Lattner735f2702004-07-08 22:30:50 +0000176static int InsertValue(Value *V,
177 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
178 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000179
180 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000181 ValueList &List = ValueTab[V->getType()];
182 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000183 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000184}
185
Chris Lattner30c89792001-09-07 16:35:17 +0000186static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000187 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000188 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000189 // Module constants occupy the lowest numbered slots...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000190 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000191 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000192 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000193 case ValID::NameVal: // Is it a named definition?
194 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
195 D.destroy(); // Free old strdup'd memory...
196 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000197 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000198 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000199 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000200 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000201 }
202
203 // If we reached here, we referenced either a symbol that we don't know about
204 // or an id number that hasn't been read yet. We may be referencing something
205 // forward, so just create an entry to be resolved later and get to it...
206 //
207 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
208
Chris Lattner355ad1f2005-03-21 06:27:42 +0000209
210 if (inFunctionScope()) {
211 if (D.Type == ValID::NameVal)
212 ThrowException("Reference to an undefined type: '" + D.getName() + "'");
213 else
214 ThrowException("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000215 }
Chris Lattner30c89792001-09-07 16:35:17 +0000216
Chris Lattner355ad1f2005-03-21 06:27:42 +0000217 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
218 if (I != CurModule.LateResolveTypes.end())
219 return I->second;
220
Chris Lattner82269592001-10-22 06:01:08 +0000221 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000222 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000223 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000224 }
Chris Lattner30c89792001-09-07 16:35:17 +0000225
Chris Lattner9232b992003-04-22 18:42:41 +0000226static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000227 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000228 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000229 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000230 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000231}
232
Chris Lattner2079fde2001-10-13 06:41:08 +0000233// getValNonImprovising - Look up the value specified by the provided type and
234// the provided ValID. If the value exists and has already been defined, return
235// it. Otherwise return null.
236//
237static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000238 if (isa<FunctionType>(Ty))
239 ThrowException("Functions are not values and "
240 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000241
Chris Lattner30c89792001-09-07 16:35:17 +0000242 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000243 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000244 unsigned Num = (unsigned)D.Num;
245
246 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000247 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000248 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000249 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000250 return VI->second[Num];
251 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000252 }
253
254 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000255 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000256 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000257
258 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000259 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000260
Chris Lattner16af11d2004-02-09 21:03:38 +0000261 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000262 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000263
Chris Lattner1a1cb112001-09-30 22:46:54 +0000264 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000265 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000266 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000267
268 D.destroy(); // Free old strdup'd memory...
269 return N;
270 }
271
Misha Brukman5a2a3822005-05-10 22:02:28 +0000272 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000273 // value will fit into the specified type...
274 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000275 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
276 ThrowException("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000277 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000278 Ty->getDescription() + "'!");
279 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000280
281 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000282 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
283 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000284 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000285 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000286 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000287 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000288 }
289 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000290 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000291 }
292
Chris Lattner2079fde2001-10-13 06:41:08 +0000293 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000294 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000295 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000296 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000297
Chris Lattner2079fde2001-10-13 06:41:08 +0000298 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000299 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000300 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000301 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000302
Chris Lattner16710e92004-10-16 18:17:13 +0000303 case ValID::ConstUndefVal: // Is it an undef value?
304 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000305
Chris Lattnerf1f03df2005-12-21 17:53:02 +0000306 case ValID::ConstZeroVal: // Is it a zero value?
307 return Constant::getNullValue(Ty);
308
Chris Lattnerd78700d2002-08-16 21:14:40 +0000309 case ValID::ConstantVal: // Fully resolved constant?
310 if (D.ConstantValue->getType() != Ty)
311 ThrowException("Constant expression type different from required type!");
312 return D.ConstantValue;
313
Chris Lattner30c89792001-09-07 16:35:17 +0000314 default:
315 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000316 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000317 } // End of switch
318
Chris Lattner2079fde2001-10-13 06:41:08 +0000319 assert(0 && "Unhandled case!");
320 return 0;
321}
322
Chris Lattner2079fde2001-10-13 06:41:08 +0000323// getVal - This function is identical to getValNonImprovising, except that if a
324// value is not already defined, it "improvises" by creating a placeholder var
325// that looks and acts just like the requested variable. When the value is
326// defined later, all uses of the placeholder variable are replaced with the
327// real thing.
328//
Chris Lattner64116f92004-07-14 01:33:11 +0000329static Value *getVal(const Type *Ty, const ValID &ID) {
330 if (Ty == Type::LabelTy)
331 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000332
Chris Lattner64116f92004-07-14 01:33:11 +0000333 // See if the value has already been defined.
334 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000335 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000336
Chris Lattner60cd9552005-03-23 01:29:26 +0000337 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
338 ThrowException("Invalid use of a composite type!");
339
Chris Lattner00950542001-06-06 20:29:01 +0000340 // If we reached here, we referenced either a symbol that we don't know about
341 // or an id number that hasn't been read yet. We may be referencing something
342 // forward, so just create an entry to be resolved later and get to it...
343 //
Chris Lattner64116f92004-07-14 01:33:11 +0000344 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000345
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000346 // Remember where this forward reference came from. FIXME, shouldn't we try
347 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000348 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000349 llvmAsmlineno)));
350
Chris Lattner79df7c02002-03-26 18:01:55 +0000351 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000352 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000353 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000354 InsertValue(V, CurModule.LateResolveValues);
355 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000356}
357
Chris Lattner2e2ed292004-07-14 06:28:35 +0000358/// getBBVal - This is used for two purposes:
359/// * If isDefinition is true, a new basic block with the specified ID is being
360/// defined.
361/// * If isDefinition is true, this is a reference to a basic block, which may
362/// or may not be a forward reference.
363///
364static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000365 assert(inFunctionScope() && "Can't get basic block at global scope!");
366
Chris Lattner2e2ed292004-07-14 06:28:35 +0000367 std::string Name;
368 BasicBlock *BB = 0;
369 switch (ID.Type) {
370 default: ThrowException("Illegal label reference " + ID.getName());
371 case ValID::NumberVal: // Is it a numbered definition?
372 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
373 CurFun.NumberedBlocks.resize(ID.Num+1);
374 BB = CurFun.NumberedBlocks[ID.Num];
375 break;
376 case ValID::NameVal: // Is it a named definition?
377 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000378 if (Value *N = CurFun.CurrentFunction->
379 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000380 BB = cast<BasicBlock>(N);
381 break;
382 }
Chris Lattner64116f92004-07-14 01:33:11 +0000383
Chris Lattner2e2ed292004-07-14 06:28:35 +0000384 // See if the block has already been defined.
385 if (BB) {
386 // If this is the definition of the block, make sure the existing value was
387 // just a forward reference. If it was a forward reference, there will be
388 // an entry for it in the PlaceHolderInfo map.
389 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
390 // The existing value was a definition, not a forward reference.
391 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000392
Chris Lattner2e2ed292004-07-14 06:28:35 +0000393 ID.destroy(); // Free strdup'd memory.
394 return BB;
395 }
396
397 // Otherwise this block has not been seen before.
398 BB = new BasicBlock("", CurFun.CurrentFunction);
399 if (ID.Type == ValID::NameVal) {
400 BB->setName(ID.Name);
401 } else {
402 CurFun.NumberedBlocks[ID.Num] = BB;
403 }
404
405 // If this is not a definition, keep track of it so we can use it as a forward
406 // reference.
407 if (!isDefinition) {
408 // Remember where this forward reference came from.
409 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
410 } else {
411 // The forward declaration could have been inserted anywhere in the
412 // function: insert it into the correct place now.
413 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
414 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
415 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000416 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000417 return BB;
418}
419
Chris Lattner00950542001-06-06 20:29:01 +0000420
421//===----------------------------------------------------------------------===//
422// Code to handle forward references in instructions
423//===----------------------------------------------------------------------===//
424//
425// This code handles the late binding needed with statements that reference
426// values not defined yet... for example, a forward branch, or the PHI node for
427// a loop body.
428//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000429// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000430// and back patchs after we are done.
431//
432
Misha Brukman5a2a3822005-05-10 22:02:28 +0000433// ResolveDefinitions - If we could not resolve some defs at parsing
434// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000435// defs now...
436//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000437static void
438ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
439 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000440 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000441 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000442 E = LateResolvers.end(); LRI != E; ++LRI) {
443 ValueList &List = LRI->second;
444 while (!List.empty()) {
445 Value *V = List.back();
446 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000447
448 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
449 CurModule.PlaceHolderInfo.find(V);
450 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
451
452 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000453
Chris Lattner735f2702004-07-08 22:30:50 +0000454 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000455 if (TheRealValue) {
456 V->replaceAllUsesWith(TheRealValue);
457 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000458 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000459 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000460 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000461 // resolver table
462 InsertValue(V, *FutureLateResolvers);
463 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000464 if (DID.Type == ValID::NameVal)
465 ThrowException("Reference to an invalid definition: '" +DID.getName()+
466 "' of type '" + V->getType()->getDescription() + "'",
467 PHI->second.second);
468 else
469 ThrowException("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000470 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000471 V->getType()->getDescription() + "'",
472 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000473 }
Chris Lattner00950542001-06-06 20:29:01 +0000474 }
475 }
476
477 LateResolvers.clear();
478}
479
Chris Lattner4a42e902001-10-22 05:56:09 +0000480// ResolveTypeTo - A brand new type was just declared. This means that (if
481// name is not null) things referencing Name can be resolved. Otherwise, things
482// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000483//
Chris Lattner4a42e902001-10-22 05:56:09 +0000484static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000485 ValID D;
486 if (Name) D = ValID::create(Name);
487 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000488
Chris Lattner355ad1f2005-03-21 06:27:42 +0000489 std::map<ValID, PATypeHolder>::iterator I =
490 CurModule.LateResolveTypes.find(D);
491 if (I != CurModule.LateResolveTypes.end()) {
492 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
493 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000494 }
495}
496
Chris Lattner1781aca2001-09-18 04:00:54 +0000497// setValueName - Set the specified value to the name given. The name may be
498// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000499// assumed to be a malloc'd string buffer, and is free'd by this function.
500//
501static void setValueName(Value *V, char *NameStr) {
502 if (NameStr) {
503 std::string Name(NameStr); // Copy string
504 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000505
Misha Brukman5a2a3822005-05-10 22:02:28 +0000506 if (V->getType() == Type::VoidTy)
Chris Lattnerc9aea522004-07-13 08:12:39 +0000507 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000508
Chris Lattner8ab406d2004-07-13 07:59:27 +0000509 assert(inFunctionScope() && "Must be in function scope!");
510 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
511 if (ST.lookup(V->getType(), Name))
512 ThrowException("Redefinition of value named '" + Name + "' in the '" +
513 V->getType()->getDescription() + "' type plane!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000514
Chris Lattnerc9aea522004-07-13 08:12:39 +0000515 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000516 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000517 }
518}
519
Chris Lattnerd88998d2004-07-14 08:23:52 +0000520/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
521/// this is a declaration, otherwise it is a definition.
Chris Lattnerb2b96672005-11-12 18:21:21 +0000522static GlobalVariable *
523ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
524 bool isConstantGlobal, const Type *Ty,
525 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000526 if (isa<FunctionType>(Ty))
527 ThrowException("Cannot declare global vars of function type!");
528
Misha Brukman5a2a3822005-05-10 22:02:28 +0000529 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000530
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000531 std::string Name;
532 if (NameStr) {
533 Name = NameStr; // Copy string
534 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000535 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000536
Chris Lattner8a327842004-07-14 21:44:00 +0000537 // See if this global value was forward referenced. If so, recycle the
538 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000539 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000540 if (!Name.empty()) {
541 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000542 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000543 ID = ValID::create((int)CurModule.Values[PTy].size());
544 }
545
546 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000547 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000548 // previously inserted.
549 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
550 CurModule.CurrentModule->getGlobalList().remove(GV);
551 CurModule.CurrentModule->getGlobalList().push_back(GV);
552 GV->setInitializer(Initializer);
553 GV->setLinkage(Linkage);
554 GV->setConstant(isConstantGlobal);
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000555 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000556 return GV;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000557 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000558
559 // If this global has a name, check to see if there is already a definition
560 // of this global in the module. If so, merge as appropriate. Note that
561 // this is really just a hack around problems in the CFE. :(
562 if (!Name.empty()) {
563 // We are a simple redefinition of a value, check to see if it is defined
564 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000565 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000566 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
567 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000568 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000569 // 2. If both initializers have the same value.
570 //
571 if (!EGV->hasInitializer() || !Initializer ||
572 EGV->getInitializer() == Initializer) {
573
574 // Make sure the existing global version gets the initializer! Make
575 // sure that it also gets marked const if the new version is.
576 if (Initializer && !EGV->hasInitializer())
577 EGV->setInitializer(Initializer);
578 if (isConstantGlobal)
579 EGV->setConstant(true);
580 EGV->setLinkage(Linkage);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000581 return EGV;
Chris Lattnera09000d2004-07-14 23:03:46 +0000582 }
583
Misha Brukman5a2a3822005-05-10 22:02:28 +0000584 ThrowException("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000585 "' in the '" + Ty->getDescription() + "' type plane!");
586 }
587 }
588
589 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000590 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000591 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000592 CurModule.CurrentModule);
593 InsertValue(GV, CurModule.Values);
Chris Lattnerb2b96672005-11-12 18:21:21 +0000594 return GV;
Chris Lattnerd88998d2004-07-14 08:23:52 +0000595}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000596
Reid Spencer75719bf2004-05-26 21:48:31 +0000597// setTypeName - Set the specified type to the name given. The name may be
598// null potentially, in which case this is a noop. The string passed in is
599// assumed to be a malloc'd string buffer, and is freed by this function.
600//
601// This function returns true if the type has already been defined, but is
602// allowed to be redefined in the specified context. If the name is a new name
603// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000604static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000605 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000606 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000607
Reid Spencer75719bf2004-05-26 21:48:31 +0000608 std::string Name(NameStr); // Copy string
609 free(NameStr); // Free old string
610
611 // We don't allow assigning names to void type
Misha Brukman5a2a3822005-05-10 22:02:28 +0000612 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000613 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000614
Chris Lattnera09000d2004-07-14 23:03:46 +0000615 // Set the type name, checking for conflicts as we do so.
616 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000617
Chris Lattnera09000d2004-07-14 23:03:46 +0000618 if (AlreadyExists) { // Inserting a name that is already defined???
619 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
620 assert(Existing && "Conflict but no matching type?");
621
Reid Spencer75719bf2004-05-26 21:48:31 +0000622 // There is only one case where this is allowed: when we are refining an
623 // opaque type. In this case, Existing will be an opaque type.
624 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
625 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000626 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000627 return true;
628 }
629
630 // Otherwise, this is an attempt to redefine a type. That's okay if
631 // the redefinition is identical to the original. This will be so if
632 // Existing and T point to the same Type object. In this one case we
633 // allow the equivalent redefinition.
634 if (Existing == T) return true; // Yes, it's equal.
635
636 // Any other kind of (non-equivalent) redefinition is an error.
637 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000638 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000639 }
640
Reid Spencer75719bf2004-05-26 21:48:31 +0000641 return false;
642}
Chris Lattner8896eda2001-07-09 19:38:36 +0000643
Chris Lattner30c89792001-09-07 16:35:17 +0000644//===----------------------------------------------------------------------===//
645// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000646//
Chris Lattner8896eda2001-07-09 19:38:36 +0000647
Chris Lattner3b073862004-02-09 03:19:29 +0000648// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000649//
650static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000651 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000652 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000653}
654
655namespace {
656 struct UpRefRecord {
657 // NestingLevel - The number of nesting levels that need to be popped before
658 // this type is resolved.
659 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000660
Chris Lattner3b073862004-02-09 03:19:29 +0000661 // LastContainedTy - This is the type at the current binding level for the
662 // type. Every time we reduce the nesting level, this gets updated.
663 const Type *LastContainedTy;
664
665 // UpRefTy - This is the actual opaque type that the upreference is
666 // represented with.
667 OpaqueType *UpRefTy;
668
669 UpRefRecord(unsigned NL, OpaqueType *URTy)
670 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
671 };
Chris Lattner30c89792001-09-07 16:35:17 +0000672}
Chris Lattner698b56e2001-07-20 19:15:08 +0000673
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000674// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000675static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000676
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000677/// HandleUpRefs - Every time we finish a new layer of types, this function is
678/// called. It loops through the UpRefs vector, which is a list of the
679/// currently active types. For each type, if the up reference is contained in
680/// the newly completed type, we decrement the level count. When the level
681/// count reaches zero, the upreferenced type is the type that is passed in:
682/// thus we can complete the cycle.
683///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000684static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000685 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000686 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000687 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000688 "' newly formed. Resolving upreferences.\n" <<
689 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000690
691 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
692 // to zero), we resolve them all together before we resolve them to Ty. At
693 // the end of the loop, if there is anything to resolve to Ty, it will be in
694 // this variable.
695 OpaqueType *TypeToResolve = 0;
696
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000697 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000698 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
699 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000700 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000701 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
702 // Decrement level of upreference
703 unsigned Level = --UpRefs[i].NestingLevel;
704 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000705 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000706 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000707 if (!TypeToResolve) {
708 TypeToResolve = UpRefs[i].UpRefTy;
709 } else {
710 UR_OUT(" * Resolving upreference for "
711 << UpRefs[i].second->getDescription() << "\n";
712 std::string OldName = UpRefs[i].UpRefTy->getDescription());
713 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
714 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
715 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
716 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000717 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000718 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000719 }
720 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000721 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000722
Chris Lattner026a8ce2004-02-09 18:53:54 +0000723 if (TypeToResolve) {
724 UR_OUT(" * Resolving upreference for "
725 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000726 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000727 TypeToResolve->refineAbstractTypeTo(Ty);
728 }
729
Chris Lattner8896eda2001-07-09 19:38:36 +0000730 return Ty;
731}
732
Chris Lattner30c89792001-09-07 16:35:17 +0000733
Chris Lattner6184feb2005-05-20 03:25:47 +0000734// common code from the two 'RunVMAsmParser' functions
735 static Module * RunParser(Module * M) {
736
737 llvmAsmlineno = 1; // Reset the current line number...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000738 ObsoleteVarArgs = false;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000739 NewVarArgs = false;
Chris Lattner6184feb2005-05-20 03:25:47 +0000740
741 CurModule.CurrentModule = M;
742 yyparse(); // Parse the file, potentially throwing exception
743
744 Module *Result = ParserResult;
745 ParserResult = 0;
746
Andrew Lenharth31c98bf2005-06-20 15:41:37 +0000747 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
748 {
749 Function* F;
750 if ((F = Result->getNamedFunction("llvm.va_start"))
751 && F->getFunctionType()->getNumParams() == 0)
752 ObsoleteVarArgs = true;
753 if((F = Result->getNamedFunction("llvm.va_copy"))
754 && F->getFunctionType()->getNumParams() == 1)
755 ObsoleteVarArgs = true;
756 }
757
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000758 if (ObsoleteVarArgs && NewVarArgs)
Chris Lattner548021f2005-06-24 18:00:40 +0000759 ThrowException("This file is corrupt: it uses both new and old style varargs");
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000760
Andrew Lenharth558bc882005-06-18 18:34:52 +0000761 if(ObsoleteVarArgs) {
762 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000763 if (F->arg_size() != 0)
764 ThrowException("Obsolete va_start takes 0 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000765
766 //foo = va_start()
767 // ->
768 //bar = alloca typeof(foo)
769 //va_start(bar)
770 //foo = load bar
771
772 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
773 const Type* ArgTy = F->getFunctionType()->getReturnType();
774 const Type* ArgTyPtr = PointerType::get(ArgTy);
775 Function* NF = Result->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000776 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000777
778 while (!F->use_empty()) {
779 CallInst* CI = cast<CallInst>(F->use_back());
780 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
781 new CallInst(NF, bar, "", CI);
782 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
783 CI->replaceAllUsesWith(foo);
784 CI->getParent()->getInstList().erase(CI);
785 }
786 Result->getFunctionList().erase(F);
787 }
788
789 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000790 if(F->arg_size() != 1)
791 ThrowException("Obsolete va_end takes 1 argument!");
792
Andrew Lenharth558bc882005-06-18 18:34:52 +0000793 //vaend foo
794 // ->
795 //bar = alloca 1 of typeof(foo)
796 //vaend bar
797 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
798 const Type* ArgTy = F->getFunctionType()->getParamType(0);
799 const Type* ArgTyPtr = PointerType::get(ArgTy);
800 Function* NF = Result->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000801 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000802
803 while (!F->use_empty()) {
804 CallInst* CI = cast<CallInst>(F->use_back());
805 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000806 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000807 new CallInst(NF, bar, "", CI);
808 CI->getParent()->getInstList().erase(CI);
809 }
810 Result->getFunctionList().erase(F);
811 }
812
813 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000814 if(F->arg_size() != 1)
815 ThrowException("Obsolete va_copy takes 1 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000816 //foo = vacopy(bar)
817 // ->
818 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000819 //b = alloca 1 of typeof(foo)
820 //store bar -> b
821 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000822 //foo = load a
823
824 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
825 const Type* ArgTy = F->getFunctionType()->getReturnType();
826 const Type* ArgTyPtr = PointerType::get(ArgTy);
827 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000828 RetTy, ArgTyPtr, ArgTyPtr,
829 (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000830
831 while (!F->use_empty()) {
832 CallInst* CI = cast<CallInst>(F->use_back());
833 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000834 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
835 new StoreInst(CI->getOperand(1), b, CI);
836 new CallInst(NF, a, b, "", CI);
837 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000838 CI->replaceAllUsesWith(foo);
839 CI->getParent()->getInstList().erase(CI);
840 }
841 Result->getFunctionList().erase(F);
842 }
843 }
844
Chris Lattner6184feb2005-05-20 03:25:47 +0000845 return Result;
846
847 }
848
Chris Lattner00950542001-06-06 20:29:01 +0000849//===----------------------------------------------------------------------===//
850// RunVMAsmParser - Define an interface to this parser
851//===----------------------------------------------------------------------===//
852//
Chris Lattner86427632004-07-14 06:39:48 +0000853Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000854 set_scan_file(F);
855
Chris Lattnera2850432001-07-22 18:36:00 +0000856 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000857 return RunParser(new Module(CurFilename));
858}
Chris Lattner00950542001-06-06 20:29:01 +0000859
Chris Lattner6184feb2005-05-20 03:25:47 +0000860Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
861 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000862
Chris Lattner6184feb2005-05-20 03:25:47 +0000863 CurFilename = "from_memory";
864 if (M == NULL) {
865 return RunParser(new Module (CurFilename));
866 } else {
867 return RunParser(M);
868 }
Chris Lattner00950542001-06-06 20:29:01 +0000869}
870
871%}
872
873%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000874 llvm::Module *ModuleVal;
875 llvm::Function *FunctionVal;
876 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
877 llvm::BasicBlock *BasicBlockVal;
878 llvm::TerminatorInst *TermInstVal;
879 llvm::Instruction *InstVal;
880 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000881
Brian Gaeked0fde302003-11-11 22:41:34 +0000882 const llvm::Type *PrimType;
883 llvm::PATypeHolder *TypeVal;
884 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000885
Brian Gaeked0fde302003-11-11 22:41:34 +0000886 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
887 std::vector<llvm::Value*> *ValueList;
888 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000889 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000890 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000891 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000892 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
893 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000894
Brian Gaeked0fde302003-11-11 22:41:34 +0000895 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000896 int64_t SInt64Val;
897 uint64_t UInt64Val;
898 int SIntVal;
899 unsigned UIntVal;
900 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000901 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000902
Chris Lattner30c89792001-09-07 16:35:17 +0000903 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000904 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000905
Brian Gaeked0fde302003-11-11 22:41:34 +0000906 llvm::Instruction::BinaryOps BinaryOpVal;
907 llvm::Instruction::TermOps TermOpVal;
908 llvm::Instruction::MemoryOps MemOpVal;
909 llvm::Instruction::OtherOps OtherOpVal;
910 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000911}
912
Chris Lattner79df7c02002-03-26 18:01:55 +0000913%type <ModuleVal> Module FunctionList
914%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000915%type <BasicBlockVal> BasicBlock InstructionList
916%type <TermInstVal> BBTerminatorInst
917%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000918%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000919%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000920%type <ArgList> ArgList ArgListH
921%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000922%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000923%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000924%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000925%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000926%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000927%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000928%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000929%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000930%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000931%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000932
Chris Lattner2079fde2001-10-13 06:41:08 +0000933// ValueRef - Unresolved reference to a definition or BB
934%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000935%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000936// Tokens and types for handling constant integer values
937//
938// ESINT64VAL - A negative number within long long range
939%token <SInt64Val> ESINT64VAL
940
941// EUINT64VAL - A positive number within uns. long long range
942%token <UInt64Val> EUINT64VAL
943%type <SInt64Val> EINT64VAL
944
945%token <SIntVal> SINTVAL // Signed 32 bit ints...
946%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
947%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000948%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000949
950// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000951%type <TypeVal> Types TypesV UpRTypes UpRTypesV
952%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000953%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
954%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000955
Chris Lattner6c23f572003-08-22 05:42:10 +0000956%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
957%type <StrVal> Name OptName OptAssign
Chris Lattner87ac9722005-11-06 06:46:28 +0000958%type <UIntVal> OptAlign OptCAlign
Chris Lattnerb2b96672005-11-12 18:21:21 +0000959%type <StrVal> OptSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +0000960
Chris Lattner91ef4602004-03-31 03:49:47 +0000961%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner164c3782005-11-12 00:11:10 +0000962%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000963%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Nate Begeman14b05292005-11-05 09:21:28 +0000964%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
Chris Lattnerddb6db42005-05-06 05:51:46 +0000965%token DEPLIBS CALL TAIL
Chris Lattnera8e8f162005-05-06 20:27:19 +0000966%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK
967%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +0000968
Misha Brukman5a2a3822005-05-10 22:02:28 +0000969// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000970%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000971
Misha Brukman5a2a3822005-05-10 22:02:28 +0000972// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000973%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000974%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000975%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000976
977// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000978%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000979
Chris Lattner027dcc52001-07-08 21:10:27 +0000980// Other Operators
981%type <OtherOpVal> ShiftOps
Robert Bocchino9c62b562006-01-10 19:04:32 +0000982%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG EXTRACTELEMENT
Andrew Lenharth558bc882005-06-18 18:34:52 +0000983%token VAARG_old VANEXT_old //OBSOLETE
Chris Lattnerddb6db42005-05-06 05:51:46 +0000984
Chris Lattner027dcc52001-07-08 21:10:27 +0000985
Chris Lattner00950542001-06-06 20:29:01 +0000986%start Module
987%%
988
989// Handle constant integer size restriction and conversion...
990//
Chris Lattner51727be2002-06-04 21:58:56 +0000991INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000992INTVAL : UINTVAL {
993 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
994 ThrowException("Value too large for type!");
995 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000996};
Chris Lattner00950542001-06-06 20:29:01 +0000997
998
Chris Lattner51727be2002-06-04 21:58:56 +0000999EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +00001000EINT64VAL : EUINT64VAL {
1001 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1002 ThrowException("Value too large for type!");
1003 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +00001004};
Chris Lattner00950542001-06-06 20:29:01 +00001005
Misha Brukman5a2a3822005-05-10 22:02:28 +00001006// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001007// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1008//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001009ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1010LogicalOps : AND | OR | XOR;
1011SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +00001012
Chris Lattner51727be2002-06-04 21:58:56 +00001013ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001014
Chris Lattnere98dda62001-07-14 06:10:16 +00001015// These are some types that allow classification if we only want a particular
1016// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001017SIntType : LONG | INT | SHORT | SBYTE;
1018UIntType : ULONG | UINT | USHORT | UBYTE;
1019IntType : SIntType | UIntType;
1020FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001021
Chris Lattnere98dda62001-07-14 06:10:16 +00001022// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001023OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001024 $$ = $1;
1025 }
Misha Brukman5a2a3822005-05-10 22:02:28 +00001026 | /*empty*/ {
1027 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001028 };
Chris Lattner00950542001-06-06 20:29:01 +00001029
Chris Lattner4ad02e72003-04-16 20:28:45 +00001030OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1031 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001032 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001033 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1034 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001035
Chris Lattnera8e8f162005-05-06 20:27:19 +00001036OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1037 CCC_TOK { $$ = CallingConv::C; } |
1038 FASTCC_TOK { $$ = CallingConv::Fast; } |
1039 COLDCC_TOK { $$ = CallingConv::Cold; } |
1040 CC_TOK EUINT64VAL {
1041 if ((unsigned)$2 != $2)
1042 ThrowException("Calling conv too large!");
1043 $$ = $2;
1044 };
1045
Chris Lattner66db8e42005-11-06 06:34:12 +00001046// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1047// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001048OptAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001049 ALIGN EUINT64VAL {
1050 $$ = $2;
1051 if ($$ != 0 && !isPowerOf2_32($$))
1052 ThrowException("Alignment must be a power of two!");
1053};
Chris Lattner66db8e42005-11-06 06:34:12 +00001054OptCAlign : /*empty*/ { $$ = 0; } |
Chris Lattnerb2b96672005-11-12 18:21:21 +00001055 ',' ALIGN EUINT64VAL {
1056 $$ = $3;
1057 if ($$ != 0 && !isPowerOf2_32($$))
1058 ThrowException("Alignment must be a power of two!");
1059};
1060
Chris Lattner66db8e42005-11-06 06:34:12 +00001061
Chris Lattner164c3782005-11-12 00:11:10 +00001062SectionString : SECTION STRINGCONSTANT {
1063 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1064 if ($2[i] == '"' || $2[i] == '\\')
1065 ThrowException("Invalid character in section name!");
1066 $$ = $2;
1067};
1068
1069OptSection : /*empty*/ { $$ = 0; } |
1070 SectionString { $$ = $1; };
Chris Lattner164c3782005-11-12 00:11:10 +00001071
Chris Lattnerb2b96672005-11-12 18:21:21 +00001072// GlobalVarAttributes - Used to pass the attributes string on a global. CurGV
1073// is set to be the global we are processing.
1074//
1075GlobalVarAttributes : /* empty */ {} |
1076 ',' GlobalVarAttribute GlobalVarAttributes {};
1077GlobalVarAttribute : SectionString {
1078 CurGV->setSection($1);
1079 free($1);
1080 }
1081 | ALIGN EUINT64VAL {
1082 if ($2 != 0 && !isPowerOf2_32($2))
1083 ThrowException("Alignment must be a power of two!");
1084 CurGV->setAlignment($2);
1085 };
Chris Lattner164c3782005-11-12 00:11:10 +00001086
Chris Lattner30c89792001-09-07 16:35:17 +00001087//===----------------------------------------------------------------------===//
1088// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001089// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001090// access to it, a user must explicitly use TypesV.
1091//
1092
1093// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001094TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1095UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001096
1097Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001098 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001099 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1100 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001101 };
Chris Lattner30c89792001-09-07 16:35:17 +00001102
1103
1104// Derived types are added later...
1105//
Chris Lattner51727be2002-06-04 21:58:56 +00001106PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1107PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001108UpRTypes : OPAQUE {
1109 $$ = new PATypeHolder(OpaqueType::get());
1110 }
1111 | PrimType {
1112 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001113 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001114UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001115 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001116};
Chris Lattner30c89792001-09-07 16:35:17 +00001117
Chris Lattner30c89792001-09-07 16:35:17 +00001118// Include derived types in the Types production.
1119//
1120UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001121 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001122 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001123 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001124 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001125 UR_OUT("New Upreference!\n");
1126 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001127 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001128 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001129 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1130 E = $3->end(); I != E; ++I)
1131 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001132 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1133 if (isVarArg) Params.pop_back();
1134
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001135 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001136 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001137 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001138 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001139 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001140 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001141 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001142 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001143 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1144 const llvm::Type* ElemTy = $4->get();
Chris Lattner9547d7f2005-11-10 01:42:43 +00001145 if ((unsigned)$2 != $2)
Brian Gaeke715c90b2004-08-20 06:00:58 +00001146 ThrowException("Unsigned result not equal to signed result");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001147 if (!ElemTy->isPrimitiveType())
Brian Gaeke715c90b2004-08-20 06:00:58 +00001148 ThrowException("Elemental type of a PackedType must be primitive");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001149 if (!isPowerOf2_32($2))
1150 ThrowException("Vector length should be a power of 2!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001151 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1152 delete $4;
1153 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001154 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001155 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001156 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1157 E = $2->end(); I != E; ++I)
1158 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001159
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001160 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001161 delete $2;
1162 }
1163 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001164 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001165 }
1166 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001167 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001168 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001169 };
Chris Lattner30c89792001-09-07 16:35:17 +00001170
Chris Lattner7e708292002-06-25 16:13:24 +00001171// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001172// declaration type lists
1173//
1174TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001175 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001176 $$->push_back(*$1); delete $1;
1177 }
1178 | TypeListI ',' UpRTypes {
1179 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001180 };
Chris Lattner30c89792001-09-07 16:35:17 +00001181
Chris Lattner7e708292002-06-25 16:13:24 +00001182// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001183ArgTypeListI : TypeListI
1184 | TypeListI ',' DOTDOTDOT {
1185 ($$=$1)->push_back(Type::VoidTy);
1186 }
1187 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001188 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001189 }
1190 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001191 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001192 };
Chris Lattner30c89792001-09-07 16:35:17 +00001193
Chris Lattnere98dda62001-07-14 06:10:16 +00001194// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001195// production is used ONLY to represent constants that show up AFTER a 'const',
1196// 'constant' or 'global' token at global scope. Constants that can be inlined
1197// into other expressions (such as integers and constexprs) are handled by the
1198// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001199//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001200ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001201 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001202 if (ATy == 0)
1203 ThrowException("Cannot make array constant with type: '" +
1204 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001205 const Type *ETy = ATy->getElementType();
1206 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001207
Chris Lattner30c89792001-09-07 16:35:17 +00001208 // Verify that we have the correct size...
1209 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001210 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001211 utostr($3->size()) + " arguments, but has size of " +
1212 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001213
Chris Lattner30c89792001-09-07 16:35:17 +00001214 // Verify all elements are correct type!
1215 for (unsigned i = 0; i < $3->size(); i++) {
1216 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001217 ThrowException("Element #" + utostr(i) + " is not of type '" +
1218 ETy->getDescription() +"' as required!\nIt is of type '"+
1219 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001220 }
1221
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001222 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001223 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001224 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001225 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001226 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001227 if (ATy == 0)
1228 ThrowException("Cannot make array constant with type: '" +
1229 (*$1)->getDescription() + "'!");
1230
1231 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001232 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001233 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001234 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001235 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001236 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001237 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001238 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001239 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001240 if (ATy == 0)
1241 ThrowException("Cannot make array constant with type: '" +
1242 (*$1)->getDescription() + "'!");
1243
Chris Lattner30c89792001-09-07 16:35:17 +00001244 int NumElements = ATy->getNumElements();
1245 const Type *ETy = ATy->getElementType();
1246 char *EndStr = UnEscapeLexed($3, true);
1247 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001248 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001249 itostr((int)(EndStr-$3)) +
1250 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001251 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001252 if (ETy == Type::SByteTy) {
1253 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001254 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001255 } else if (ETy == Type::UByteTy) {
1256 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001257 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001258 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001259 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001260 ThrowException("Cannot build string arrays of non byte sized elements!");
1261 }
Chris Lattner30c89792001-09-07 16:35:17 +00001262 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001263 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001264 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001265 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001266 | Types '<' ConstVector '>' { // Nonempty unsized arr
1267 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1268 if (PTy == 0)
1269 ThrowException("Cannot make packed constant with type: '" +
1270 (*$1)->getDescription() + "'!");
1271 const Type *ETy = PTy->getElementType();
1272 int NumElements = PTy->getNumElements();
1273
1274 // Verify that we have the correct size...
1275 if (NumElements != -1 && NumElements != (int)$3->size())
1276 ThrowException("Type mismatch: constant sized packed initialized with " +
1277 utostr($3->size()) + " arguments, but has size of " +
1278 itostr(NumElements) + "!");
1279
1280 // Verify all elements are correct type!
1281 for (unsigned i = 0; i < $3->size(); i++) {
1282 if (ETy != (*$3)[i]->getType())
1283 ThrowException("Element #" + utostr(i) + " is not of type '" +
1284 ETy->getDescription() +"' as required!\nIt is of type '"+
1285 (*$3)[i]->getType()->getDescription() + "'.");
1286 }
1287
1288 $$ = ConstantPacked::get(PTy, *$3);
1289 delete $1; delete $3;
1290 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001291 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001292 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001293 if (STy == 0)
1294 ThrowException("Cannot make struct constant with type: '" +
1295 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001296
Chris Lattnerc6212f12003-05-21 16:06:56 +00001297 if ($3->size() != STy->getNumContainedTypes())
1298 ThrowException("Illegal number of initializers for structure type!");
1299
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001300 // Check to ensure that constants are compatible with the type initializer!
1301 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001302 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001303 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001304 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001305 "' for element #" + utostr(i) +
1306 " of structure initializer!");
1307
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001308 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001309 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001310 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001311 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001312 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001313 if (STy == 0)
1314 ThrowException("Cannot make struct constant with type: '" +
1315 (*$1)->getDescription() + "'!");
1316
1317 if (STy->getNumContainedTypes() != 0)
1318 ThrowException("Illegal number of initializers for structure type!");
1319
1320 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1321 delete $1;
1322 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001323 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001324 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001325 if (PTy == 0)
1326 ThrowException("Cannot make null pointer constant with type: '" +
1327 (*$1)->getDescription() + "'!");
1328
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001329 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001330 delete $1;
1331 }
Chris Lattner16710e92004-10-16 18:17:13 +00001332 | Types UNDEF {
1333 $$ = UndefValue::get($1->get());
1334 delete $1;
1335 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001336 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001337 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001338 if (Ty == 0)
1339 ThrowException("Global const reference must be a pointer type!");
1340
Chris Lattner3101c252002-08-15 17:58:33 +00001341 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001342 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001343 // the context of a function, getValNonImprovising will search the functions
1344 // symbol table instead of the module symbol table for the global symbol,
1345 // which throws things all off. To get around this, we just tell
1346 // getValNonImprovising that we are at global scope here.
1347 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001348 Function *SavedCurFn = CurFun.CurrentFunction;
1349 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001350
Chris Lattner2079fde2001-10-13 06:41:08 +00001351 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001352
Chris Lattner394f2eb2003-10-16 20:12:13 +00001353 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001354
Chris Lattner2079fde2001-10-13 06:41:08 +00001355 // If this is an initializer for a constant pointer, which is referencing a
1356 // (currently) undefined variable, create a stub now that shall be replaced
1357 // in the future with the right type of variable.
1358 //
1359 if (V == 0) {
1360 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1361 const PointerType *PT = cast<PointerType>(Ty);
1362
1363 // First check to see if the forward references value is already created!
1364 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001365 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001366
1367 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001368 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001369 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001370 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001371 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001372 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001373
Reid Spencer3271ed52004-07-18 00:08:11 +00001374 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001375 GlobalValue *GV;
1376 if (const FunctionType *FTy =
1377 dyn_cast<FunctionType>(PT->getElementType())) {
1378 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1379 CurModule.CurrentModule);
1380 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001381 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001382 GlobalValue::ExternalLinkage, 0,
1383 Name, CurModule.CurrentModule);
1384 }
Chris Lattner8a327842004-07-14 21:44:00 +00001385
Reid Spencer3271ed52004-07-18 00:08:11 +00001386 // Keep track of the fact that we have a forward ref to recycle it
1387 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1388 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001389 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001390 }
1391
Reid Spencer3271ed52004-07-18 00:08:11 +00001392 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001393 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001394 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001395 | Types ConstExpr {
1396 if ($1->get() != $2->getType())
1397 ThrowException("Mismatched types for constant expression!");
1398 $$ = $2;
1399 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001400 }
1401 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001402 const Type *Ty = $1->get();
1403 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1404 ThrowException("Cannot create a null initialized value of this type!");
1405 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001406 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001407 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001408
Chris Lattnere43f40b2002-10-09 00:25:32 +00001409ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001410 if (!ConstantSInt::isValueValidForType($1, $2))
1411 ThrowException("Constant value doesn't fit in type!");
1412 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001413 }
1414 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001415 if (!ConstantUInt::isValueValidForType($1, $2))
1416 ThrowException("Constant value doesn't fit in type!");
1417 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001418 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001419 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001420 $$ = ConstantBool::True;
1421 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001422 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001423 $$ = ConstantBool::False;
1424 }
1425 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001426 if (!ConstantFP::isValueValidForType($1, $2))
1427 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001428 $$ = ConstantFP::get($1, $2);
1429 };
1430
Chris Lattner00950542001-06-06 20:29:01 +00001431
Chris Lattnerd78700d2002-08-16 21:14:40 +00001432ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001433 if (!$3->getType()->isFirstClassType())
1434 ThrowException("cast constant expression from a non-primitive type: '" +
1435 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001436 if (!$5->get()->isFirstClassType())
1437 ThrowException("cast constant expression to a non-primitive type: '" +
1438 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001439 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001440 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001441 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001442 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1443 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001444 ThrowException("GetElementPtr requires a pointer operand!");
1445
Chris Lattner830b6f92004-04-05 01:30:04 +00001446 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1447 // indices to uint struct indices for compatibility.
1448 generic_gep_type_iterator<std::vector<Value*>::iterator>
1449 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1450 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1451 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1452 if (isa<StructType>(*GTI)) // Only change struct indices
1453 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1454 if (CUI->getType() == Type::UByteTy)
1455 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1456
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001457 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001458 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001459 if (!IdxTy)
1460 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001461
Chris Lattner9232b992003-04-22 18:42:41 +00001462 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001463 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1464 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001465 IdxVec.push_back(C);
1466 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001467 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001468
Chris Lattnerd78700d2002-08-16 21:14:40 +00001469 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001470
Chris Lattnerd78700d2002-08-16 21:14:40 +00001471 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001472 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001473 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1474 if ($3->getType() != Type::BoolTy)
1475 ThrowException("Select condition must be of boolean type!");
1476 if ($5->getType() != $7->getType())
1477 ThrowException("Select operand types must match!");
1478 $$ = ConstantExpr::getSelect($3, $5, $7);
1479 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001480 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001481 if ($3->getType() != $5->getType())
1482 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001483 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1484 // To retain backward compatibility with these early compilers, we emit a
1485 // cast to the appropriate integer type automatically if we are in the
1486 // broken case. See PR424 for more information.
1487 if (!isa<PointerType>($3->getType())) {
1488 $$ = ConstantExpr::get($1, $3, $5);
1489 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001490 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001491 switch (CurModule.CurrentModule->getPointerSize()) {
1492 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1493 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1494 default: ThrowException("invalid pointer binary constant expr!");
1495 }
1496 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1497 ConstantExpr::getCast($5, IntPtrTy));
1498 $$ = ConstantExpr::getCast($$, $3->getType());
1499 }
1500 }
1501 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1502 if ($3->getType() != $5->getType())
1503 ThrowException("Logical operator types must match!");
Chris Lattner0a017832005-12-21 18:31:29 +00001504 if (!$3->getType()->isIntegral()) {
1505 if (!isa<PackedType>($3->getType()) ||
1506 !cast<PackedType>($3->getType())->getElementType()->isIntegral())
1507 ThrowException("Logical operator requires integral operands!");
1508 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001509 $$ = ConstantExpr::get($1, $3, $5);
1510 }
1511 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1512 if ($3->getType() != $5->getType())
1513 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001514 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001515 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001516 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001517 if ($5->getType() != Type::UByteTy)
1518 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001519 if (!$3->getType()->isInteger())
1520 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001521 $$ = ConstantExpr::get($1, $3, $5);
Robert Bocchino9c62b562006-01-10 19:04:32 +00001522 }
1523 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
1524 if (!isa<PackedType>($3->getType()))
1525 ThrowException("First operand of extractelement must be "
1526 "packed type!");
1527 if ($5->getType() != Type::UIntTy)
1528 ThrowException("Second operand of extractelement must be uint!");
1529 $$ = ConstantExpr::getExtractElement($3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001530 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001531
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001532// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001533ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001534 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001535 }
1536 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001537 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001538 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001539 };
Chris Lattner00950542001-06-06 20:29:01 +00001540
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001541
Chris Lattner1781aca2001-09-18 04:00:54 +00001542// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001543GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001544
Chris Lattner00950542001-06-06 20:29:01 +00001545
Chris Lattner0e73ce62002-05-02 19:11:13 +00001546//===----------------------------------------------------------------------===//
1547// Rules to match Modules
1548//===----------------------------------------------------------------------===//
1549
1550// Module rule: Capture the result of parsing the whole file into a result
1551// variable...
1552//
1553Module : FunctionList {
1554 $$ = ParserResult = $1;
1555 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001556};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001557
Chris Lattner7e708292002-06-25 16:13:24 +00001558// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001559//
1560FunctionList : FunctionList Function {
1561 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001562 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001563 }
1564 | FunctionList FunctionProto {
1565 $$ = $1;
1566 }
1567 | FunctionList IMPLEMENTATION {
1568 $$ = $1;
1569 }
1570 | ConstPool {
1571 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001572 // Emit an error if there are any unresolved types left.
1573 if (!CurModule.LateResolveTypes.empty()) {
1574 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1575 if (DID.Type == ValID::NameVal)
1576 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1577 else
1578 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1579 }
Chris Lattner51727be2002-06-04 21:58:56 +00001580 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001581
Chris Lattnere98dda62001-07-14 06:10:16 +00001582// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001583ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001584 // Eagerly resolve types. This is not an optimization, this is a
1585 // requirement that is due to the fact that we could have this:
1586 //
1587 // %list = type { %list * }
1588 // %list = type { %list * } ; repeated type decl
1589 //
1590 // If types are not resolved eagerly, then the two types will not be
1591 // determined to be the same type!
1592 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001593 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001594
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001595 if (!setTypeName(*$4, $2) && !$2) {
1596 // If this is a named type that is not a redefinition, add it to the slot
1597 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001598 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001599 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001600
1601 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001602 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001603 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001604 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001605 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001606 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
Chris Lattnerb2b96672005-11-12 18:21:21 +00001607 CurGV = ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
1608 } GlobalVarAttributes {
1609 CurGV = 0;
Chris Lattner1781aca2001-09-18 04:00:54 +00001610 }
Chris Lattnerb2b96672005-11-12 18:21:21 +00001611 | ConstPool OptAssign EXTERNAL GlobalType Types {
1612 CurGV = ParseGlobalVariable($2, GlobalValue::ExternalLinkage,
1613 $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001614 delete $5;
Chris Lattnerb2b96672005-11-12 18:21:21 +00001615 } GlobalVarAttributes {
1616 CurGV = 0;
Chris Lattnere98dda62001-07-14 06:10:16 +00001617 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001618 | ConstPool TARGET TargetDefinition {
1619 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001620 | ConstPool DEPLIBS '=' LibrariesDefinition {
1621 }
Chris Lattner00950542001-06-06 20:29:01 +00001622 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001623 };
Chris Lattner00950542001-06-06 20:29:01 +00001624
1625
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001626
1627BigOrLittle : BIG { $$ = Module::BigEndian; };
1628BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1629
1630TargetDefinition : ENDIAN '=' BigOrLittle {
1631 CurModule.CurrentModule->setEndianness($3);
1632 }
1633 | POINTERSIZE '=' EUINT64VAL {
1634 if ($3 == 32)
1635 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1636 else if ($3 == 64)
1637 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1638 else
1639 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001640 }
1641 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001642 CurModule.CurrentModule->setTargetTriple($3);
1643 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001644 };
1645
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001646LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001647
1648LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001649 CurModule.CurrentModule->addLibrary($3);
1650 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001651 }
1652 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001653 CurModule.CurrentModule->addLibrary($1);
1654 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001655 }
1656 | /* empty: end of list */ {
1657 }
1658 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001659
Chris Lattner00950542001-06-06 20:29:01 +00001660//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001661// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001662//===----------------------------------------------------------------------===//
1663
Chris Lattner6c23f572003-08-22 05:42:10 +00001664Name : VAR_ID | STRINGCONSTANT;
1665OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001666
Chris Lattner6c23f572003-08-22 05:42:10 +00001667ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001668 if (*$1 == Type::VoidTy)
1669 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001670 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001671};
Chris Lattner00950542001-06-06 20:29:01 +00001672
Chris Lattner69da5cf2002-10-13 20:57:00 +00001673ArgListH : ArgListH ',' ArgVal {
1674 $$ = $1;
1675 $1->push_back(*$3);
1676 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001677 }
1678 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001679 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001680 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001681 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001682 };
Chris Lattner00950542001-06-06 20:29:01 +00001683
1684ArgList : ArgListH {
1685 $$ = $1;
1686 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001687 | ArgListH ',' DOTDOTDOT {
1688 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001689 $$->push_back(std::pair<PATypeHolder*,
1690 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001691 }
1692 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001693 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1694 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001695 }
Chris Lattner00950542001-06-06 20:29:01 +00001696 | /* empty */ {
1697 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001698 };
Chris Lattner00950542001-06-06 20:29:01 +00001699
Chris Lattner164c3782005-11-12 00:11:10 +00001700FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1701 OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001702 UnEscapeLexed($3);
1703 std::string FunctionName($3);
1704 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001705
Chris Lattnera8e8f162005-05-06 20:27:19 +00001706 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001707 ThrowException("LLVM functions cannot return aggregate types!");
1708
Chris Lattner9232b992003-04-22 18:42:41 +00001709 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001710 if ($5) { // If there are arguments...
1711 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1712 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001713 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001714 }
Chris Lattner00950542001-06-06 20:29:01 +00001715
Chris Lattner2079fde2001-10-13 06:41:08 +00001716 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1717 if (isVarArg) ParamTypeList.pop_back();
1718
Chris Lattnera8e8f162005-05-06 20:27:19 +00001719 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001720 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001721 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001722
Chris Lattnera09000d2004-07-14 23:03:46 +00001723 ValID ID;
1724 if (!FunctionName.empty()) {
1725 ID = ValID::create((char*)FunctionName.c_str());
1726 } else {
1727 ID = ValID::create((int)CurModule.Values[PFT].size());
1728 }
1729
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001730 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001731 // See if this function was forward referenced. If so, recycle the object.
1732 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1733 // Move the function to the end of the list, from whereever it was
1734 // previously inserted.
1735 Fn = cast<Function>(FWRef);
1736 CurModule.CurrentModule->getFunctionList().remove(Fn);
1737 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1738 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1739 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1740 // If this is the case, either we need to be a forward decl, or it needs
1741 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001742 if (!CurFun.isDeclare && !Fn->isExternal())
1743 ThrowException("Redefinition of function '" + FunctionName + "'!");
1744
Chris Lattnera09000d2004-07-14 23:03:46 +00001745 // Make sure to strip off any argument names so we can't get conflicts.
1746 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001747 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001748 AI != AE; ++AI)
1749 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001750
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001751 } else { // Not already defined?
1752 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1753 CurModule.CurrentModule);
1754 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001755 }
Chris Lattner00950542001-06-06 20:29:01 +00001756
Chris Lattner394f2eb2003-10-16 20:12:13 +00001757 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001758 Fn->setCallingConv($1);
Chris Lattner164c3782005-11-12 00:11:10 +00001759 Fn->setAlignment($8);
1760 if ($7) {
1761 Fn->setSection($7);
1762 free($7);
1763 }
Chris Lattner00950542001-06-06 20:29:01 +00001764
Chris Lattner7e708292002-06-25 16:13:24 +00001765 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001766 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001767 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001768 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001769 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001770 delete $5->back().first;
1771 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001772 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001773 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001774 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1775 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001776 delete I->first; // Delete the typeholder...
1777
Chris Lattner8ab406d2004-07-13 07:59:27 +00001778 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001779 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001780 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001781
Chris Lattnera8e8f162005-05-06 20:27:19 +00001782 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001783 }
Chris Lattner51727be2002-06-04 21:58:56 +00001784};
Chris Lattner00950542001-06-06 20:29:01 +00001785
Chris Lattner9b02cc32002-05-03 18:23:48 +00001786BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1787
Chris Lattner4ad02e72003-04-16 20:28:45 +00001788FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001789 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001790
Chris Lattner4ad02e72003-04-16 20:28:45 +00001791 // Make sure that we keep track of the linkage type even if there was a
1792 // previous "declare".
1793 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001794};
Chris Lattner00950542001-06-06 20:29:01 +00001795
Chris Lattner9b02cc32002-05-03 18:23:48 +00001796END : ENDTOK | '}'; // Allow end of '}' to end a function
1797
Chris Lattner79df7c02002-03-26 18:01:55 +00001798Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001799 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001800};
Chris Lattner00950542001-06-06 20:29:01 +00001801
Chris Lattner394f2eb2003-10-16 20:12:13 +00001802FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1803 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001804 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001805};
Chris Lattner00950542001-06-06 20:29:01 +00001806
1807//===----------------------------------------------------------------------===//
1808// Rules to match Basic Blocks
1809//===----------------------------------------------------------------------===//
1810
1811ConstValueRef : ESINT64VAL { // A reference to a direct constant
1812 $$ = ValID::create($1);
1813 }
1814 | EUINT64VAL {
1815 $$ = ValID::create($1);
1816 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001817 | FPVAL { // Perhaps it's an FP constant?
1818 $$ = ValID::create($1);
1819 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001820 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001821 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001822 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001823 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001824 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001825 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001826 | NULL_TOK {
1827 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001828 }
Chris Lattner16710e92004-10-16 18:17:13 +00001829 | UNDEF {
1830 $$ = ValID::createUndef();
1831 }
Chris Lattnerf1f03df2005-12-21 17:53:02 +00001832 | ZEROINITIALIZER { // A vector zero constant.
1833 $$ = ValID::createZeroInit();
1834 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001835 | '<' ConstVector '>' { // Nonempty unsized packed vector
1836 const Type *ETy = (*$2)[0]->getType();
1837 int NumElements = $2->size();
1838
1839 PackedType* pt = PackedType::get(ETy, NumElements);
1840 PATypeHolder* PTy = new PATypeHolder(
1841 HandleUpRefs(
1842 PackedType::get(
1843 ETy,
1844 NumElements)
1845 )
1846 );
1847
1848 // Verify all elements are correct type!
1849 for (unsigned i = 0; i < $2->size(); i++) {
1850 if (ETy != (*$2)[i]->getType())
1851 ThrowException("Element #" + utostr(i) + " is not of type '" +
1852 ETy->getDescription() +"' as required!\nIt is of type '" +
1853 (*$2)[i]->getType()->getDescription() + "'.");
1854 }
1855
1856 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1857 delete PTy; delete $2;
1858 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001859 | ConstExpr {
1860 $$ = ValID::create($1);
1861 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001862
Chris Lattner2079fde2001-10-13 06:41:08 +00001863// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1864// another value.
1865//
1866SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001867 $$ = ValID::create($1);
1868 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001869 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001870 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001871 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001872
1873// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001874ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001875
Chris Lattner00950542001-06-06 20:29:01 +00001876
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001877// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1878// type immediately preceeds the value reference, and allows complex constant
1879// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001880ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001881 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001882 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001883
Chris Lattner00950542001-06-06 20:29:01 +00001884BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001885 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001886 }
Chris Lattner7e708292002-06-25 16:13:24 +00001887 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001888 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001889 };
Chris Lattner00950542001-06-06 20:29:01 +00001890
1891
1892// Basic blocks are terminated by branching instructions:
1893// br, br/cc, switch, ret
1894//
Chris Lattner2079fde2001-10-13 06:41:08 +00001895BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001896 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001897 InsertValue($3);
1898
1899 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001900 InsertValue($1);
1901 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001902 };
Chris Lattner00950542001-06-06 20:29:01 +00001903
1904InstructionList : InstructionList Inst {
1905 $1->getInstList().push_back($2);
1906 $$ = $1;
1907 }
1908 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001909 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001910
1911 // Make sure to move the basic block to the correct location in the
1912 // function, instead of leaving it inserted wherever it was first
1913 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001914 Function::BasicBlockListType &BBL =
1915 CurFun.CurrentFunction->getBasicBlockList();
1916 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner22ae2322004-07-13 08:39:15 +00001917 }
1918 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001919 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001920
1921 // Make sure to move the basic block to the correct location in the
1922 // function, instead of leaving it inserted wherever it was first
1923 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001924 Function::BasicBlockListType &BBL =
1925 CurFun.CurrentFunction->getBasicBlockList();
1926 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner51727be2002-06-04 21:58:56 +00001927 };
Chris Lattner00950542001-06-06 20:29:01 +00001928
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001929BBTerminatorInst : RET ResolvedVal { // Return with a result...
1930 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001931 }
1932 | RET VOID { // Return with no result...
1933 $$ = new ReturnInst();
1934 }
1935 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001936 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001937 } // Conditional Branch...
1938 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001939 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001940 }
1941 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001942 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001943 $$ = S;
1944
Chris Lattner9232b992003-04-22 18:42:41 +00001945 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001946 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001947 for (; I != E; ++I) {
1948 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
1949 S->addCase(CI, I->second);
1950 else
1951 ThrowException("Switch case is constant, but not a simple integer!");
1952 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001953 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001954 }
Chris Lattner196850c2003-05-15 21:30:00 +00001955 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001956 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00001957 $$ = S;
1958 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001959 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1960 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001961 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001962 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001963
Chris Lattnera8e8f162005-05-06 20:27:19 +00001964 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001965 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001966 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001967 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001968 if ($6) {
1969 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00001970 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001971 ParamTypes.push_back((*I)->getType());
1972 }
1973
1974 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1975 if (isVarArg) ParamTypes.pop_back();
1976
Chris Lattnera8e8f162005-05-06 20:27:19 +00001977 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001978 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001979 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001980
Chris Lattnera8e8f162005-05-06 20:27:19 +00001981 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001982
Chris Lattnera8e8f162005-05-06 20:27:19 +00001983 BasicBlock *Normal = getBBVal($10);
1984 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00001985
1986 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001987 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001988 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001989 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001990 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001991 // correctly!
1992 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001993 FunctionType::param_iterator I = Ty->param_begin();
1994 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001995 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001996
1997 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001998 if ((*ArgI)->getType() != *I)
1999 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2000 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002001
2002 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002003 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00002004
Chris Lattnera8e8f162005-05-06 20:27:19 +00002005 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00002006 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002007 cast<InvokeInst>($$)->setCallingConv($2);
2008
2009 delete $3;
2010 delete $6;
Chris Lattner36143fc2003-09-08 18:54:55 +00002011 }
2012 | UNWIND {
2013 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00002014 }
2015 | UNREACHABLE {
2016 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00002017 };
Chris Lattner2079fde2001-10-13 06:41:08 +00002018
2019
Chris Lattner00950542001-06-06 20:29:01 +00002020
2021JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
2022 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002023 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00002024 if (V == 0)
2025 ThrowException("May only switch on a constant pool value!");
2026
Chris Lattner64116f92004-07-14 01:33:11 +00002027 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00002028 }
2029 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002030 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002031 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00002032
2033 if (V == 0)
2034 ThrowException("May only switch on a constant pool value!");
2035
Chris Lattner64116f92004-07-14 01:33:11 +00002036 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00002037 };
Chris Lattner00950542001-06-06 20:29:01 +00002038
2039Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00002040 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00002041 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00002042 InsertValue($2);
2043 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00002044};
Chris Lattner00950542001-06-06 20:29:01 +00002045
Chris Lattnerc24d2082001-06-11 15:04:20 +00002046PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00002047 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00002048 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00002049 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002050 }
2051 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2052 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00002053 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00002054 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00002055 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002056
2057
Chris Lattner30c89792001-09-07 16:35:17 +00002058ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00002059 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002060 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00002061 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002062 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00002063 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002064 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00002065 };
Chris Lattner00950542001-06-06 20:29:01 +00002066
2067// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00002068ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00002069
Chris Lattnerddb6db42005-05-06 05:51:46 +00002070OptTailCall : TAIL CALL {
2071 $$ = true;
2072 }
2073 | CALL {
2074 $$ = false;
2075 };
2076
2077
2078
Chris Lattner4a6482b2002-09-10 19:57:26 +00002079InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00002080 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2081 !isa<PackedType>((*$2).get()))
2082 ThrowException(
2083 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00002084 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
2085 ThrowException("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002086 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2087 if ($$ == 0)
2088 ThrowException("binary operator returned null!");
2089 delete $2;
2090 }
2091 | LogicalOps Types ValueRef ',' ValueRef {
Chris Lattner0a017832005-12-21 18:31:29 +00002092 if (!(*$2)->isIntegral()) {
2093 if (!isa<PackedType>($2->get()) ||
2094 !cast<PackedType>($2->get())->getElementType()->isIntegral())
2095 ThrowException("Logical operator requires integral operands!");
2096 }
Chris Lattner4a6482b2002-09-10 19:57:26 +00002097 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2098 if ($$ == 0)
2099 ThrowException("binary operator returned null!");
2100 delete $2;
2101 }
2102 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002103 if(isa<PackedType>((*$2).get())) {
2104 ThrowException(
2105 "PackedTypes currently not supported in setcc instructions!");
2106 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00002107 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00002108 if ($$ == 0)
2109 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002110 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002111 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002112 | NOT ResolvedVal {
2113 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2114 << " Replacing with 'xor'.\n";
2115
2116 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2117 if (Ones == 0)
2118 ThrowException("Expected integral type for not instruction!");
2119
2120 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002121 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00002122 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00002123 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002124 | ShiftOps ResolvedVal ',' ResolvedVal {
2125 if ($4->getType() != Type::UByteTy)
2126 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002127 if (!$2->getType()->isInteger())
2128 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002129 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00002130 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002131 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002132 if (!$4->get()->isFirstClassType())
2133 ThrowException("cast instruction to a non-primitive type: '" +
2134 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002135 $$ = new CastInst($2, *$4);
2136 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002137 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002138 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2139 if ($2->getType() != Type::BoolTy)
2140 ThrowException("select condition must be boolean!");
2141 if ($4->getType() != $6->getType())
2142 ThrowException("select value types should match!");
2143 $$ = new SelectInst($2, $4, $6);
2144 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002145 | VAARG ResolvedVal ',' Types {
Andrew Lenharthe78f50d2005-06-19 03:53:56 +00002146 NewVarArgs = true;
Chris Lattner99e7ab72003-10-18 05:53:13 +00002147 $$ = new VAArgInst($2, *$4);
2148 delete $4;
2149 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002150 | VAARG_old ResolvedVal ',' Types {
2151 ObsoleteVarArgs = true;
2152 const Type* ArgTy = $2->getType();
2153 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002154 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002155
2156 //b = vaarg a, t ->
2157 //foo = alloca 1 of t
2158 //bar = vacopy a
2159 //store bar -> foo
2160 //b = vaarg foo, t
2161 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2162 CurBB->getInstList().push_back(foo);
2163 CallInst* bar = new CallInst(NF, $2);
2164 CurBB->getInstList().push_back(bar);
2165 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2166 $$ = new VAArgInst(foo, *$4);
2167 delete $4;
2168 }
2169 | VANEXT_old ResolvedVal ',' Types {
2170 ObsoleteVarArgs = true;
2171 const Type* ArgTy = $2->getType();
2172 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002173 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002174
2175 //b = vanext a, t ->
2176 //foo = alloca 1 of t
2177 //bar = vacopy a
2178 //store bar -> foo
2179 //tmp = vaarg foo, t
2180 //b = load foo
2181 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2182 CurBB->getInstList().push_back(foo);
2183 CallInst* bar = new CallInst(NF, $2);
2184 CurBB->getInstList().push_back(bar);
2185 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2186 Instruction* tmp = new VAArgInst(foo, *$4);
2187 CurBB->getInstList().push_back(tmp);
2188 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002189 delete $4;
2190 }
Robert Bocchino9c62b562006-01-10 19:04:32 +00002191 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
2192 if (!isa<PackedType>($2->getType()))
2193 ThrowException("First operand of extractelement must be a "
2194 "packed type val!");
2195 if ($4->getType() != Type::UIntTy)
2196 ThrowException("Second operand of extractelement must be a uint!");
2197 $$ = new ExtractElementInst($2, $4);
2198 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002199 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002200 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002201 if (!Ty->isFirstClassType())
2202 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002203 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002204 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002205 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002206 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00002207 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002208 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002209 $2->pop_front();
2210 }
2211 delete $2; // Free the list...
Chris Lattnerddb6db42005-05-06 05:51:46 +00002212 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002213 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002214 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002215 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002216
Chris Lattnera8e8f162005-05-06 20:27:19 +00002217 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002218 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002219 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002220 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002221 if ($6) {
2222 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002223 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002224 ParamTypes.push_back((*I)->getType());
2225 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002226
2227 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2228 if (isVarArg) ParamTypes.pop_back();
2229
Chris Lattnera8e8f162005-05-06 20:27:19 +00002230 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Chris Lattner595f06c2005-02-01 01:47:42 +00002231 ThrowException("LLVM functions cannot return aggregate types!");
2232
Chris Lattnera8e8f162005-05-06 20:27:19 +00002233 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002234 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002235 }
Chris Lattner00950542001-06-06 20:29:01 +00002236
Chris Lattnera8e8f162005-05-06 20:27:19 +00002237 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002238
Chris Lattner8b81bf52001-07-25 22:47:46 +00002239 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002240 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002241 // Make sure no arguments is a good thing!
2242 if (Ty->getNumParams() != 0)
2243 ThrowException("No arguments passed to a function that "
2244 "expects arguments!");
2245
Chris Lattner9232b992003-04-22 18:42:41 +00002246 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002247 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002248 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002249 // correctly!
2250 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002251 FunctionType::param_iterator I = Ty->param_begin();
2252 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002253 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002254
2255 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002256 if ((*ArgI)->getType() != *I)
2257 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2258 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002259
Chris Lattner8b81bf52001-07-25 22:47:46 +00002260 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002261 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002262
Chris Lattnera8e8f162005-05-06 20:27:19 +00002263 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002264 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002265 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002266 cast<CallInst>($$)->setCallingConv($2);
2267 delete $3;
2268 delete $6;
Chris Lattner00950542001-06-06 20:29:01 +00002269 }
2270 | MemoryInst {
2271 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002272 };
Chris Lattner00950542001-06-06 20:29:01 +00002273
Chris Lattner6cdb0112001-11-26 16:54:11 +00002274
2275// IndexList - List of indices for GEP based instructions...
2276IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002277 $$ = $2;
2278 } | /* empty */ {
2279 $$ = new std::vector<Value*>();
2280 };
2281
2282OptVolatile : VOLATILE {
2283 $$ = true;
2284 }
2285 | /* empty */ {
2286 $$ = false;
2287 };
2288
Chris Lattner027dcc52001-07-08 21:10:27 +00002289
Chris Lattnerddb6db42005-05-06 05:51:46 +00002290
Chris Lattner66db8e42005-11-06 06:34:12 +00002291MemoryInst : MALLOC Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002292 $$ = new MallocInst(*$2, 0, $3);
Chris Lattner30c89792001-09-07 16:35:17 +00002293 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002294 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002295 | MALLOC Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002296 $$ = new MallocInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002297 delete $2;
2298 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002299 | ALLOCA Types OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002300 $$ = new AllocaInst(*$2, 0, $3);
Nate Begeman14b05292005-11-05 09:21:28 +00002301 delete $2;
2302 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002303 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Chris Lattner66db8e42005-11-06 06:34:12 +00002304 $$ = new AllocaInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002305 delete $2;
2306 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002307 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002308 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002309 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002310 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002311 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002312 }
2313
Chris Lattner15c9c032003-09-08 18:20:29 +00002314 | OptVolatile LOAD Types ValueRef {
2315 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002316 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002317 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002318 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2319 ThrowException("Can't load from pointer of non-first-class type: " +
2320 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002321 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002322 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002323 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002324 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2325 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002326 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002327 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002328 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002329 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002330 if (ElTy != $3->getType())
2331 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002332 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002333
Chris Lattner79ad13802003-09-08 20:29:46 +00002334 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002335 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002336 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002337 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002338 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002339 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002340
2341 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2342 // indices to uint struct indices for compatibility.
2343 generic_gep_type_iterator<std::vector<Value*>::iterator>
2344 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2345 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2346 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2347 if (isa<StructType>(*GTI)) // Only change struct indices
2348 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2349 if (CUI->getType() == Type::UByteTy)
2350 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2351
Chris Lattner30c89792001-09-07 16:35:17 +00002352 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002353 ThrowException("Invalid getelementptr indices for type '" +
2354 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002355 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2356 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002357 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002358
Brian Gaeked0fde302003-11-11 22:41:34 +00002359
Chris Lattner00950542001-06-06 20:29:01 +00002360%%
Chris Lattner09083092001-07-08 04:57:15 +00002361int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002362 std::string where
2363 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002364 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002365 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002366 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002367 errMsg += "end-of-file.";
2368 else
Chris Lattner9232b992003-04-22 18:42:41 +00002369 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002370 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002371 return 0;
2372}