blob: 7945b37ed52de35f91926c7fb410dd4dac72f949 [file] [log] [blame]
Chris Lattner2f7c9632001-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 Lattner2f7c9632001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000011#include "llvm/iTerminators.h"
12#include "llvm/iMemory.h"
Chris Lattnera6834c12002-09-10 22:37:46 +000013#include "llvm/iOperators.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
Chris Lattner5de22042001-11-27 00:03:19 +000015#include "Support/STLExtras.h"
16#include "Support/DepthFirstIterator.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000017#include <list>
Chris Lattner3cd8c562002-07-30 18:54:25 +000018#include <utility>
Chris Lattneraa534bf2001-09-07 16:35:17 +000019#include <algorithm>
Chris Lattner2f7c9632001-06-06 20:29:01 +000020
Chris Lattner322e49a2001-10-16 19:54:17 +000021int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattnera6821822001-07-08 04:57:15 +000022int yylex(); // declaration" of xxx warnings.
Chris Lattner2f7c9632001-06-06 20:29:01 +000023int yyparse();
24
25static Module *ParserResult;
Chris Lattner89da8a32003-04-22 18:42:41 +000026std::string CurFilename;
Chris Lattner2f7c9632001-06-06 20:29:01 +000027
Chris Lattneraa534bf2001-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 Lattnerdb3b2022002-08-14 17:12:33 +000033#define UR_OUT(X) std::cerr << X
Chris Lattneraa534bf2001-09-07 16:35:17 +000034#else
35#define UR_OUT(X)
36#endif
37
Vikram S. Adve7064eaf2002-07-14 22:59:28 +000038#define YYERROR_VERBOSE 1
39
Chris Lattner319c47a2002-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 Lattner113f4f42002-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 Lattner2f7c9632001-06-06 20:29:01 +000050//
Chris Lattner89da8a32003-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 Lattner2f7c9632001-06-06 20:29:01 +000054
55static struct PerModuleInfo {
56 Module *CurrentModule;
Chris Lattner89da8a32003-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 Lattner2f7c9632001-06-06 20:29:01 +000061
Chris Lattnerf26e0d92001-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 Lattner3462ae32001-12-03 22:26:30 +000065 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattnerf26e0d92001-10-13 06:41:08 +000066 //
Chris Lattner89da8a32003-04-22 18:42:41 +000067 typedef std::map<std::pair<const PointerType *,
68 ValID>, GlobalVariable*> GlobalRefsType;
Chris Lattnerf26e0d92001-10-13 06:41:08 +000069 GlobalRefsType GlobalRefs;
70
Chris Lattner2f7c9632001-06-06 20:29:01 +000071 void ModuleDone() {
Chris Lattner113f4f42002-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 Lattneraa534bf2001-09-07 16:35:17 +000075 //
Chris Lattner2f7c9632001-06-06 20:29:01 +000076 ResolveDefinitions(LateResolveValues);
77
Chris Lattnerf26e0d92001-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 Lattner89da8a32003-04-22 18:42:41 +000082 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner1a8ea8a2002-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 Lattnerf26e0d92001-10-13 06:41:08 +000090 }
91
Chris Lattner113f4f42002-06-25 16:13:24 +000092 Values.clear(); // Clear out function local definitions
Chris Lattneraa534bf2001-09-07 16:35:17 +000093 Types.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +000094 CurrentModule = 0;
95 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +000096
97
Vikram S. Adve7064eaf2002-07-14 22:59:28 +000098 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattnerf26e0d92001-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 Lattner89da8a32003-04-22 18:42:41 +0000105 GlobalRefsType::iterator I =
106 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattnerf26e0d92001-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. Adve7064eaf2002-07-14 22:59:28 +0000113 // allowed to be is ConstantPointerRef's.
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000114 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
Chris Lattner8022a0f2002-10-14 03:28:42 +0000115 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
116 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000117
Chris Lattner8022a0f2002-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. Adve7064eaf2002-07-14 22:59:28 +0000122
123 // Remove OldGV from the module...
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000124 CurrentModule->getGlobalList().remove(OldGV);
125 delete OldGV; // Delete the old placeholder
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000126
Chris Lattnerf26e0d92001-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 Lattner2f7c9632001-06-06 20:29:01 +0000132} CurModule;
133
Chris Lattner57698e22002-03-26 18:01:55 +0000134static struct PerFunctionInfo {
Chris Lattner113f4f42002-06-25 16:13:24 +0000135 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136
Chris Lattner89da8a32003-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 Lattner113f4f42002-06-25 16:13:24 +0000141 bool isDeclare; // Is this function a forward declararation?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000142
Chris Lattner57698e22002-03-26 18:01:55 +0000143 inline PerFunctionInfo() {
144 CurrentFunction = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +0000145 isDeclare = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000146 }
147
Chris Lattner57698e22002-03-26 18:01:55 +0000148 inline ~PerFunctionInfo() {}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149
Chris Lattner57698e22002-03-26 18:01:55 +0000150 inline void FunctionStart(Function *M) {
151 CurrentFunction = M;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000152 }
153
Chris Lattner57698e22002-03-26 18:01:55 +0000154 void FunctionDone() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000155 // If we could not resolve some blocks at parsing time (forward branches)
156 // resolve the branches now...
Chris Lattner322e49a2001-10-16 19:54:17 +0000157 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000158
Chris Lattner1fcfaf52003-04-25 21:47:33 +0000159 // Make sure to resolve any constant expr references that might exist within
160 // the function we just declared itself.
161 ValID FID;
162 if (CurrentFunction->hasName()) {
163 FID = ValID::create((char*)CurrentFunction->getName().c_str());
164 } else {
165 unsigned Slot = CurrentFunction->getType()->getUniqueID();
166 assert(CurModule.Values.size() > Slot && "Function not inserted?");
167 // Figure out which slot number if is...
168 for (unsigned i = 0; ; ++i) {
169 assert(i < CurModule.Values[Slot].size() && "Function not found!");
170 if (CurModule.Values[Slot][i] == CurrentFunction) {
171 FID = ValID::create((int)i);
172 break;
173 }
174 }
175 }
176 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
177
Chris Lattner113f4f42002-06-25 16:13:24 +0000178 Values.clear(); // Clear out function local definitions
Chris Lattneraa534bf2001-09-07 16:35:17 +0000179 Types.clear();
Chris Lattner57698e22002-03-26 18:01:55 +0000180 CurrentFunction = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +0000181 isDeclare = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000182 }
Chris Lattner113f4f42002-06-25 16:13:24 +0000183} CurMeth; // Info for the current function...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000184
Chris Lattner57698e22002-03-26 18:01:55 +0000185static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000186
Chris Lattner2f7c9632001-06-06 20:29:01 +0000187
188//===----------------------------------------------------------------------===//
189// Code to handle definitions of all the types
190//===----------------------------------------------------------------------===//
191
Chris Lattner89da8a32003-04-22 18:42:41 +0000192static int InsertValue(Value *D,
193 std::vector<ValueList> &ValueTab = CurMeth.Values) {
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000194 if (D->hasName()) return -1; // Is this a numbered definition?
195
196 // Yes, insert the value into the value table...
197 unsigned type = D->getType()->getUniqueID();
198 if (ValueTab.size() <= type)
199 ValueTab.resize(type+1, ValueList());
200 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
201 ValueTab[type].push_back(D);
202 return ValueTab[type].size()-1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000203}
204
Chris Lattneraa534bf2001-09-07 16:35:17 +0000205// TODO: FIXME when Type are not const
Chris Lattner89da8a32003-04-22 18:42:41 +0000206static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000207 Types.push_back(Ty);
208}
209
210static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000211 switch (D.Type) {
Chris Lattner8eedb942002-07-18 05:18:37 +0000212 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattneraa534bf2001-09-07 16:35:17 +0000213 unsigned Num = (unsigned)D.Num;
214
215 // Module constants occupy the lowest numbered slots...
216 if (Num < CurModule.Types.size())
217 return CurModule.Types[Num];
218
219 Num -= CurModule.Types.size();
220
221 // Check that the number is within bounds...
222 if (Num <= CurMeth.Types.size())
223 return CurMeth.Types[Num];
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000224 break;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000225 }
Chris Lattner8eedb942002-07-18 05:18:37 +0000226 case ValID::NameVal: { // Is it a named definition?
Chris Lattner89da8a32003-04-22 18:42:41 +0000227 std::string Name(D.Name);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000228 SymbolTable *SymTab = 0;
Chris Lattner98cf1f52002-11-20 18:36:02 +0000229 Value *N = 0;
230 if (inFunctionScope()) {
231 SymTab = &CurMeth.CurrentFunction->getSymbolTable();
232 N = SymTab->lookup(Type::TypeTy, Name);
233 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000234
235 if (N == 0) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000236 // Symbol table doesn't automatically chain yet... because the function
Chris Lattneraa534bf2001-09-07 16:35:17 +0000237 // hasn't been added to the module...
238 //
Chris Lattner98cf1f52002-11-20 18:36:02 +0000239 SymTab = &CurModule.CurrentModule->getSymbolTable();
240 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000241 if (N == 0) break;
242 }
243
244 D.destroy(); // Free old strdup'd memory...
Chris Lattner8f191122001-10-01 18:26:53 +0000245 return cast<const Type>(N);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000246 }
247 default:
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000248 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattneraa534bf2001-09-07 16:35:17 +0000249 }
250
251 // If we reached here, we referenced either a symbol that we don't know about
252 // or an id number that hasn't been read yet. We may be referencing something
253 // forward, so just create an entry to be resolved later and get to it...
254 //
255 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
256
Chris Lattner89da8a32003-04-22 18:42:41 +0000257 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000258 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
259
Chris Lattner89da8a32003-04-22 18:42:41 +0000260 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000261 if (I != LateResolver.end()) {
262 return I->second;
263 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000264
Chris Lattnere6b470c2001-10-22 06:01:08 +0000265 Type *Typ = OpaqueType::get();
Chris Lattner89da8a32003-04-22 18:42:41 +0000266 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000267 return Typ;
268}
269
Chris Lattner89da8a32003-04-22 18:42:41 +0000270static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner98cf1f52002-11-20 18:36:02 +0000271 SymbolTable &SymTab =
Chris Lattner18489fb62002-05-02 19:27:42 +0000272 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
273 CurModule.CurrentModule->getSymbolTable();
Chris Lattner98cf1f52002-11-20 18:36:02 +0000274 return SymTab.lookup(Ty, Name);
Chris Lattner60e0dd72001-10-03 06:12:09 +0000275}
276
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000277// getValNonImprovising - Look up the value specified by the provided type and
278// the provided ValID. If the value exists and has already been defined, return
279// it. Otherwise return null.
280//
281static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner57698e22002-03-26 18:01:55 +0000282 if (isa<FunctionType>(Ty))
283 ThrowException("Functions are not values and "
284 "must be referenced as pointers");
Chris Lattner322e49a2001-10-16 19:54:17 +0000285
Chris Lattneraa534bf2001-09-07 16:35:17 +0000286 switch (D.Type) {
Chris Lattnerfbdec252001-09-30 22:46:54 +0000287 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattneraa534bf2001-09-07 16:35:17 +0000288 unsigned type = Ty->getUniqueID();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000289 unsigned Num = (unsigned)D.Num;
290
291 // Module constants occupy the lowest numbered slots...
292 if (type < CurModule.Values.size()) {
293 if (Num < CurModule.Values[type].size())
294 return CurModule.Values[type][Num];
295
296 Num -= CurModule.Values[type].size();
297 }
298
299 // Make sure that our type is within bounds
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000300 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000301
302 // Check that the number is within bounds...
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000303 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000304
305 return CurMeth.Values[type][Num];
306 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000307
Chris Lattnerfbdec252001-09-30 22:46:54 +0000308 case ValID::NameVal: { // Is it a named definition?
Chris Lattner89da8a32003-04-22 18:42:41 +0000309 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000310 if (N == 0) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000311
312 D.destroy(); // Free old strdup'd memory...
313 return N;
314 }
315
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000316 // Check to make sure that "Ty" is an integral type, and that our
317 // value will fit into the specified type...
318 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattner7f325cd2002-08-16 21:14:40 +0000319 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
320 ThrowException("Signed integral constant '" +
321 itostr(D.ConstPool64) + "' is invalid for type '" +
322 Ty->getDescription() + "'!");
323 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000324
325 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner3462ae32001-12-03 22:26:30 +0000326 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
327 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner8eedb942002-07-18 05:18:37 +0000328 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
329 "' is invalid or out of range!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000330 } else { // This is really a signed reference. Transmogrify.
Chris Lattner3462ae32001-12-03 22:26:30 +0000331 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000332 }
333 } else {
Chris Lattner3462ae32001-12-03 22:26:30 +0000334 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000335 }
336
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000337 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner3462ae32001-12-03 22:26:30 +0000338 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000339 ThrowException("FP constant invalid for type!!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000340 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000341
342 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner181cc322002-05-06 16:15:30 +0000343 if (!isa<PointerType>(Ty))
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000344 ThrowException("Cannot create a a non pointer null!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000345 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000346
Chris Lattner7f325cd2002-08-16 21:14:40 +0000347 case ValID::ConstantVal: // Fully resolved constant?
348 if (D.ConstantValue->getType() != Ty)
349 ThrowException("Constant expression type different from required type!");
350 return D.ConstantValue;
351
Chris Lattneraa534bf2001-09-07 16:35:17 +0000352 default:
353 assert(0 && "Unhandled case!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000354 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000355 } // End of switch
356
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000357 assert(0 && "Unhandled case!");
358 return 0;
359}
360
361
362// getVal - This function is identical to getValNonImprovising, except that if a
363// value is not already defined, it "improvises" by creating a placeholder var
364// that looks and acts just like the requested variable. When the value is
365// defined later, all uses of the placeholder variable are replaced with the
366// real thing.
367//
368static Value *getVal(const Type *Ty, const ValID &D) {
369 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
370
371 // See if the value has already been defined...
372 Value *V = getValNonImprovising(Ty, D);
373 if (V) return V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000374
375 // If we reached here, we referenced either a symbol that we don't know about
376 // or an id number that hasn't been read yet. We may be referencing something
377 // forward, so just create an entry to be resolved later and get to it...
378 //
Chris Lattner2f7c9632001-06-06 20:29:01 +0000379 Value *d = 0;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000380 switch (Ty->getPrimitiveID()) {
381 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000382 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000383 }
384
385 assert(d != 0 && "How did we not make something?");
Chris Lattner57698e22002-03-26 18:01:55 +0000386 if (inFunctionScope())
Chris Lattner322e49a2001-10-16 19:54:17 +0000387 InsertValue(d, CurMeth.LateResolveValues);
388 else
389 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000390 return d;
391}
392
393
394//===----------------------------------------------------------------------===//
395// Code to handle forward references in instructions
396//===----------------------------------------------------------------------===//
397//
398// This code handles the late binding needed with statements that reference
399// values not defined yet... for example, a forward branch, or the PHI node for
400// a loop body.
401//
402// This keeps a table (CurMeth.LateResolveValues) of all such forward references
403// and back patchs after we are done.
404//
405
406// ResolveDefinitions - If we could not resolve some defs at parsing
407// time (forward branches, phi functions for loops, etc...) resolve the
408// defs now...
409//
Chris Lattner89da8a32003-04-22 18:42:41 +0000410static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
411 std::vector<ValueList> *FutureLateResolvers) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000412 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
413 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
414 while (!LateResolvers[ty].empty()) {
415 Value *V = LateResolvers[ty].back();
Chris Lattner322e49a2001-10-16 19:54:17 +0000416 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
417
Chris Lattner2f7c9632001-06-06 20:29:01 +0000418 LateResolvers[ty].pop_back();
419 ValID &DID = getValIDFromPlaceHolder(V);
420
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000421 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner322e49a2001-10-16 19:54:17 +0000422 if (TheRealValue) {
423 V->replaceAllUsesWith(TheRealValue);
424 delete V;
425 } else if (FutureLateResolvers) {
Chris Lattner57698e22002-03-26 18:01:55 +0000426 // Functions have their unresolved items forwarded to the module late
Chris Lattner322e49a2001-10-16 19:54:17 +0000427 // resolver table
428 InsertValue(V, *FutureLateResolvers);
429 } else {
Chris Lattner18489fb62002-05-02 19:27:42 +0000430 if (DID.Type == ValID::NameVal)
Chris Lattneraa534bf2001-09-07 16:35:17 +0000431 ThrowException("Reference to an invalid definition: '" +DID.getName()+
432 "' of type '" + V->getType()->getDescription() + "'",
433 getLineNumFromPlaceHolder(V));
434 else
435 ThrowException("Reference to an invalid definition: #" +
436 itostr(DID.Num) + " of type '" +
437 V->getType()->getDescription() + "'",
438 getLineNumFromPlaceHolder(V));
439 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000440 }
441 }
442
443 LateResolvers.clear();
444}
445
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000446// ResolveTypeTo - A brand new type was just declared. This means that (if
447// name is not null) things referencing Name can be resolved. Otherwise, things
448// refering to the number can be resolved. Do this now.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000449//
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000450static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner89da8a32003-04-22 18:42:41 +0000451 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000452 CurMeth.Types : CurModule.Types;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000453
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000454 ValID D;
455 if (Name) D = ValID::create(Name);
456 else D = ValID::create((int)Types.size());
Chris Lattneraa534bf2001-09-07 16:35:17 +0000457
Chris Lattner89da8a32003-04-22 18:42:41 +0000458 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000459 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
460
Chris Lattner89da8a32003-04-22 18:42:41 +0000461 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000462 if (I != LateResolver.end()) {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000463 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000464 LateResolver.erase(I);
465 }
466}
467
468// ResolveTypes - At this point, all types should be resolved. Any that aren't
469// are errors.
470//
Chris Lattner89da8a32003-04-22 18:42:41 +0000471static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000472 if (!LateResolveTypes.empty()) {
Chris Lattnere6b470c2001-10-22 06:01:08 +0000473 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000474
475 if (DID.Type == ValID::NameVal)
Chris Lattnere6b470c2001-10-22 06:01:08 +0000476 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000477 else
Chris Lattnere6b470c2001-10-22 06:01:08 +0000478 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000479 }
480}
481
Chris Lattner571a6b02001-10-03 01:49:25 +0000482
Chris Lattner7fb14f52001-09-18 04:00:54 +0000483// setValueName - Set the specified value to the name given. The name may be
484// null potentially, in which case this is a noop. The string passed in is
485// assumed to be a malloc'd string buffer, and is freed by this function.
486//
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000487// This function returns true if the value has already been defined, but is
488// allowed to be redefined in the specified context. If the name is a new name
489// for the typeplane, false is returned.
490//
491static bool setValueName(Value *V, char *NameStr) {
492 if (NameStr == 0) return false;
Chris Lattner322e49a2001-10-16 19:54:17 +0000493
Chris Lattner89da8a32003-04-22 18:42:41 +0000494 std::string Name(NameStr); // Copy string
Chris Lattner7fb14f52001-09-18 04:00:54 +0000495 free(NameStr); // Free old string
496
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000497 if (V->getType() == Type::VoidTy)
498 ThrowException("Can't assign name '" + Name +
499 "' to a null valued instruction!");
500
Chris Lattner98cf1f52002-11-20 18:36:02 +0000501 SymbolTable &ST = inFunctionScope() ?
502 CurMeth.CurrentFunction->getSymbolTable() :
503 CurModule.CurrentModule->getSymbolTable();
Chris Lattneraa534bf2001-09-07 16:35:17 +0000504
Chris Lattner98cf1f52002-11-20 18:36:02 +0000505 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000506 if (Existing) { // Inserting a name that is already defined???
507 // There is only one case where this is allowed: when we are refining an
508 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner571a6b02001-10-03 01:49:25 +0000509 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000510 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000511 // We ARE replacing an opaque type!
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000512 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000513 return true;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000514 }
Chris Lattner571a6b02001-10-03 01:49:25 +0000515 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000516
Chris Lattner4b717c02001-10-01 16:18:37 +0000517 // Otherwise, we are a simple redefinition of a value, check to see if it
518 // is defined the same as the old one...
Chris Lattner8ce4e792003-03-03 23:28:55 +0000519 if (const Type *Ty = dyn_cast<Type>(Existing)) {
520 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattnerdb3b2022002-08-14 17:12:33 +0000521 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000522 // << cast<const Type>(V)->getDescription() << "!\n";
Chris Lattner8ce4e792003-03-03 23:28:55 +0000523 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
524 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000525 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner78326d22001-10-03 19:35:57 +0000526 // We are allowed to redefine a global variable in two circumstances:
527 // 1. If at least one of the globals is uninitialized or
528 // 2. If both initializers have the same value.
529 //
Chris Lattnere504b432001-10-03 19:35:04 +0000530 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerb90181e2003-02-02 16:40:20 +0000531 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
532 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000533
Chris Lattnerb90181e2003-02-02 16:40:20 +0000534 // Make sure the existing global version gets the initializer! Make
535 // sure that it also gets marked const if the new version is.
Chris Lattnere504b432001-10-03 19:35:04 +0000536 if (GV->hasInitializer() && !EGV->hasInitializer())
537 EGV->setInitializer(GV->getInitializer());
Chris Lattnerb90181e2003-02-02 16:40:20 +0000538 if (GV->isConstant())
539 EGV->setConstant(true);
Chris Lattner379a8d22003-04-16 20:28:45 +0000540 EGV->setLinkage(GV->getLinkage());
Chris Lattnere504b432001-10-03 19:35:04 +0000541
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000542 delete GV; // Destroy the duplicate!
Chris Lattnere504b432001-10-03 19:35:04 +0000543 return true; // They are equivalent!
544 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000545 }
Chris Lattner4b717c02001-10-01 16:18:37 +0000546 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000547 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattneraa534bf2001-09-07 16:35:17 +0000548 V->getType()->getDescription() + "' type plane!");
Chris Lattner26e50dc2001-07-28 17:48:55 +0000549 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000550
Chris Lattner98cf1f52002-11-20 18:36:02 +0000551 V->setName(Name, &ST);
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000552 return false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000553}
554
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000555
Chris Lattneraa534bf2001-09-07 16:35:17 +0000556//===----------------------------------------------------------------------===//
557// Code for handling upreferences in type names...
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000558//
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000559
Chris Lattneraa534bf2001-09-07 16:35:17 +0000560// TypeContains - Returns true if Ty contains E in it.
561//
562static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattnerbb09a102001-09-28 22:56:31 +0000563 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000564}
Chris Lattner1caf0bb2001-07-20 19:15:08 +0000565
Chris Lattneraa534bf2001-09-07 16:35:17 +0000566
Chris Lattner89da8a32003-04-22 18:42:41 +0000567static std::vector<std::pair<unsigned, OpaqueType *> > UpRefs;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000568
Chris Lattner30752bd92002-04-04 19:23:55 +0000569static PATypeHolder HandleUpRefs(const Type *ty) {
570 PATypeHolder Ty(ty);
Chris Lattnerf29b2312001-11-02 07:46:26 +0000571 UR_OUT("Type '" << ty->getDescription() <<
572 "' newly formed. Resolving upreferences.\n" <<
573 UpRefs.size() << " upreferences active!\n");
Chris Lattneraa534bf2001-09-07 16:35:17 +0000574 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattnerf29b2312001-11-02 07:46:26 +0000575 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattneraa534bf2001-09-07 16:35:17 +0000576 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerf29b2312001-11-02 07:46:26 +0000577 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000578 if (TypeContains(Ty, UpRefs[i].second)) {
579 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattnerf29b2312001-11-02 07:46:26 +0000580 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000581 if (Level == 0) { // Upreference should be resolved!
Chris Lattnerf29b2312001-11-02 07:46:26 +0000582 UR_OUT(" * Resolving upreference for "
583 << UpRefs[i].second->getDescription() << endl;
Chris Lattner89da8a32003-04-22 18:42:41 +0000584 std::string OldName = UpRefs[i].second->getDescription());
Chris Lattneraa534bf2001-09-07 16:35:17 +0000585 UpRefs[i].second->refineAbstractTypeTo(Ty);
586 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerf29b2312001-11-02 07:46:26 +0000587 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattneraa534bf2001-09-07 16:35:17 +0000588 << (const void*)Ty << ", " << Ty->getDescription() << endl);
589 continue;
590 }
591 }
592
593 ++i; // Otherwise, no resolve, move on...
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000594 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000595 // FIXME: TODO: this should return the updated type
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000596 return Ty;
597}
598
Chris Lattneraa534bf2001-09-07 16:35:17 +0000599
Chris Lattner2f7c9632001-06-06 20:29:01 +0000600//===----------------------------------------------------------------------===//
601// RunVMAsmParser - Define an interface to this parser
602//===----------------------------------------------------------------------===//
603//
Chris Lattner89da8a32003-04-22 18:42:41 +0000604Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000605 llvmAsmin = F;
Chris Lattnerf2d1e792001-07-22 18:36:00 +0000606 CurFilename = Filename;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000607 llvmAsmlineno = 1; // Reset the current line number...
608
Chris Lattnerb672b562003-04-22 18:02:52 +0000609 // Allocate a new module to read
610 CurModule.CurrentModule = new Module(Filename);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000611 yyparse(); // Parse the file.
612 Module *Result = ParserResult;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000613 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
614 ParserResult = 0;
615
616 return Result;
617}
618
619%}
620
621%union {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000622 Module *ModuleVal;
Chris Lattner57698e22002-03-26 18:01:55 +0000623 Function *FunctionVal;
Chris Lattner149376d2002-10-13 20:57:00 +0000624 std::pair<PATypeHolder*, char*> *ArgVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000625 BasicBlock *BasicBlockVal;
626 TerminatorInst *TermInstVal;
627 Instruction *InstVal;
Chris Lattner3462ae32001-12-03 22:26:30 +0000628 Constant *ConstVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000629
Chris Lattneraa534bf2001-09-07 16:35:17 +0000630 const Type *PrimType;
Chris Lattner30752bd92002-04-04 19:23:55 +0000631 PATypeHolder *TypeVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000632 Value *ValueVal;
633
Chris Lattner149376d2002-10-13 20:57:00 +0000634 std::vector<std::pair<PATypeHolder*,char*> > *ArgList;
Chris Lattner7f74a562002-01-20 22:54:45 +0000635 std::vector<Value*> *ValueList;
Chris Lattner30752bd92002-04-04 19:23:55 +0000636 std::list<PATypeHolder> *TypeList;
Chris Lattner7f74a562002-01-20 22:54:45 +0000637 std::list<std::pair<Value*,
638 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattnerae234e12002-04-09 19:41:42 +0000639 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner7f74a562002-01-20 22:54:45 +0000640 std::vector<Constant*> *ConstVector;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000641
Chris Lattner379a8d22003-04-16 20:28:45 +0000642 GlobalValue::LinkageTypes Linkage;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000643 int64_t SInt64Val;
644 uint64_t UInt64Val;
645 int SIntVal;
646 unsigned UIntVal;
647 double FPVal;
Chris Lattner7fb14f52001-09-18 04:00:54 +0000648 bool BoolVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000649
Chris Lattneraa534bf2001-09-07 16:35:17 +0000650 char *StrVal; // This memory is strdup'd!
651 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000652
Chris Lattneraa534bf2001-09-07 16:35:17 +0000653 Instruction::BinaryOps BinaryOpVal;
654 Instruction::TermOps TermOpVal;
655 Instruction::MemoryOps MemOpVal;
656 Instruction::OtherOps OtherOpVal;
Chris Lattner20126132003-04-22 19:07:06 +0000657 Module::Endianness Endianness;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000658}
659
Chris Lattner57698e22002-03-26 18:01:55 +0000660%type <ModuleVal> Module FunctionList
661%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner2f7c9632001-06-06 20:29:01 +0000662%type <BasicBlockVal> BasicBlock InstructionList
663%type <TermInstVal> BBTerminatorInst
664%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000665%type <ConstVal> ConstVal ConstExpr
Chris Lattner476148f2001-11-26 16:54:11 +0000666%type <ConstVector> ConstVector
Chris Lattnerae234e12002-04-09 19:41:42 +0000667%type <ArgList> ArgList ArgListH
668%type <ArgVal> ArgVal
Chris Lattner931ef3b2001-06-11 15:04:20 +0000669%type <PHIList> PHIList
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000670%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner476148f2001-11-26 16:54:11 +0000671%type <ValueList> IndexList // For GEP derived indices
Chris Lattneraa534bf2001-09-07 16:35:17 +0000672%type <TypeList> TypeListI ArgTypeListI
Chris Lattner2f7c9632001-06-06 20:29:01 +0000673%type <JumpTable> JumpTable
Chris Lattner379a8d22003-04-16 20:28:45 +0000674%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
675%type <Linkage> OptLinkage
Chris Lattner20126132003-04-22 19:07:06 +0000676%type <Endianness> BigOrLittle
Chris Lattner2f7c9632001-06-06 20:29:01 +0000677
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000678// ValueRef - Unresolved reference to a definition or BB
679%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattner252afba2001-07-26 16:29:15 +0000680%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner2f7c9632001-06-06 20:29:01 +0000681// Tokens and types for handling constant integer values
682//
683// ESINT64VAL - A negative number within long long range
684%token <SInt64Val> ESINT64VAL
685
686// EUINT64VAL - A positive number within uns. long long range
687%token <UInt64Val> EUINT64VAL
688%type <SInt64Val> EINT64VAL
689
690%token <SIntVal> SINTVAL // Signed 32 bit ints...
691%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
692%type <SIntVal> INTVAL
Chris Lattner212f70d2001-07-15 00:17:01 +0000693%token <FPVal> FPVAL // Float or Double constant
Chris Lattner2f7c9632001-06-06 20:29:01 +0000694
695// Built in types...
Chris Lattneraa534bf2001-09-07 16:35:17 +0000696%type <TypeVal> Types TypesV UpRTypes UpRTypesV
697%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattneraa534bf2001-09-07 16:35:17 +0000698%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
699%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner2f7c9632001-06-06 20:29:01 +0000700
701%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattnerf4c7dd92002-05-22 22:33:00 +0000702%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner2f7c9632001-06-06 20:29:01 +0000703
704
Chris Lattnere2dd92b2002-10-06 22:45:09 +0000705%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT
Chris Lattner379a8d22003-04-16 20:28:45 +0000706%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE APPENDING
Chris Lattner20126132003-04-22 19:07:06 +0000707%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner2f7c9632001-06-06 20:29:01 +0000708
709// Basic Block Terminating Operators
710%token <TermOpVal> RET BR SWITCH
711
Chris Lattner2f7c9632001-06-06 20:29:01 +0000712// Binary Operators
713%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattnerc0686fe2002-09-10 19:57:26 +0000714%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000715%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000716%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner2f7c9632001-06-06 20:29:01 +0000717
718// Memory Instructions
Vikram S. Adve7064eaf2002-07-14 22:59:28 +0000719%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner2f7c9632001-06-06 20:29:01 +0000720
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000721// Other Operators
722%type <OtherOpVal> ShiftOps
Chris Lattnerf70da102003-05-08 02:44:12 +0000723%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR VA_ARG
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000724
Chris Lattner2f7c9632001-06-06 20:29:01 +0000725%start Module
726%%
727
728// Handle constant integer size restriction and conversion...
729//
730
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000731INTVAL : SINTVAL;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000732INTVAL : UINTVAL {
733 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
734 ThrowException("Value too large for type!");
735 $$ = (int32_t)$1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000736};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000737
738
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000739EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner2f7c9632001-06-06 20:29:01 +0000740EINT64VAL : EUINT64VAL {
741 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
742 ThrowException("Value too large for type!");
743 $$ = (int64_t)$1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000744};
Chris Lattner2f7c9632001-06-06 20:29:01 +0000745
Chris Lattner2f7c9632001-06-06 20:29:01 +0000746// Operations that are notably excluded from this list include:
747// RET, BR, & SWITCH because they end basic blocks and are treated specially.
748//
Chris Lattnerc0686fe2002-09-10 19:57:26 +0000749ArithmeticOps: ADD | SUB | MUL | DIV | REM;
750LogicalOps : AND | OR | XOR;
751SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
752BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
753
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000754ShiftOps : SHL | SHR;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000755
Chris Lattner56f73d42001-07-14 06:10:16 +0000756// These are some types that allow classification if we only want a particular
757// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000758SIntType : LONG | INT | SHORT | SBYTE;
759UIntType : ULONG | UINT | USHORT | UBYTE;
760IntType : SIntType | UIntType;
761FPType : FLOAT | DOUBLE;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000762
Chris Lattner56f73d42001-07-14 06:10:16 +0000763// OptAssign - Value producing statements have an optional assignment component
Chris Lattner2f7c9632001-06-06 20:29:01 +0000764OptAssign : VAR_ID '=' {
765 $$ = $1;
766 }
767 | /*empty*/ {
768 $$ = 0;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000769 };
Chris Lattner2f7c9632001-06-06 20:29:01 +0000770
Chris Lattner379a8d22003-04-16 20:28:45 +0000771OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
772 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
773 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
774 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000775
776//===----------------------------------------------------------------------===//
777// Types includes all predefined types... except void, because it can only be
Chris Lattner113f4f42002-06-25 16:13:24 +0000778// used in specific contexts (function returning void for example). To have
Chris Lattneraa534bf2001-09-07 16:35:17 +0000779// access to it, a user must explicitly use TypesV.
780//
781
782// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000783TypesV : Types | VOID { $$ = new PATypeHolder($1); };
784UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000785
786Types : UpRTypes {
Chris Lattner30752bd92002-04-04 19:23:55 +0000787 if (UpRefs.size())
788 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
789 $$ = $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000790 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000791
792
793// Derived types are added later...
794//
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000795PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
796PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner30752bd92002-04-04 19:23:55 +0000797UpRTypes : OPAQUE {
798 $$ = new PATypeHolder(OpaqueType::get());
799 }
800 | PrimType {
801 $$ = new PATypeHolder($1);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000802 };
Chris Lattner7f325cd2002-08-16 21:14:40 +0000803UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner30752bd92002-04-04 19:23:55 +0000804 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000805};
Chris Lattneraa534bf2001-09-07 16:35:17 +0000806
Chris Lattneraa534bf2001-09-07 16:35:17 +0000807// Include derived types in the Types production.
808//
809UpRTypes : '\\' EUINT64VAL { // Type UpReference
810 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
811 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner89da8a32003-04-22 18:42:41 +0000812 UpRefs.push_back(std::make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner30752bd92002-04-04 19:23:55 +0000813 $$ = new PATypeHolder(OT);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000814 UR_OUT("New Upreference!\n");
815 }
Chris Lattner57698e22002-03-26 18:01:55 +0000816 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner89da8a32003-04-22 18:42:41 +0000817 std::vector<const Type*> Params;
Chris Lattner7f74a562002-01-20 22:54:45 +0000818 mapto($3->begin(), $3->end(), std::back_inserter(Params),
819 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000820 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
821 if (isVarArg) Params.pop_back();
822
Chris Lattner30752bd92002-04-04 19:23:55 +0000823 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000824 delete $3; // Delete the argument list
825 delete $1; // Delete the old type handle
826 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000827 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner30752bd92002-04-04 19:23:55 +0000828 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000829 delete $4;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000830 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000831 | '{' TypeListI '}' { // Structure type?
Chris Lattner89da8a32003-04-22 18:42:41 +0000832 std::vector<const Type*> Elements;
Chris Lattner7f74a562002-01-20 22:54:45 +0000833 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
834 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000835
Chris Lattner30752bd92002-04-04 19:23:55 +0000836 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000837 delete $2;
838 }
839 | '{' '}' { // Empty structure type?
Chris Lattner89da8a32003-04-22 18:42:41 +0000840 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000841 }
842 | UpRTypes '*' { // Pointer type?
Chris Lattner30752bd92002-04-04 19:23:55 +0000843 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000844 delete $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000845 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000846
Chris Lattner113f4f42002-06-25 16:13:24 +0000847// TypeList - Used for struct declarations and as a basis for function type
Chris Lattneraa534bf2001-09-07 16:35:17 +0000848// declaration type lists
849//
850TypeListI : UpRTypes {
Chris Lattner89da8a32003-04-22 18:42:41 +0000851 $$ = new std::list<PATypeHolder>();
Chris Lattneraa534bf2001-09-07 16:35:17 +0000852 $$->push_back(*$1); delete $1;
853 }
854 | TypeListI ',' UpRTypes {
855 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000856 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000857
Chris Lattner113f4f42002-06-25 16:13:24 +0000858// ArgTypeList - List of types for a function type declaration...
Chris Lattneraa534bf2001-09-07 16:35:17 +0000859ArgTypeListI : TypeListI
860 | TypeListI ',' DOTDOTDOT {
861 ($$=$1)->push_back(Type::VoidTy);
862 }
863 | DOTDOTDOT {
Chris Lattner89da8a32003-04-22 18:42:41 +0000864 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000865 }
866 | /*empty*/ {
Chris Lattner89da8a32003-04-22 18:42:41 +0000867 $$ = new std::list<PATypeHolder>();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +0000868 };
Chris Lattneraa534bf2001-09-07 16:35:17 +0000869
Chris Lattner56f73d42001-07-14 06:10:16 +0000870// ConstVal - The various declarations that go into the constant pool. This
Chris Lattner7f325cd2002-08-16 21:14:40 +0000871// production is used ONLY to represent constants that show up AFTER a 'const',
872// 'constant' or 'global' token at global scope. Constants that can be inlined
873// into other expressions (such as integers and constexprs) are handled by the
874// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattner56f73d42001-07-14 06:10:16 +0000875//
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000876ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
877 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
878 if (ATy == 0)
879 ThrowException("Cannot make array constant with type: '" +
880 (*$1)->getDescription() + "'!");
Chris Lattneraa534bf2001-09-07 16:35:17 +0000881 const Type *ETy = ATy->getElementType();
882 int NumElements = ATy->getNumElements();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000883
Chris Lattneraa534bf2001-09-07 16:35:17 +0000884 // Verify that we have the correct size...
885 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner2f7c9632001-06-06 20:29:01 +0000886 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattneraa534bf2001-09-07 16:35:17 +0000887 utostr($3->size()) + " arguments, but has size of " +
888 itostr(NumElements) + "!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000889
Chris Lattneraa534bf2001-09-07 16:35:17 +0000890 // Verify all elements are correct type!
891 for (unsigned i = 0; i < $3->size(); i++) {
892 if (ETy != (*$3)[i]->getType())
Chris Lattner2f7c9632001-06-06 20:29:01 +0000893 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +0000894 ETy->getDescription() +"' as required!\nIt is of type '"+
895 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000896 }
897
Chris Lattner3462ae32001-12-03 22:26:30 +0000898 $$ = ConstantArray::get(ATy, *$3);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000899 delete $1; delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000900 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000901 | Types '[' ']' {
902 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
903 if (ATy == 0)
904 ThrowException("Cannot make array constant with type: '" +
905 (*$1)->getDescription() + "'!");
906
907 int NumElements = ATy->getNumElements();
Chris Lattneraa534bf2001-09-07 16:35:17 +0000908 if (NumElements != -1 && NumElements != 0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000909 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattneraa534bf2001-09-07 16:35:17 +0000910 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner89da8a32003-04-22 18:42:41 +0000911 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattneraa534bf2001-09-07 16:35:17 +0000912 delete $1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000913 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000914 | Types 'c' STRINGCONSTANT {
915 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
916 if (ATy == 0)
917 ThrowException("Cannot make array constant with type: '" +
918 (*$1)->getDescription() + "'!");
919
Chris Lattneraa534bf2001-09-07 16:35:17 +0000920 int NumElements = ATy->getNumElements();
921 const Type *ETy = ATy->getElementType();
922 char *EndStr = UnEscapeLexed($3, true);
923 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner26e50dc2001-07-28 17:48:55 +0000924 ThrowException("Can't build string constant of size " +
Chris Lattneraa534bf2001-09-07 16:35:17 +0000925 itostr((int)(EndStr-$3)) +
926 " when array has size " + itostr(NumElements) + "!");
Chris Lattner89da8a32003-04-22 18:42:41 +0000927 std::vector<Constant*> Vals;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000928 if (ETy == Type::SByteTy) {
929 for (char *C = $3; C != EndStr; ++C)
Chris Lattner3462ae32001-12-03 22:26:30 +0000930 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000931 } else if (ETy == Type::UByteTy) {
932 for (char *C = $3; C != EndStr; ++C)
Chris Lattner63a25242003-01-30 22:24:26 +0000933 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner26e50dc2001-07-28 17:48:55 +0000934 } else {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000935 free($3);
Chris Lattner26e50dc2001-07-28 17:48:55 +0000936 ThrowException("Cannot build string arrays of non byte sized elements!");
937 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000938 free($3);
Chris Lattner3462ae32001-12-03 22:26:30 +0000939 $$ = ConstantArray::get(ATy, Vals);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000940 delete $1;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000941 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000942 | Types '{' ConstVector '}' {
943 const StructType *STy = dyn_cast<const StructType>($1->get());
944 if (STy == 0)
945 ThrowException("Cannot make struct constant with type: '" +
946 (*$1)->getDescription() + "'!");
Chris Lattner4b1d1062003-04-15 16:09:31 +0000947
Chris Lattner5bb23152003-05-21 16:06:56 +0000948 if ($3->size() != STy->getNumContainedTypes())
949 ThrowException("Illegal number of initializers for structure type!");
950
Chris Lattner4b1d1062003-04-15 16:09:31 +0000951 // Check to ensure that constants are compatible with the type initializer!
952 for (unsigned i = 0, e = $3->size(); i != e; ++i)
953 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
954 ThrowException("Expected type '" +
955 STy->getElementTypes()[i]->getDescription() +
956 "' for element #" + utostr(i) +
957 " of structure initializer!");
958
Chris Lattner3462ae32001-12-03 22:26:30 +0000959 $$ = ConstantStruct::get(STy, *$3);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000960 delete $1; delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000961 }
Chris Lattner5bb23152003-05-21 16:06:56 +0000962 | Types '{' '}' {
963 const StructType *STy = dyn_cast<const StructType>($1->get());
964 if (STy == 0)
965 ThrowException("Cannot make struct constant with type: '" +
966 (*$1)->getDescription() + "'!");
967
968 if (STy->getNumContainedTypes() != 0)
969 ThrowException("Illegal number of initializers for structure type!");
970
971 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
972 delete $1;
973 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000974 | Types NULL_TOK {
975 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
976 if (PTy == 0)
977 ThrowException("Cannot make null pointer constant with type: '" +
978 (*$1)->getDescription() + "'!");
979
Chris Lattner3462ae32001-12-03 22:26:30 +0000980 $$ = ConstantPointerNull::get(PTy);
Chris Lattner571a6b02001-10-03 01:49:25 +0000981 delete $1;
982 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000983 | Types SymbolicValueRef {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000984 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
985 if (Ty == 0)
986 ThrowException("Global const reference must be a pointer type!");
987
Chris Lattner61643a02002-08-15 17:58:33 +0000988 // ConstExprs can exist in the body of a function, thus creating
989 // ConstantPointerRefs whenever they refer to a variable. Because we are in
990 // the context of a function, getValNonImprovising will search the functions
991 // symbol table instead of the module symbol table for the global symbol,
992 // which throws things all off. To get around this, we just tell
993 // getValNonImprovising that we are at global scope here.
994 //
995 Function *SavedCurFn = CurMeth.CurrentFunction;
996 CurMeth.CurrentFunction = 0;
997
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000998 Value *V = getValNonImprovising(Ty, $2);
Chris Lattner60e0dd72001-10-03 06:12:09 +0000999
Chris Lattner61643a02002-08-15 17:58:33 +00001000 CurMeth.CurrentFunction = SavedCurFn;
1001
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001002 // If this is an initializer for a constant pointer, which is referencing a
1003 // (currently) undefined variable, create a stub now that shall be replaced
1004 // in the future with the right type of variable.
1005 //
1006 if (V == 0) {
1007 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1008 const PointerType *PT = cast<PointerType>(Ty);
1009
1010 // First check to see if the forward references value is already created!
1011 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner89da8a32003-04-22 18:42:41 +00001012 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001013
1014 if (I != CurModule.GlobalRefs.end()) {
1015 V = I->second; // Placeholder already exists, use it...
1016 } else {
1017 // TODO: Include line number info by creating a subclass of
1018 // TODO: GlobalVariable here that includes the said information!
1019
1020 // Create a placeholder for the global variable reference...
Chris Lattner2413b162001-12-04 00:03:30 +00001021 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner379a8d22003-04-16 20:28:45 +00001022 false,
1023 GlobalValue::ExternalLinkage);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001024 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner89da8a32003-04-22 18:42:41 +00001025 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001026
1027 // Must temporarily push this value into the module table...
1028 CurModule.CurrentModule->getGlobalList().push_back(GV);
1029 V = GV;
1030 }
Chris Lattner60e0dd72001-10-03 06:12:09 +00001031 }
1032
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001033 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattner3462ae32001-12-03 22:26:30 +00001034 $$ = ConstantPointerRef::get(GV);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001035 delete $1; // Free the type handle
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001036 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001037 | Types ConstExpr {
1038 if ($1->get() != $2->getType())
1039 ThrowException("Mismatched types for constant expression!");
1040 $$ = $2;
1041 delete $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001042 };
Chris Lattner60e0dd72001-10-03 06:12:09 +00001043
Chris Lattnerfa01a522002-10-09 00:25:32 +00001044ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001045 if (!ConstantSInt::isValueValidForType($1, $2))
1046 ThrowException("Constant value doesn't fit in type!");
1047 $$ = ConstantSInt::get($1, $2);
Chris Lattnerfa01a522002-10-09 00:25:32 +00001048 }
1049 | UIntType EUINT64VAL { // integral constants
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001050 if (!ConstantUInt::isValueValidForType($1, $2))
1051 ThrowException("Constant value doesn't fit in type!");
1052 $$ = ConstantUInt::get($1, $2);
Chris Lattnerfa01a522002-10-09 00:25:32 +00001053 }
1054 | BOOL TRUE { // Boolean constants
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001055 $$ = ConstantBool::True;
1056 }
Chris Lattnerfa01a522002-10-09 00:25:32 +00001057 | BOOL FALSE { // Boolean constants
Chris Lattner7f1c98d2002-08-15 18:17:28 +00001058 $$ = ConstantBool::False;
1059 }
1060 | FPType FPVAL { // Float & Double constants
1061 $$ = ConstantFP::get($1, $2);
1062 };
1063
Chris Lattner2f7c9632001-06-06 20:29:01 +00001064
Chris Lattner7f325cd2002-08-16 21:14:40 +00001065ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerd8ecff72002-08-15 19:37:11 +00001066 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerd8ecff72002-08-15 19:37:11 +00001067 delete $5;
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001068 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001069 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1070 if (!isa<PointerType>($3->getType()))
Chris Lattner3cd8c562002-07-30 18:54:25 +00001071 ThrowException("GetElementPtr requires a pointer operand!");
1072
1073 const Type *IdxTy =
Chris Lattner7f325cd2002-08-16 21:14:40 +00001074 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattner3cd8c562002-07-30 18:54:25 +00001075 if (!IdxTy)
1076 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattner3cd8c562002-07-30 18:54:25 +00001077
Chris Lattner89da8a32003-04-22 18:42:41 +00001078 std::vector<Constant*> IdxVec;
Chris Lattner7f325cd2002-08-16 21:14:40 +00001079 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1080 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattner980ddf52002-07-18 00:14:27 +00001081 IdxVec.push_back(C);
1082 else
Chris Lattner3cd8c562002-07-30 18:54:25 +00001083 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattner980ddf52002-07-18 00:14:27 +00001084
Chris Lattner7f325cd2002-08-16 21:14:40 +00001085 delete $4;
Chris Lattner980ddf52002-07-18 00:14:27 +00001086
Chris Lattner7f325cd2002-08-16 21:14:40 +00001087 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001088 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001089 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattner3cd8c562002-07-30 18:54:25 +00001090 if ($3->getType() != $5->getType())
1091 ThrowException("Binary operator types must match!");
Chris Lattner7f325cd2002-08-16 21:14:40 +00001092 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001093 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001094 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattner3cd8c562002-07-30 18:54:25 +00001095 if ($5->getType() != Type::UByteTy)
1096 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner7f325cd2002-08-16 21:14:40 +00001097 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001098 };
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001099
1100
Chris Lattner56f73d42001-07-14 06:10:16 +00001101// ConstVector - A list of comma seperated constants.
Chris Lattner2f7c9632001-06-06 20:29:01 +00001102ConstVector : ConstVector ',' ConstVal {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001103 ($$ = $1)->push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001104 }
1105 | ConstVal {
Chris Lattner89da8a32003-04-22 18:42:41 +00001106 $$ = new std::vector<Constant*>();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001107 $$->push_back($1);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001108 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001109
Chris Lattner252afba2001-07-26 16:29:15 +00001110
Chris Lattner7fb14f52001-09-18 04:00:54 +00001111// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001112GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner7fb14f52001-09-18 04:00:54 +00001113
Chris Lattner2f7c9632001-06-06 20:29:01 +00001114
Chris Lattner7b804d62002-05-02 19:11:13 +00001115//===----------------------------------------------------------------------===//
1116// Rules to match Modules
1117//===----------------------------------------------------------------------===//
1118
1119// Module rule: Capture the result of parsing the whole file into a result
1120// variable...
1121//
1122Module : FunctionList {
1123 $$ = ParserResult = $1;
1124 CurModule.ModuleDone();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001125};
Chris Lattner7b804d62002-05-02 19:11:13 +00001126
Chris Lattner113f4f42002-06-25 16:13:24 +00001127// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner7b804d62002-05-02 19:11:13 +00001128//
1129FunctionList : FunctionList Function {
1130 $$ = $1;
1131 assert($2->getParent() == 0 && "Function already in module!");
1132 $1->getFunctionList().push_back($2);
1133 CurMeth.FunctionDone();
1134 }
1135 | FunctionList FunctionProto {
1136 $$ = $1;
1137 }
1138 | FunctionList IMPLEMENTATION {
1139 $$ = $1;
1140 }
1141 | ConstPool {
1142 $$ = CurModule.CurrentModule;
1143 // Resolve circular types before we parse the body of the module
1144 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001145 };
Chris Lattner7b804d62002-05-02 19:11:13 +00001146
Chris Lattner56f73d42001-07-14 06:10:16 +00001147// ConstPool - Constants with optional names assigned to them.
Chris Lattner571a6b02001-10-03 01:49:25 +00001148ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattner8ce4e792003-03-03 23:28:55 +00001149 if (!setValueName($4, $2))
1150 InsertValue($4);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001151 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001152 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattnerd50fa5a2001-10-22 05:56:09 +00001153 // Eagerly resolve types. This is not an optimization, this is a
1154 // requirement that is due to the fact that we could have this:
1155 //
1156 // %list = type { %list * }
1157 // %list = type { %list * } ; repeated type decl
1158 //
1159 // If types are not resolved eagerly, then the two types will not be
1160 // determined to be the same type!
1161 //
1162 ResolveTypeTo($2, $4->get());
1163
Chris Lattner7fb14f52001-09-18 04:00:54 +00001164 // TODO: FIXME when Type are not const
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001165 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1166 // If this is not a redefinition of a type...
1167 if (!$2) {
1168 InsertType($4->get(),
Chris Lattner57698e22002-03-26 18:01:55 +00001169 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001170 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001171 }
Chris Lattner6265d792001-10-21 23:02:41 +00001172
1173 delete $4;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001174 }
Chris Lattner57698e22002-03-26 18:01:55 +00001175 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner26e50dc2001-07-28 17:48:55 +00001176 }
Chris Lattner379a8d22003-04-16 20:28:45 +00001177 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattner841d8b92001-11-26 18:54:16 +00001178 const Type *Ty = $5->getType();
Chris Lattner7fb14f52001-09-18 04:00:54 +00001179 // Global declarations appear in Constant Pool
Chris Lattner3462ae32001-12-03 22:26:30 +00001180 Constant *Initializer = $5;
Chris Lattner7fb14f52001-09-18 04:00:54 +00001181 if (Initializer == 0)
1182 ThrowException("Global value initializer is not a constant!");
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001183
Chris Lattner841d8b92001-11-26 18:54:16 +00001184 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001185 if (!setValueName(GV, $2)) { // If not redefining...
1186 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001187 int Slot = InsertValue(GV, CurModule.Values);
1188
1189 if (Slot != -1) {
1190 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1191 } else {
1192 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1193 (char*)GV->getName().c_str()));
1194 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001195 }
Chris Lattner7fb14f52001-09-18 04:00:54 +00001196 }
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001197 | ConstPool OptAssign EXTERNAL GlobalType Types {
1198 const Type *Ty = *$5;
Chris Lattner7fb14f52001-09-18 04:00:54 +00001199 // Global declarations appear in Constant Pool
Chris Lattner379a8d22003-04-16 20:28:45 +00001200 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001201 if (!setValueName(GV, $2)) { // If not redefining...
1202 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001203 int Slot = InsertValue(GV, CurModule.Values);
1204
1205 if (Slot != -1) {
1206 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1207 } else {
1208 assert(GV->hasName() && "Not named and not numbered!?");
1209 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1210 (char*)GV->getName().c_str()));
1211 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001212 }
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001213 delete $5;
Chris Lattner56f73d42001-07-14 06:10:16 +00001214 }
Chris Lattner20126132003-04-22 19:07:06 +00001215 | ConstPool TARGET TargetDefinition {
1216 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001217 | /* empty: end of list */ {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001218 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001219
1220
Chris Lattner20126132003-04-22 19:07:06 +00001221
1222BigOrLittle : BIG { $$ = Module::BigEndian; };
1223BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1224
1225TargetDefinition : ENDIAN '=' BigOrLittle {
1226 CurModule.CurrentModule->setEndianness($3);
1227 }
1228 | POINTERSIZE '=' EUINT64VAL {
1229 if ($3 == 32)
1230 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1231 else if ($3 == 64)
1232 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1233 else
1234 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1235 };
1236
1237
Chris Lattner2f7c9632001-06-06 20:29:01 +00001238//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +00001239// Rules to match Function Headers
Chris Lattner2f7c9632001-06-06 20:29:01 +00001240//===----------------------------------------------------------------------===//
1241
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001242OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001243
1244ArgVal : Types OptVAR_ID {
Chris Lattner149376d2002-10-13 20:57:00 +00001245 if (*$1 == Type::VoidTy)
1246 ThrowException("void typed arguments are invalid!");
Chris Lattner89da8a32003-04-22 18:42:41 +00001247 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001248};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001249
Chris Lattner149376d2002-10-13 20:57:00 +00001250ArgListH : ArgListH ',' ArgVal {
1251 $$ = $1;
1252 $1->push_back(*$3);
1253 delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001254 }
1255 | ArgVal {
Chris Lattner89da8a32003-04-22 18:42:41 +00001256 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner149376d2002-10-13 20:57:00 +00001257 $$->push_back(*$1);
Chris Lattner29f67092002-03-08 18:41:32 +00001258 delete $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001259 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001260
1261ArgList : ArgListH {
1262 $$ = $1;
1263 }
Chris Lattner149376d2002-10-13 20:57:00 +00001264 | ArgListH ',' DOTDOTDOT {
1265 $$ = $1;
Chris Lattner89da8a32003-04-22 18:42:41 +00001266 $$->push_back(std::pair<PATypeHolder*,
1267 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner149376d2002-10-13 20:57:00 +00001268 }
1269 | DOTDOTDOT {
Chris Lattner89da8a32003-04-22 18:42:41 +00001270 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1271 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner149376d2002-10-13 20:57:00 +00001272 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001273 | /* empty */ {
1274 $$ = 0;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001275 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001276
Chris Lattnerf4c7dd92002-05-22 22:33:00 +00001277FuncName : VAR_ID | STRINGCONSTANT;
1278
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001279FunctionHeaderH : TypesV FuncName '(' ArgList ')' {
1280 UnEscapeLexed($2);
Chris Lattner89da8a32003-04-22 18:42:41 +00001281 std::string FunctionName($2);
Chris Lattner841d8b92001-11-26 18:54:16 +00001282
Chris Lattner89da8a32003-04-22 18:42:41 +00001283 std::vector<const Type*> ParamTypeList;
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001284 if ($4) { // If there are arguments...
Chris Lattner89da8a32003-04-22 18:42:41 +00001285 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001286 I != $4->end(); ++I)
Chris Lattner149376d2002-10-13 20:57:00 +00001287 ParamTypeList.push_back(I->first->get());
Chris Lattner19613292002-10-15 21:41:14 +00001288 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001289
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001290 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1291 if (isVarArg) ParamTypeList.pop_back();
1292
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001293 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattner19613292002-10-15 21:41:14 +00001294 const PointerType *PFT = PointerType::get(FT);
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001295 delete $1;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001296
Chris Lattner19613292002-10-15 21:41:14 +00001297 Function *Fn = 0;
1298 // Is the function already in symtab?
1299 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1300 // Yes it is. If this is the case, either we need to be a forward decl,
1301 // or it needs to be.
1302 if (!CurMeth.isDeclare && !Fn->isExternal())
1303 ThrowException("Redefinition of function '" + FunctionName + "'!");
1304
Chris Lattner19613292002-10-15 21:41:14 +00001305 // If we found a preexisting function prototype, remove it from the
1306 // module, so that we don't get spurious conflicts with global & local
1307 // variables.
1308 //
1309 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner108308a2002-03-08 19:11:42 +00001310
Chris Lattner19613292002-10-15 21:41:14 +00001311 // Make sure to strip off any argument names so we can't get conflicts...
1312 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1313 AI->setName("");
Chris Lattner8445b412002-07-15 00:10:33 +00001314
Chris Lattner19613292002-10-15 21:41:14 +00001315 } else { // Not already defined?
Chris Lattner379a8d22003-04-16 20:28:45 +00001316 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattner19613292002-10-15 21:41:14 +00001317 InsertValue(Fn, CurModule.Values);
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001318 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattner17f729e2001-07-15 06:35:53 +00001319 }
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001320 free($2); // Free strdup'd memory!
Chris Lattner2f7c9632001-06-06 20:29:01 +00001321
Chris Lattner19613292002-10-15 21:41:14 +00001322 CurMeth.FunctionStart(Fn);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001323
Chris Lattner113f4f42002-06-25 16:13:24 +00001324 // Add all of the arguments we parsed to the function...
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001325 if ($4) { // Is null if empty...
Chris Lattner149376d2002-10-13 20:57:00 +00001326 if (isVarArg) { // Nuke the last entry
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001327 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner149376d2002-10-13 20:57:00 +00001328 "Not a varargs marker!");
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001329 delete $4->back().first;
1330 $4->pop_back(); // Delete the last entry
Chris Lattner149376d2002-10-13 20:57:00 +00001331 }
Chris Lattner19613292002-10-15 21:41:14 +00001332 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner89da8a32003-04-22 18:42:41 +00001333 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001334 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner149376d2002-10-13 20:57:00 +00001335 delete I->first; // Delete the typeholder...
1336
1337 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattner29f67092002-03-08 18:41:32 +00001338 assert(0 && "No arg redef allowed!");
Chris Lattner29f67092002-03-08 18:41:32 +00001339
Chris Lattner149376d2002-10-13 20:57:00 +00001340 InsertValue(ArgIt);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001341 }
Chris Lattner149376d2002-10-13 20:57:00 +00001342
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001343 delete $4; // We're now done with the argument list
Chris Lattner2f7c9632001-06-06 20:29:01 +00001344 }
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001345};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001346
Chris Lattner1e1a9b42002-05-03 18:23:48 +00001347BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1348
Chris Lattner379a8d22003-04-16 20:28:45 +00001349FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner57698e22002-03-26 18:01:55 +00001350 $$ = CurMeth.CurrentFunction;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001351
Chris Lattner379a8d22003-04-16 20:28:45 +00001352 // Make sure that we keep track of the linkage type even if there was a
1353 // previous "declare".
1354 $$->setLinkage($1);
Chris Lattnerfcfb1762003-04-16 18:13:57 +00001355
Chris Lattner113f4f42002-06-25 16:13:24 +00001356 // Resolve circular types before we parse the body of the function.
Chris Lattneraa534bf2001-09-07 16:35:17 +00001357 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001358};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001359
Chris Lattner1e1a9b42002-05-03 18:23:48 +00001360END : ENDTOK | '}'; // Allow end of '}' to end a function
1361
Chris Lattner57698e22002-03-26 18:01:55 +00001362Function : BasicBlockList END {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001363 $$ = $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001364};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001365
Chris Lattner57698e22002-03-26 18:01:55 +00001366FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1367 $$ = CurMeth.CurrentFunction;
1368 assert($$->getParent() == 0 && "Function already in module!");
1369 CurModule.CurrentModule->getFunctionList().push_back($$);
1370 CurMeth.FunctionDone();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001371};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001372
1373//===----------------------------------------------------------------------===//
1374// Rules to match Basic Blocks
1375//===----------------------------------------------------------------------===//
1376
1377ConstValueRef : ESINT64VAL { // A reference to a direct constant
1378 $$ = ValID::create($1);
1379 }
1380 | EUINT64VAL {
1381 $$ = ValID::create($1);
1382 }
Chris Lattner212f70d2001-07-15 00:17:01 +00001383 | FPVAL { // Perhaps it's an FP constant?
1384 $$ = ValID::create($1);
1385 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001386 | TRUE {
Chris Lattner7f325cd2002-08-16 21:14:40 +00001387 $$ = ValID::create(ConstantBool::True);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001388 }
1389 | FALSE {
Chris Lattner7f325cd2002-08-16 21:14:40 +00001390 $$ = ValID::create(ConstantBool::False);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001391 }
Chris Lattnerfbdec252001-09-30 22:46:54 +00001392 | NULL_TOK {
1393 $$ = ValID::createNull();
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001394 }
Chris Lattner7f325cd2002-08-16 21:14:40 +00001395 | ConstExpr {
1396 $$ = ValID::create($1);
1397 };
Chris Lattnerfbdec252001-09-30 22:46:54 +00001398
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001399// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1400// another value.
1401//
1402SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner2f7c9632001-06-06 20:29:01 +00001403 $$ = ValID::create($1);
1404 }
Chris Lattner212f70d2001-07-15 00:17:01 +00001405 | VAR_ID { // Is it a named reference...?
Chris Lattner2f7c9632001-06-06 20:29:01 +00001406 $$ = ValID::create($1);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001407 };
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001408
1409// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001410ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001411
Chris Lattner2f7c9632001-06-06 20:29:01 +00001412
Chris Lattner252afba2001-07-26 16:29:15 +00001413// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1414// type immediately preceeds the value reference, and allows complex constant
1415// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattner571a6b02001-10-03 01:49:25 +00001416ResolvedVal : Types ValueRef {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001417 $$ = getVal(*$1, $2); delete $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001418 };
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001419
Chris Lattner2f7c9632001-06-06 20:29:01 +00001420BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner113f4f42002-06-25 16:13:24 +00001421 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001422 }
Chris Lattner113f4f42002-06-25 16:13:24 +00001423 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1424 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001425 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001426
1427
1428// Basic blocks are terminated by branching instructions:
1429// br, br/cc, switch, ret
1430//
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001431BasicBlock : InstructionList OptAssign BBTerminatorInst {
1432 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1433 InsertValue($3);
1434
1435 $1->getInstList().push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001436 InsertValue($1);
1437 $$ = $1;
1438 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001439 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1440 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1441 InsertValue($4);
1442
1443 $2->getInstList().push_back($4);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001444 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001445
1446 InsertValue($2);
1447 $$ = $2;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001448 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001449
1450InstructionList : InstructionList Inst {
1451 $1->getInstList().push_back($2);
1452 $$ = $1;
1453 }
1454 | /* empty */ {
Chris Lattner319c47a2002-08-21 23:51:21 +00001455 $$ = CurBB = new BasicBlock();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001456 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001457
Chris Lattner252afba2001-07-26 16:29:15 +00001458BBTerminatorInst : RET ResolvedVal { // Return with a result...
1459 $$ = new ReturnInst($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001460 }
1461 | RET VOID { // Return with no result...
1462 $$ = new ReturnInst();
1463 }
1464 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner4b717c02001-10-01 16:18:37 +00001465 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001466 } // Conditional Branch...
1467 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner4b717c02001-10-01 16:18:37 +00001468 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1469 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner2f7c9632001-06-06 20:29:01 +00001470 getVal(Type::BoolTy, $3));
1471 }
1472 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1473 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner4b717c02001-10-01 16:18:37 +00001474 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001475 $$ = S;
1476
Chris Lattner89da8a32003-04-22 18:42:41 +00001477 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattnerae234e12002-04-09 19:41:42 +00001478 E = $8->end();
1479 for (; I != E; ++I)
Chris Lattner2f7c9632001-06-06 20:29:01 +00001480 S->dest_push_back(I->first, I->second);
1481 }
Chris Lattner9f3648b2003-05-15 21:30:00 +00001482 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1483 SwitchInst *S = new SwitchInst(getVal($2, $3),
1484 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1485 $$ = S;
1486 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001487 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1488 EXCEPT ResolvedVal {
Chris Lattner19613292002-10-15 21:41:14 +00001489 const PointerType *PFTy;
Chris Lattner57698e22002-03-26 18:01:55 +00001490 const FunctionType *Ty;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001491
Chris Lattner19613292002-10-15 21:41:14 +00001492 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1493 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001494 // Pull out the types of all of the arguments...
Chris Lattner89da8a32003-04-22 18:42:41 +00001495 std::vector<const Type*> ParamTypes;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001496 if ($5) {
Chris Lattner89da8a32003-04-22 18:42:41 +00001497 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1498 I != E; ++I)
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001499 ParamTypes.push_back((*I)->getType());
1500 }
1501
1502 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1503 if (isVarArg) ParamTypes.pop_back();
1504
Chris Lattner57698e22002-03-26 18:01:55 +00001505 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner19613292002-10-15 21:41:14 +00001506 PFTy = PointerType::get(Ty);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001507 }
1508 delete $2;
1509
Chris Lattner19613292002-10-15 21:41:14 +00001510 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001511
1512 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1513 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1514
1515 if (Normal == 0 || Except == 0)
1516 ThrowException("Invoke instruction without label destinations!");
1517
1518 // Create the call node...
1519 if (!$5) { // Has no arguments?
Chris Lattner89da8a32003-04-22 18:42:41 +00001520 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001521 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00001522 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001523 // correctly!
1524 //
Chris Lattner57698e22002-03-26 18:01:55 +00001525 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1526 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner89da8a32003-04-22 18:42:41 +00001527 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001528
1529 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1530 if ((*ArgI)->getType() != *I)
1531 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001532 (*I)->getDescription() + "'!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001533
1534 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1535 ThrowException("Invalid number of parameters detected!");
1536
Chris Lattner476148f2001-11-26 16:54:11 +00001537 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001538 }
1539 delete $5;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001540 };
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001541
1542
Chris Lattner2f7c9632001-06-06 20:29:01 +00001543
1544JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1545 $$ = $1;
Chris Lattner3462ae32001-12-03 22:26:30 +00001546 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001547 if (V == 0)
1548 ThrowException("May only switch on a constant pool value!");
1549
Chris Lattner89da8a32003-04-22 18:42:41 +00001550 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001551 }
1552 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner89da8a32003-04-22 18:42:41 +00001553 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattner3462ae32001-12-03 22:26:30 +00001554 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001555
1556 if (V == 0)
1557 ThrowException("May only switch on a constant pool value!");
1558
Chris Lattner89da8a32003-04-22 18:42:41 +00001559 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001560 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001561
1562Inst : OptAssign InstVal {
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001563 // Is this definition named?? if so, assign the name...
1564 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001565 InsertValue($2);
1566 $$ = $2;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001567};
Chris Lattner2f7c9632001-06-06 20:29:01 +00001568
Chris Lattner931ef3b2001-06-11 15:04:20 +00001569PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner89da8a32003-04-22 18:42:41 +00001570 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1571 $$->push_back(std::make_pair(getVal(*$1, $3),
1572 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattneraa534bf2001-09-07 16:35:17 +00001573 delete $1;
Chris Lattner931ef3b2001-06-11 15:04:20 +00001574 }
1575 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1576 $$ = $1;
Chris Lattner89da8a32003-04-22 18:42:41 +00001577 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1578 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001579 };
Chris Lattner931ef3b2001-06-11 15:04:20 +00001580
1581
Chris Lattneraa534bf2001-09-07 16:35:17 +00001582ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner89da8a32003-04-22 18:42:41 +00001583 $$ = new std::vector<Value*>();
Chris Lattner252afba2001-07-26 16:29:15 +00001584 $$->push_back($1);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001585 }
Chris Lattner252afba2001-07-26 16:29:15 +00001586 | ValueRefList ',' ResolvedVal {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001587 $$ = $1;
Chris Lattner252afba2001-07-26 16:29:15 +00001588 $1->push_back($3);
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001589 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001590
1591// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001592ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001593
Chris Lattnerc0686fe2002-09-10 19:57:26 +00001594InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1595 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1596 ThrowException("Arithmetic operator requires integer or FP operands!");
1597 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1598 if ($$ == 0)
1599 ThrowException("binary operator returned null!");
1600 delete $2;
1601 }
1602 | LogicalOps Types ValueRef ',' ValueRef {
1603 if (!(*$2)->isIntegral())
1604 ThrowException("Logical operator requires integral operands!");
1605 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1606 if ($$ == 0)
1607 ThrowException("binary operator returned null!");
1608 delete $2;
1609 }
1610 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattnera6834c12002-09-10 22:37:46 +00001611 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001612 if ($$ == 0)
1613 ThrowException("binary operator returned null!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001614 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001615 }
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001616 | NOT ResolvedVal {
1617 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1618 << " Replacing with 'xor'.\n";
1619
1620 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1621 if (Ones == 0)
1622 ThrowException("Expected integral type for not instruction!");
1623
1624 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001625 if ($$ == 0)
Chris Lattnerdb3b2022002-08-14 17:12:33 +00001626 ThrowException("Could not create a xor instruction!");
Chris Lattnera6821822001-07-08 04:57:15 +00001627 }
Chris Lattner252afba2001-07-26 16:29:15 +00001628 | ShiftOps ResolvedVal ',' ResolvedVal {
1629 if ($4->getType() != Type::UByteTy)
1630 ThrowException("Shift amount must be ubyte!");
1631 $$ = new ShiftInst($1, $2, $4);
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001632 }
Chris Lattner252afba2001-07-26 16:29:15 +00001633 | CAST ResolvedVal TO Types {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001634 $$ = new CastInst($2, *$4);
1635 delete $4;
Chris Lattnera6821822001-07-08 04:57:15 +00001636 }
Chris Lattnerf70da102003-05-08 02:44:12 +00001637 | VA_ARG ResolvedVal ',' Types {
1638 $$ = new VarArgInst($2, *$4);
1639 delete $4;
1640 }
Chris Lattner931ef3b2001-06-11 15:04:20 +00001641 | PHI PHIList {
1642 const Type *Ty = $2->front().first->getType();
1643 $$ = new PHINode(Ty);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001644 while ($2->begin() != $2->end()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +00001645 if ($2->front().first->getType() != Ty)
1646 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerda558102001-10-02 03:41:24 +00001647 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001648 $2->pop_front();
1649 }
1650 delete $2; // Free the list...
1651 }
Chris Lattner26e50dc2001-07-28 17:48:55 +00001652 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattner19613292002-10-15 21:41:14 +00001653 const PointerType *PFTy;
Chris Lattner57698e22002-03-26 18:01:55 +00001654 const FunctionType *Ty;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001655
Chris Lattner19613292002-10-15 21:41:14 +00001656 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1657 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001658 // Pull out the types of all of the arguments...
Chris Lattner89da8a32003-04-22 18:42:41 +00001659 std::vector<const Type*> ParamTypes;
Chris Lattner7fac0702001-10-03 14:53:21 +00001660 if ($5) {
Chris Lattner89da8a32003-04-22 18:42:41 +00001661 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1662 I != E; ++I)
Chris Lattner7fac0702001-10-03 14:53:21 +00001663 ParamTypes.push_back((*I)->getType());
1664 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001665
1666 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1667 if (isVarArg) ParamTypes.pop_back();
1668
Chris Lattner57698e22002-03-26 18:01:55 +00001669 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner19613292002-10-15 21:41:14 +00001670 PFTy = PointerType::get(Ty);
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001671 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001672 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001673
Chris Lattner19613292002-10-15 21:41:14 +00001674 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2f7c9632001-06-06 20:29:01 +00001675
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001676 // Create the call node...
1677 if (!$5) { // Has no arguments?
Chris Lattner91e08322002-07-25 20:52:56 +00001678 // Make sure no arguments is a good thing!
1679 if (Ty->getNumParams() != 0)
1680 ThrowException("No arguments passed to a function that "
1681 "expects arguments!");
1682
Chris Lattner89da8a32003-04-22 18:42:41 +00001683 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001684 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00001685 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2f7c9632001-06-06 20:29:01 +00001686 // correctly!
1687 //
Chris Lattner57698e22002-03-26 18:01:55 +00001688 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1689 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner89da8a32003-04-22 18:42:41 +00001690 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001691
1692 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1693 if ((*ArgI)->getType() != *I)
1694 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001695 (*I)->getDescription() + "'!");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001696
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001697 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner2f7c9632001-06-06 20:29:01 +00001698 ThrowException("Invalid number of parameters detected!");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001699
Chris Lattner476148f2001-11-26 16:54:11 +00001700 $$ = new CallInst(V, *$5);
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001701 }
1702 delete $5;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001703 }
1704 | MemoryInst {
1705 $$ = $1;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001706 };
Chris Lattner2f7c9632001-06-06 20:29:01 +00001707
Chris Lattner476148f2001-11-26 16:54:11 +00001708
1709// IndexList - List of indices for GEP based instructions...
1710IndexList : ',' ValueRefList {
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001711 $$ = $2;
1712} | /* empty */ {
Chris Lattner89da8a32003-04-22 18:42:41 +00001713 $$ = new std::vector<Value*>();
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001714};
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001715
Chris Lattner2f7c9632001-06-06 20:29:01 +00001716MemoryInst : MALLOC Types {
Chris Lattner322bf4f2002-09-13 22:28:45 +00001717 $$ = new MallocInst(*$2);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001718 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001719 }
1720 | MALLOC Types ',' UINT ValueRef {
Chris Lattner322bf4f2002-09-13 22:28:45 +00001721 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattneraa534bf2001-09-07 16:35:17 +00001722 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001723 }
1724 | ALLOCA Types {
Chris Lattner322bf4f2002-09-13 22:28:45 +00001725 $$ = new AllocaInst(*$2);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001726 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001727 }
1728 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner322bf4f2002-09-13 22:28:45 +00001729 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattneraa534bf2001-09-07 16:35:17 +00001730 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001731 }
Chris Lattner252afba2001-07-26 16:29:15 +00001732 | FREE ResolvedVal {
Chris Lattner181cc322002-05-06 16:15:30 +00001733 if (!isa<PointerType>($2->getType()))
Chris Lattner252afba2001-07-26 16:29:15 +00001734 ThrowException("Trying to free nonpointer type " +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001735 $2->getType()->getDescription() + "!");
Chris Lattner252afba2001-07-26 16:29:15 +00001736 $$ = new FreeInst($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001737 }
1738
Chris Lattner476148f2001-11-26 16:54:11 +00001739 | LOAD Types ValueRef IndexList {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001740 if (!isa<PointerType>($2->get()))
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001741 ThrowException("Can't load from nonpointer type: " +
1742 (*$2)->getDescription());
Chris Lattner030effa2002-08-22 22:48:55 +00001743 if (GetElementPtrInst::getIndexedType(*$2, *$4) == 0)
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001744 ThrowException("Invalid indices for load instruction!");
1745
Chris Lattner319c47a2002-08-21 23:51:21 +00001746 Value *Src = getVal(*$2, $3);
1747 if (!$4->empty()) {
1748 std::cerr << "WARNING: Use of index load instruction:"
1749 << " replacing with getelementptr/load pair.\n";
1750 // Create a getelementptr hack instruction to do the right thing for
1751 // compatibility.
1752 //
1753 Instruction *I = new GetElementPtrInst(Src, *$4);
1754 CurBB->getInstList().push_back(I);
1755 Src = I;
1756 }
1757
1758 $$ = new LoadInst(Src);
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001759 delete $4; // Free the vector...
Chris Lattneraa534bf2001-09-07 16:35:17 +00001760 delete $2;
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001761 }
Chris Lattner476148f2001-11-26 16:54:11 +00001762 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001763 if (!isa<PointerType>($4->get()))
Chris Lattneref99d3c2001-12-14 16:28:42 +00001764 ThrowException("Can't store to a nonpointer type: " +
1765 (*$4)->getDescription());
Chris Lattner030effa2002-08-22 22:48:55 +00001766 const Type *ElTy = GetElementPtrInst::getIndexedType(*$4, *$6);
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001767 if (ElTy == 0)
1768 ThrowException("Can't store into that field list!");
Chris Lattner252afba2001-07-26 16:29:15 +00001769 if (ElTy != $2->getType())
Chris Lattneref99d3c2001-12-14 16:28:42 +00001770 ThrowException("Can't store '" + $2->getType()->getDescription() +
1771 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner319c47a2002-08-21 23:51:21 +00001772
1773 Value *Ptr = getVal(*$4, $5);
1774 if (!$6->empty()) {
1775 std::cerr << "WARNING: Use of index store instruction:"
1776 << " replacing with getelementptr/store pair.\n";
1777 // Create a getelementptr hack instruction to do the right thing for
1778 // compatibility.
1779 //
1780 Instruction *I = new GetElementPtrInst(Ptr, *$6);
1781 CurBB->getInstList().push_back(I);
1782 Ptr = I;
1783 }
1784
1785 $$ = new StoreInst($2, Ptr);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001786 delete $4; delete $6;
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001787 }
Chris Lattner476148f2001-11-26 16:54:11 +00001788 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattnerbb4fe2c2002-09-11 01:17:27 +00001789 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
1790 if ((*$4)[i]->getType() == Type::UIntTy) {
1791 std::cerr << "WARNING: Use of uint type indexes to getelementptr "
1792 << "instruction: replacing with casts to long type.\n";
1793 Instruction *I = new CastInst((*$4)[i], Type::LongTy);
1794 CurBB->getInstList().push_back(I);
1795 (*$4)[i] = I;
1796 }
1797 }
1798
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001799 if (!isa<PointerType>($2->get()))
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001800 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001801 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattneref99d3c2001-12-14 16:28:42 +00001802 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001803 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1804 delete $2; delete $4;
Chris Lattner8cb1dfb2002-06-04 21:58:56 +00001805 };
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001806
Chris Lattner2f7c9632001-06-06 20:29:01 +00001807%%
Chris Lattnera6821822001-07-08 04:57:15 +00001808int yyerror(const char *ErrorMsg) {
Chris Lattner89da8a32003-04-22 18:42:41 +00001809 std::string where
1810 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001811 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner89da8a32003-04-22 18:42:41 +00001812 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001813 if (yychar == YYEMPTY)
1814 errMsg += "end-of-file.";
1815 else
Chris Lattner89da8a32003-04-22 18:42:41 +00001816 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adve7064eaf2002-07-14 22:59:28 +00001817 ThrowException(errMsg);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001818 return 0;
1819}