blob: eb56f113e8a7f1942be0db5f3ca89f1dfd7e8c12 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This file implements the bison parser for LLVM assembly languages files.
4//
Chris Lattnercf3056d2003-10-13 03:32:08 +00005//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00006
Chris Lattner00950542001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000011#include "llvm/iTerminators.h"
12#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000013#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000015#include "Support/STLExtras.h"
16#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000018#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000019#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000020
Chris Lattner386a3b72001-10-16 19:54:17 +000021int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000022int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000023int yyparse();
24
25static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000026std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner30c89792001-09-07 16:35:17 +000028// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
29// relating to upreferences in the input stream.
30//
31//#define DEBUG_UPREFS 1
32#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000033#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000034#else
35#define UR_OUT(X)
36#endif
37
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000038#define YYERROR_VERBOSE 1
39
Chris Lattner99e7ab72003-10-18 05:53:13 +000040// HACK ALERT: This variable is used to implement the automatic conversion of
41// variable argument instructions from their old to new forms. When this
42// compatiblity "Feature" is removed, this should be too.
43//
44static BasicBlock *CurBB;
45static bool ObsoleteVarArgs;
46
47
Chris Lattner7e708292002-06-25 16:13:24 +000048// This contains info used when building the body of a function. It is
49// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000050//
Chris Lattner9232b992003-04-22 18:42:41 +000051typedef std::vector<Value *> ValueList; // Numbered defs
52static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
53 std::vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000054
55static struct PerModuleInfo {
56 Module *CurrentModule;
Chris Lattner9232b992003-04-22 18:42:41 +000057 std::vector<ValueList> Values; // Module level numbered definitions
58 std::vector<ValueList> LateResolveValues;
59 std::vector<PATypeHolder> Types;
60 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000061
Chris Lattner2079fde2001-10-13 06:41:08 +000062 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
63 // references to global values. Global values may be referenced before they
64 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000065 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000066 //
Chris Lattner9232b992003-04-22 18:42:41 +000067 typedef std::map<std::pair<const PointerType *,
68 ValID>, GlobalVariable*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000069 GlobalRefsType GlobalRefs;
70
Chris Lattner00950542001-06-06 20:29:01 +000071 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000072 // If we could not resolve some functions at function compilation time
73 // (calls to functions before they are defined), resolve them now... Types
74 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000075 //
Chris Lattner00950542001-06-06 20:29:01 +000076 ResolveDefinitions(LateResolveValues);
77
Chris Lattner2079fde2001-10-13 06:41:08 +000078 // Check to make sure that all global value forward references have been
79 // resolved!
80 //
81 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000082 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000083
84 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
85 I != E; ++I) {
86 UndefinedReferences += " " + I->first.first->getDescription() + " " +
87 I->first.second.getName() + "\n";
88 }
89 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000090 }
91
Chris Lattner7e708292002-06-25 16:13:24 +000092 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000093 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000094 CurrentModule = 0;
95 }
Chris Lattner2079fde2001-10-13 06:41:08 +000096
97
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000098 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +000099 // is used to remove things from the forward declaration map, resolving them
100 // to the correct thing as needed.
101 //
102 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
103 // Check to see if there is a forward reference to this global variable...
104 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000105 GlobalRefsType::iterator I =
106 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000107
108 if (I != GlobalRefs.end()) {
109 GlobalVariable *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000110 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner2079fde2001-10-13 06:41:08 +0000111
112 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000113 // allowed to be is ConstantPointerRef's.
Chris Lattnerfd059242003-10-15 16:48:29 +0000114 assert(OldGV->hasOneUse() && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000115 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
116 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000117
Chris Lattner9e932bd2002-10-14 03:28:42 +0000118 // Change the const pool reference to point to the real global variable
119 // now. This should drop a use from the OldGV.
120 CPR->mutateReferences(OldGV, GV);
121 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000122
123 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000124 CurrentModule->getGlobalList().remove(OldGV);
125 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000126
Chris Lattner2079fde2001-10-13 06:41:08 +0000127 // Remove the map entry for the global now that it has been created...
128 GlobalRefs.erase(I);
129 }
130 }
131
Chris Lattner00950542001-06-06 20:29:01 +0000132} CurModule;
133
Chris Lattner79df7c02002-03-26 18:01:55 +0000134static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000135 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000136
Chris Lattner9232b992003-04-22 18:42:41 +0000137 std::vector<ValueList> Values; // Keep track of numbered definitions
138 std::vector<ValueList> LateResolveValues;
139 std::vector<PATypeHolder> Types;
140 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000141 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000142
Chris Lattner79df7c02002-03-26 18:01:55 +0000143 inline PerFunctionInfo() {
144 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000145 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000146 }
147
Chris Lattner79df7c02002-03-26 18:01:55 +0000148 inline void FunctionStart(Function *M) {
149 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000150 }
151
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000153 // If we could not resolve some blocks at parsing time (forward branches)
154 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000155 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000156
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000157 // Make sure to resolve any constant expr references that might exist within
158 // the function we just declared itself.
159 ValID FID;
160 if (CurrentFunction->hasName()) {
161 FID = ValID::create((char*)CurrentFunction->getName().c_str());
162 } else {
163 unsigned Slot = CurrentFunction->getType()->getUniqueID();
164 assert(CurModule.Values.size() > Slot && "Function not inserted?");
165 // Figure out which slot number if is...
166 for (unsigned i = 0; ; ++i) {
167 assert(i < CurModule.Values[Slot].size() && "Function not found!");
168 if (CurModule.Values[Slot][i] == CurrentFunction) {
169 FID = ValID::create((int)i);
170 break;
171 }
172 }
173 }
174 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
175
Chris Lattner7e708292002-06-25 16:13:24 +0000176 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000177 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000178 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000179 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000180 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000181} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000182
Chris Lattner394f2eb2003-10-16 20:12:13 +0000183static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000184
Chris Lattner00950542001-06-06 20:29:01 +0000185
186//===----------------------------------------------------------------------===//
187// Code to handle definitions of all the types
188//===----------------------------------------------------------------------===//
189
Chris Lattner9232b992003-04-22 18:42:41 +0000190static int InsertValue(Value *D,
Chris Lattner394f2eb2003-10-16 20:12:13 +0000191 std::vector<ValueList> &ValueTab = CurFun.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000192 if (D->hasName()) return -1; // Is this a numbered definition?
193
194 // Yes, insert the value into the value table...
195 unsigned type = D->getType()->getUniqueID();
196 if (ValueTab.size() <= type)
197 ValueTab.resize(type+1, ValueList());
198 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
199 ValueTab[type].push_back(D);
200 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000201}
202
Chris Lattner30c89792001-09-07 16:35:17 +0000203// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000204static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000205 Types.push_back(Ty);
206}
207
208static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000209 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000210 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000211 unsigned Num = (unsigned)D.Num;
212
213 // Module constants occupy the lowest numbered slots...
214 if (Num < CurModule.Types.size())
215 return CurModule.Types[Num];
216
217 Num -= CurModule.Types.size();
218
219 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000220 if (Num <= CurFun.Types.size())
221 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000222 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000223 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000224 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000225 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000226 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000227 Value *N = 0;
228 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000229 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000230 N = SymTab->lookup(Type::TypeTy, Name);
231 }
Chris Lattner30c89792001-09-07 16:35:17 +0000232
233 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000234 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000235 // hasn't been added to the module...
236 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000237 SymTab = &CurModule.CurrentModule->getSymbolTable();
238 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000239 if (N == 0) break;
240 }
241
242 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000243 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000244 }
245 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000246 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000247 }
248
249 // If we reached here, we referenced either a symbol that we don't know about
250 // or an id number that hasn't been read yet. We may be referencing something
251 // forward, so just create an entry to be resolved later and get to it...
252 //
253 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
254
Chris Lattner9232b992003-04-22 18:42:41 +0000255 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000256 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000257
Chris Lattner9232b992003-04-22 18:42:41 +0000258 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000259 if (I != LateResolver.end()) {
260 return I->second;
261 }
Chris Lattner30c89792001-09-07 16:35:17 +0000262
Chris Lattner82269592001-10-22 06:01:08 +0000263 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000264 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000265 return Typ;
266}
267
Chris Lattner9232b992003-04-22 18:42:41 +0000268static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000269 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000270 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000271 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000272 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000273}
274
Chris Lattner2079fde2001-10-13 06:41:08 +0000275// getValNonImprovising - Look up the value specified by the provided type and
276// the provided ValID. If the value exists and has already been defined, return
277// it. Otherwise return null.
278//
279static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000280 if (isa<FunctionType>(Ty))
281 ThrowException("Functions are not values and "
282 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000283
Chris Lattner30c89792001-09-07 16:35:17 +0000284 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000285 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000286 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000287 unsigned Num = (unsigned)D.Num;
288
289 // Module constants occupy the lowest numbered slots...
290 if (type < CurModule.Values.size()) {
291 if (Num < CurModule.Values[type].size())
292 return CurModule.Values[type][Num];
293
294 Num -= CurModule.Values[type].size();
295 }
296
297 // Make sure that our type is within bounds
Chris Lattner394f2eb2003-10-16 20:12:13 +0000298 if (CurFun.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000299
300 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000301 if (CurFun.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000302
Chris Lattner394f2eb2003-10-16 20:12:13 +0000303 return CurFun.Values[type][Num];
Chris Lattner00950542001-06-06 20:29:01 +0000304 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000305
Chris Lattner1a1cb112001-09-30 22:46:54 +0000306 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000307 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000308 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000309
310 D.destroy(); // Free old strdup'd memory...
311 return N;
312 }
313
Chris Lattner2079fde2001-10-13 06:41:08 +0000314 // Check to make sure that "Ty" is an integral type, and that our
315 // value will fit into the specified type...
316 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000317 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
318 ThrowException("Signed integral constant '" +
319 itostr(D.ConstPool64) + "' is invalid for type '" +
320 Ty->getDescription() + "'!");
321 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000322
323 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000324 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
325 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000326 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
327 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000328 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000329 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000330 }
331 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000332 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000333 }
334
Chris Lattner2079fde2001-10-13 06:41:08 +0000335 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000336 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000337 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000338 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000339
340 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000341 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000342 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000343 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000344
Chris Lattnerd78700d2002-08-16 21:14:40 +0000345 case ValID::ConstantVal: // Fully resolved constant?
346 if (D.ConstantValue->getType() != Ty)
347 ThrowException("Constant expression type different from required type!");
348 return D.ConstantValue;
349
Chris Lattner30c89792001-09-07 16:35:17 +0000350 default:
351 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000352 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000353 } // End of switch
354
Chris Lattner2079fde2001-10-13 06:41:08 +0000355 assert(0 && "Unhandled case!");
356 return 0;
357}
358
359
360// getVal - This function is identical to getValNonImprovising, except that if a
361// value is not already defined, it "improvises" by creating a placeholder var
362// that looks and acts just like the requested variable. When the value is
363// defined later, all uses of the placeholder variable are replaced with the
364// real thing.
365//
366static Value *getVal(const Type *Ty, const ValID &D) {
367 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
368
369 // See if the value has already been defined...
370 Value *V = getValNonImprovising(Ty, D);
371 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000372
373 // If we reached here, we referenced either a symbol that we don't know about
374 // or an id number that hasn't been read yet. We may be referencing something
375 // forward, so just create an entry to be resolved later and get to it...
376 //
Chris Lattner00950542001-06-06 20:29:01 +0000377 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000378 switch (Ty->getPrimitiveID()) {
379 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000380 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000381 }
382
383 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000384 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000385 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000386 else
387 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000388 return d;
389}
390
391
392//===----------------------------------------------------------------------===//
393// Code to handle forward references in instructions
394//===----------------------------------------------------------------------===//
395//
396// This code handles the late binding needed with statements that reference
397// values not defined yet... for example, a forward branch, or the PHI node for
398// a loop body.
399//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000400// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000401// and back patchs after we are done.
402//
403
404// ResolveDefinitions - If we could not resolve some defs at parsing
405// time (forward branches, phi functions for loops, etc...) resolve the
406// defs now...
407//
Chris Lattner9232b992003-04-22 18:42:41 +0000408static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
409 std::vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000410 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
411 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
412 while (!LateResolvers[ty].empty()) {
413 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000414 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
415
Chris Lattner00950542001-06-06 20:29:01 +0000416 LateResolvers[ty].pop_back();
417 ValID &DID = getValIDFromPlaceHolder(V);
418
Chris Lattner2079fde2001-10-13 06:41:08 +0000419 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000420 if (TheRealValue) {
421 V->replaceAllUsesWith(TheRealValue);
422 delete V;
423 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000424 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000425 // resolver table
426 InsertValue(V, *FutureLateResolvers);
427 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000428 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000429 ThrowException("Reference to an invalid definition: '" +DID.getName()+
430 "' of type '" + V->getType()->getDescription() + "'",
431 getLineNumFromPlaceHolder(V));
432 else
433 ThrowException("Reference to an invalid definition: #" +
434 itostr(DID.Num) + " of type '" +
435 V->getType()->getDescription() + "'",
436 getLineNumFromPlaceHolder(V));
437 }
Chris Lattner00950542001-06-06 20:29:01 +0000438 }
439 }
440
441 LateResolvers.clear();
442}
443
Chris Lattner4a42e902001-10-22 05:56:09 +0000444// ResolveTypeTo - A brand new type was just declared. This means that (if
445// name is not null) things referencing Name can be resolved. Otherwise, things
446// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000447//
Chris Lattner4a42e902001-10-22 05:56:09 +0000448static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000449 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000450 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000451
Chris Lattner4a42e902001-10-22 05:56:09 +0000452 ValID D;
453 if (Name) D = ValID::create(Name);
454 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000455
Chris Lattner9232b992003-04-22 18:42:41 +0000456 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000457 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000458
Chris Lattner9232b992003-04-22 18:42:41 +0000459 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000460 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000461 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000462 LateResolver.erase(I);
463 }
464}
465
466// ResolveTypes - At this point, all types should be resolved. Any that aren't
467// are errors.
468//
Chris Lattner9232b992003-04-22 18:42:41 +0000469static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000470 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000471 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000472
473 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000474 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000475 else
Chris Lattner82269592001-10-22 06:01:08 +0000476 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000477 }
478}
479
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000480
Chris Lattner1781aca2001-09-18 04:00:54 +0000481// setValueName - Set the specified value to the name given. The name may be
482// null potentially, in which case this is a noop. The string passed in is
483// assumed to be a malloc'd string buffer, and is freed by this function.
484//
Chris Lattnerb7474512001-10-03 15:39:04 +0000485// This function returns true if the value has already been defined, but is
486// allowed to be redefined in the specified context. If the name is a new name
487// for the typeplane, false is returned.
488//
489static bool setValueName(Value *V, char *NameStr) {
490 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000491
Chris Lattner9232b992003-04-22 18:42:41 +0000492 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000493 free(NameStr); // Free old string
494
Chris Lattner2079fde2001-10-13 06:41:08 +0000495 if (V->getType() == Type::VoidTy)
496 ThrowException("Can't assign name '" + Name +
497 "' to a null valued instruction!");
498
Chris Lattner6e6026b2002-11-20 18:36:02 +0000499 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000500 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000501 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000502
Chris Lattner6e6026b2002-11-20 18:36:02 +0000503 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000504 if (Existing) { // Inserting a name that is already defined???
505 // There is only one case where this is allowed: when we are refining an
506 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner949a3622003-07-23 15:30:06 +0000507 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000508 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000509 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000510 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000511 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000512 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000513 }
Chris Lattner30c89792001-09-07 16:35:17 +0000514
Chris Lattner9636a912001-10-01 16:18:37 +0000515 // Otherwise, we are a simple redefinition of a value, check to see if it
516 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000517 if (const Type *Ty = dyn_cast<Type>(Existing)) {
518 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000519 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattner949a3622003-07-23 15:30:06 +0000520 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000521 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
522 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000523 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000524 // We are allowed to redefine a global variable in two circumstances:
525 // 1. If at least one of the globals is uninitialized or
526 // 2. If both initializers have the same value.
527 //
Chris Lattner89219832001-10-03 19:35:04 +0000528 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000529 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
530 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000531
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000532 // Make sure the existing global version gets the initializer! Make
533 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000534 if (GV->hasInitializer() && !EGV->hasInitializer())
535 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000536 if (GV->isConstant())
537 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000538 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000539
Chris Lattner2079fde2001-10-13 06:41:08 +0000540 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000541 return true; // They are equivalent!
542 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000543 }
Chris Lattner9636a912001-10-01 16:18:37 +0000544 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000545 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000546 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000547 }
Chris Lattner00950542001-06-06 20:29:01 +0000548
Chris Lattner6e6026b2002-11-20 18:36:02 +0000549 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000550 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000551}
552
Chris Lattner8896eda2001-07-09 19:38:36 +0000553
Chris Lattner30c89792001-09-07 16:35:17 +0000554//===----------------------------------------------------------------------===//
555// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000556//
Chris Lattner8896eda2001-07-09 19:38:36 +0000557
Chris Lattner30c89792001-09-07 16:35:17 +0000558// TypeContains - Returns true if Ty contains E in it.
559//
560static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000561 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000562}
Chris Lattner698b56e2001-07-20 19:15:08 +0000563
Chris Lattner30c89792001-09-07 16:35:17 +0000564
Chris Lattner9232b992003-04-22 18:42:41 +0000565static std::vector<std::pair<unsigned, OpaqueType *> > UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000566
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000567static PATypeHolder HandleUpRefs(const Type *ty) {
568 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000569 UR_OUT("Type '" << ty->getDescription() <<
570 "' newly formed. Resolving upreferences.\n" <<
571 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000572 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000573 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000574 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000575 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000576 if (TypeContains(Ty, UpRefs[i].second)) {
577 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000578 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000579 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000580 UR_OUT(" * Resolving upreference for "
581 << UpRefs[i].second->getDescription() << endl;
Chris Lattner9232b992003-04-22 18:42:41 +0000582 std::string OldName = UpRefs[i].second->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +0000583 UpRefs[i].second->refineAbstractTypeTo(Ty);
584 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000585 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000586 << (const void*)Ty << ", " << Ty->getDescription() << endl);
587 continue;
588 }
589 }
590
591 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000592 }
Chris Lattner30c89792001-09-07 16:35:17 +0000593 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000594 return Ty;
595}
596
Chris Lattner30c89792001-09-07 16:35:17 +0000597
Chris Lattner00950542001-06-06 20:29:01 +0000598//===----------------------------------------------------------------------===//
599// RunVMAsmParser - Define an interface to this parser
600//===----------------------------------------------------------------------===//
601//
Chris Lattner9232b992003-04-22 18:42:41 +0000602Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000603 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000604 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000605 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000606 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000607
Chris Lattner75f20532003-04-22 18:02:52 +0000608 // Allocate a new module to read
609 CurModule.CurrentModule = new Module(Filename);
Chris Lattner00950542001-06-06 20:29:01 +0000610 yyparse(); // Parse the file.
Chris Lattner99e7ab72003-10-18 05:53:13 +0000611
Chris Lattner00950542001-06-06 20:29:01 +0000612 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000613
614 // Check to see if they called va_start but not va_arg..
615 if (!ObsoleteVarArgs)
616 if (Function *F = Result->getNamedFunction("llvm.va_start"))
617 if (F->asize() == 1) {
618 std::cerr << "WARNING: this file uses obsolete features. "
619 << "Assemble and disassemble to update it.\n";
620 ObsoleteVarArgs = true;
621 }
622
623
624 if (ObsoleteVarArgs) {
625 // If the user is making use of obsolete varargs intrinsics, adjust them for
626 // the user.
627 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
628 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
629
630 const Type *RetTy = F->getFunctionType()->getParamType(0);
631 RetTy = cast<PointerType>(RetTy)->getElementType();
632 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
633
634 while (!F->use_empty()) {
635 CallInst *CI = cast<CallInst>(F->use_back());
636 Value *V = new CallInst(NF, "", CI);
637 new StoreInst(V, CI->getOperand(1), CI);
638 CI->getParent()->getInstList().erase(CI);
639 }
640 Result->getFunctionList().erase(F);
641 }
642
643 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
644 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
645 const Type *ArgTy = F->getFunctionType()->getParamType(0);
646 ArgTy = cast<PointerType>(ArgTy)->getElementType();
647 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
648 ArgTy, 0);
649
650 while (!F->use_empty()) {
651 CallInst *CI = cast<CallInst>(F->use_back());
652 Value *V = new LoadInst(CI->getOperand(1), "", CI);
653 new CallInst(NF, V, "", CI);
654 CI->getParent()->getInstList().erase(CI);
655 }
656 Result->getFunctionList().erase(F);
657 }
658
659 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
660 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
661 const Type *ArgTy = F->getFunctionType()->getParamType(0);
662 ArgTy = cast<PointerType>(ArgTy)->getElementType();
663 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
664 ArgTy, 0);
665
666 while (!F->use_empty()) {
667 CallInst *CI = cast<CallInst>(F->use_back());
668 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
669 new StoreInst(V, CI->getOperand(1), CI);
670 CI->getParent()->getInstList().erase(CI);
671 }
672 Result->getFunctionList().erase(F);
673 }
674 }
675
Chris Lattner00950542001-06-06 20:29:01 +0000676 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
677 ParserResult = 0;
678
679 return Result;
680}
681
682%}
683
684%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000685 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000686 Function *FunctionVal;
Chris Lattner69da5cf2002-10-13 20:57:00 +0000687 std::pair<PATypeHolder*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000688 BasicBlock *BasicBlockVal;
689 TerminatorInst *TermInstVal;
690 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000691 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000692
Chris Lattner30c89792001-09-07 16:35:17 +0000693 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000694 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000695 Value *ValueVal;
696
Chris Lattner69da5cf2002-10-13 20:57:00 +0000697 std::vector<std::pair<PATypeHolder*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000698 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000699 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000700 std::list<std::pair<Value*,
701 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000702 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000703 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000704
Chris Lattner4ad02e72003-04-16 20:28:45 +0000705 GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000706 int64_t SInt64Val;
707 uint64_t UInt64Val;
708 int SIntVal;
709 unsigned UIntVal;
710 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000711 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000712
Chris Lattner30c89792001-09-07 16:35:17 +0000713 char *StrVal; // This memory is strdup'd!
714 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000715
Chris Lattner30c89792001-09-07 16:35:17 +0000716 Instruction::BinaryOps BinaryOpVal;
717 Instruction::TermOps TermOpVal;
718 Instruction::MemoryOps MemOpVal;
719 Instruction::OtherOps OtherOpVal;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000720 Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000721}
722
Chris Lattner79df7c02002-03-26 18:01:55 +0000723%type <ModuleVal> Module FunctionList
724%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000725%type <BasicBlockVal> BasicBlock InstructionList
726%type <TermInstVal> BBTerminatorInst
727%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000728%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000729%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000730%type <ArgList> ArgList ArgListH
731%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000732%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000733%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000734%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000735%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000736%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000737%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000738%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000739%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000740%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000741
Chris Lattner2079fde2001-10-13 06:41:08 +0000742// ValueRef - Unresolved reference to a definition or BB
743%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000744%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000745// Tokens and types for handling constant integer values
746//
747// ESINT64VAL - A negative number within long long range
748%token <SInt64Val> ESINT64VAL
749
750// EUINT64VAL - A positive number within uns. long long range
751%token <UInt64Val> EUINT64VAL
752%type <SInt64Val> EINT64VAL
753
754%token <SIntVal> SINTVAL // Signed 32 bit ints...
755%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
756%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000757%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000758
759// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000760%type <TypeVal> Types TypesV UpRTypes UpRTypesV
761%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000762%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
763%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000764
Chris Lattner6c23f572003-08-22 05:42:10 +0000765%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
766%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000767
768
Chris Lattnerf4600482003-06-28 20:01:34 +0000769%token IMPLEMENTATION ZEROINITIALIZER TRUE FALSE BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000770%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnerf797cab2003-10-10 04:54:02 +0000771%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000772%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000773
774// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000775%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000776
Chris Lattner00950542001-06-06 20:29:01 +0000777// Binary Operators
778%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000779%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000780%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000781%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000782
783// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000784%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000785
Chris Lattner027dcc52001-07-08 21:10:27 +0000786// Other Operators
787%type <OtherOpVal> ShiftOps
Chris Lattner3b237fc2003-10-19 21:34:28 +0000788%token <OtherOpVal> PHI_TOK CALL CAST SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000789%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000790
Chris Lattner00950542001-06-06 20:29:01 +0000791%start Module
792%%
793
794// Handle constant integer size restriction and conversion...
795//
Chris Lattner51727be2002-06-04 21:58:56 +0000796INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000797INTVAL : UINTVAL {
798 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
799 ThrowException("Value too large for type!");
800 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000801};
Chris Lattner00950542001-06-06 20:29:01 +0000802
803
Chris Lattner51727be2002-06-04 21:58:56 +0000804EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000805EINT64VAL : EUINT64VAL {
806 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
807 ThrowException("Value too large for type!");
808 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000809};
Chris Lattner00950542001-06-06 20:29:01 +0000810
Chris Lattner00950542001-06-06 20:29:01 +0000811// Operations that are notably excluded from this list include:
812// RET, BR, & SWITCH because they end basic blocks and are treated specially.
813//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000814ArithmeticOps: ADD | SUB | MUL | DIV | REM;
815LogicalOps : AND | OR | XOR;
816SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
817BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
818
Chris Lattner51727be2002-06-04 21:58:56 +0000819ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000820
Chris Lattnere98dda62001-07-14 06:10:16 +0000821// These are some types that allow classification if we only want a particular
822// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000823SIntType : LONG | INT | SHORT | SBYTE;
824UIntType : ULONG | UINT | USHORT | UBYTE;
825IntType : SIntType | UIntType;
826FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000827
Chris Lattnere98dda62001-07-14 06:10:16 +0000828// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000829OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000830 $$ = $1;
831 }
832 | /*empty*/ {
833 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000834 };
Chris Lattner00950542001-06-06 20:29:01 +0000835
Chris Lattner4ad02e72003-04-16 20:28:45 +0000836OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
837 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000838 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000839 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
840 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000841
842//===----------------------------------------------------------------------===//
843// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000844// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000845// access to it, a user must explicitly use TypesV.
846//
847
848// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000849TypesV : Types | VOID { $$ = new PATypeHolder($1); };
850UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000851
852Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000853 if (UpRefs.size())
854 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
855 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000856 };
Chris Lattner30c89792001-09-07 16:35:17 +0000857
858
859// Derived types are added later...
860//
Chris Lattner51727be2002-06-04 21:58:56 +0000861PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
862PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000863UpRTypes : OPAQUE {
864 $$ = new PATypeHolder(OpaqueType::get());
865 }
866 | PrimType {
867 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000868 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000869UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000870 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000871};
Chris Lattner30c89792001-09-07 16:35:17 +0000872
Chris Lattner30c89792001-09-07 16:35:17 +0000873// Include derived types in the Types production.
874//
875UpRTypes : '\\' EUINT64VAL { // Type UpReference
876 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
877 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner9232b992003-04-22 18:42:41 +0000878 UpRefs.push_back(std::make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000879 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000880 UR_OUT("New Upreference!\n");
881 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000882 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000883 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000884 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000885 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000886 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
887 if (isVarArg) Params.pop_back();
888
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000889 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000890 delete $3; // Delete the argument list
891 delete $1; // Delete the old type handle
892 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000893 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000894 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000895 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000896 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000897 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000898 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000899 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +0000900 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000901
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000902 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000903 delete $2;
904 }
905 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000906 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000907 }
908 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000909 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000910 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000911 };
Chris Lattner30c89792001-09-07 16:35:17 +0000912
Chris Lattner7e708292002-06-25 16:13:24 +0000913// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000914// declaration type lists
915//
916TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +0000917 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000918 $$->push_back(*$1); delete $1;
919 }
920 | TypeListI ',' UpRTypes {
921 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000922 };
Chris Lattner30c89792001-09-07 16:35:17 +0000923
Chris Lattner7e708292002-06-25 16:13:24 +0000924// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000925ArgTypeListI : TypeListI
926 | TypeListI ',' DOTDOTDOT {
927 ($$=$1)->push_back(Type::VoidTy);
928 }
929 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +0000930 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000931 }
932 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +0000933 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000934 };
Chris Lattner30c89792001-09-07 16:35:17 +0000935
Chris Lattnere98dda62001-07-14 06:10:16 +0000936// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000937// production is used ONLY to represent constants that show up AFTER a 'const',
938// 'constant' or 'global' token at global scope. Constants that can be inlined
939// into other expressions (such as integers and constexprs) are handled by the
940// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000941//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000942ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +0000943 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000944 if (ATy == 0)
945 ThrowException("Cannot make array constant with type: '" +
946 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000947 const Type *ETy = ATy->getElementType();
948 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000949
Chris Lattner30c89792001-09-07 16:35:17 +0000950 // Verify that we have the correct size...
951 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000952 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000953 utostr($3->size()) + " arguments, but has size of " +
954 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000955
Chris Lattner30c89792001-09-07 16:35:17 +0000956 // Verify all elements are correct type!
957 for (unsigned i = 0; i < $3->size(); i++) {
958 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000959 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000960 ETy->getDescription() +"' as required!\nIt is of type '"+
961 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000962 }
963
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000964 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000965 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000966 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000967 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +0000968 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000969 if (ATy == 0)
970 ThrowException("Cannot make array constant with type: '" +
971 (*$1)->getDescription() + "'!");
972
973 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000974 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000975 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000976 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +0000977 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000978 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000979 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000980 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +0000981 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000982 if (ATy == 0)
983 ThrowException("Cannot make array constant with type: '" +
984 (*$1)->getDescription() + "'!");
985
Chris Lattner30c89792001-09-07 16:35:17 +0000986 int NumElements = ATy->getNumElements();
987 const Type *ETy = ATy->getElementType();
988 char *EndStr = UnEscapeLexed($3, true);
989 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000990 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000991 itostr((int)(EndStr-$3)) +
992 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +0000993 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000994 if (ETy == Type::SByteTy) {
995 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000996 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000997 } else if (ETy == Type::UByteTy) {
998 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +0000999 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001000 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001001 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001002 ThrowException("Cannot build string arrays of non byte sized elements!");
1003 }
Chris Lattner30c89792001-09-07 16:35:17 +00001004 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001005 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001006 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001007 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001008 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001009 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001010 if (STy == 0)
1011 ThrowException("Cannot make struct constant with type: '" +
1012 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001013
Chris Lattnerc6212f12003-05-21 16:06:56 +00001014 if ($3->size() != STy->getNumContainedTypes())
1015 ThrowException("Illegal number of initializers for structure type!");
1016
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001017 // Check to ensure that constants are compatible with the type initializer!
1018 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1019 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
1020 ThrowException("Expected type '" +
1021 STy->getElementTypes()[i]->getDescription() +
1022 "' for element #" + utostr(i) +
1023 " of structure initializer!");
1024
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001025 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001026 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001027 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001028 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001029 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001030 if (STy == 0)
1031 ThrowException("Cannot make struct constant with type: '" +
1032 (*$1)->getDescription() + "'!");
1033
1034 if (STy->getNumContainedTypes() != 0)
1035 ThrowException("Illegal number of initializers for structure type!");
1036
1037 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1038 delete $1;
1039 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001040 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001041 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001042 if (PTy == 0)
1043 ThrowException("Cannot make null pointer constant with type: '" +
1044 (*$1)->getDescription() + "'!");
1045
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001046 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001047 delete $1;
1048 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001049 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001050 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001051 if (Ty == 0)
1052 ThrowException("Global const reference must be a pointer type!");
1053
Chris Lattner3101c252002-08-15 17:58:33 +00001054 // ConstExprs can exist in the body of a function, thus creating
1055 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1056 // the context of a function, getValNonImprovising will search the functions
1057 // symbol table instead of the module symbol table for the global symbol,
1058 // which throws things all off. To get around this, we just tell
1059 // getValNonImprovising that we are at global scope here.
1060 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001061 Function *SavedCurFn = CurFun.CurrentFunction;
1062 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001063
Chris Lattner2079fde2001-10-13 06:41:08 +00001064 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001065
Chris Lattner394f2eb2003-10-16 20:12:13 +00001066 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001067
Chris Lattner2079fde2001-10-13 06:41:08 +00001068 // If this is an initializer for a constant pointer, which is referencing a
1069 // (currently) undefined variable, create a stub now that shall be replaced
1070 // in the future with the right type of variable.
1071 //
1072 if (V == 0) {
1073 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1074 const PointerType *PT = cast<PointerType>(Ty);
1075
1076 // First check to see if the forward references value is already created!
1077 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001078 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001079
1080 if (I != CurModule.GlobalRefs.end()) {
1081 V = I->second; // Placeholder already exists, use it...
1082 } else {
1083 // TODO: Include line number info by creating a subclass of
1084 // TODO: GlobalVariable here that includes the said information!
1085
1086 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001087 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001088 false,
1089 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001090 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001091 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001092
1093 // Must temporarily push this value into the module table...
1094 CurModule.CurrentModule->getGlobalList().push_back(GV);
1095 V = GV;
1096 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001097 }
1098
Chris Lattner2079fde2001-10-13 06:41:08 +00001099 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001100 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001101 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001102 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001103 | Types ConstExpr {
1104 if ($1->get() != $2->getType())
1105 ThrowException("Mismatched types for constant expression!");
1106 $$ = $2;
1107 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001108 }
1109 | Types ZEROINITIALIZER {
1110 $$ = Constant::getNullValue($1->get());
1111 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001112 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001113
Chris Lattnere43f40b2002-10-09 00:25:32 +00001114ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001115 if (!ConstantSInt::isValueValidForType($1, $2))
1116 ThrowException("Constant value doesn't fit in type!");
1117 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001118 }
1119 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001120 if (!ConstantUInt::isValueValidForType($1, $2))
1121 ThrowException("Constant value doesn't fit in type!");
1122 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001123 }
1124 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001125 $$ = ConstantBool::True;
1126 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001127 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001128 $$ = ConstantBool::False;
1129 }
1130 | FPType FPVAL { // Float & Double constants
1131 $$ = ConstantFP::get($1, $2);
1132 };
1133
Chris Lattner00950542001-06-06 20:29:01 +00001134
Chris Lattnerd78700d2002-08-16 21:14:40 +00001135ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001136 if (!$5->get()->isFirstClassType())
1137 ThrowException("cast constant expression to a non-primitive type: '" +
1138 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001139 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001140 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001141 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001142 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1143 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001144 ThrowException("GetElementPtr requires a pointer operand!");
1145
1146 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001147 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001148 if (!IdxTy)
1149 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001150
Chris Lattner9232b992003-04-22 18:42:41 +00001151 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001152 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1153 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001154 IdxVec.push_back(C);
1155 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001156 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001157
Chris Lattnerd78700d2002-08-16 21:14:40 +00001158 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001159
Chris Lattnerd78700d2002-08-16 21:14:40 +00001160 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001161 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001162 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001163 if ($3->getType() != $5->getType())
1164 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001165 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001166 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001167 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001168 if ($5->getType() != Type::UByteTy)
1169 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001170 if (!$3->getType()->isInteger())
1171 ThrowException("Shift constant expression requires integer operand!");
Chris Lattner5e458e22003-05-21 17:48:56 +00001172 $$ = ConstantExpr::getShift($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001173 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001174
1175
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001176// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001177ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001178 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001179 }
1180 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001181 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001182 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001183 };
Chris Lattner00950542001-06-06 20:29:01 +00001184
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001185
Chris Lattner1781aca2001-09-18 04:00:54 +00001186// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001187GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001188
Chris Lattner00950542001-06-06 20:29:01 +00001189
Chris Lattner0e73ce62002-05-02 19:11:13 +00001190//===----------------------------------------------------------------------===//
1191// Rules to match Modules
1192//===----------------------------------------------------------------------===//
1193
1194// Module rule: Capture the result of parsing the whole file into a result
1195// variable...
1196//
1197Module : FunctionList {
1198 $$ = ParserResult = $1;
1199 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001200};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001201
Chris Lattner7e708292002-06-25 16:13:24 +00001202// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001203//
1204FunctionList : FunctionList Function {
1205 $$ = $1;
1206 assert($2->getParent() == 0 && "Function already in module!");
1207 $1->getFunctionList().push_back($2);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001208 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001209 }
1210 | FunctionList FunctionProto {
1211 $$ = $1;
1212 }
1213 | FunctionList IMPLEMENTATION {
1214 $$ = $1;
1215 }
1216 | ConstPool {
1217 $$ = CurModule.CurrentModule;
1218 // Resolve circular types before we parse the body of the module
1219 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001220 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001221
Chris Lattnere98dda62001-07-14 06:10:16 +00001222// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001223ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001224 if (!setValueName($4, $2))
1225 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001226 }
Chris Lattner30c89792001-09-07 16:35:17 +00001227 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001228 // Eagerly resolve types. This is not an optimization, this is a
1229 // requirement that is due to the fact that we could have this:
1230 //
1231 // %list = type { %list * }
1232 // %list = type { %list * } ; repeated type decl
1233 //
1234 // If types are not resolved eagerly, then the two types will not be
1235 // determined to be the same type!
1236 //
1237 ResolveTypeTo($2, $4->get());
1238
Chris Lattner1781aca2001-09-18 04:00:54 +00001239 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001240 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1241 // If this is not a redefinition of a type...
1242 if (!$2) {
1243 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001244 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001245 }
Chris Lattner30c89792001-09-07 16:35:17 +00001246 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001247
1248 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001249 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001250 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001251 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001252 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001253 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001254 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001255 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001256 if (Initializer == 0)
1257 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001258
Chris Lattnerdda71962001-11-26 18:54:16 +00001259 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001260 if (!setValueName(GV, $2)) { // If not redefining...
1261 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001262 int Slot = InsertValue(GV, CurModule.Values);
1263
1264 if (Slot != -1) {
1265 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1266 } else {
1267 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1268 (char*)GV->getName().c_str()));
1269 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001270 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001271 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001272 | ConstPool OptAssign EXTERNAL GlobalType Types {
1273 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001274 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001275 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001276 if (!setValueName(GV, $2)) { // If not redefining...
1277 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001278 int Slot = InsertValue(GV, CurModule.Values);
1279
1280 if (Slot != -1) {
1281 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1282 } else {
1283 assert(GV->hasName() && "Not named and not numbered!?");
1284 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1285 (char*)GV->getName().c_str()));
1286 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001287 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001288 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001289 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001290 | ConstPool TARGET TargetDefinition {
1291 }
Chris Lattner00950542001-06-06 20:29:01 +00001292 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001293 };
Chris Lattner00950542001-06-06 20:29:01 +00001294
1295
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001296
1297BigOrLittle : BIG { $$ = Module::BigEndian; };
1298BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1299
1300TargetDefinition : ENDIAN '=' BigOrLittle {
1301 CurModule.CurrentModule->setEndianness($3);
1302 }
1303 | POINTERSIZE '=' EUINT64VAL {
1304 if ($3 == 32)
1305 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1306 else if ($3 == 64)
1307 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1308 else
1309 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1310 };
1311
1312
Chris Lattner00950542001-06-06 20:29:01 +00001313//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001314// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001315//===----------------------------------------------------------------------===//
1316
Chris Lattner6c23f572003-08-22 05:42:10 +00001317Name : VAR_ID | STRINGCONSTANT;
1318OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001319
Chris Lattner6c23f572003-08-22 05:42:10 +00001320ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001321 if (*$1 == Type::VoidTy)
1322 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001323 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001324};
Chris Lattner00950542001-06-06 20:29:01 +00001325
Chris Lattner69da5cf2002-10-13 20:57:00 +00001326ArgListH : ArgListH ',' ArgVal {
1327 $$ = $1;
1328 $1->push_back(*$3);
1329 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001330 }
1331 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001332 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001333 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001334 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001335 };
Chris Lattner00950542001-06-06 20:29:01 +00001336
1337ArgList : ArgListH {
1338 $$ = $1;
1339 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001340 | ArgListH ',' DOTDOTDOT {
1341 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001342 $$->push_back(std::pair<PATypeHolder*,
1343 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001344 }
1345 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001346 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1347 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001348 }
Chris Lattner00950542001-06-06 20:29:01 +00001349 | /* empty */ {
1350 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001351 };
Chris Lattner00950542001-06-06 20:29:01 +00001352
Chris Lattner6c23f572003-08-22 05:42:10 +00001353FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001354 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001355 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001356
Chris Lattner9232b992003-04-22 18:42:41 +00001357 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001358 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001359 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001360 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001361 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001362 }
Chris Lattner00950542001-06-06 20:29:01 +00001363
Chris Lattner2079fde2001-10-13 06:41:08 +00001364 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1365 if (isVarArg) ParamTypeList.pop_back();
1366
Chris Lattner1f862af2003-04-16 18:13:57 +00001367 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001368 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001369 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001370
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001371 Function *Fn = 0;
1372 // Is the function already in symtab?
1373 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1374 // Yes it is. If this is the case, either we need to be a forward decl,
1375 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001376 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001377 ThrowException("Redefinition of function '" + FunctionName + "'!");
1378
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001379 // If we found a preexisting function prototype, remove it from the
1380 // module, so that we don't get spurious conflicts with global & local
1381 // variables.
1382 //
1383 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001384
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001385 // Make sure to strip off any argument names so we can't get conflicts...
1386 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1387 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001388
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001389 } else { // Not already defined?
Chris Lattner4ad02e72003-04-16 20:28:45 +00001390 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001391 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001392 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001393 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001394 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001395
Chris Lattner394f2eb2003-10-16 20:12:13 +00001396 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001397
Chris Lattner7e708292002-06-25 16:13:24 +00001398 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001399 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001400 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001401 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001402 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001403 delete $4->back().first;
1404 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001405 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001406 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001407 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001408 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001409 delete I->first; // Delete the typeholder...
1410
1411 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001412 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001413
Chris Lattner69da5cf2002-10-13 20:57:00 +00001414 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001415 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001416
Chris Lattner1f862af2003-04-16 18:13:57 +00001417 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001418 }
Chris Lattner51727be2002-06-04 21:58:56 +00001419};
Chris Lattner00950542001-06-06 20:29:01 +00001420
Chris Lattner9b02cc32002-05-03 18:23:48 +00001421BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1422
Chris Lattner4ad02e72003-04-16 20:28:45 +00001423FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001424 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001425
Chris Lattner4ad02e72003-04-16 20:28:45 +00001426 // Make sure that we keep track of the linkage type even if there was a
1427 // previous "declare".
1428 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001429
Chris Lattner7e708292002-06-25 16:13:24 +00001430 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001431 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001432};
Chris Lattner00950542001-06-06 20:29:01 +00001433
Chris Lattner9b02cc32002-05-03 18:23:48 +00001434END : ENDTOK | '}'; // Allow end of '}' to end a function
1435
Chris Lattner79df7c02002-03-26 18:01:55 +00001436Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001437 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001438};
Chris Lattner00950542001-06-06 20:29:01 +00001439
Chris Lattner394f2eb2003-10-16 20:12:13 +00001440FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1441 $$ = CurFun.CurrentFunction;
Chris Lattner79df7c02002-03-26 18:01:55 +00001442 assert($$->getParent() == 0 && "Function already in module!");
1443 CurModule.CurrentModule->getFunctionList().push_back($$);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001444 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001445};
Chris Lattner00950542001-06-06 20:29:01 +00001446
1447//===----------------------------------------------------------------------===//
1448// Rules to match Basic Blocks
1449//===----------------------------------------------------------------------===//
1450
1451ConstValueRef : ESINT64VAL { // A reference to a direct constant
1452 $$ = ValID::create($1);
1453 }
1454 | EUINT64VAL {
1455 $$ = ValID::create($1);
1456 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001457 | FPVAL { // Perhaps it's an FP constant?
1458 $$ = ValID::create($1);
1459 }
Chris Lattner00950542001-06-06 20:29:01 +00001460 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001461 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001462 }
1463 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001464 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001465 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001466 | NULL_TOK {
1467 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001468 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001469 | ConstExpr {
1470 $$ = ValID::create($1);
1471 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001472
Chris Lattner2079fde2001-10-13 06:41:08 +00001473// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1474// another value.
1475//
1476SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001477 $$ = ValID::create($1);
1478 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001479 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001480 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001481 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001482
1483// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001484ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001485
Chris Lattner00950542001-06-06 20:29:01 +00001486
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001487// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1488// type immediately preceeds the value reference, and allows complex constant
1489// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001490ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001491 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001492 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001493
Chris Lattner00950542001-06-06 20:29:01 +00001494BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001495 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001496 }
Chris Lattner7e708292002-06-25 16:13:24 +00001497 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1498 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001499 };
Chris Lattner00950542001-06-06 20:29:01 +00001500
1501
1502// Basic blocks are terminated by branching instructions:
1503// br, br/cc, switch, ret
1504//
Chris Lattner2079fde2001-10-13 06:41:08 +00001505BasicBlock : InstructionList OptAssign BBTerminatorInst {
1506 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1507 InsertValue($3);
1508
1509 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001510 InsertValue($1);
1511 $$ = $1;
1512 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001513 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1514 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1515 InsertValue($4);
1516
1517 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001518 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001519
1520 InsertValue($2);
1521 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001522 };
Chris Lattner00950542001-06-06 20:29:01 +00001523
1524InstructionList : InstructionList Inst {
1525 $1->getInstList().push_back($2);
1526 $$ = $1;
1527 }
1528 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001529 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001530 };
Chris Lattner00950542001-06-06 20:29:01 +00001531
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001532BBTerminatorInst : RET ResolvedVal { // Return with a result...
1533 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001534 }
1535 | RET VOID { // Return with no result...
1536 $$ = new ReturnInst();
1537 }
1538 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001539 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001540 } // Conditional Branch...
1541 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001542 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1543 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001544 getVal(Type::BoolTy, $3));
1545 }
1546 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1547 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001548 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001549 $$ = S;
1550
Chris Lattner9232b992003-04-22 18:42:41 +00001551 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001552 E = $8->end();
1553 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001554 S->addCase(I->first, I->second);
Chris Lattner00950542001-06-06 20:29:01 +00001555 }
Chris Lattner196850c2003-05-15 21:30:00 +00001556 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1557 SwitchInst *S = new SwitchInst(getVal($2, $3),
1558 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1559 $$ = S;
1560 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001561 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1562 EXCEPT ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001563 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001564 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001565
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001566 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1567 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001568 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001569 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001570 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001571 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1572 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001573 ParamTypes.push_back((*I)->getType());
1574 }
1575
1576 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1577 if (isVarArg) ParamTypes.pop_back();
1578
Chris Lattner79df7c02002-03-26 18:01:55 +00001579 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001580 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001581 }
1582 delete $2;
1583
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001584 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001585
1586 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1587 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1588
1589 if (Normal == 0 || Except == 0)
1590 ThrowException("Invoke instruction without label destinations!");
1591
1592 // Create the call node...
1593 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001594 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001595 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001596 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001597 // correctly!
1598 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001599 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1600 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001601 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001602
1603 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1604 if ((*ArgI)->getType() != *I)
1605 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001606 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001607
1608 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1609 ThrowException("Invalid number of parameters detected!");
1610
Chris Lattner6cdb0112001-11-26 16:54:11 +00001611 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001612 }
1613 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001614 }
1615 | UNWIND {
1616 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001617 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001618
1619
Chris Lattner00950542001-06-06 20:29:01 +00001620
1621JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1622 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001623 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001624 if (V == 0)
1625 ThrowException("May only switch on a constant pool value!");
1626
Chris Lattner9232b992003-04-22 18:42:41 +00001627 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001628 }
1629 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001630 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001631 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001632
1633 if (V == 0)
1634 ThrowException("May only switch on a constant pool value!");
1635
Chris Lattner9232b992003-04-22 18:42:41 +00001636 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001637 };
Chris Lattner00950542001-06-06 20:29:01 +00001638
1639Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001640 // Is this definition named?? if so, assign the name...
1641 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001642 InsertValue($2);
1643 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001644};
Chris Lattner00950542001-06-06 20:29:01 +00001645
Chris Lattnerc24d2082001-06-11 15:04:20 +00001646PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001647 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1648 $$->push_back(std::make_pair(getVal(*$1, $3),
1649 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001650 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001651 }
1652 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1653 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001654 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1655 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001656 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001657
1658
Chris Lattner30c89792001-09-07 16:35:17 +00001659ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001660 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001661 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001662 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001663 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001664 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001665 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001666 };
Chris Lattner00950542001-06-06 20:29:01 +00001667
1668// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001669ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001670
Chris Lattner4a6482b2002-09-10 19:57:26 +00001671InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1672 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1673 ThrowException("Arithmetic operator requires integer or FP operands!");
1674 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1675 if ($$ == 0)
1676 ThrowException("binary operator returned null!");
1677 delete $2;
1678 }
1679 | LogicalOps Types ValueRef ',' ValueRef {
1680 if (!(*$2)->isIntegral())
1681 ThrowException("Logical operator requires integral operands!");
1682 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1683 if ($$ == 0)
1684 ThrowException("binary operator returned null!");
1685 delete $2;
1686 }
1687 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001688 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001689 if ($$ == 0)
1690 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001691 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001692 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001693 | NOT ResolvedVal {
1694 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1695 << " Replacing with 'xor'.\n";
1696
1697 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1698 if (Ones == 0)
1699 ThrowException("Expected integral type for not instruction!");
1700
1701 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001702 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001703 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001704 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001705 | ShiftOps ResolvedVal ',' ResolvedVal {
1706 if ($4->getType() != Type::UByteTy)
1707 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001708 if (!$2->getType()->isInteger())
1709 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001710 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001711 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001712 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001713 if (!$4->get()->isFirstClassType())
1714 ThrowException("cast instruction to a non-primitive type: '" +
1715 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001716 $$ = new CastInst($2, *$4);
1717 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001718 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001719 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001720 // FIXME: This is emulation code for an obsolete syntax. This should be
1721 // removed at some point.
1722 if (!ObsoleteVarArgs) {
1723 std::cerr << "WARNING: this file uses obsolete features. "
1724 << "Assemble and disassemble to update it.\n";
1725 ObsoleteVarArgs = true;
1726 }
1727
1728 // First, load the valist...
1729 Instruction *CurVAList = new LoadInst($2, "");
1730 CurBB->getInstList().push_back(CurVAList);
1731
1732 // Emit the vaarg instruction.
1733 $$ = new VAArgInst(CurVAList, *$4);
1734
1735 // Now we must advance the pointer and update it in memory.
1736 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1737 CurBB->getInstList().push_back(TheVANext);
1738
1739 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1740 delete $4;
1741 }
1742 | VAARG ResolvedVal ',' Types {
1743 $$ = new VAArgInst($2, *$4);
1744 delete $4;
1745 }
1746 | VANEXT ResolvedVal ',' Types {
1747 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001748 delete $4;
1749 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001750 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001751 const Type *Ty = $2->front().first->getType();
1752 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001753 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001754 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001755 if ($2->front().first->getType() != Ty)
1756 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001757 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001758 $2->pop_front();
1759 }
1760 delete $2; // Free the list...
1761 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001762 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001763 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001764 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001765
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001766 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1767 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001768 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001769 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001770 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001771 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1772 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001773 ParamTypes.push_back((*I)->getType());
1774 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001775
1776 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1777 if (isVarArg) ParamTypes.pop_back();
1778
Chris Lattner79df7c02002-03-26 18:01:55 +00001779 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001780 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001781 }
Chris Lattner30c89792001-09-07 16:35:17 +00001782 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001783
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001784 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001785
Chris Lattner8b81bf52001-07-25 22:47:46 +00001786 // Create the call node...
1787 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001788 // Make sure no arguments is a good thing!
1789 if (Ty->getNumParams() != 0)
1790 ThrowException("No arguments passed to a function that "
1791 "expects arguments!");
1792
Chris Lattner9232b992003-04-22 18:42:41 +00001793 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001794 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001795 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001796 // correctly!
1797 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001798 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1799 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001800 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001801
1802 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1803 if ((*ArgI)->getType() != *I)
1804 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001805 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001806
Chris Lattner8b81bf52001-07-25 22:47:46 +00001807 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001808 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001809
Chris Lattner6cdb0112001-11-26 16:54:11 +00001810 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001811 }
1812 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001813 }
1814 | MemoryInst {
1815 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001816 };
Chris Lattner00950542001-06-06 20:29:01 +00001817
Chris Lattner6cdb0112001-11-26 16:54:11 +00001818
1819// IndexList - List of indices for GEP based instructions...
1820IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001821 $$ = $2;
1822 } | /* empty */ {
1823 $$ = new std::vector<Value*>();
1824 };
1825
1826OptVolatile : VOLATILE {
1827 $$ = true;
1828 }
1829 | /* empty */ {
1830 $$ = false;
1831 };
1832
Chris Lattner027dcc52001-07-08 21:10:27 +00001833
Chris Lattner00950542001-06-06 20:29:01 +00001834MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001835 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001836 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001837 }
1838 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001839 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001840 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001841 }
1842 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001843 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001844 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001845 }
1846 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001847 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001848 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001849 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001850 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001851 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001852 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001853 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001854 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001855 }
1856
Chris Lattner15c9c032003-09-08 18:20:29 +00001857 | OptVolatile LOAD Types ValueRef {
1858 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001859 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001860 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001861 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001862 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001863 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001864 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1865 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001866 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001867 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001868 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001869 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00001870 if (ElTy != $3->getType())
1871 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00001872 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001873
Chris Lattner79ad13802003-09-08 20:29:46 +00001874 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001875 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001876 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001877 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001878 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001879 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001880 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001881 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001882 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1883 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001884 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001885
Chris Lattner00950542001-06-06 20:29:01 +00001886%%
Chris Lattner09083092001-07-08 04:57:15 +00001887int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00001888 std::string where
1889 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001890 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00001891 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001892 if (yychar == YYEMPTY)
1893 errMsg += "end-of-file.";
1894 else
Chris Lattner9232b992003-04-22 18:42:41 +00001895 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001896 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001897 return 0;
1898}