blob: 31b286b87edf65ae04f60ca5dd2842131fcff6a6 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Misha Brukman5a2a3822005-05-10 22:02:28 +00002//
John Criswell856ba762003-10-21 15:17:13 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman5a2a3822005-05-10 22:02:28 +00007//
John Criswell856ba762003-10-21 15:17:13 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattnera8e8f162005-05-06 20:27:19 +000016#include "llvm/CallingConv.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000017#include "llvm/Instructions.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Module.h"
Alkis Evlogimenoseb62bc72004-07-29 12:17:34 +000019#include "llvm/SymbolTable.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000020#include "llvm/Support/GetElementPtrTypeIterator.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000021#include "llvm/ADT/STLExtras.h"
Chris Lattner87ac9722005-11-06 06:46:28 +000022#include "llvm/Support/MathExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000023#include <algorithm>
24#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000025#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000026#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner386a3b72001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000030int yyparse();
31
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
Chris Lattner86427632004-07-14 06:39:48 +000033 std::string CurFilename;
34}
35using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000036
Chris Lattner00950542001-06-06 20:29:01 +000037static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000038
Chris Lattner30c89792001-09-07 16:35:17 +000039// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
40// relating to upreferences in the input stream.
41//
42//#define DEBUG_UPREFS 1
43#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000044#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000045#else
46#define UR_OUT(X)
47#endif
48
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000049#define YYERROR_VERBOSE 1
50
Andrew Lenharth558bc882005-06-18 18:34:52 +000051static bool ObsoleteVarArgs;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +000052static bool NewVarArgs;
Andrew Lenharth558bc882005-06-18 18:34:52 +000053static BasicBlock* CurBB;
54
55
Chris Lattner7e708292002-06-25 16:13:24 +000056// This contains info used when building the body of a function. It is
57// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000058//
Chris Lattner9232b992003-04-22 18:42:41 +000059typedef std::vector<Value *> ValueList; // Numbered defs
Misha Brukman5a2a3822005-05-10 22:02:28 +000060static void
61ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
62 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000063
64static struct PerModuleInfo {
65 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000066 std::map<const Type *, ValueList> Values; // Module level numbered definitions
67 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000068 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000069 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000070
Chris Lattnerbc721ed2004-07-13 08:28:21 +000071 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
72 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000073 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000074 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
75
Chris Lattner2079fde2001-10-13 06:41:08 +000076 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
77 // references to global values. Global values may be referenced before they
78 // are defined, and if so, the temporary object that they represent is held
Reid Spencer3271ed52004-07-18 00:08:11 +000079 // here. This is used for forward references of GlobalValues.
Chris Lattner2079fde2001-10-13 06:41:08 +000080 //
Chris Lattner9232b992003-04-22 18:42:41 +000081 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000082 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000083 GlobalRefsType GlobalRefs;
84
Chris Lattner00950542001-06-06 20:29:01 +000085 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000086 // If we could not resolve some functions at function compilation time
87 // (calls to functions before they are defined), resolve them now... Types
88 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000089 //
Chris Lattner00950542001-06-06 20:29:01 +000090 ResolveDefinitions(LateResolveValues);
91
Chris Lattner2079fde2001-10-13 06:41:08 +000092 // Check to make sure that all global value forward references have been
93 // resolved!
94 //
95 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000096 std::string UndefinedReferences = "Unresolved global references exist:\n";
Misha Brukman5a2a3822005-05-10 22:02:28 +000097
Chris Lattner749ce032002-03-11 22:12:39 +000098 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
99 I != E; ++I) {
100 UndefinedReferences += " " + I->first.first->getDescription() + " " +
101 I->first.second.getName() + "\n";
102 }
103 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000104 }
105
Chris Lattner7e708292002-06-25 16:13:24 +0000106 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000107 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000108 CurrentModule = 0;
109 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000110
111
Chris Lattner8a327842004-07-14 21:44:00 +0000112 // GetForwardRefForGlobal - Check to see if there is a forward reference
113 // for this global. If so, remove it from the GlobalRefs map and return it.
114 // If not, just return null.
115 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
116 // Check to see if there is a forward reference to this global variable...
117 // if there is, eliminate it and patch the reference to use the new def'n.
118 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
119 GlobalValue *Ret = 0;
120 if (I != GlobalRefs.end()) {
121 Ret = I->second;
122 GlobalRefs.erase(I);
123 }
124 return Ret;
125 }
Chris Lattner00950542001-06-06 20:29:01 +0000126} CurModule;
127
Chris Lattner79df7c02002-03-26 18:01:55 +0000128static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000129 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000130
Chris Lattner735f2702004-07-08 22:30:50 +0000131 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
132 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner7e708292002-06-25 16:13:24 +0000133 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000134
Chris Lattner2e2ed292004-07-14 06:28:35 +0000135 /// BBForwardRefs - When we see forward references to basic blocks, keep
136 /// track of them here.
137 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
138 std::vector<BasicBlock*> NumberedBlocks;
139 unsigned NextBBNum;
140
Chris Lattner79df7c02002-03-26 18:01:55 +0000141 inline PerFunctionInfo() {
142 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000143 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000144 }
145
Chris Lattner79df7c02002-03-26 18:01:55 +0000146 inline void FunctionStart(Function *M) {
147 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000148 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000149 }
150
Chris Lattner79df7c02002-03-26 18:01:55 +0000151 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000152 NumberedBlocks.clear();
153
154 // Any forward referenced blocks left?
155 if (!BBForwardRefs.empty())
156 ThrowException("Undefined reference to label " +
Chris Lattner83beacd2005-02-24 04:59:49 +0000157 BBForwardRefs.begin()->first->getName());
Chris Lattner2e2ed292004-07-14 06:28:35 +0000158
159 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000160 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000161
Chris Lattner7e708292002-06-25 16:13:24 +0000162 Values.clear(); // Clear out function local definitions
Chris Lattner79df7c02002-03-26 18:01:55 +0000163 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000164 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000165 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000166} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000167
Chris Lattner394f2eb2003-10-16 20:12:13 +0000168static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000169
Chris Lattner00950542001-06-06 20:29:01 +0000170
171//===----------------------------------------------------------------------===//
172// Code to handle definitions of all the types
173//===----------------------------------------------------------------------===//
174
Chris Lattner735f2702004-07-08 22:30:50 +0000175static int InsertValue(Value *V,
176 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
177 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000178
179 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000180 ValueList &List = ValueTab[V->getType()];
181 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000182 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000183}
184
Chris Lattner30c89792001-09-07 16:35:17 +0000185static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000186 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000187 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000188 // Module constants occupy the lowest numbered slots...
Misha Brukman5a2a3822005-05-10 22:02:28 +0000189 if ((unsigned)D.Num < CurModule.Types.size())
Chris Lattnera09000d2004-07-14 23:03:46 +0000190 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000191 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000192 case ValID::NameVal: // Is it a named definition?
193 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
194 D.destroy(); // Free old strdup'd memory...
195 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000196 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000197 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000198 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000199 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000200 }
201
202 // If we reached here, we referenced either a symbol that we don't know about
203 // or an id number that hasn't been read yet. We may be referencing something
204 // forward, so just create an entry to be resolved later and get to it...
205 //
206 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
207
Chris Lattner355ad1f2005-03-21 06:27:42 +0000208
209 if (inFunctionScope()) {
210 if (D.Type == ValID::NameVal)
211 ThrowException("Reference to an undefined type: '" + D.getName() + "'");
212 else
213 ThrowException("Reference to an undefined type: #" + itostr(D.Num));
Chris Lattner4a42e902001-10-22 05:56:09 +0000214 }
Chris Lattner30c89792001-09-07 16:35:17 +0000215
Chris Lattner355ad1f2005-03-21 06:27:42 +0000216 std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D);
217 if (I != CurModule.LateResolveTypes.end())
218 return I->second;
219
Chris Lattner82269592001-10-22 06:01:08 +0000220 Type *Typ = OpaqueType::get();
Chris Lattner355ad1f2005-03-21 06:27:42 +0000221 CurModule.LateResolveTypes.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000222 return Typ;
Chris Lattner355ad1f2005-03-21 06:27:42 +0000223 }
Chris Lattner30c89792001-09-07 16:35:17 +0000224
Chris Lattner9232b992003-04-22 18:42:41 +0000225static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000226 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000227 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000228 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000229 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000230}
231
Chris Lattner2079fde2001-10-13 06:41:08 +0000232// getValNonImprovising - Look up the value specified by the provided type and
233// the provided ValID. If the value exists and has already been defined, return
234// it. Otherwise return null.
235//
236static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000237 if (isa<FunctionType>(Ty))
238 ThrowException("Functions are not values and "
239 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000240
Chris Lattner30c89792001-09-07 16:35:17 +0000241 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000242 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000243 unsigned Num = (unsigned)D.Num;
244
245 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000246 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000247 if (VI != CurModule.Values.end()) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000248 if (Num < VI->second.size())
Chris Lattner16af11d2004-02-09 21:03:38 +0000249 return VI->second[Num];
250 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000251 }
252
253 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000254 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000255 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000256
257 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000258 if (VI->second.size() <= Num) return 0;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000259
Chris Lattner16af11d2004-02-09 21:03:38 +0000260 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000261 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000262
Chris Lattner1a1cb112001-09-30 22:46:54 +0000263 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000264 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000265 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000266
267 D.destroy(); // Free old strdup'd memory...
268 return N;
269 }
270
Misha Brukman5a2a3822005-05-10 22:02:28 +0000271 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner2079fde2001-10-13 06:41:08 +0000272 // value will fit into the specified type...
273 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000274 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
275 ThrowException("Signed integral constant '" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000276 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattnerd78700d2002-08-16 21:14:40 +0000277 Ty->getDescription() + "'!");
278 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000279
280 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000281 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
282 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Reid Spencer3271ed52004-07-18 00:08:11 +0000283 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
Chris Lattnerf8dff732002-07-18 05:18:37 +0000284 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000285 } else { // This is really a signed reference. Transmogrify.
Reid Spencer3271ed52004-07-18 00:08:11 +0000286 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000287 }
288 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000289 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000290 }
291
Chris Lattner2079fde2001-10-13 06:41:08 +0000292 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000293 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000294 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000295 return ConstantFP::get(Ty, D.ConstPoolFP);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000296
Chris Lattner2079fde2001-10-13 06:41:08 +0000297 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000298 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000299 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000300 return ConstantPointerNull::get(cast<PointerType>(Ty));
Misha Brukman5a2a3822005-05-10 22:02:28 +0000301
Chris Lattner16710e92004-10-16 18:17:13 +0000302 case ValID::ConstUndefVal: // Is it an undef value?
303 return UndefValue::get(Ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000304
Chris Lattnerd78700d2002-08-16 21:14:40 +0000305 case ValID::ConstantVal: // Fully resolved constant?
306 if (D.ConstantValue->getType() != Ty)
307 ThrowException("Constant expression type different from required type!");
308 return D.ConstantValue;
309
Chris Lattner30c89792001-09-07 16:35:17 +0000310 default:
311 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000312 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000313 } // End of switch
314
Chris Lattner2079fde2001-10-13 06:41:08 +0000315 assert(0 && "Unhandled case!");
316 return 0;
317}
318
Chris Lattner2079fde2001-10-13 06:41:08 +0000319// getVal - This function is identical to getValNonImprovising, except that if a
320// value is not already defined, it "improvises" by creating a placeholder var
321// that looks and acts just like the requested variable. When the value is
322// defined later, all uses of the placeholder variable are replaced with the
323// real thing.
324//
Chris Lattner64116f92004-07-14 01:33:11 +0000325static Value *getVal(const Type *Ty, const ValID &ID) {
326 if (Ty == Type::LabelTy)
327 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000328
Chris Lattner64116f92004-07-14 01:33:11 +0000329 // See if the value has already been defined.
330 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000331 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000332
Chris Lattner60cd9552005-03-23 01:29:26 +0000333 if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty))
334 ThrowException("Invalid use of a composite type!");
335
Chris Lattner00950542001-06-06 20:29:01 +0000336 // If we reached here, we referenced either a symbol that we don't know about
337 // or an id number that hasn't been read yet. We may be referencing something
338 // forward, so just create an entry to be resolved later and get to it...
339 //
Chris Lattner64116f92004-07-14 01:33:11 +0000340 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000341
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000342 // Remember where this forward reference came from. FIXME, shouldn't we try
343 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000344 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000345 llvmAsmlineno)));
346
Chris Lattner79df7c02002-03-26 18:01:55 +0000347 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000348 InsertValue(V, CurFun.LateResolveValues);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000349 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000350 InsertValue(V, CurModule.LateResolveValues);
351 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000352}
353
Chris Lattner2e2ed292004-07-14 06:28:35 +0000354/// getBBVal - This is used for two purposes:
355/// * If isDefinition is true, a new basic block with the specified ID is being
356/// defined.
357/// * If isDefinition is true, this is a reference to a basic block, which may
358/// or may not be a forward reference.
359///
360static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000361 assert(inFunctionScope() && "Can't get basic block at global scope!");
362
Chris Lattner2e2ed292004-07-14 06:28:35 +0000363 std::string Name;
364 BasicBlock *BB = 0;
365 switch (ID.Type) {
366 default: ThrowException("Illegal label reference " + ID.getName());
367 case ValID::NumberVal: // Is it a numbered definition?
368 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
369 CurFun.NumberedBlocks.resize(ID.Num+1);
370 BB = CurFun.NumberedBlocks[ID.Num];
371 break;
372 case ValID::NameVal: // Is it a named definition?
373 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000374 if (Value *N = CurFun.CurrentFunction->
375 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000376 BB = cast<BasicBlock>(N);
377 break;
378 }
Chris Lattner64116f92004-07-14 01:33:11 +0000379
Chris Lattner2e2ed292004-07-14 06:28:35 +0000380 // See if the block has already been defined.
381 if (BB) {
382 // If this is the definition of the block, make sure the existing value was
383 // just a forward reference. If it was a forward reference, there will be
384 // an entry for it in the PlaceHolderInfo map.
385 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
386 // The existing value was a definition, not a forward reference.
387 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000388
Chris Lattner2e2ed292004-07-14 06:28:35 +0000389 ID.destroy(); // Free strdup'd memory.
390 return BB;
391 }
392
393 // Otherwise this block has not been seen before.
394 BB = new BasicBlock("", CurFun.CurrentFunction);
395 if (ID.Type == ValID::NameVal) {
396 BB->setName(ID.Name);
397 } else {
398 CurFun.NumberedBlocks[ID.Num] = BB;
399 }
400
401 // If this is not a definition, keep track of it so we can use it as a forward
402 // reference.
403 if (!isDefinition) {
404 // Remember where this forward reference came from.
405 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
406 } else {
407 // The forward declaration could have been inserted anywhere in the
408 // function: insert it into the correct place now.
409 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
410 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
411 }
Chris Lattnere8343a52004-10-26 18:26:14 +0000412 ID.destroy();
Chris Lattner64116f92004-07-14 01:33:11 +0000413 return BB;
414}
415
Chris Lattner00950542001-06-06 20:29:01 +0000416
417//===----------------------------------------------------------------------===//
418// Code to handle forward references in instructions
419//===----------------------------------------------------------------------===//
420//
421// This code handles the late binding needed with statements that reference
422// values not defined yet... for example, a forward branch, or the PHI node for
423// a loop body.
424//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000425// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000426// and back patchs after we are done.
427//
428
Misha Brukman5a2a3822005-05-10 22:02:28 +0000429// ResolveDefinitions - If we could not resolve some defs at parsing
430// time (forward branches, phi functions for loops, etc...) resolve the
Chris Lattner00950542001-06-06 20:29:01 +0000431// defs now...
432//
Misha Brukman5a2a3822005-05-10 22:02:28 +0000433static void
434ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
435 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000436 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000437 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000438 E = LateResolvers.end(); LRI != E; ++LRI) {
439 ValueList &List = LRI->second;
440 while (!List.empty()) {
441 Value *V = List.back();
442 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000443
444 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
445 CurModule.PlaceHolderInfo.find(V);
446 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
447
448 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000449
Chris Lattner735f2702004-07-08 22:30:50 +0000450 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000451 if (TheRealValue) {
452 V->replaceAllUsesWith(TheRealValue);
453 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000454 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000455 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000456 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000457 // resolver table
458 InsertValue(V, *FutureLateResolvers);
459 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +0000460 if (DID.Type == ValID::NameVal)
461 ThrowException("Reference to an invalid definition: '" +DID.getName()+
462 "' of type '" + V->getType()->getDescription() + "'",
463 PHI->second.second);
464 else
465 ThrowException("Reference to an invalid definition: #" +
Misha Brukman5a2a3822005-05-10 22:02:28 +0000466 itostr(DID.Num) + " of type '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000467 V->getType()->getDescription() + "'",
468 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000469 }
Chris Lattner00950542001-06-06 20:29:01 +0000470 }
471 }
472
473 LateResolvers.clear();
474}
475
Chris Lattner4a42e902001-10-22 05:56:09 +0000476// ResolveTypeTo - A brand new type was just declared. This means that (if
477// name is not null) things referencing Name can be resolved. Otherwise, things
478// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000479//
Chris Lattner4a42e902001-10-22 05:56:09 +0000480static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner355ad1f2005-03-21 06:27:42 +0000481 ValID D;
482 if (Name) D = ValID::create(Name);
483 else D = ValID::create((int)CurModule.Types.size());
Chris Lattner00950542001-06-06 20:29:01 +0000484
Chris Lattner355ad1f2005-03-21 06:27:42 +0000485 std::map<ValID, PATypeHolder>::iterator I =
486 CurModule.LateResolveTypes.find(D);
487 if (I != CurModule.LateResolveTypes.end()) {
488 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
489 CurModule.LateResolveTypes.erase(I);
Chris Lattner30c89792001-09-07 16:35:17 +0000490 }
491}
492
Chris Lattner1781aca2001-09-18 04:00:54 +0000493// setValueName - Set the specified value to the name given. The name may be
494// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000495// assumed to be a malloc'd string buffer, and is free'd by this function.
496//
497static void setValueName(Value *V, char *NameStr) {
498 if (NameStr) {
499 std::string Name(NameStr); // Copy string
500 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000501
Misha Brukman5a2a3822005-05-10 22:02:28 +0000502 if (V->getType() == Type::VoidTy)
Chris Lattnerc9aea522004-07-13 08:12:39 +0000503 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000504
Chris Lattner8ab406d2004-07-13 07:59:27 +0000505 assert(inFunctionScope() && "Must be in function scope!");
506 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
507 if (ST.lookup(V->getType(), Name))
508 ThrowException("Redefinition of value named '" + Name + "' in the '" +
509 V->getType()->getDescription() + "' type plane!");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000510
Chris Lattnerc9aea522004-07-13 08:12:39 +0000511 // Set the name.
Chris Lattner262bb9a2005-03-05 19:04:07 +0000512 V->setName(Name);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000513 }
514}
515
Chris Lattnerd88998d2004-07-14 08:23:52 +0000516/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
517/// this is a declaration, otherwise it is a definition.
518static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
519 bool isConstantGlobal, const Type *Ty,
Chris Lattner164c3782005-11-12 00:11:10 +0000520 Constant *Initializer, char *Section,
521 unsigned Align) {
Chris Lattner87ac9722005-11-06 06:46:28 +0000522 if (Align != 0 && !isPowerOf2_32(Align))
523 ThrowException("Global alignment must be a power of two!");
524
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000525 if (isa<FunctionType>(Ty))
526 ThrowException("Cannot declare global vars of function type!");
527
Misha Brukman5a2a3822005-05-10 22:02:28 +0000528 const PointerType *PTy = PointerType::get(Ty);
Chris Lattnera09000d2004-07-14 23:03:46 +0000529
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000530 std::string Name;
531 if (NameStr) {
532 Name = NameStr; // Copy string
533 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000534 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000535
Chris Lattner8a327842004-07-14 21:44:00 +0000536 // See if this global value was forward referenced. If so, recycle the
537 // object.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000538 ValID ID;
Chris Lattner8a327842004-07-14 21:44:00 +0000539 if (!Name.empty()) {
540 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000541 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000542 ID = ValID::create((int)CurModule.Values[PTy].size());
543 }
544
545 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000546 // Move the global to the end of the list, from whereever it was
Chris Lattner8a327842004-07-14 21:44:00 +0000547 // previously inserted.
548 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
549 CurModule.CurrentModule->getGlobalList().remove(GV);
550 CurModule.CurrentModule->getGlobalList().push_back(GV);
551 GV->setInitializer(Initializer);
552 GV->setLinkage(Linkage);
553 GV->setConstant(isConstantGlobal);
Chris Lattner87ac9722005-11-06 06:46:28 +0000554 GV->setAlignment(Align);
Chris Lattner164c3782005-11-12 00:11:10 +0000555 if (Section) {
556 free(Section);
557 GV->setSection(Section);
558 }
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000559 InsertValue(GV, CurModule.Values);
Chris Lattnera09000d2004-07-14 23:03:46 +0000560 return;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000561 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000562
563 // If this global has a name, check to see if there is already a definition
564 // of this global in the module. If so, merge as appropriate. Note that
565 // this is really just a hack around problems in the CFE. :(
566 if (!Name.empty()) {
567 // We are a simple redefinition of a value, check to see if it is defined
568 // the same as the old one.
Misha Brukman5a2a3822005-05-10 22:02:28 +0000569 if (GlobalVariable *EGV =
Chris Lattnera09000d2004-07-14 23:03:46 +0000570 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
571 // We are allowed to redefine a global variable in two circumstances:
Misha Brukman5a2a3822005-05-10 22:02:28 +0000572 // 1. If at least one of the globals is uninitialized or
Chris Lattnera09000d2004-07-14 23:03:46 +0000573 // 2. If both initializers have the same value.
574 //
575 if (!EGV->hasInitializer() || !Initializer ||
576 EGV->getInitializer() == Initializer) {
577
578 // Make sure the existing global version gets the initializer! Make
579 // sure that it also gets marked const if the new version is.
580 if (Initializer && !EGV->hasInitializer())
581 EGV->setInitializer(Initializer);
582 if (isConstantGlobal)
583 EGV->setConstant(true);
584 EGV->setLinkage(Linkage);
Chris Lattner87ac9722005-11-06 06:46:28 +0000585 EGV->setAlignment(Align);
Chris Lattner164c3782005-11-12 00:11:10 +0000586 if (Section) {
587 free(Section);
588 EGV->setSection(Section);
589 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000590 return;
591 }
592
Misha Brukman5a2a3822005-05-10 22:02:28 +0000593 ThrowException("Redefinition of global variable named '" + Name +
Chris Lattnera09000d2004-07-14 23:03:46 +0000594 "' in the '" + Ty->getDescription() + "' type plane!");
595 }
596 }
597
598 // Otherwise there is no existing GV to use, create one now.
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000599 GlobalVariable *GV =
Misha Brukman5a2a3822005-05-10 22:02:28 +0000600 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000601 CurModule.CurrentModule);
Chris Lattner87ac9722005-11-06 06:46:28 +0000602 GV->setAlignment(Align);
Chris Lattner164c3782005-11-12 00:11:10 +0000603 if (Section) {
604 free(Section);
605 GV->setSection(Section);
606 }
Chris Lattnera2d4b3c2004-07-16 01:18:09 +0000607 InsertValue(GV, CurModule.Values);
Chris Lattnerd88998d2004-07-14 08:23:52 +0000608}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000609
Reid Spencer75719bf2004-05-26 21:48:31 +0000610// setTypeName - Set the specified type to the name given. The name may be
611// null potentially, in which case this is a noop. The string passed in is
612// assumed to be a malloc'd string buffer, and is freed by this function.
613//
614// This function returns true if the type has already been defined, but is
615// allowed to be redefined in the specified context. If the name is a new name
616// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000617static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000618 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000619 if (NameStr == 0) return false;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000620
Reid Spencer75719bf2004-05-26 21:48:31 +0000621 std::string Name(NameStr); // Copy string
622 free(NameStr); // Free old string
623
624 // We don't allow assigning names to void type
Misha Brukman5a2a3822005-05-10 22:02:28 +0000625 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000626 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000627
Chris Lattnera09000d2004-07-14 23:03:46 +0000628 // Set the type name, checking for conflicts as we do so.
629 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000630
Chris Lattnera09000d2004-07-14 23:03:46 +0000631 if (AlreadyExists) { // Inserting a name that is already defined???
632 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
633 assert(Existing && "Conflict but no matching type?");
634
Reid Spencer75719bf2004-05-26 21:48:31 +0000635 // There is only one case where this is allowed: when we are refining an
636 // opaque type. In this case, Existing will be an opaque type.
637 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
638 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000639 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000640 return true;
641 }
642
643 // Otherwise, this is an attempt to redefine a type. That's okay if
644 // the redefinition is identical to the original. This will be so if
645 // Existing and T point to the same Type object. In this one case we
646 // allow the equivalent redefinition.
647 if (Existing == T) return true; // Yes, it's equal.
648
649 // Any other kind of (non-equivalent) redefinition is an error.
650 ThrowException("Redefinition of type named '" + Name + "' in the '" +
Reid Spencer3271ed52004-07-18 00:08:11 +0000651 T->getDescription() + "' type plane!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000652 }
653
Reid Spencer75719bf2004-05-26 21:48:31 +0000654 return false;
655}
Chris Lattner8896eda2001-07-09 19:38:36 +0000656
Chris Lattner30c89792001-09-07 16:35:17 +0000657//===----------------------------------------------------------------------===//
658// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000659//
Chris Lattner8896eda2001-07-09 19:38:36 +0000660
Chris Lattner3b073862004-02-09 03:19:29 +0000661// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000662//
663static bool TypeContains(const Type *Ty, const Type *E) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000664 return std::find(Ty->subtype_begin(), Ty->subtype_end(),
Chris Lattner6d8dbec2004-12-08 16:13:53 +0000665 E) != Ty->subtype_end();
Chris Lattner3b073862004-02-09 03:19:29 +0000666}
667
668namespace {
669 struct UpRefRecord {
670 // NestingLevel - The number of nesting levels that need to be popped before
671 // this type is resolved.
672 unsigned NestingLevel;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000673
Chris Lattner3b073862004-02-09 03:19:29 +0000674 // LastContainedTy - This is the type at the current binding level for the
675 // type. Every time we reduce the nesting level, this gets updated.
676 const Type *LastContainedTy;
677
678 // UpRefTy - This is the actual opaque type that the upreference is
679 // represented with.
680 OpaqueType *UpRefTy;
681
682 UpRefRecord(unsigned NL, OpaqueType *URTy)
683 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
684 };
Chris Lattner30c89792001-09-07 16:35:17 +0000685}
Chris Lattner698b56e2001-07-20 19:15:08 +0000686
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000687// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000688static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000689
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000690/// HandleUpRefs - Every time we finish a new layer of types, this function is
691/// called. It loops through the UpRefs vector, which is a list of the
692/// currently active types. For each type, if the up reference is contained in
693/// the newly completed type, we decrement the level count. When the level
694/// count reaches zero, the upreferenced type is the type that is passed in:
695/// thus we can complete the cycle.
696///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000697static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000698 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000699 PATypeHolder Ty(ty);
Misha Brukman5a2a3822005-05-10 22:02:28 +0000700 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000701 "' newly formed. Resolving upreferences.\n" <<
702 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000703
704 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
705 // to zero), we resolve them all together before we resolve them to Ty. At
706 // the end of the loop, if there is anything to resolve to Ty, it will be in
707 // this variable.
708 OpaqueType *TypeToResolve = 0;
709
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000710 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Misha Brukman5a2a3822005-05-10 22:02:28 +0000711 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
712 << UpRefs[i].second->getDescription() << ") = "
Reid Spencer3271ed52004-07-18 00:08:11 +0000713 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000714 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
715 // Decrement level of upreference
716 unsigned Level = --UpRefs[i].NestingLevel;
717 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000718 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Misha Brukman5a2a3822005-05-10 22:02:28 +0000719 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000720 if (!TypeToResolve) {
721 TypeToResolve = UpRefs[i].UpRefTy;
722 } else {
723 UR_OUT(" * Resolving upreference for "
724 << UpRefs[i].second->getDescription() << "\n";
725 std::string OldName = UpRefs[i].UpRefTy->getDescription());
726 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
727 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
728 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
729 }
Reid Spencer3271ed52004-07-18 00:08:11 +0000730 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000731 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000732 }
733 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000734 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000735
Chris Lattner026a8ce2004-02-09 18:53:54 +0000736 if (TypeToResolve) {
737 UR_OUT(" * Resolving upreference for "
738 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000739 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000740 TypeToResolve->refineAbstractTypeTo(Ty);
741 }
742
Chris Lattner8896eda2001-07-09 19:38:36 +0000743 return Ty;
744}
745
Chris Lattner30c89792001-09-07 16:35:17 +0000746
Chris Lattner6184feb2005-05-20 03:25:47 +0000747// common code from the two 'RunVMAsmParser' functions
748 static Module * RunParser(Module * M) {
749
750 llvmAsmlineno = 1; // Reset the current line number...
Andrew Lenharth558bc882005-06-18 18:34:52 +0000751 ObsoleteVarArgs = false;
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000752 NewVarArgs = false;
Chris Lattner6184feb2005-05-20 03:25:47 +0000753
754 CurModule.CurrentModule = M;
755 yyparse(); // Parse the file, potentially throwing exception
756
757 Module *Result = ParserResult;
758 ParserResult = 0;
759
Andrew Lenharth31c98bf2005-06-20 15:41:37 +0000760 //Not all functions use vaarg, so make a second check for ObsoleteVarArgs
761 {
762 Function* F;
763 if ((F = Result->getNamedFunction("llvm.va_start"))
764 && F->getFunctionType()->getNumParams() == 0)
765 ObsoleteVarArgs = true;
766 if((F = Result->getNamedFunction("llvm.va_copy"))
767 && F->getFunctionType()->getNumParams() == 1)
768 ObsoleteVarArgs = true;
769 }
770
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000771 if (ObsoleteVarArgs && NewVarArgs)
Chris Lattner548021f2005-06-24 18:00:40 +0000772 ThrowException("This file is corrupt: it uses both new and old style varargs");
Andrew Lenharthe78f50d2005-06-19 03:53:56 +0000773
Andrew Lenharth558bc882005-06-18 18:34:52 +0000774 if(ObsoleteVarArgs) {
775 if(Function* F = Result->getNamedFunction("llvm.va_start")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000776 if (F->arg_size() != 0)
777 ThrowException("Obsolete va_start takes 0 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000778
779 //foo = va_start()
780 // ->
781 //bar = alloca typeof(foo)
782 //va_start(bar)
783 //foo = load bar
784
785 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
786 const Type* ArgTy = F->getFunctionType()->getReturnType();
787 const Type* ArgTyPtr = PointerType::get(ArgTy);
788 Function* NF = Result->getOrInsertFunction("llvm.va_start",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000789 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000790
791 while (!F->use_empty()) {
792 CallInst* CI = cast<CallInst>(F->use_back());
793 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI);
794 new CallInst(NF, bar, "", CI);
795 Value* foo = new LoadInst(bar, "vastart.fix.2", CI);
796 CI->replaceAllUsesWith(foo);
797 CI->getParent()->getInstList().erase(CI);
798 }
799 Result->getFunctionList().erase(F);
800 }
801
802 if(Function* F = Result->getNamedFunction("llvm.va_end")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000803 if(F->arg_size() != 1)
804 ThrowException("Obsolete va_end takes 1 argument!");
805
Andrew Lenharth558bc882005-06-18 18:34:52 +0000806 //vaend foo
807 // ->
808 //bar = alloca 1 of typeof(foo)
809 //vaend bar
810 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
811 const Type* ArgTy = F->getFunctionType()->getParamType(0);
812 const Type* ArgTyPtr = PointerType::get(ArgTy);
813 Function* NF = Result->getOrInsertFunction("llvm.va_end",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000814 RetTy, ArgTyPtr, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000815
816 while (!F->use_empty()) {
817 CallInst* CI = cast<CallInst>(F->use_back());
818 AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI);
Andrew Lenharth017fba92005-06-19 14:04:55 +0000819 new StoreInst(CI->getOperand(1), bar, CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000820 new CallInst(NF, bar, "", CI);
821 CI->getParent()->getInstList().erase(CI);
822 }
823 Result->getFunctionList().erase(F);
824 }
825
826 if(Function* F = Result->getNamedFunction("llvm.va_copy")) {
Andrew Lenharth213e5572005-06-22 21:04:42 +0000827 if(F->arg_size() != 1)
828 ThrowException("Obsolete va_copy takes 1 argument!");
Andrew Lenharth558bc882005-06-18 18:34:52 +0000829 //foo = vacopy(bar)
830 // ->
831 //a = alloca 1 of typeof(foo)
Andrew Lenharth213e5572005-06-22 21:04:42 +0000832 //b = alloca 1 of typeof(foo)
833 //store bar -> b
834 //vacopy(a, b)
Andrew Lenharth558bc882005-06-18 18:34:52 +0000835 //foo = load a
836
837 const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID);
838 const Type* ArgTy = F->getFunctionType()->getReturnType();
839 const Type* ArgTyPtr = PointerType::get(ArgTy);
840 Function* NF = Result->getOrInsertFunction("llvm.va_copy",
Jeff Cohen66c5fd62005-10-23 04:37:20 +0000841 RetTy, ArgTyPtr, ArgTyPtr,
842 (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000843
844 while (!F->use_empty()) {
845 CallInst* CI = cast<CallInst>(F->use_back());
846 AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI);
Andrew Lenharth213e5572005-06-22 21:04:42 +0000847 AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI);
848 new StoreInst(CI->getOperand(1), b, CI);
849 new CallInst(NF, a, b, "", CI);
850 Value* foo = new LoadInst(a, "vacopy.fix.3", CI);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000851 CI->replaceAllUsesWith(foo);
852 CI->getParent()->getInstList().erase(CI);
853 }
854 Result->getFunctionList().erase(F);
855 }
856 }
857
Chris Lattner6184feb2005-05-20 03:25:47 +0000858 return Result;
859
860 }
861
Chris Lattner00950542001-06-06 20:29:01 +0000862//===----------------------------------------------------------------------===//
863// RunVMAsmParser - Define an interface to this parser
864//===----------------------------------------------------------------------===//
865//
Chris Lattner86427632004-07-14 06:39:48 +0000866Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner6184feb2005-05-20 03:25:47 +0000867 set_scan_file(F);
868
Chris Lattnera2850432001-07-22 18:36:00 +0000869 CurFilename = Filename;
Chris Lattner6184feb2005-05-20 03:25:47 +0000870 return RunParser(new Module(CurFilename));
871}
Chris Lattner00950542001-06-06 20:29:01 +0000872
Chris Lattner6184feb2005-05-20 03:25:47 +0000873Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) {
874 set_scan_string(AsmString);
Chris Lattner42570982003-11-26 07:24:58 +0000875
Chris Lattner6184feb2005-05-20 03:25:47 +0000876 CurFilename = "from_memory";
877 if (M == NULL) {
878 return RunParser(new Module (CurFilename));
879 } else {
880 return RunParser(M);
881 }
Chris Lattner00950542001-06-06 20:29:01 +0000882}
883
884%}
885
886%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000887 llvm::Module *ModuleVal;
888 llvm::Function *FunctionVal;
889 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
890 llvm::BasicBlock *BasicBlockVal;
891 llvm::TerminatorInst *TermInstVal;
892 llvm::Instruction *InstVal;
893 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000894
Brian Gaeked0fde302003-11-11 22:41:34 +0000895 const llvm::Type *PrimType;
896 llvm::PATypeHolder *TypeVal;
897 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000898
Brian Gaeked0fde302003-11-11 22:41:34 +0000899 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
900 std::vector<llvm::Value*> *ValueList;
901 std::list<llvm::PATypeHolder> *TypeList;
Misha Brukman5a2a3822005-05-10 22:02:28 +0000902 // Represent the RHS of PHI node
Brian Gaeked0fde302003-11-11 22:41:34 +0000903 std::list<std::pair<llvm::Value*,
Misha Brukman5a2a3822005-05-10 22:02:28 +0000904 llvm::BasicBlock*> > *PHIList;
Brian Gaeked0fde302003-11-11 22:41:34 +0000905 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
906 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000907
Brian Gaeked0fde302003-11-11 22:41:34 +0000908 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000909 int64_t SInt64Val;
910 uint64_t UInt64Val;
911 int SIntVal;
912 unsigned UIntVal;
913 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000914 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000915
Chris Lattner30c89792001-09-07 16:35:17 +0000916 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000917 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000918
Brian Gaeked0fde302003-11-11 22:41:34 +0000919 llvm::Instruction::BinaryOps BinaryOpVal;
920 llvm::Instruction::TermOps TermOpVal;
921 llvm::Instruction::MemoryOps MemOpVal;
922 llvm::Instruction::OtherOps OtherOpVal;
923 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000924}
925
Chris Lattner79df7c02002-03-26 18:01:55 +0000926%type <ModuleVal> Module FunctionList
927%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000928%type <BasicBlockVal> BasicBlock InstructionList
929%type <TermInstVal> BBTerminatorInst
930%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000931%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000932%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000933%type <ArgList> ArgList ArgListH
934%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000935%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000936%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000937%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000938%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000939%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000940%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000941%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattnerddb6db42005-05-06 05:51:46 +0000942%type <BoolVal> OptTailCall // TAIL CALL or plain CALL.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000943%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000944%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000945
Chris Lattner2079fde2001-10-13 06:41:08 +0000946// ValueRef - Unresolved reference to a definition or BB
947%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000948%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000949// Tokens and types for handling constant integer values
950//
951// ESINT64VAL - A negative number within long long range
952%token <SInt64Val> ESINT64VAL
953
954// EUINT64VAL - A positive number within uns. long long range
955%token <UInt64Val> EUINT64VAL
956%type <SInt64Val> EINT64VAL
957
958%token <SIntVal> SINTVAL // Signed 32 bit ints...
959%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
960%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000961%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000962
963// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000964%type <TypeVal> Types TypesV UpRTypes UpRTypesV
965%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000966%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
967%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000968
Chris Lattner6c23f572003-08-22 05:42:10 +0000969%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
970%type <StrVal> Name OptName OptAssign
Chris Lattner87ac9722005-11-06 06:46:28 +0000971%type <UIntVal> OptAlign OptCAlign
Chris Lattner164c3782005-11-12 00:11:10 +0000972%type <StrVal> OptSection OptCSection SectionString
Chris Lattner00950542001-06-06 20:29:01 +0000973
Chris Lattner91ef4602004-03-31 03:49:47 +0000974%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner164c3782005-11-12 00:11:10 +0000975%token DECLARE GLOBAL CONSTANT SECTION VOLATILE
Chris Lattner16710e92004-10-16 18:17:13 +0000976%token TO DOTDOTDOT NULL_TOK UNDEF CONST INTERNAL LINKONCE WEAK APPENDING
Nate Begeman14b05292005-11-05 09:21:28 +0000977%token OPAQUE NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG ALIGN
Chris Lattnerddb6db42005-05-06 05:51:46 +0000978%token DEPLIBS CALL TAIL
Chris Lattnera8e8f162005-05-06 20:27:19 +0000979%token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK
980%type <UIntVal> OptCallingConv
Chris Lattner00950542001-06-06 20:29:01 +0000981
Misha Brukman5a2a3822005-05-10 22:02:28 +0000982// Basic Block Terminating Operators
Chris Lattner16710e92004-10-16 18:17:13 +0000983%token <TermOpVal> RET BR SWITCH INVOKE UNWIND UNREACHABLE
Chris Lattner00950542001-06-06 20:29:01 +0000984
Misha Brukman5a2a3822005-05-10 22:02:28 +0000985// Binary Operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000986%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000987%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000988%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000989
990// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000991%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000992
Chris Lattner027dcc52001-07-08 21:10:27 +0000993// Other Operators
994%type <OtherOpVal> ShiftOps
Andrew Lenharth558bc882005-06-18 18:34:52 +0000995%token <OtherOpVal> PHI_TOK CAST SELECT SHL SHR VAARG
996%token VAARG_old VANEXT_old //OBSOLETE
Chris Lattnerddb6db42005-05-06 05:51:46 +0000997
Chris Lattner027dcc52001-07-08 21:10:27 +0000998
Chris Lattner00950542001-06-06 20:29:01 +0000999%start Module
1000%%
1001
1002// Handle constant integer size restriction and conversion...
1003//
Chris Lattner51727be2002-06-04 21:58:56 +00001004INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +00001005INTVAL : UINTVAL {
1006 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
1007 ThrowException("Value too large for type!");
1008 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +00001009};
Chris Lattner00950542001-06-06 20:29:01 +00001010
1011
Chris Lattner51727be2002-06-04 21:58:56 +00001012EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +00001013EINT64VAL : EUINT64VAL {
1014 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
1015 ThrowException("Value too large for type!");
1016 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +00001017};
Chris Lattner00950542001-06-06 20:29:01 +00001018
Misha Brukman5a2a3822005-05-10 22:02:28 +00001019// Operations that are notably excluded from this list include:
Chris Lattner00950542001-06-06 20:29:01 +00001020// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1021//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001022ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1023LogicalOps : AND | OR | XOR;
1024SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Chris Lattner4a6482b2002-09-10 19:57:26 +00001025
Chris Lattner51727be2002-06-04 21:58:56 +00001026ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001027
Chris Lattnere98dda62001-07-14 06:10:16 +00001028// These are some types that allow classification if we only want a particular
1029// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001030SIntType : LONG | INT | SHORT | SBYTE;
1031UIntType : ULONG | UINT | USHORT | UBYTE;
1032IntType : SIntType | UIntType;
1033FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001034
Chris Lattnere98dda62001-07-14 06:10:16 +00001035// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001036OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001037 $$ = $1;
1038 }
Misha Brukman5a2a3822005-05-10 22:02:28 +00001039 | /*empty*/ {
1040 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001041 };
Chris Lattner00950542001-06-06 20:29:01 +00001042
Chris Lattner4ad02e72003-04-16 20:28:45 +00001043OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1044 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001045 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001046 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1047 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001048
Chris Lattnera8e8f162005-05-06 20:27:19 +00001049OptCallingConv : /*empty*/ { $$ = CallingConv::C; } |
1050 CCC_TOK { $$ = CallingConv::C; } |
1051 FASTCC_TOK { $$ = CallingConv::Fast; } |
1052 COLDCC_TOK { $$ = CallingConv::Cold; } |
1053 CC_TOK EUINT64VAL {
1054 if ((unsigned)$2 != $2)
1055 ThrowException("Calling conv too large!");
1056 $$ = $2;
1057 };
1058
Chris Lattner66db8e42005-11-06 06:34:12 +00001059// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1060// a comma before it.
Chris Lattner87ac9722005-11-06 06:46:28 +00001061OptAlign : /*empty*/ { $$ = 0; } |
1062 ALIGN EUINT64VAL { $$ = $2; };
Chris Lattner66db8e42005-11-06 06:34:12 +00001063OptCAlign : /*empty*/ { $$ = 0; } |
1064 ',' ALIGN EUINT64VAL { $$ = $3; };
1065
Chris Lattner164c3782005-11-12 00:11:10 +00001066SectionString : SECTION STRINGCONSTANT {
1067 for (unsigned i = 0, e = strlen($2); i != e; ++i)
1068 if ($2[i] == '"' || $2[i] == '\\')
1069 ThrowException("Invalid character in section name!");
1070 $$ = $2;
1071};
1072
1073OptSection : /*empty*/ { $$ = 0; } |
1074 SectionString { $$ = $1; };
1075OptCSection : /*empty*/ { $$ = 0; } |
1076 ',' SectionString { $$ = $2; };
1077
1078
Chris Lattner30c89792001-09-07 16:35:17 +00001079//===----------------------------------------------------------------------===//
1080// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001081// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001082// access to it, a user must explicitly use TypesV.
1083//
1084
1085// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001086TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1087UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001088
1089Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001090 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001091 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1092 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001093 };
Chris Lattner30c89792001-09-07 16:35:17 +00001094
1095
1096// Derived types are added later...
1097//
Chris Lattner51727be2002-06-04 21:58:56 +00001098PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1099PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001100UpRTypes : OPAQUE {
1101 $$ = new PATypeHolder(OpaqueType::get());
1102 }
1103 | PrimType {
1104 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001105 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001106UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001107 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001108};
Chris Lattner30c89792001-09-07 16:35:17 +00001109
Chris Lattner30c89792001-09-07 16:35:17 +00001110// Include derived types in the Types production.
1111//
1112UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001113 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001114 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001115 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001116 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001117 UR_OUT("New Upreference!\n");
1118 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001119 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001120 std::vector<const Type*> Params;
Chris Lattnera08976b2005-02-22 23:13:58 +00001121 for (std::list<llvm::PATypeHolder>::iterator I = $3->begin(),
1122 E = $3->end(); I != E; ++I)
1123 Params.push_back(*I);
Chris Lattner2079fde2001-10-13 06:41:08 +00001124 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1125 if (isVarArg) Params.pop_back();
1126
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001127 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001128 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001129 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001130 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001131 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001132 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001133 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001134 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001135 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1136 const llvm::Type* ElemTy = $4->get();
Chris Lattner9547d7f2005-11-10 01:42:43 +00001137 if ((unsigned)$2 != $2)
Brian Gaeke715c90b2004-08-20 06:00:58 +00001138 ThrowException("Unsigned result not equal to signed result");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001139 if (!ElemTy->isPrimitiveType())
Brian Gaeke715c90b2004-08-20 06:00:58 +00001140 ThrowException("Elemental type of a PackedType must be primitive");
Chris Lattner9547d7f2005-11-10 01:42:43 +00001141 if (!isPowerOf2_32($2))
1142 ThrowException("Vector length should be a power of 2!");
Brian Gaeke715c90b2004-08-20 06:00:58 +00001143 $$ = new PATypeHolder(HandleUpRefs(PackedType::get(*$4, (unsigned)$2)));
1144 delete $4;
1145 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001146 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001147 std::vector<const Type*> Elements;
Chris Lattnera08976b2005-02-22 23:13:58 +00001148 for (std::list<llvm::PATypeHolder>::iterator I = $2->begin(),
1149 E = $2->end(); I != E; ++I)
1150 Elements.push_back(*I);
Misha Brukman5a2a3822005-05-10 22:02:28 +00001151
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001152 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001153 delete $2;
1154 }
1155 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001156 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001157 }
1158 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001159 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001160 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001161 };
Chris Lattner30c89792001-09-07 16:35:17 +00001162
Chris Lattner7e708292002-06-25 16:13:24 +00001163// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001164// declaration type lists
1165//
1166TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001167 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001168 $$->push_back(*$1); delete $1;
1169 }
1170 | TypeListI ',' UpRTypes {
1171 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001172 };
Chris Lattner30c89792001-09-07 16:35:17 +00001173
Chris Lattner7e708292002-06-25 16:13:24 +00001174// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001175ArgTypeListI : TypeListI
1176 | TypeListI ',' DOTDOTDOT {
1177 ($$=$1)->push_back(Type::VoidTy);
1178 }
1179 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001180 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001181 }
1182 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001183 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001184 };
Chris Lattner30c89792001-09-07 16:35:17 +00001185
Chris Lattnere98dda62001-07-14 06:10:16 +00001186// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001187// production is used ONLY to represent constants that show up AFTER a 'const',
1188// 'constant' or 'global' token at global scope. Constants that can be inlined
1189// into other expressions (such as integers and constexprs) are handled by the
1190// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001191//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001192ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001193 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001194 if (ATy == 0)
1195 ThrowException("Cannot make array constant with type: '" +
1196 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001197 const Type *ETy = ATy->getElementType();
1198 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001199
Chris Lattner30c89792001-09-07 16:35:17 +00001200 // Verify that we have the correct size...
1201 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001202 ThrowException("Type mismatch: constant sized array initialized with " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001203 utostr($3->size()) + " arguments, but has size of " +
1204 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001205
Chris Lattner30c89792001-09-07 16:35:17 +00001206 // Verify all elements are correct type!
1207 for (unsigned i = 0; i < $3->size(); i++) {
1208 if (ETy != (*$3)[i]->getType())
Reid Spencer3271ed52004-07-18 00:08:11 +00001209 ThrowException("Element #" + utostr(i) + " is not of type '" +
1210 ETy->getDescription() +"' as required!\nIt is of type '"+
1211 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001212 }
1213
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001214 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001215 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001216 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001217 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001218 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001219 if (ATy == 0)
1220 ThrowException("Cannot make array constant with type: '" +
1221 (*$1)->getDescription() + "'!");
1222
1223 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001224 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001225 ThrowException("Type mismatch: constant sized array initialized with 0"
Reid Spencer3271ed52004-07-18 00:08:11 +00001226 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001227 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001228 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001229 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001230 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001231 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001232 if (ATy == 0)
1233 ThrowException("Cannot make array constant with type: '" +
1234 (*$1)->getDescription() + "'!");
1235
Chris Lattner30c89792001-09-07 16:35:17 +00001236 int NumElements = ATy->getNumElements();
1237 const Type *ETy = ATy->getElementType();
1238 char *EndStr = UnEscapeLexed($3, true);
1239 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001240 ThrowException("Can't build string constant of size " +
Reid Spencer3271ed52004-07-18 00:08:11 +00001241 itostr((int)(EndStr-$3)) +
1242 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001243 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001244 if (ETy == Type::SByteTy) {
1245 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001246 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001247 } else if (ETy == Type::UByteTy) {
1248 for (char *C = $3; C != EndStr; ++C)
Reid Spencer3271ed52004-07-18 00:08:11 +00001249 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001250 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001251 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001252 ThrowException("Cannot build string arrays of non byte sized elements!");
1253 }
Chris Lattner30c89792001-09-07 16:35:17 +00001254 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001255 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001256 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001257 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001258 | Types '<' ConstVector '>' { // Nonempty unsized arr
1259 const PackedType *PTy = dyn_cast<PackedType>($1->get());
1260 if (PTy == 0)
1261 ThrowException("Cannot make packed constant with type: '" +
1262 (*$1)->getDescription() + "'!");
1263 const Type *ETy = PTy->getElementType();
1264 int NumElements = PTy->getNumElements();
1265
1266 // Verify that we have the correct size...
1267 if (NumElements != -1 && NumElements != (int)$3->size())
1268 ThrowException("Type mismatch: constant sized packed initialized with " +
1269 utostr($3->size()) + " arguments, but has size of " +
1270 itostr(NumElements) + "!");
1271
1272 // Verify all elements are correct type!
1273 for (unsigned i = 0; i < $3->size(); i++) {
1274 if (ETy != (*$3)[i]->getType())
1275 ThrowException("Element #" + utostr(i) + " is not of type '" +
1276 ETy->getDescription() +"' as required!\nIt is of type '"+
1277 (*$3)[i]->getType()->getDescription() + "'.");
1278 }
1279
1280 $$ = ConstantPacked::get(PTy, *$3);
1281 delete $1; delete $3;
1282 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001283 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001284 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001285 if (STy == 0)
1286 ThrowException("Cannot make struct constant with type: '" +
1287 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001288
Chris Lattnerc6212f12003-05-21 16:06:56 +00001289 if ($3->size() != STy->getNumContainedTypes())
1290 ThrowException("Illegal number of initializers for structure type!");
1291
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001292 // Check to ensure that constants are compatible with the type initializer!
1293 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001294 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001295 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001296 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001297 "' for element #" + utostr(i) +
1298 " of structure initializer!");
1299
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001300 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001301 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001302 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001303 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001304 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001305 if (STy == 0)
1306 ThrowException("Cannot make struct constant with type: '" +
1307 (*$1)->getDescription() + "'!");
1308
1309 if (STy->getNumContainedTypes() != 0)
1310 ThrowException("Illegal number of initializers for structure type!");
1311
1312 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1313 delete $1;
1314 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001315 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001316 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001317 if (PTy == 0)
1318 ThrowException("Cannot make null pointer constant with type: '" +
1319 (*$1)->getDescription() + "'!");
1320
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001321 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001322 delete $1;
1323 }
Chris Lattner16710e92004-10-16 18:17:13 +00001324 | Types UNDEF {
1325 $$ = UndefValue::get($1->get());
1326 delete $1;
1327 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001328 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001329 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001330 if (Ty == 0)
1331 ThrowException("Global const reference must be a pointer type!");
1332
Chris Lattner3101c252002-08-15 17:58:33 +00001333 // ConstExprs can exist in the body of a function, thus creating
Reid Spencer3271ed52004-07-18 00:08:11 +00001334 // GlobalValues whenever they refer to a variable. Because we are in
Chris Lattner3101c252002-08-15 17:58:33 +00001335 // the context of a function, getValNonImprovising will search the functions
1336 // symbol table instead of the module symbol table for the global symbol,
1337 // which throws things all off. To get around this, we just tell
1338 // getValNonImprovising that we are at global scope here.
1339 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001340 Function *SavedCurFn = CurFun.CurrentFunction;
1341 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001342
Chris Lattner2079fde2001-10-13 06:41:08 +00001343 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001344
Chris Lattner394f2eb2003-10-16 20:12:13 +00001345 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001346
Chris Lattner2079fde2001-10-13 06:41:08 +00001347 // If this is an initializer for a constant pointer, which is referencing a
1348 // (currently) undefined variable, create a stub now that shall be replaced
1349 // in the future with the right type of variable.
1350 //
1351 if (V == 0) {
1352 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1353 const PointerType *PT = cast<PointerType>(Ty);
1354
1355 // First check to see if the forward references value is already created!
1356 PerModuleInfo::GlobalRefsType::iterator I =
Reid Spencer3271ed52004-07-18 00:08:11 +00001357 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001358
1359 if (I != CurModule.GlobalRefs.end()) {
Reid Spencer3271ed52004-07-18 00:08:11 +00001360 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001361 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001362 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001363 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001364 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001365
Reid Spencer3271ed52004-07-18 00:08:11 +00001366 // Create the forward referenced global.
Chris Lattnera09000d2004-07-14 23:03:46 +00001367 GlobalValue *GV;
1368 if (const FunctionType *FTy =
1369 dyn_cast<FunctionType>(PT->getElementType())) {
1370 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1371 CurModule.CurrentModule);
1372 } else {
Reid Spencer3271ed52004-07-18 00:08:11 +00001373 GV = new GlobalVariable(PT->getElementType(), false,
Chris Lattnera09000d2004-07-14 23:03:46 +00001374 GlobalValue::ExternalLinkage, 0,
1375 Name, CurModule.CurrentModule);
1376 }
Chris Lattner8a327842004-07-14 21:44:00 +00001377
Reid Spencer3271ed52004-07-18 00:08:11 +00001378 // Keep track of the fact that we have a forward ref to recycle it
1379 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
1380 V = GV;
Chris Lattner2079fde2001-10-13 06:41:08 +00001381 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001382 }
1383
Reid Spencer3271ed52004-07-18 00:08:11 +00001384 $$ = cast<GlobalValue>(V);
Chris Lattner2079fde2001-10-13 06:41:08 +00001385 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001386 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001387 | Types ConstExpr {
1388 if ($1->get() != $2->getType())
1389 ThrowException("Mismatched types for constant expression!");
1390 $$ = $2;
1391 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001392 }
1393 | Types ZEROINITIALIZER {
Chris Lattner78915932004-11-28 16:45:45 +00001394 const Type *Ty = $1->get();
1395 if (isa<FunctionType>(Ty) || Ty == Type::LabelTy || isa<OpaqueType>(Ty))
1396 ThrowException("Cannot create a null initialized value of this type!");
1397 $$ = Constant::getNullValue(Ty);
Chris Lattnerf4600482003-06-28 20:01:34 +00001398 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001399 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001400
Chris Lattnere43f40b2002-10-09 00:25:32 +00001401ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001402 if (!ConstantSInt::isValueValidForType($1, $2))
1403 ThrowException("Constant value doesn't fit in type!");
1404 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001405 }
1406 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001407 if (!ConstantUInt::isValueValidForType($1, $2))
1408 ThrowException("Constant value doesn't fit in type!");
1409 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001410 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001411 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001412 $$ = ConstantBool::True;
1413 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001414 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001415 $$ = ConstantBool::False;
1416 }
1417 | FPType FPVAL { // Float & Double constants
Reid Spencer9f9b3ac2004-12-06 22:18:25 +00001418 if (!ConstantFP::isValueValidForType($1, $2))
1419 ThrowException("Floating point constant invalid for type!!");
Chris Lattnerd05e3592002-08-15 18:17:28 +00001420 $$ = ConstantFP::get($1, $2);
1421 };
1422
Chris Lattner00950542001-06-06 20:29:01 +00001423
Chris Lattnerd78700d2002-08-16 21:14:40 +00001424ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001425 if (!$3->getType()->isFirstClassType())
1426 ThrowException("cast constant expression from a non-primitive type: '" +
1427 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001428 if (!$5->get()->isFirstClassType())
1429 ThrowException("cast constant expression to a non-primitive type: '" +
1430 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001431 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001432 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001433 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001434 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1435 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001436 ThrowException("GetElementPtr requires a pointer operand!");
1437
Chris Lattner830b6f92004-04-05 01:30:04 +00001438 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1439 // indices to uint struct indices for compatibility.
1440 generic_gep_type_iterator<std::vector<Value*>::iterator>
1441 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1442 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1443 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1444 if (isa<StructType>(*GTI)) // Only change struct indices
1445 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1446 if (CUI->getType() == Type::UByteTy)
1447 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1448
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001449 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001450 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001451 if (!IdxTy)
1452 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001453
Chris Lattner9232b992003-04-22 18:42:41 +00001454 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001455 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1456 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001457 IdxVec.push_back(C);
1458 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001459 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001460
Chris Lattnerd78700d2002-08-16 21:14:40 +00001461 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001462
Chris Lattnerd78700d2002-08-16 21:14:40 +00001463 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001464 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001465 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1466 if ($3->getType() != Type::BoolTy)
1467 ThrowException("Select condition must be of boolean type!");
1468 if ($5->getType() != $7->getType())
1469 ThrowException("Select operand types must match!");
1470 $$ = ConstantExpr::getSelect($3, $5, $7);
1471 }
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001472 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001473 if ($3->getType() != $5->getType())
1474 ThrowException("Binary operator types must match!");
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001475 // HACK: llvm 1.3 and earlier used to emit invalid pointer constant exprs.
1476 // To retain backward compatibility with these early compilers, we emit a
1477 // cast to the appropriate integer type automatically if we are in the
1478 // broken case. See PR424 for more information.
1479 if (!isa<PointerType>($3->getType())) {
1480 $$ = ConstantExpr::get($1, $3, $5);
1481 } else {
Chris Lattner42db1a02004-08-20 18:07:39 +00001482 const Type *IntPtrTy = 0;
Chris Lattnerc6d2f152004-08-17 17:26:41 +00001483 switch (CurModule.CurrentModule->getPointerSize()) {
1484 case Module::Pointer32: IntPtrTy = Type::IntTy; break;
1485 case Module::Pointer64: IntPtrTy = Type::LongTy; break;
1486 default: ThrowException("invalid pointer binary constant expr!");
1487 }
1488 $$ = ConstantExpr::get($1, ConstantExpr::getCast($3, IntPtrTy),
1489 ConstantExpr::getCast($5, IntPtrTy));
1490 $$ = ConstantExpr::getCast($$, $3->getType());
1491 }
1492 }
1493 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1494 if ($3->getType() != $5->getType())
1495 ThrowException("Logical operator types must match!");
1496 if (!$3->getType()->isIntegral())
1497 ThrowException("Logical operands must have integral types!");
1498 $$ = ConstantExpr::get($1, $3, $5);
1499 }
1500 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1501 if ($3->getType() != $5->getType())
1502 ThrowException("setcc operand types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001503 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001504 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001505 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001506 if ($5->getType() != Type::UByteTy)
1507 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001508 if (!$3->getType()->isInteger())
1509 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001510 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001511 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001512
1513
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001514// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001515ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001516 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001517 }
1518 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001519 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001520 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001521 };
Chris Lattner00950542001-06-06 20:29:01 +00001522
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001523
Chris Lattner1781aca2001-09-18 04:00:54 +00001524// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001525GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001526
Chris Lattner00950542001-06-06 20:29:01 +00001527
Chris Lattner0e73ce62002-05-02 19:11:13 +00001528//===----------------------------------------------------------------------===//
1529// Rules to match Modules
1530//===----------------------------------------------------------------------===//
1531
1532// Module rule: Capture the result of parsing the whole file into a result
1533// variable...
1534//
1535Module : FunctionList {
1536 $$ = ParserResult = $1;
1537 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001538};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001539
Chris Lattner7e708292002-06-25 16:13:24 +00001540// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001541//
1542FunctionList : FunctionList Function {
1543 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001544 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001545 }
1546 | FunctionList FunctionProto {
1547 $$ = $1;
1548 }
1549 | FunctionList IMPLEMENTATION {
1550 $$ = $1;
1551 }
1552 | ConstPool {
1553 $$ = CurModule.CurrentModule;
Chris Lattner355ad1f2005-03-21 06:27:42 +00001554 // Emit an error if there are any unresolved types left.
1555 if (!CurModule.LateResolveTypes.empty()) {
1556 const ValID &DID = CurModule.LateResolveTypes.begin()->first;
1557 if (DID.Type == ValID::NameVal)
1558 ThrowException("Reference to an undefined type: '"+DID.getName() + "'");
1559 else
1560 ThrowException("Reference to an undefined type: #" + itostr(DID.Num));
1561 }
Chris Lattner51727be2002-06-04 21:58:56 +00001562 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001563
Chris Lattnere98dda62001-07-14 06:10:16 +00001564// ConstPool - Constants with optional names assigned to them.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001565ConstPool : ConstPool OptAssign TYPE TypesV {
Chris Lattner4a42e902001-10-22 05:56:09 +00001566 // Eagerly resolve types. This is not an optimization, this is a
1567 // requirement that is due to the fact that we could have this:
1568 //
1569 // %list = type { %list * }
1570 // %list = type { %list * } ; repeated type decl
1571 //
1572 // If types are not resolved eagerly, then the two types will not be
1573 // determined to be the same type!
1574 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001575 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001576
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001577 if (!setTypeName(*$4, $2) && !$2) {
1578 // If this is a named type that is not a redefinition, add it to the slot
1579 // table.
Chris Lattner355ad1f2005-03-21 06:27:42 +00001580 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001581 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001582
1583 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001584 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001585 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001586 }
Chris Lattner164c3782005-11-12 00:11:10 +00001587 | ConstPool OptAssign OptLinkage GlobalType ConstVal OptCSection OptCAlign {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001588 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
Chris Lattner164c3782005-11-12 00:11:10 +00001589 ParseGlobalVariable($2, $3, $4, $5->getType(), $5, $6, $7);
Chris Lattner1781aca2001-09-18 04:00:54 +00001590 }
Chris Lattner164c3782005-11-12 00:11:10 +00001591 | ConstPool OptAssign EXTERNAL GlobalType Types OptCSection OptCAlign {
1592 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0, $6, $7);
Chris Lattner1f862af2003-04-16 18:13:57 +00001593 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001594 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001595 | ConstPool TARGET TargetDefinition {
1596 }
Reid Spencer0be13e72004-07-25 17:58:28 +00001597 | ConstPool DEPLIBS '=' LibrariesDefinition {
1598 }
Chris Lattner00950542001-06-06 20:29:01 +00001599 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001600 };
Chris Lattner00950542001-06-06 20:29:01 +00001601
1602
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001603
1604BigOrLittle : BIG { $$ = Module::BigEndian; };
1605BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1606
1607TargetDefinition : ENDIAN '=' BigOrLittle {
1608 CurModule.CurrentModule->setEndianness($3);
1609 }
1610 | POINTERSIZE '=' EUINT64VAL {
1611 if ($3 == 32)
1612 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1613 else if ($3 == 64)
1614 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1615 else
1616 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
Reid Spencer0be13e72004-07-25 17:58:28 +00001617 }
1618 | TRIPLE '=' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001619 CurModule.CurrentModule->setTargetTriple($3);
1620 free($3);
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001621 };
1622
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001623LibrariesDefinition : '[' LibList ']';
Reid Spencer0be13e72004-07-25 17:58:28 +00001624
1625LibList : LibList ',' STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001626 CurModule.CurrentModule->addLibrary($3);
1627 free($3);
Reid Spencer0be13e72004-07-25 17:58:28 +00001628 }
1629 | STRINGCONSTANT {
Reid Spencerfeaf10e2004-07-25 21:30:51 +00001630 CurModule.CurrentModule->addLibrary($1);
1631 free($1);
Reid Spencer0be13e72004-07-25 17:58:28 +00001632 }
1633 | /* empty: end of list */ {
1634 }
1635 ;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001636
Chris Lattner00950542001-06-06 20:29:01 +00001637//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001638// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001639//===----------------------------------------------------------------------===//
1640
Chris Lattner6c23f572003-08-22 05:42:10 +00001641Name : VAR_ID | STRINGCONSTANT;
1642OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001643
Chris Lattner6c23f572003-08-22 05:42:10 +00001644ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001645 if (*$1 == Type::VoidTy)
1646 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001647 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001648};
Chris Lattner00950542001-06-06 20:29:01 +00001649
Chris Lattner69da5cf2002-10-13 20:57:00 +00001650ArgListH : ArgListH ',' ArgVal {
1651 $$ = $1;
1652 $1->push_back(*$3);
1653 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001654 }
1655 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001656 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001657 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001658 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001659 };
Chris Lattner00950542001-06-06 20:29:01 +00001660
1661ArgList : ArgListH {
1662 $$ = $1;
1663 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001664 | ArgListH ',' DOTDOTDOT {
1665 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001666 $$->push_back(std::pair<PATypeHolder*,
1667 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001668 }
1669 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001670 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1671 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001672 }
Chris Lattner00950542001-06-06 20:29:01 +00001673 | /* empty */ {
1674 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001675 };
Chris Lattner00950542001-06-06 20:29:01 +00001676
Chris Lattner164c3782005-11-12 00:11:10 +00001677FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
1678 OptSection OptAlign {
Chris Lattnera8e8f162005-05-06 20:27:19 +00001679 UnEscapeLexed($3);
1680 std::string FunctionName($3);
1681 free($3); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001682
Chris Lattnera8e8f162005-05-06 20:27:19 +00001683 if (!(*$2)->isFirstClassType() && *$2 != Type::VoidTy)
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001684 ThrowException("LLVM functions cannot return aggregate types!");
Chris Lattner164c3782005-11-12 00:11:10 +00001685 if ($8 != 0 && !isPowerOf2_32($8))
Chris Lattner87ac9722005-11-06 06:46:28 +00001686 ThrowException("Function alignment must be a power of two!");
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001687
Chris Lattner9232b992003-04-22 18:42:41 +00001688 std::vector<const Type*> ParamTypeList;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001689 if ($5) { // If there are arguments...
1690 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1691 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001692 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001693 }
Chris Lattner00950542001-06-06 20:29:01 +00001694
Chris Lattner2079fde2001-10-13 06:41:08 +00001695 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1696 if (isVarArg) ParamTypeList.pop_back();
1697
Chris Lattnera8e8f162005-05-06 20:27:19 +00001698 const FunctionType *FT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001699 const PointerType *PFT = PointerType::get(FT);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001700 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001701
Chris Lattnera09000d2004-07-14 23:03:46 +00001702 ValID ID;
1703 if (!FunctionName.empty()) {
1704 ID = ValID::create((char*)FunctionName.c_str());
1705 } else {
1706 ID = ValID::create((int)CurModule.Values[PFT].size());
1707 }
1708
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001709 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001710 // See if this function was forward referenced. If so, recycle the object.
1711 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1712 // Move the function to the end of the list, from whereever it was
1713 // previously inserted.
1714 Fn = cast<Function>(FWRef);
1715 CurModule.CurrentModule->getFunctionList().remove(Fn);
1716 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1717 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1718 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1719 // If this is the case, either we need to be a forward decl, or it needs
1720 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001721 if (!CurFun.isDeclare && !Fn->isExternal())
1722 ThrowException("Redefinition of function '" + FunctionName + "'!");
1723
Chris Lattnera09000d2004-07-14 23:03:46 +00001724 // Make sure to strip off any argument names so we can't get conflicts.
1725 if (Fn->isExternal())
Chris Lattnere4d5c442005-03-15 04:54:21 +00001726 for (Function::arg_iterator AI = Fn->arg_begin(), AE = Fn->arg_end();
Chris Lattnera09000d2004-07-14 23:03:46 +00001727 AI != AE; ++AI)
1728 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001729
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001730 } else { // Not already defined?
1731 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1732 CurModule.CurrentModule);
1733 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001734 }
Chris Lattner00950542001-06-06 20:29:01 +00001735
Chris Lattner394f2eb2003-10-16 20:12:13 +00001736 CurFun.FunctionStart(Fn);
Chris Lattnera8e8f162005-05-06 20:27:19 +00001737 Fn->setCallingConv($1);
Chris Lattner164c3782005-11-12 00:11:10 +00001738 Fn->setAlignment($8);
1739 if ($7) {
1740 Fn->setSection($7);
1741 free($7);
1742 }
Chris Lattner00950542001-06-06 20:29:01 +00001743
Chris Lattner7e708292002-06-25 16:13:24 +00001744 // Add all of the arguments we parsed to the function...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001745 if ($5) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001746 if (isVarArg) { // Nuke the last entry
Chris Lattnera8e8f162005-05-06 20:27:19 +00001747 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001748 "Not a varargs marker!");
Chris Lattnera8e8f162005-05-06 20:27:19 +00001749 delete $5->back().first;
1750 $5->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001751 }
Chris Lattnere4d5c442005-03-15 04:54:21 +00001752 Function::arg_iterator ArgIt = Fn->arg_begin();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001753 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $5->begin();
1754 I != $5->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001755 delete I->first; // Delete the typeholder...
1756
Chris Lattner8ab406d2004-07-13 07:59:27 +00001757 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001758 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001759 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001760
Chris Lattnera8e8f162005-05-06 20:27:19 +00001761 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001762 }
Chris Lattner51727be2002-06-04 21:58:56 +00001763};
Chris Lattner00950542001-06-06 20:29:01 +00001764
Chris Lattner9b02cc32002-05-03 18:23:48 +00001765BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1766
Chris Lattner4ad02e72003-04-16 20:28:45 +00001767FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001768 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001769
Chris Lattner4ad02e72003-04-16 20:28:45 +00001770 // Make sure that we keep track of the linkage type even if there was a
1771 // previous "declare".
1772 $$->setLinkage($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001773};
Chris Lattner00950542001-06-06 20:29:01 +00001774
Chris Lattner9b02cc32002-05-03 18:23:48 +00001775END : ENDTOK | '}'; // Allow end of '}' to end a function
1776
Chris Lattner79df7c02002-03-26 18:01:55 +00001777Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001778 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001779};
Chris Lattner00950542001-06-06 20:29:01 +00001780
Chris Lattner394f2eb2003-10-16 20:12:13 +00001781FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1782 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001783 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001784};
Chris Lattner00950542001-06-06 20:29:01 +00001785
1786//===----------------------------------------------------------------------===//
1787// Rules to match Basic Blocks
1788//===----------------------------------------------------------------------===//
1789
1790ConstValueRef : ESINT64VAL { // A reference to a direct constant
1791 $$ = ValID::create($1);
1792 }
1793 | EUINT64VAL {
1794 $$ = ValID::create($1);
1795 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001796 | FPVAL { // Perhaps it's an FP constant?
1797 $$ = ValID::create($1);
1798 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001799 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001800 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001801 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001802 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001803 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001804 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001805 | NULL_TOK {
1806 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001807 }
Chris Lattner16710e92004-10-16 18:17:13 +00001808 | UNDEF {
1809 $$ = ValID::createUndef();
1810 }
Brian Gaeke715c90b2004-08-20 06:00:58 +00001811 | '<' ConstVector '>' { // Nonempty unsized packed vector
1812 const Type *ETy = (*$2)[0]->getType();
1813 int NumElements = $2->size();
1814
1815 PackedType* pt = PackedType::get(ETy, NumElements);
1816 PATypeHolder* PTy = new PATypeHolder(
1817 HandleUpRefs(
1818 PackedType::get(
1819 ETy,
1820 NumElements)
1821 )
1822 );
1823
1824 // Verify all elements are correct type!
1825 for (unsigned i = 0; i < $2->size(); i++) {
1826 if (ETy != (*$2)[i]->getType())
1827 ThrowException("Element #" + utostr(i) + " is not of type '" +
1828 ETy->getDescription() +"' as required!\nIt is of type '" +
1829 (*$2)[i]->getType()->getDescription() + "'.");
1830 }
1831
1832 $$ = ValID::create(ConstantPacked::get(pt, *$2));
1833 delete PTy; delete $2;
1834 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001835 | ConstExpr {
1836 $$ = ValID::create($1);
1837 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001838
Chris Lattner2079fde2001-10-13 06:41:08 +00001839// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1840// another value.
1841//
1842SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001843 $$ = ValID::create($1);
1844 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001845 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001846 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001847 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001848
1849// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001850ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001851
Chris Lattner00950542001-06-06 20:29:01 +00001852
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001853// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1854// type immediately preceeds the value reference, and allows complex constant
1855// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001856ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001857 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001858 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001859
Chris Lattner00950542001-06-06 20:29:01 +00001860BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001861 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001862 }
Chris Lattner7e708292002-06-25 16:13:24 +00001863 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001864 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001865 };
Chris Lattner00950542001-06-06 20:29:01 +00001866
1867
1868// Basic blocks are terminated by branching instructions:
1869// br, br/cc, switch, ret
1870//
Chris Lattner2079fde2001-10-13 06:41:08 +00001871BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001872 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001873 InsertValue($3);
1874
1875 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001876 InsertValue($1);
1877 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001878 };
Chris Lattner00950542001-06-06 20:29:01 +00001879
1880InstructionList : InstructionList Inst {
1881 $1->getInstList().push_back($2);
1882 $$ = $1;
1883 }
1884 | /* empty */ {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001885 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001886
1887 // Make sure to move the basic block to the correct location in the
1888 // function, instead of leaving it inserted wherever it was first
1889 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001890 Function::BasicBlockListType &BBL =
1891 CurFun.CurrentFunction->getBasicBlockList();
1892 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner22ae2322004-07-13 08:39:15 +00001893 }
1894 | LABELSTR {
Andrew Lenharth558bc882005-06-18 18:34:52 +00001895 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner8d65a062004-07-26 01:40:20 +00001896
1897 // Make sure to move the basic block to the correct location in the
1898 // function, instead of leaving it inserted wherever it was first
1899 // referenced.
Chris Lattnerf924a4c2005-05-06 19:58:35 +00001900 Function::BasicBlockListType &BBL =
1901 CurFun.CurrentFunction->getBasicBlockList();
1902 BBL.splice(BBL.end(), BBL, $$);
Chris Lattner51727be2002-06-04 21:58:56 +00001903 };
Chris Lattner00950542001-06-06 20:29:01 +00001904
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001905BBTerminatorInst : RET ResolvedVal { // Return with a result...
1906 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001907 }
1908 | RET VOID { // Return with no result...
1909 $$ = new ReturnInst();
1910 }
1911 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001912 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001913 } // Conditional Branch...
1914 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001915 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001916 }
1917 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001918 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), $8->size());
Chris Lattner00950542001-06-06 20:29:01 +00001919 $$ = S;
1920
Chris Lattner9232b992003-04-22 18:42:41 +00001921 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001922 E = $8->end();
Chris Lattner69331f52005-02-24 05:25:17 +00001923 for (; I != E; ++I) {
1924 if (ConstantInt *CI = dyn_cast<ConstantInt>(I->first))
1925 S->addCase(CI, I->second);
1926 else
1927 ThrowException("Switch case is constant, but not a simple integer!");
1928 }
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001929 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001930 }
Chris Lattner196850c2003-05-15 21:30:00 +00001931 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattnerce1df9e2005-01-29 00:35:55 +00001932 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6), 0);
Chris Lattner196850c2003-05-15 21:30:00 +00001933 $$ = S;
1934 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001935 | INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1936 TO LABEL ValueRef UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001937 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001938 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001939
Chris Lattnera8e8f162005-05-06 20:27:19 +00001940 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001941 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001942 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001943 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00001944 if ($6) {
1945 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00001946 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001947 ParamTypes.push_back((*I)->getType());
1948 }
1949
1950 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1951 if (isVarArg) ParamTypes.pop_back();
1952
Chris Lattnera8e8f162005-05-06 20:27:19 +00001953 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001954 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001955 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001956
Chris Lattnera8e8f162005-05-06 20:27:19 +00001957 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001958
Chris Lattnera8e8f162005-05-06 20:27:19 +00001959 BasicBlock *Normal = getBBVal($10);
1960 BasicBlock *Except = getBBVal($13);
Chris Lattner2079fde2001-10-13 06:41:08 +00001961
1962 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00001963 if (!$6) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001964 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001965 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001966 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001967 // correctly!
1968 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001969 FunctionType::param_iterator I = Ty->param_begin();
1970 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00001971 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001972
1973 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00001974 if ((*ArgI)->getType() != *I)
1975 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
1976 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001977
1978 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00001979 ThrowException("Invalid number of parameters detected!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001980
Chris Lattnera8e8f162005-05-06 20:27:19 +00001981 $$ = new InvokeInst(V, Normal, Except, *$6);
Chris Lattner2079fde2001-10-13 06:41:08 +00001982 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00001983 cast<InvokeInst>($$)->setCallingConv($2);
1984
1985 delete $3;
1986 delete $6;
Chris Lattner36143fc2003-09-08 18:54:55 +00001987 }
1988 | UNWIND {
1989 $$ = new UnwindInst();
Chris Lattner16710e92004-10-16 18:17:13 +00001990 }
1991 | UNREACHABLE {
1992 $$ = new UnreachableInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001993 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001994
1995
Chris Lattner00950542001-06-06 20:29:01 +00001996
1997JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1998 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001999 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00002000 if (V == 0)
2001 ThrowException("May only switch on a constant pool value!");
2002
Chris Lattner64116f92004-07-14 01:33:11 +00002003 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00002004 }
2005 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00002006 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00002007 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00002008
2009 if (V == 0)
2010 ThrowException("May only switch on a constant pool value!");
2011
Chris Lattner64116f92004-07-14 01:33:11 +00002012 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00002013 };
Chris Lattner00950542001-06-06 20:29:01 +00002014
2015Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00002016 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00002017 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00002018 InsertValue($2);
2019 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00002020};
Chris Lattner00950542001-06-06 20:29:01 +00002021
Chris Lattnerc24d2082001-06-11 15:04:20 +00002022PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00002023 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00002024 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00002025 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00002026 }
2027 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
2028 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00002029 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00002030 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00002031 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00002032
2033
Chris Lattner30c89792001-09-07 16:35:17 +00002034ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00002035 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002036 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00002037 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002038 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00002039 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002040 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00002041 };
Chris Lattner00950542001-06-06 20:29:01 +00002042
2043// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00002044ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00002045
Chris Lattnerddb6db42005-05-06 05:51:46 +00002046OptTailCall : TAIL CALL {
2047 $$ = true;
2048 }
2049 | CALL {
2050 $$ = false;
2051 };
2052
2053
2054
Chris Lattner4a6482b2002-09-10 19:57:26 +00002055InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Brian Gaeke715c90b2004-08-20 06:00:58 +00002056 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint() &&
2057 !isa<PackedType>((*$2).get()))
2058 ThrowException(
2059 "Arithmetic operator requires integer, FP, or packed operands!");
Misha Brukman5a2a3822005-05-10 22:02:28 +00002060 if (isa<PackedType>((*$2).get()) && $1 == Instruction::Rem)
2061 ThrowException("Rem not supported on packed types!");
Chris Lattner4a6482b2002-09-10 19:57:26 +00002062 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2063 if ($$ == 0)
2064 ThrowException("binary operator returned null!");
2065 delete $2;
2066 }
2067 | LogicalOps Types ValueRef ',' ValueRef {
2068 if (!(*$2)->isIntegral())
2069 ThrowException("Logical operator requires integral operands!");
2070 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
2071 if ($$ == 0)
2072 ThrowException("binary operator returned null!");
2073 delete $2;
2074 }
2075 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer57b6eec2004-08-21 16:11:02 +00002076 if(isa<PackedType>((*$2).get())) {
2077 ThrowException(
2078 "PackedTypes currently not supported in setcc instructions!");
2079 }
Chris Lattner1cff96a2002-09-10 22:37:46 +00002080 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00002081 if ($$ == 0)
2082 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00002083 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002084 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00002085 | NOT ResolvedVal {
2086 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
2087 << " Replacing with 'xor'.\n";
2088
2089 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
2090 if (Ones == 0)
2091 ThrowException("Expected integral type for not instruction!");
2092
2093 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00002094 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00002095 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00002096 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002097 | ShiftOps ResolvedVal ',' ResolvedVal {
2098 if ($4->getType() != Type::UByteTy)
2099 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00002100 if (!$2->getType()->isInteger())
2101 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002102 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00002103 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002104 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00002105 if (!$4->get()->isFirstClassType())
2106 ThrowException("cast instruction to a non-primitive type: '" +
2107 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002108 $$ = new CastInst($2, *$4);
2109 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00002110 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00002111 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2112 if ($2->getType() != Type::BoolTy)
2113 ThrowException("select condition must be boolean!");
2114 if ($4->getType() != $6->getType())
2115 ThrowException("select value types should match!");
2116 $$ = new SelectInst($2, $4, $6);
2117 }
Chris Lattner99e7ab72003-10-18 05:53:13 +00002118 | VAARG ResolvedVal ',' Types {
Andrew Lenharthe78f50d2005-06-19 03:53:56 +00002119 NewVarArgs = true;
Chris Lattner99e7ab72003-10-18 05:53:13 +00002120 $$ = new VAArgInst($2, *$4);
2121 delete $4;
2122 }
Andrew Lenharth558bc882005-06-18 18:34:52 +00002123 | VAARG_old ResolvedVal ',' Types {
2124 ObsoleteVarArgs = true;
2125 const Type* ArgTy = $2->getType();
2126 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002127 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002128
2129 //b = vaarg a, t ->
2130 //foo = alloca 1 of t
2131 //bar = vacopy a
2132 //store bar -> foo
2133 //b = vaarg foo, t
2134 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vaarg.fix");
2135 CurBB->getInstList().push_back(foo);
2136 CallInst* bar = new CallInst(NF, $2);
2137 CurBB->getInstList().push_back(bar);
2138 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2139 $$ = new VAArgInst(foo, *$4);
2140 delete $4;
2141 }
2142 | VANEXT_old ResolvedVal ',' Types {
2143 ObsoleteVarArgs = true;
2144 const Type* ArgTy = $2->getType();
2145 Function* NF = CurModule.CurrentModule->
Jeff Cohen66c5fd62005-10-23 04:37:20 +00002146 getOrInsertFunction("llvm.va_copy", ArgTy, ArgTy, (Type *)0);
Andrew Lenharth558bc882005-06-18 18:34:52 +00002147
2148 //b = vanext a, t ->
2149 //foo = alloca 1 of t
2150 //bar = vacopy a
2151 //store bar -> foo
2152 //tmp = vaarg foo, t
2153 //b = load foo
2154 AllocaInst* foo = new AllocaInst(ArgTy, 0, "vanext.fix");
2155 CurBB->getInstList().push_back(foo);
2156 CallInst* bar = new CallInst(NF, $2);
2157 CurBB->getInstList().push_back(bar);
2158 CurBB->getInstList().push_back(new StoreInst(bar, foo));
2159 Instruction* tmp = new VAArgInst(foo, *$4);
2160 CurBB->getInstList().push_back(tmp);
2161 $$ = new LoadInst(foo);
Chris Lattner8f77dae2003-05-08 02:44:12 +00002162 delete $4;
2163 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00002164 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002165 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00002166 if (!Ty->isFirstClassType())
2167 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00002168 $$ = new PHINode(Ty);
Chris Lattnerce1df9e2005-01-29 00:35:55 +00002169 ((PHINode*)$$)->reserveOperandSpace($2->size());
Chris Lattner00950542001-06-06 20:29:01 +00002170 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00002171 if ($2->front().first->getType() != Ty)
Reid Spencer3271ed52004-07-18 00:08:11 +00002172 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00002173 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00002174 $2->pop_front();
2175 }
2176 delete $2; // Free the list...
Chris Lattnerddb6db42005-05-06 05:51:46 +00002177 }
Chris Lattnera8e8f162005-05-06 20:27:19 +00002178 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002179 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00002180 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00002181
Chris Lattnera8e8f162005-05-06 20:27:19 +00002182 if (!(PFTy = dyn_cast<PointerType>($3->get())) ||
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002183 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00002184 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00002185 std::vector<const Type*> ParamTypes;
Chris Lattnera8e8f162005-05-06 20:27:19 +00002186 if ($6) {
2187 for (std::vector<Value*>::iterator I = $6->begin(), E = $6->end();
Chris Lattner9232b992003-04-22 18:42:41 +00002188 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00002189 ParamTypes.push_back((*I)->getType());
2190 }
Chris Lattner2079fde2001-10-13 06:41:08 +00002191
2192 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
2193 if (isVarArg) ParamTypes.pop_back();
2194
Chris Lattnera8e8f162005-05-06 20:27:19 +00002195 if (!(*$3)->isFirstClassType() && *$3 != Type::VoidTy)
Chris Lattner595f06c2005-02-01 01:47:42 +00002196 ThrowException("LLVM functions cannot return aggregate types!");
2197
Chris Lattnera8e8f162005-05-06 20:27:19 +00002198 Ty = FunctionType::get($3->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00002199 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002200 }
Chris Lattner00950542001-06-06 20:29:01 +00002201
Chris Lattnera8e8f162005-05-06 20:27:19 +00002202 Value *V = getVal(PFTy, $4); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00002203
Chris Lattner8b81bf52001-07-25 22:47:46 +00002204 // Create the call node...
Chris Lattnera8e8f162005-05-06 20:27:19 +00002205 if (!$6) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00002206 // Make sure no arguments is a good thing!
2207 if (Ty->getNumParams() != 0)
2208 ThrowException("No arguments passed to a function that "
2209 "expects arguments!");
2210
Chris Lattner9232b992003-04-22 18:42:41 +00002211 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00002212 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00002213 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00002214 // correctly!
2215 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00002216 FunctionType::param_iterator I = Ty->param_begin();
2217 FunctionType::param_iterator E = Ty->param_end();
Chris Lattnera8e8f162005-05-06 20:27:19 +00002218 std::vector<Value*>::iterator ArgI = $6->begin(), ArgE = $6->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00002219
2220 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
Reid Spencer3271ed52004-07-18 00:08:11 +00002221 if ((*ArgI)->getType() != *I)
2222 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
2223 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002224
Chris Lattner8b81bf52001-07-25 22:47:46 +00002225 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Reid Spencer3271ed52004-07-18 00:08:11 +00002226 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002227
Chris Lattnera8e8f162005-05-06 20:27:19 +00002228 $$ = new CallInst(V, *$6);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002229 }
Chris Lattnerddb6db42005-05-06 05:51:46 +00002230 cast<CallInst>($$)->setTailCall($1);
Chris Lattnera8e8f162005-05-06 20:27:19 +00002231 cast<CallInst>($$)->setCallingConv($2);
2232 delete $3;
2233 delete $6;
Chris Lattner00950542001-06-06 20:29:01 +00002234 }
2235 | MemoryInst {
2236 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002237 };
Chris Lattner00950542001-06-06 20:29:01 +00002238
Chris Lattner6cdb0112001-11-26 16:54:11 +00002239
2240// IndexList - List of indices for GEP based instructions...
2241IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002242 $$ = $2;
2243 } | /* empty */ {
2244 $$ = new std::vector<Value*>();
2245 };
2246
2247OptVolatile : VOLATILE {
2248 $$ = true;
2249 }
2250 | /* empty */ {
2251 $$ = false;
2252 };
2253
Chris Lattner027dcc52001-07-08 21:10:27 +00002254
Chris Lattnerddb6db42005-05-06 05:51:46 +00002255
Chris Lattner66db8e42005-11-06 06:34:12 +00002256MemoryInst : MALLOC Types OptCAlign {
Chris Lattner87ac9722005-11-06 06:46:28 +00002257 if ($3 != 0 && !isPowerOf2_32($3))
2258 ThrowException("Alignment amount '" + utostr($3) +
2259 "' is not a power of 2!");
Chris Lattner66db8e42005-11-06 06:34:12 +00002260 $$ = new MallocInst(*$2, 0, $3);
Chris Lattner30c89792001-09-07 16:35:17 +00002261 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002262 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002263 | MALLOC Types ',' UINT ValueRef OptCAlign {
Chris Lattner87ac9722005-11-06 06:46:28 +00002264 if ($6 != 0 && !isPowerOf2_32($6))
Chris Lattner66db8e42005-11-06 06:34:12 +00002265 ThrowException("Alignment amount '" + utostr($6) +
Chris Lattnerac6e5c12005-11-05 21:54:03 +00002266 "' is not a power of 2!");
Chris Lattner66db8e42005-11-06 06:34:12 +00002267 $$ = new MallocInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002268 delete $2;
2269 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002270 | ALLOCA Types OptCAlign {
Chris Lattner87ac9722005-11-06 06:46:28 +00002271 if ($3 != 0 && !isPowerOf2_32($3))
Chris Lattner66db8e42005-11-06 06:34:12 +00002272 ThrowException("Alignment amount '" + utostr($3) +
Chris Lattnerac6e5c12005-11-05 21:54:03 +00002273 "' is not a power of 2!");
Chris Lattner66db8e42005-11-06 06:34:12 +00002274 $$ = new AllocaInst(*$2, 0, $3);
Nate Begeman14b05292005-11-05 09:21:28 +00002275 delete $2;
2276 }
Chris Lattner66db8e42005-11-06 06:34:12 +00002277 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Chris Lattner87ac9722005-11-06 06:46:28 +00002278 if ($6 != 0 && !isPowerOf2_32($6))
Chris Lattner66db8e42005-11-06 06:34:12 +00002279 ThrowException("Alignment amount '" + utostr($6) +
Chris Lattnerac6e5c12005-11-05 21:54:03 +00002280 "' is not a power of 2!");
Chris Lattner66db8e42005-11-06 06:34:12 +00002281 $$ = new AllocaInst(*$2, getVal($4, $5), $6);
Nate Begeman14b05292005-11-05 09:21:28 +00002282 delete $2;
2283 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002284 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002285 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002286 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002287 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002288 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002289 }
2290
Chris Lattner15c9c032003-09-08 18:20:29 +00002291 | OptVolatile LOAD Types ValueRef {
2292 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002293 ThrowException("Can't load from nonpointer type: " +
Reid Spencer3271ed52004-07-18 00:08:11 +00002294 (*$3)->getDescription());
Chris Lattner1c1bb332004-10-09 02:18:58 +00002295 if (!cast<PointerType>($3->get())->getElementType()->isFirstClassType())
2296 ThrowException("Can't load from pointer of non-first-class type: " +
2297 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002298 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002299 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002300 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002301 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2302 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002303 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002304 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002305 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002306 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002307 if (ElTy != $3->getType())
2308 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002309 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002310
Chris Lattner79ad13802003-09-08 20:29:46 +00002311 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002312 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002313 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002314 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002315 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002316 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002317
2318 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2319 // indices to uint struct indices for compatibility.
2320 generic_gep_type_iterator<std::vector<Value*>::iterator>
2321 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2322 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2323 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2324 if (isa<StructType>(*GTI)) // Only change struct indices
2325 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2326 if (CUI->getType() == Type::UByteTy)
2327 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2328
Chris Lattner30c89792001-09-07 16:35:17 +00002329 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002330 ThrowException("Invalid getelementptr indices for type '" +
2331 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002332 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2333 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002334 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002335
Brian Gaeked0fde302003-11-11 22:41:34 +00002336
Chris Lattner00950542001-06-06 20:29:01 +00002337%%
Chris Lattner09083092001-07-08 04:57:15 +00002338int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002339 std::string where
2340 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002341 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002342 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002343 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002344 errMsg += "end-of-file.";
2345 else
Chris Lattner9232b992003-04-22 18:42:41 +00002346 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002347 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002348 return 0;
2349}