blob: 19cda682a16d1bf2fdf8f3e8d883bec54632d4b0 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
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 Lattner0383cc42002-08-21 23:51:21 +000040// HACK ALERT: This variable is used to implement the automatic conversion of
41// load/store instructions with indexes into a load/store + getelementptr pair
42// of instructions. When this compatiblity "Feature" is removed, this should be
43// too.
44//
45static BasicBlock *CurBB;
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...
110 I->first.second.destroy(); // Free string memory if neccesary
111
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 Lattner2079fde2001-10-13 06:41:08 +0000114 assert(OldGV->use_size() == 1 && "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 ~PerFunctionInfo() {}
Chris Lattner00950542001-06-06 20:29:01 +0000149
Chris Lattner79df7c02002-03-26 18:01:55 +0000150 inline void FunctionStart(Function *M) {
151 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000152 }
153
Chris Lattner79df7c02002-03-26 18:01:55 +0000154 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000155 // If we could not resolve some blocks at parsing time (forward branches)
156 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000157 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000158
Chris Lattner7e708292002-06-25 16:13:24 +0000159 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000160 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000161 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000162 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000163 }
Chris Lattner7e708292002-06-25 16:13:24 +0000164} CurMeth; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000165
Chris Lattner79df7c02002-03-26 18:01:55 +0000166static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000167
Chris Lattner00950542001-06-06 20:29:01 +0000168
169//===----------------------------------------------------------------------===//
170// Code to handle definitions of all the types
171//===----------------------------------------------------------------------===//
172
Chris Lattner9232b992003-04-22 18:42:41 +0000173static int InsertValue(Value *D,
174 std::vector<ValueList> &ValueTab = CurMeth.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000175 if (D->hasName()) return -1; // Is this a numbered definition?
176
177 // Yes, insert the value into the value table...
178 unsigned type = D->getType()->getUniqueID();
179 if (ValueTab.size() <= type)
180 ValueTab.resize(type+1, ValueList());
181 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
182 ValueTab[type].push_back(D);
183 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000184}
185
Chris Lattner30c89792001-09-07 16:35:17 +0000186// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000187static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000188 Types.push_back(Ty);
189}
190
191static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000192 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000193 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000194 unsigned Num = (unsigned)D.Num;
195
196 // Module constants occupy the lowest numbered slots...
197 if (Num < CurModule.Types.size())
198 return CurModule.Types[Num];
199
200 Num -= CurModule.Types.size();
201
202 // Check that the number is within bounds...
203 if (Num <= CurMeth.Types.size())
204 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000205 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000206 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000207 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000208 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000209 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000210 Value *N = 0;
211 if (inFunctionScope()) {
212 SymTab = &CurMeth.CurrentFunction->getSymbolTable();
213 N = SymTab->lookup(Type::TypeTy, Name);
214 }
Chris Lattner30c89792001-09-07 16:35:17 +0000215
216 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000217 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000218 // hasn't been added to the module...
219 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000220 SymTab = &CurModule.CurrentModule->getSymbolTable();
221 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000222 if (N == 0) break;
223 }
224
225 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000226 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000227 }
228 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000229 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000230 }
231
232 // If we reached here, we referenced either a symbol that we don't know about
233 // or an id number that hasn't been read yet. We may be referencing something
234 // forward, so just create an entry to be resolved later and get to it...
235 //
236 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
237
Chris Lattner9232b992003-04-22 18:42:41 +0000238 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000239 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
240
Chris Lattner9232b992003-04-22 18:42:41 +0000241 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000242 if (I != LateResolver.end()) {
243 return I->second;
244 }
Chris Lattner30c89792001-09-07 16:35:17 +0000245
Chris Lattner82269592001-10-22 06:01:08 +0000246 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000247 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000248 return Typ;
249}
250
Chris Lattner9232b992003-04-22 18:42:41 +0000251static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000252 SymbolTable &SymTab =
Chris Lattner9705a152002-05-02 19:27:42 +0000253 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
254 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000255 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000256}
257
Chris Lattner2079fde2001-10-13 06:41:08 +0000258// getValNonImprovising - Look up the value specified by the provided type and
259// the provided ValID. If the value exists and has already been defined, return
260// it. Otherwise return null.
261//
262static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000263 if (isa<FunctionType>(Ty))
264 ThrowException("Functions are not values and "
265 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000266
Chris Lattner30c89792001-09-07 16:35:17 +0000267 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000268 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000269 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000270 unsigned Num = (unsigned)D.Num;
271
272 // Module constants occupy the lowest numbered slots...
273 if (type < CurModule.Values.size()) {
274 if (Num < CurModule.Values[type].size())
275 return CurModule.Values[type][Num];
276
277 Num -= CurModule.Values[type].size();
278 }
279
280 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000281 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000282
283 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000284 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000285
286 return CurMeth.Values[type][Num];
287 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000288
Chris Lattner1a1cb112001-09-30 22:46:54 +0000289 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000290 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000291 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000292
293 D.destroy(); // Free old strdup'd memory...
294 return N;
295 }
296
Chris Lattner2079fde2001-10-13 06:41:08 +0000297 // Check to make sure that "Ty" is an integral type, and that our
298 // value will fit into the specified type...
299 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000300 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
301 ThrowException("Signed integral constant '" +
302 itostr(D.ConstPool64) + "' is invalid for type '" +
303 Ty->getDescription() + "'!");
304 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000305
306 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000307 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
308 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000309 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
310 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000311 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000312 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000313 }
314 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000315 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000316 }
317
Chris Lattner2079fde2001-10-13 06:41:08 +0000318 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000319 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000320 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000321 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000322
323 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000324 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000325 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000326 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000327
Chris Lattnerd78700d2002-08-16 21:14:40 +0000328 case ValID::ConstantVal: // Fully resolved constant?
329 if (D.ConstantValue->getType() != Ty)
330 ThrowException("Constant expression type different from required type!");
331 return D.ConstantValue;
332
Chris Lattner30c89792001-09-07 16:35:17 +0000333 default:
334 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000335 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000336 } // End of switch
337
Chris Lattner2079fde2001-10-13 06:41:08 +0000338 assert(0 && "Unhandled case!");
339 return 0;
340}
341
342
343// getVal - This function is identical to getValNonImprovising, except that if a
344// value is not already defined, it "improvises" by creating a placeholder var
345// that looks and acts just like the requested variable. When the value is
346// defined later, all uses of the placeholder variable are replaced with the
347// real thing.
348//
349static Value *getVal(const Type *Ty, const ValID &D) {
350 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
351
352 // See if the value has already been defined...
353 Value *V = getValNonImprovising(Ty, D);
354 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000355
356 // If we reached here, we referenced either a symbol that we don't know about
357 // or an id number that hasn't been read yet. We may be referencing something
358 // forward, so just create an entry to be resolved later and get to it...
359 //
Chris Lattner00950542001-06-06 20:29:01 +0000360 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000361 switch (Ty->getPrimitiveID()) {
362 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000363 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000364 }
365
366 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000367 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000368 InsertValue(d, CurMeth.LateResolveValues);
369 else
370 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000371 return d;
372}
373
374
375//===----------------------------------------------------------------------===//
376// Code to handle forward references in instructions
377//===----------------------------------------------------------------------===//
378//
379// This code handles the late binding needed with statements that reference
380// values not defined yet... for example, a forward branch, or the PHI node for
381// a loop body.
382//
383// This keeps a table (CurMeth.LateResolveValues) of all such forward references
384// and back patchs after we are done.
385//
386
387// ResolveDefinitions - If we could not resolve some defs at parsing
388// time (forward branches, phi functions for loops, etc...) resolve the
389// defs now...
390//
Chris Lattner9232b992003-04-22 18:42:41 +0000391static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
392 std::vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000393 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
394 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
395 while (!LateResolvers[ty].empty()) {
396 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000397 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
398
Chris Lattner00950542001-06-06 20:29:01 +0000399 LateResolvers[ty].pop_back();
400 ValID &DID = getValIDFromPlaceHolder(V);
401
Chris Lattner2079fde2001-10-13 06:41:08 +0000402 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000403 if (TheRealValue) {
404 V->replaceAllUsesWith(TheRealValue);
405 delete V;
406 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000407 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000408 // resolver table
409 InsertValue(V, *FutureLateResolvers);
410 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000411 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000412 ThrowException("Reference to an invalid definition: '" +DID.getName()+
413 "' of type '" + V->getType()->getDescription() + "'",
414 getLineNumFromPlaceHolder(V));
415 else
416 ThrowException("Reference to an invalid definition: #" +
417 itostr(DID.Num) + " of type '" +
418 V->getType()->getDescription() + "'",
419 getLineNumFromPlaceHolder(V));
420 }
Chris Lattner00950542001-06-06 20:29:01 +0000421 }
422 }
423
424 LateResolvers.clear();
425}
426
Chris Lattner4a42e902001-10-22 05:56:09 +0000427// ResolveTypeTo - A brand new type was just declared. This means that (if
428// name is not null) things referencing Name can be resolved. Otherwise, things
429// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000430//
Chris Lattner4a42e902001-10-22 05:56:09 +0000431static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000432 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000433 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000434
Chris Lattner4a42e902001-10-22 05:56:09 +0000435 ValID D;
436 if (Name) D = ValID::create(Name);
437 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000438
Chris Lattner9232b992003-04-22 18:42:41 +0000439 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000440 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
441
Chris Lattner9232b992003-04-22 18:42:41 +0000442 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000443 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000444 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000445 LateResolver.erase(I);
446 }
447}
448
449// ResolveTypes - At this point, all types should be resolved. Any that aren't
450// are errors.
451//
Chris Lattner9232b992003-04-22 18:42:41 +0000452static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000453 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000454 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000455
456 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000457 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000458 else
Chris Lattner82269592001-10-22 06:01:08 +0000459 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000460 }
461}
462
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000463
Chris Lattner1781aca2001-09-18 04:00:54 +0000464// setValueName - Set the specified value to the name given. The name may be
465// null potentially, in which case this is a noop. The string passed in is
466// assumed to be a malloc'd string buffer, and is freed by this function.
467//
Chris Lattnerb7474512001-10-03 15:39:04 +0000468// This function returns true if the value has already been defined, but is
469// allowed to be redefined in the specified context. If the name is a new name
470// for the typeplane, false is returned.
471//
472static bool setValueName(Value *V, char *NameStr) {
473 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000474
Chris Lattner9232b992003-04-22 18:42:41 +0000475 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000476 free(NameStr); // Free old string
477
Chris Lattner2079fde2001-10-13 06:41:08 +0000478 if (V->getType() == Type::VoidTy)
479 ThrowException("Can't assign name '" + Name +
480 "' to a null valued instruction!");
481
Chris Lattner6e6026b2002-11-20 18:36:02 +0000482 SymbolTable &ST = inFunctionScope() ?
483 CurMeth.CurrentFunction->getSymbolTable() :
484 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000485
Chris Lattner6e6026b2002-11-20 18:36:02 +0000486 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000487 if (Existing) { // Inserting a name that is already defined???
488 // There is only one case where this is allowed: when we are refining an
489 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000490 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000491 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000492 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000493 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000494 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000495 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000496 }
Chris Lattner30c89792001-09-07 16:35:17 +0000497
Chris Lattner9636a912001-10-01 16:18:37 +0000498 // Otherwise, we are a simple redefinition of a value, check to see if it
499 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000500 if (const Type *Ty = dyn_cast<Type>(Existing)) {
501 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000502 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattnerb7474512001-10-03 15:39:04 +0000503 // << cast<const Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000504 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
505 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000506 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000507 // We are allowed to redefine a global variable in two circumstances:
508 // 1. If at least one of the globals is uninitialized or
509 // 2. If both initializers have the same value.
510 //
Chris Lattner89219832001-10-03 19:35:04 +0000511 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000512 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
513 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000514
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000515 // Make sure the existing global version gets the initializer! Make
516 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000517 if (GV->hasInitializer() && !EGV->hasInitializer())
518 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000519 if (GV->isConstant())
520 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000521 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000522
Chris Lattner2079fde2001-10-13 06:41:08 +0000523 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000524 return true; // They are equivalent!
525 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000526 }
Chris Lattner9636a912001-10-01 16:18:37 +0000527 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000528 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000529 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000530 }
Chris Lattner00950542001-06-06 20:29:01 +0000531
Chris Lattner6e6026b2002-11-20 18:36:02 +0000532 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000533 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000534}
535
Chris Lattner8896eda2001-07-09 19:38:36 +0000536
Chris Lattner30c89792001-09-07 16:35:17 +0000537//===----------------------------------------------------------------------===//
538// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000539//
Chris Lattner8896eda2001-07-09 19:38:36 +0000540
Chris Lattner30c89792001-09-07 16:35:17 +0000541// TypeContains - Returns true if Ty contains E in it.
542//
543static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000544 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000545}
Chris Lattner698b56e2001-07-20 19:15:08 +0000546
Chris Lattner30c89792001-09-07 16:35:17 +0000547
Chris Lattner9232b992003-04-22 18:42:41 +0000548static std::vector<std::pair<unsigned, OpaqueType *> > UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000549
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000550static PATypeHolder HandleUpRefs(const Type *ty) {
551 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000552 UR_OUT("Type '" << ty->getDescription() <<
553 "' newly formed. Resolving upreferences.\n" <<
554 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000555 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000556 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000557 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000558 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000559 if (TypeContains(Ty, UpRefs[i].second)) {
560 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000561 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000562 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000563 UR_OUT(" * Resolving upreference for "
564 << UpRefs[i].second->getDescription() << endl;
Chris Lattner9232b992003-04-22 18:42:41 +0000565 std::string OldName = UpRefs[i].second->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +0000566 UpRefs[i].second->refineAbstractTypeTo(Ty);
567 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000568 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000569 << (const void*)Ty << ", " << Ty->getDescription() << endl);
570 continue;
571 }
572 }
573
574 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000575 }
Chris Lattner30c89792001-09-07 16:35:17 +0000576 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000577 return Ty;
578}
579
Chris Lattner30c89792001-09-07 16:35:17 +0000580
Chris Lattner00950542001-06-06 20:29:01 +0000581//===----------------------------------------------------------------------===//
582// RunVMAsmParser - Define an interface to this parser
583//===----------------------------------------------------------------------===//
584//
Chris Lattner9232b992003-04-22 18:42:41 +0000585Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000586 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000587 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000588 llvmAsmlineno = 1; // Reset the current line number...
589
Chris Lattner75f20532003-04-22 18:02:52 +0000590 // Allocate a new module to read
591 CurModule.CurrentModule = new Module(Filename);
Chris Lattner00950542001-06-06 20:29:01 +0000592 yyparse(); // Parse the file.
593 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000594 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
595 ParserResult = 0;
596
597 return Result;
598}
599
600%}
601
602%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000603 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000604 Function *FunctionVal;
Chris Lattner69da5cf2002-10-13 20:57:00 +0000605 std::pair<PATypeHolder*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000606 BasicBlock *BasicBlockVal;
607 TerminatorInst *TermInstVal;
608 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000609 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000610
Chris Lattner30c89792001-09-07 16:35:17 +0000611 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000612 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000613 Value *ValueVal;
614
Chris Lattner69da5cf2002-10-13 20:57:00 +0000615 std::vector<std::pair<PATypeHolder*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000616 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000617 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000618 std::list<std::pair<Value*,
619 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000620 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000621 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000622
Chris Lattner4ad02e72003-04-16 20:28:45 +0000623 GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000624 int64_t SInt64Val;
625 uint64_t UInt64Val;
626 int SIntVal;
627 unsigned UIntVal;
628 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000629 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000630
Chris Lattner30c89792001-09-07 16:35:17 +0000631 char *StrVal; // This memory is strdup'd!
632 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000633
Chris Lattner30c89792001-09-07 16:35:17 +0000634 Instruction::BinaryOps BinaryOpVal;
635 Instruction::TermOps TermOpVal;
636 Instruction::MemoryOps MemOpVal;
637 Instruction::OtherOps OtherOpVal;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000638 Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000639}
640
Chris Lattner79df7c02002-03-26 18:01:55 +0000641%type <ModuleVal> Module FunctionList
642%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000643%type <BasicBlockVal> BasicBlock InstructionList
644%type <TermInstVal> BBTerminatorInst
645%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000646%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000647%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000648%type <ArgList> ArgList ArgListH
649%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000650%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000651%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000652%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000653%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000654%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000655%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
656%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000657%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000658
Chris Lattner2079fde2001-10-13 06:41:08 +0000659// ValueRef - Unresolved reference to a definition or BB
660%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000661%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000662// Tokens and types for handling constant integer values
663//
664// ESINT64VAL - A negative number within long long range
665%token <SInt64Val> ESINT64VAL
666
667// EUINT64VAL - A positive number within uns. long long range
668%token <UInt64Val> EUINT64VAL
669%type <SInt64Val> EINT64VAL
670
671%token <SIntVal> SINTVAL // Signed 32 bit ints...
672%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
673%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000674%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000675
676// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000677%type <TypeVal> Types TypesV UpRTypes UpRTypesV
678%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000679%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
680%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000681
682%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000683%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000684
685
Chris Lattner08c0e6a2002-10-06 22:45:09 +0000686%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT
Chris Lattner4ad02e72003-04-16 20:28:45 +0000687%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000688%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000689
690// Basic Block Terminating Operators
691%token <TermOpVal> RET BR SWITCH
692
Chris Lattner00950542001-06-06 20:29:01 +0000693// Binary Operators
694%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000695%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000696%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000697%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000698
699// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000700%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000701
Chris Lattner027dcc52001-07-08 21:10:27 +0000702// Other Operators
703%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000704%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000705
Chris Lattner00950542001-06-06 20:29:01 +0000706%start Module
707%%
708
709// Handle constant integer size restriction and conversion...
710//
711
Chris Lattner51727be2002-06-04 21:58:56 +0000712INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000713INTVAL : UINTVAL {
714 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
715 ThrowException("Value too large for type!");
716 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000717};
Chris Lattner00950542001-06-06 20:29:01 +0000718
719
Chris Lattner51727be2002-06-04 21:58:56 +0000720EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000721EINT64VAL : EUINT64VAL {
722 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
723 ThrowException("Value too large for type!");
724 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000725};
Chris Lattner00950542001-06-06 20:29:01 +0000726
Chris Lattner00950542001-06-06 20:29:01 +0000727// Operations that are notably excluded from this list include:
728// RET, BR, & SWITCH because they end basic blocks and are treated specially.
729//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000730ArithmeticOps: ADD | SUB | MUL | DIV | REM;
731LogicalOps : AND | OR | XOR;
732SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
733BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
734
Chris Lattner51727be2002-06-04 21:58:56 +0000735ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000736
Chris Lattnere98dda62001-07-14 06:10:16 +0000737// These are some types that allow classification if we only want a particular
738// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000739SIntType : LONG | INT | SHORT | SBYTE;
740UIntType : ULONG | UINT | USHORT | UBYTE;
741IntType : SIntType | UIntType;
742FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000743
Chris Lattnere98dda62001-07-14 06:10:16 +0000744// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000745OptAssign : VAR_ID '=' {
746 $$ = $1;
747 }
748 | /*empty*/ {
749 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000750 };
Chris Lattner00950542001-06-06 20:29:01 +0000751
Chris Lattner4ad02e72003-04-16 20:28:45 +0000752OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
753 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
754 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
755 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000756
757//===----------------------------------------------------------------------===//
758// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000759// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000760// access to it, a user must explicitly use TypesV.
761//
762
763// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000764TypesV : Types | VOID { $$ = new PATypeHolder($1); };
765UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000766
767Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000768 if (UpRefs.size())
769 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
770 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000771 };
Chris Lattner30c89792001-09-07 16:35:17 +0000772
773
774// Derived types are added later...
775//
Chris Lattner51727be2002-06-04 21:58:56 +0000776PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
777PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000778UpRTypes : OPAQUE {
779 $$ = new PATypeHolder(OpaqueType::get());
780 }
781 | PrimType {
782 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000783 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000784UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000785 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000786};
Chris Lattner30c89792001-09-07 16:35:17 +0000787
Chris Lattner30c89792001-09-07 16:35:17 +0000788// Include derived types in the Types production.
789//
790UpRTypes : '\\' EUINT64VAL { // Type UpReference
791 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
792 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner9232b992003-04-22 18:42:41 +0000793 UpRefs.push_back(std::make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000794 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000795 UR_OUT("New Upreference!\n");
796 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000797 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000798 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000799 mapto($3->begin(), $3->end(), std::back_inserter(Params),
800 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000801 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
802 if (isVarArg) Params.pop_back();
803
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000804 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000805 delete $3; // Delete the argument list
806 delete $1; // Delete the old type handle
807 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000808 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000809 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000810 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000811 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000812 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000813 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000814 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
815 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000816
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000817 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000818 delete $2;
819 }
820 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000821 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000822 }
823 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000824 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000825 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000826 };
Chris Lattner30c89792001-09-07 16:35:17 +0000827
Chris Lattner7e708292002-06-25 16:13:24 +0000828// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000829// declaration type lists
830//
831TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +0000832 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000833 $$->push_back(*$1); delete $1;
834 }
835 | TypeListI ',' UpRTypes {
836 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000837 };
Chris Lattner30c89792001-09-07 16:35:17 +0000838
Chris Lattner7e708292002-06-25 16:13:24 +0000839// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000840ArgTypeListI : TypeListI
841 | TypeListI ',' DOTDOTDOT {
842 ($$=$1)->push_back(Type::VoidTy);
843 }
844 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +0000845 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000846 }
847 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +0000848 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000849 };
Chris Lattner30c89792001-09-07 16:35:17 +0000850
Chris Lattnere98dda62001-07-14 06:10:16 +0000851// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000852// production is used ONLY to represent constants that show up AFTER a 'const',
853// 'constant' or 'global' token at global scope. Constants that can be inlined
854// into other expressions (such as integers and constexprs) are handled by the
855// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000856//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000857ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
858 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
859 if (ATy == 0)
860 ThrowException("Cannot make array constant with type: '" +
861 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000862 const Type *ETy = ATy->getElementType();
863 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000864
Chris Lattner30c89792001-09-07 16:35:17 +0000865 // Verify that we have the correct size...
866 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000867 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000868 utostr($3->size()) + " arguments, but has size of " +
869 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000870
Chris Lattner30c89792001-09-07 16:35:17 +0000871 // Verify all elements are correct type!
872 for (unsigned i = 0; i < $3->size(); i++) {
873 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000874 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000875 ETy->getDescription() +"' as required!\nIt is of type '"+
876 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000877 }
878
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000879 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000880 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000881 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000882 | Types '[' ']' {
883 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
884 if (ATy == 0)
885 ThrowException("Cannot make array constant with type: '" +
886 (*$1)->getDescription() + "'!");
887
888 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000889 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000890 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000891 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +0000892 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000893 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000894 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000895 | Types 'c' STRINGCONSTANT {
896 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
897 if (ATy == 0)
898 ThrowException("Cannot make array constant with type: '" +
899 (*$1)->getDescription() + "'!");
900
Chris Lattner30c89792001-09-07 16:35:17 +0000901 int NumElements = ATy->getNumElements();
902 const Type *ETy = ATy->getElementType();
903 char *EndStr = UnEscapeLexed($3, true);
904 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000905 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000906 itostr((int)(EndStr-$3)) +
907 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +0000908 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000909 if (ETy == Type::SByteTy) {
910 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000911 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000912 } else if (ETy == Type::UByteTy) {
913 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +0000914 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000915 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000916 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000917 ThrowException("Cannot build string arrays of non byte sized elements!");
918 }
Chris Lattner30c89792001-09-07 16:35:17 +0000919 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000920 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000921 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000922 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000923 | Types '{' ConstVector '}' {
924 const StructType *STy = dyn_cast<const StructType>($1->get());
925 if (STy == 0)
926 ThrowException("Cannot make struct constant with type: '" +
927 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +0000928
929 // Check to ensure that constants are compatible with the type initializer!
930 for (unsigned i = 0, e = $3->size(); i != e; ++i)
931 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
932 ThrowException("Expected type '" +
933 STy->getElementTypes()[i]->getDescription() +
934 "' for element #" + utostr(i) +
935 " of structure initializer!");
936
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000937 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000938 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000939 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000940 | Types NULL_TOK {
941 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
942 if (PTy == 0)
943 ThrowException("Cannot make null pointer constant with type: '" +
944 (*$1)->getDescription() + "'!");
945
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000946 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000947 delete $1;
948 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000949 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000950 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
951 if (Ty == 0)
952 ThrowException("Global const reference must be a pointer type!");
953
Chris Lattner3101c252002-08-15 17:58:33 +0000954 // ConstExprs can exist in the body of a function, thus creating
955 // ConstantPointerRefs whenever they refer to a variable. Because we are in
956 // the context of a function, getValNonImprovising will search the functions
957 // symbol table instead of the module symbol table for the global symbol,
958 // which throws things all off. To get around this, we just tell
959 // getValNonImprovising that we are at global scope here.
960 //
961 Function *SavedCurFn = CurMeth.CurrentFunction;
962 CurMeth.CurrentFunction = 0;
963
Chris Lattner2079fde2001-10-13 06:41:08 +0000964 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000965
Chris Lattner3101c252002-08-15 17:58:33 +0000966 CurMeth.CurrentFunction = SavedCurFn;
967
968
Chris Lattner2079fde2001-10-13 06:41:08 +0000969 // If this is an initializer for a constant pointer, which is referencing a
970 // (currently) undefined variable, create a stub now that shall be replaced
971 // in the future with the right type of variable.
972 //
973 if (V == 0) {
974 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
975 const PointerType *PT = cast<PointerType>(Ty);
976
977 // First check to see if the forward references value is already created!
978 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +0000979 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +0000980
981 if (I != CurModule.GlobalRefs.end()) {
982 V = I->second; // Placeholder already exists, use it...
983 } else {
984 // TODO: Include line number info by creating a subclass of
985 // TODO: GlobalVariable here that includes the said information!
986
987 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000988 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +0000989 false,
990 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +0000991 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +0000992 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +0000993
994 // Must temporarily push this value into the module table...
995 CurModule.CurrentModule->getGlobalList().push_back(GV);
996 V = GV;
997 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000998 }
999
Chris Lattner2079fde2001-10-13 06:41:08 +00001000 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001001 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001002 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001003 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001004 | Types ConstExpr {
1005 if ($1->get() != $2->getType())
1006 ThrowException("Mismatched types for constant expression!");
1007 $$ = $2;
1008 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001009 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001010
Chris Lattnere43f40b2002-10-09 00:25:32 +00001011ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001012 if (!ConstantSInt::isValueValidForType($1, $2))
1013 ThrowException("Constant value doesn't fit in type!");
1014 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001015 }
1016 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001017 if (!ConstantUInt::isValueValidForType($1, $2))
1018 ThrowException("Constant value doesn't fit in type!");
1019 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001020 }
1021 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001022 $$ = ConstantBool::True;
1023 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001024 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001025 $$ = ConstantBool::False;
1026 }
1027 | FPType FPVAL { // Float & Double constants
1028 $$ = ConstantFP::get($1, $2);
1029 };
1030
Chris Lattner00950542001-06-06 20:29:01 +00001031
Chris Lattnerd78700d2002-08-16 21:14:40 +00001032ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001033 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001034 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001035 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001036 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1037 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001038 ThrowException("GetElementPtr requires a pointer operand!");
1039
1040 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001041 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001042 if (!IdxTy)
1043 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001044
Chris Lattner9232b992003-04-22 18:42:41 +00001045 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001046 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1047 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001048 IdxVec.push_back(C);
1049 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001050 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001051
Chris Lattnerd78700d2002-08-16 21:14:40 +00001052 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001053
Chris Lattnerd78700d2002-08-16 21:14:40 +00001054 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001055 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001056 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001057 if ($3->getType() != $5->getType())
1058 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001059 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001060 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001061 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001062 if ($5->getType() != Type::UByteTy)
1063 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001064 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001065 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001066
1067
Chris Lattnere98dda62001-07-14 06:10:16 +00001068// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001069ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001070 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001071 }
1072 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001073 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001074 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001075 };
Chris Lattner00950542001-06-06 20:29:01 +00001076
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001077
Chris Lattner1781aca2001-09-18 04:00:54 +00001078// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001079GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001080
Chris Lattner00950542001-06-06 20:29:01 +00001081
Chris Lattner0e73ce62002-05-02 19:11:13 +00001082//===----------------------------------------------------------------------===//
1083// Rules to match Modules
1084//===----------------------------------------------------------------------===//
1085
1086// Module rule: Capture the result of parsing the whole file into a result
1087// variable...
1088//
1089Module : FunctionList {
1090 $$ = ParserResult = $1;
1091 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001092};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001093
Chris Lattner7e708292002-06-25 16:13:24 +00001094// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001095//
1096FunctionList : FunctionList Function {
1097 $$ = $1;
1098 assert($2->getParent() == 0 && "Function already in module!");
1099 $1->getFunctionList().push_back($2);
1100 CurMeth.FunctionDone();
1101 }
1102 | FunctionList FunctionProto {
1103 $$ = $1;
1104 }
1105 | FunctionList IMPLEMENTATION {
1106 $$ = $1;
1107 }
1108 | ConstPool {
1109 $$ = CurModule.CurrentModule;
1110 // Resolve circular types before we parse the body of the module
1111 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001112 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001113
Chris Lattnere98dda62001-07-14 06:10:16 +00001114// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001115ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001116 if (!setValueName($4, $2))
1117 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001118 }
Chris Lattner30c89792001-09-07 16:35:17 +00001119 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001120 // Eagerly resolve types. This is not an optimization, this is a
1121 // requirement that is due to the fact that we could have this:
1122 //
1123 // %list = type { %list * }
1124 // %list = type { %list * } ; repeated type decl
1125 //
1126 // If types are not resolved eagerly, then the two types will not be
1127 // determined to be the same type!
1128 //
1129 ResolveTypeTo($2, $4->get());
1130
Chris Lattner1781aca2001-09-18 04:00:54 +00001131 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001132 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1133 // If this is not a redefinition of a type...
1134 if (!$2) {
1135 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001136 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001137 }
Chris Lattner30c89792001-09-07 16:35:17 +00001138 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001139
1140 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001141 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001142 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001143 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001144 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001145 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001146 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001147 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001148 if (Initializer == 0)
1149 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001150
Chris Lattnerdda71962001-11-26 18:54:16 +00001151 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001152 if (!setValueName(GV, $2)) { // If not redefining...
1153 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001154 int Slot = InsertValue(GV, CurModule.Values);
1155
1156 if (Slot != -1) {
1157 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1158 } else {
1159 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1160 (char*)GV->getName().c_str()));
1161 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001162 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001163 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001164 | ConstPool OptAssign EXTERNAL GlobalType Types {
1165 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001166 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001167 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001168 if (!setValueName(GV, $2)) { // If not redefining...
1169 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001170 int Slot = InsertValue(GV, CurModule.Values);
1171
1172 if (Slot != -1) {
1173 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1174 } else {
1175 assert(GV->hasName() && "Not named and not numbered!?");
1176 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1177 (char*)GV->getName().c_str()));
1178 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001179 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001180 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001181 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001182 | ConstPool TARGET TargetDefinition {
1183 }
Chris Lattner00950542001-06-06 20:29:01 +00001184 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001185 };
Chris Lattner00950542001-06-06 20:29:01 +00001186
1187
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001188
1189BigOrLittle : BIG { $$ = Module::BigEndian; };
1190BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1191
1192TargetDefinition : ENDIAN '=' BigOrLittle {
1193 CurModule.CurrentModule->setEndianness($3);
1194 }
1195 | POINTERSIZE '=' EUINT64VAL {
1196 if ($3 == 32)
1197 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1198 else if ($3 == 64)
1199 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1200 else
1201 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1202 };
1203
1204
Chris Lattner00950542001-06-06 20:29:01 +00001205//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001206// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001207//===----------------------------------------------------------------------===//
1208
Chris Lattner51727be2002-06-04 21:58:56 +00001209OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001210
1211ArgVal : Types OptVAR_ID {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001212 if (*$1 == Type::VoidTy)
1213 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001214 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001215};
Chris Lattner00950542001-06-06 20:29:01 +00001216
Chris Lattner69da5cf2002-10-13 20:57:00 +00001217ArgListH : ArgListH ',' ArgVal {
1218 $$ = $1;
1219 $1->push_back(*$3);
1220 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001221 }
1222 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001223 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001224 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001225 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001226 };
Chris Lattner00950542001-06-06 20:29:01 +00001227
1228ArgList : ArgListH {
1229 $$ = $1;
1230 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001231 | ArgListH ',' DOTDOTDOT {
1232 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001233 $$->push_back(std::pair<PATypeHolder*,
1234 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001235 }
1236 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001237 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1238 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001239 }
Chris Lattner00950542001-06-06 20:29:01 +00001240 | /* empty */ {
1241 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001242 };
Chris Lattner00950542001-06-06 20:29:01 +00001243
Chris Lattner8ebccb72002-05-22 22:33:00 +00001244FuncName : VAR_ID | STRINGCONSTANT;
1245
Chris Lattner1f862af2003-04-16 18:13:57 +00001246FunctionHeaderH : TypesV FuncName '(' ArgList ')' {
1247 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001248 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001249
Chris Lattner9232b992003-04-22 18:42:41 +00001250 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001251 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001252 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001253 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001254 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001255 }
Chris Lattner00950542001-06-06 20:29:01 +00001256
Chris Lattner2079fde2001-10-13 06:41:08 +00001257 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1258 if (isVarArg) ParamTypeList.pop_back();
1259
Chris Lattner1f862af2003-04-16 18:13:57 +00001260 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001261 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001262 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001263
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001264 Function *Fn = 0;
1265 // Is the function already in symtab?
1266 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1267 // Yes it is. If this is the case, either we need to be a forward decl,
1268 // or it needs to be.
1269 if (!CurMeth.isDeclare && !Fn->isExternal())
1270 ThrowException("Redefinition of function '" + FunctionName + "'!");
1271
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001272 // If we found a preexisting function prototype, remove it from the
1273 // module, so that we don't get spurious conflicts with global & local
1274 // variables.
1275 //
1276 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001277
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001278 // Make sure to strip off any argument names so we can't get conflicts...
1279 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1280 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001281
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001282 } else { // Not already defined?
Chris Lattner4ad02e72003-04-16 20:28:45 +00001283 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001284 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001285 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001286 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001287 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001288
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001289 CurMeth.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001290
Chris Lattner7e708292002-06-25 16:13:24 +00001291 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001292 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001293 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001294 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001295 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001296 delete $4->back().first;
1297 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001298 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001299 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001300 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001301 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001302 delete I->first; // Delete the typeholder...
1303
1304 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001305 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001306
Chris Lattner69da5cf2002-10-13 20:57:00 +00001307 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001308 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001309
Chris Lattner1f862af2003-04-16 18:13:57 +00001310 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001311 }
Chris Lattner51727be2002-06-04 21:58:56 +00001312};
Chris Lattner00950542001-06-06 20:29:01 +00001313
Chris Lattner9b02cc32002-05-03 18:23:48 +00001314BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1315
Chris Lattner4ad02e72003-04-16 20:28:45 +00001316FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001317 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001318
Chris Lattner4ad02e72003-04-16 20:28:45 +00001319 // Make sure that we keep track of the linkage type even if there was a
1320 // previous "declare".
1321 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001322
Chris Lattner7e708292002-06-25 16:13:24 +00001323 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001324 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001325};
Chris Lattner00950542001-06-06 20:29:01 +00001326
Chris Lattner9b02cc32002-05-03 18:23:48 +00001327END : ENDTOK | '}'; // Allow end of '}' to end a function
1328
Chris Lattner79df7c02002-03-26 18:01:55 +00001329Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001330 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001331};
Chris Lattner00950542001-06-06 20:29:01 +00001332
Chris Lattner79df7c02002-03-26 18:01:55 +00001333FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1334 $$ = CurMeth.CurrentFunction;
1335 assert($$->getParent() == 0 && "Function already in module!");
1336 CurModule.CurrentModule->getFunctionList().push_back($$);
1337 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001338};
Chris Lattner00950542001-06-06 20:29:01 +00001339
1340//===----------------------------------------------------------------------===//
1341// Rules to match Basic Blocks
1342//===----------------------------------------------------------------------===//
1343
1344ConstValueRef : ESINT64VAL { // A reference to a direct constant
1345 $$ = ValID::create($1);
1346 }
1347 | EUINT64VAL {
1348 $$ = ValID::create($1);
1349 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001350 | FPVAL { // Perhaps it's an FP constant?
1351 $$ = ValID::create($1);
1352 }
Chris Lattner00950542001-06-06 20:29:01 +00001353 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001354 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001355 }
1356 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001357 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001358 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001359 | NULL_TOK {
1360 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001361 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001362 | ConstExpr {
1363 $$ = ValID::create($1);
1364 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001365
Chris Lattner2079fde2001-10-13 06:41:08 +00001366// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1367// another value.
1368//
1369SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001370 $$ = ValID::create($1);
1371 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001372 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001373 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001374 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001375
1376// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001377ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001378
Chris Lattner00950542001-06-06 20:29:01 +00001379
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001380// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1381// type immediately preceeds the value reference, and allows complex constant
1382// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001383ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001384 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001385 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001386
Chris Lattner00950542001-06-06 20:29:01 +00001387BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001388 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001389 }
Chris Lattner7e708292002-06-25 16:13:24 +00001390 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1391 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001392 };
Chris Lattner00950542001-06-06 20:29:01 +00001393
1394
1395// Basic blocks are terminated by branching instructions:
1396// br, br/cc, switch, ret
1397//
Chris Lattner2079fde2001-10-13 06:41:08 +00001398BasicBlock : InstructionList OptAssign BBTerminatorInst {
1399 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1400 InsertValue($3);
1401
1402 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001403 InsertValue($1);
1404 $$ = $1;
1405 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001406 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1407 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1408 InsertValue($4);
1409
1410 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001411 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001412
1413 InsertValue($2);
1414 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001415 };
Chris Lattner00950542001-06-06 20:29:01 +00001416
1417InstructionList : InstructionList Inst {
1418 $1->getInstList().push_back($2);
1419 $$ = $1;
1420 }
1421 | /* empty */ {
Chris Lattner0383cc42002-08-21 23:51:21 +00001422 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001423 };
Chris Lattner00950542001-06-06 20:29:01 +00001424
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001425BBTerminatorInst : RET ResolvedVal { // Return with a result...
1426 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001427 }
1428 | RET VOID { // Return with no result...
1429 $$ = new ReturnInst();
1430 }
1431 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001432 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001433 } // Conditional Branch...
1434 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001435 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1436 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001437 getVal(Type::BoolTy, $3));
1438 }
1439 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1440 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001441 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001442 $$ = S;
1443
Chris Lattner9232b992003-04-22 18:42:41 +00001444 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001445 E = $8->end();
1446 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001447 S->dest_push_back(I->first, I->second);
1448 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001449 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1450 EXCEPT ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001451 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001452 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001453
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001454 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1455 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001456 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001457 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001458 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001459 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1460 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001461 ParamTypes.push_back((*I)->getType());
1462 }
1463
1464 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1465 if (isVarArg) ParamTypes.pop_back();
1466
Chris Lattner79df7c02002-03-26 18:01:55 +00001467 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001468 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001469 }
1470 delete $2;
1471
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001472 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001473
1474 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1475 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1476
1477 if (Normal == 0 || Except == 0)
1478 ThrowException("Invoke instruction without label destinations!");
1479
1480 // Create the call node...
1481 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001482 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001483 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001484 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001485 // correctly!
1486 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001487 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1488 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001489 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001490
1491 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1492 if ((*ArgI)->getType() != *I)
1493 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001494 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001495
1496 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1497 ThrowException("Invalid number of parameters detected!");
1498
Chris Lattner6cdb0112001-11-26 16:54:11 +00001499 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001500 }
1501 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001502 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001503
1504
Chris Lattner00950542001-06-06 20:29:01 +00001505
1506JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1507 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001508 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001509 if (V == 0)
1510 ThrowException("May only switch on a constant pool value!");
1511
Chris Lattner9232b992003-04-22 18:42:41 +00001512 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001513 }
1514 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001515 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001516 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001517
1518 if (V == 0)
1519 ThrowException("May only switch on a constant pool value!");
1520
Chris Lattner9232b992003-04-22 18:42:41 +00001521 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001522 };
Chris Lattner00950542001-06-06 20:29:01 +00001523
1524Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001525 // Is this definition named?? if so, assign the name...
1526 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001527 InsertValue($2);
1528 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001529};
Chris Lattner00950542001-06-06 20:29:01 +00001530
Chris Lattnerc24d2082001-06-11 15:04:20 +00001531PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001532 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1533 $$->push_back(std::make_pair(getVal(*$1, $3),
1534 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001535 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001536 }
1537 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1538 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001539 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1540 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001541 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001542
1543
Chris Lattner30c89792001-09-07 16:35:17 +00001544ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001545 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001546 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001547 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001548 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001549 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001550 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001551 };
Chris Lattner00950542001-06-06 20:29:01 +00001552
1553// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001554ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001555
Chris Lattner4a6482b2002-09-10 19:57:26 +00001556InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1557 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1558 ThrowException("Arithmetic operator requires integer or FP operands!");
1559 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1560 if ($$ == 0)
1561 ThrowException("binary operator returned null!");
1562 delete $2;
1563 }
1564 | LogicalOps Types ValueRef ',' ValueRef {
1565 if (!(*$2)->isIntegral())
1566 ThrowException("Logical operator requires integral operands!");
1567 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1568 if ($$ == 0)
1569 ThrowException("binary operator returned null!");
1570 delete $2;
1571 }
1572 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001573 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001574 if ($$ == 0)
1575 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001576 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001577 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001578 | NOT ResolvedVal {
1579 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1580 << " Replacing with 'xor'.\n";
1581
1582 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1583 if (Ones == 0)
1584 ThrowException("Expected integral type for not instruction!");
1585
1586 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001587 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001588 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001589 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001590 | ShiftOps ResolvedVal ',' ResolvedVal {
1591 if ($4->getType() != Type::UByteTy)
1592 ThrowException("Shift amount must be ubyte!");
1593 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001594 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001595 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001596 $$ = new CastInst($2, *$4);
1597 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001598 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001599 | PHI PHIList {
1600 const Type *Ty = $2->front().first->getType();
1601 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001602 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001603 if ($2->front().first->getType() != Ty)
1604 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001605 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001606 $2->pop_front();
1607 }
1608 delete $2; // Free the list...
1609 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001610 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001611 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001612 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001613
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001614 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1615 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001616 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001617 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001618 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001619 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1620 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001621 ParamTypes.push_back((*I)->getType());
1622 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001623
1624 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1625 if (isVarArg) ParamTypes.pop_back();
1626
Chris Lattner79df7c02002-03-26 18:01:55 +00001627 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001628 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001629 }
Chris Lattner30c89792001-09-07 16:35:17 +00001630 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001631
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001632 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001633
Chris Lattner8b81bf52001-07-25 22:47:46 +00001634 // Create the call node...
1635 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001636 // Make sure no arguments is a good thing!
1637 if (Ty->getNumParams() != 0)
1638 ThrowException("No arguments passed to a function that "
1639 "expects arguments!");
1640
Chris Lattner9232b992003-04-22 18:42:41 +00001641 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001642 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001643 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001644 // correctly!
1645 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001646 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1647 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001648 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001649
1650 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1651 if ((*ArgI)->getType() != *I)
1652 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001653 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001654
Chris Lattner8b81bf52001-07-25 22:47:46 +00001655 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001656 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001657
Chris Lattner6cdb0112001-11-26 16:54:11 +00001658 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001659 }
1660 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001661 }
1662 | MemoryInst {
1663 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001664 };
Chris Lattner00950542001-06-06 20:29:01 +00001665
Chris Lattner6cdb0112001-11-26 16:54:11 +00001666
1667// IndexList - List of indices for GEP based instructions...
1668IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001669 $$ = $2;
1670} | /* empty */ {
Chris Lattner9232b992003-04-22 18:42:41 +00001671 $$ = new std::vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001672};
Chris Lattner027dcc52001-07-08 21:10:27 +00001673
Chris Lattner00950542001-06-06 20:29:01 +00001674MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001675 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001676 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001677 }
1678 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001679 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001680 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001681 }
1682 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001683 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001684 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001685 }
1686 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001687 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001688 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001689 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001690 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001691 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001692 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001693 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001694 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001695 }
1696
Chris Lattner6cdb0112001-11-26 16:54:11 +00001697 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001698 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001699 ThrowException("Can't load from nonpointer type: " +
1700 (*$2)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001701 if (GetElementPtrInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001702 ThrowException("Invalid indices for load instruction!");
1703
Chris Lattner0383cc42002-08-21 23:51:21 +00001704 Value *Src = getVal(*$2, $3);
1705 if (!$4->empty()) {
1706 std::cerr << "WARNING: Use of index load instruction:"
1707 << " replacing with getelementptr/load pair.\n";
1708 // Create a getelementptr hack instruction to do the right thing for
1709 // compatibility.
1710 //
1711 Instruction *I = new GetElementPtrInst(Src, *$4);
1712 CurBB->getInstList().push_back(I);
1713 Src = I;
1714 }
1715
1716 $$ = new LoadInst(Src);
Chris Lattner027dcc52001-07-08 21:10:27 +00001717 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001718 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001719 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001720 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001721 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001722 ThrowException("Can't store to a nonpointer type: " +
1723 (*$4)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001724 const Type *ElTy = GetElementPtrInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001725 if (ElTy == 0)
1726 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001727 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001728 ThrowException("Can't store '" + $2->getType()->getDescription() +
1729 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001730
1731 Value *Ptr = getVal(*$4, $5);
1732 if (!$6->empty()) {
1733 std::cerr << "WARNING: Use of index store instruction:"
1734 << " replacing with getelementptr/store pair.\n";
1735 // Create a getelementptr hack instruction to do the right thing for
1736 // compatibility.
1737 //
1738 Instruction *I = new GetElementPtrInst(Ptr, *$6);
1739 CurBB->getInstList().push_back(I);
1740 Ptr = I;
1741 }
1742
1743 $$ = new StoreInst($2, Ptr);
Chris Lattner30c89792001-09-07 16:35:17 +00001744 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001745 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001746 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner0235fe22002-09-11 01:17:27 +00001747 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
1748 if ((*$4)[i]->getType() == Type::UIntTy) {
1749 std::cerr << "WARNING: Use of uint type indexes to getelementptr "
1750 << "instruction: replacing with casts to long type.\n";
1751 Instruction *I = new CastInst((*$4)[i], Type::LongTy);
1752 CurBB->getInstList().push_back(I);
1753 (*$4)[i] = I;
1754 }
1755 }
1756
Chris Lattner51727be2002-06-04 21:58:56 +00001757 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001758 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001759 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001760 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001761 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1762 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001763 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001764
Chris Lattner00950542001-06-06 20:29:01 +00001765%%
Chris Lattner09083092001-07-08 04:57:15 +00001766int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00001767 std::string where
1768 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001769 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00001770 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001771 if (yychar == YYEMPTY)
1772 errMsg += "end-of-file.";
1773 else
Chris Lattner9232b992003-04-22 18:42:41 +00001774 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001775 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001776 return 0;
1777}