blob: d865742c85b6176e346bde3ce906165a5a072150 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
Chris Lattner00950542001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000011#include "llvm/GlobalVariable.h"
Chris Lattner00950542001-06-06 20:29:01 +000012#include "llvm/iTerminators.h"
13#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
Chris Lattner46748042002-04-09 19:41:42 +000015#include "llvm/Argument.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000016#include "Support/STLExtras.h"
17#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include <list>
19#include <utility> // Get definition of pair class
Chris Lattner30c89792001-09-07 16:35:17 +000020#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000021#include <iostream>
22using std::list;
23using std::vector;
24using std::pair;
25using std::map;
26using std::pair;
27using std::make_pair;
28using std::cerr;
29using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000030
Chris Lattner386a3b72001-10-16 19:54:17 +000031int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000032int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000033int yyparse();
34
35static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000036string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
43#define UR_OUT(X) cerr << X
44#else
45#define UR_OUT(X)
46#endif
47
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000048#define YYERROR_VERBOSE 1
49
Chris Lattner7e708292002-06-25 16:13:24 +000050// This contains info used when building the body of a function. It is
51// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000052//
53typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000054static void ResolveDefinitions(vector<ValueList> &LateResolvers,
55 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000056
57static struct PerModuleInfo {
58 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000059 vector<ValueList> Values; // Module level numbered definitions
60 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +000061 vector<PATypeHolder> Types;
62 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000063
Chris Lattner2079fde2001-10-13 06:41:08 +000064 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
65 // references to global values. Global values may be referenced before they
66 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000067 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000068 //
69 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
70 GlobalRefsType GlobalRefs;
71
Chris Lattner00950542001-06-06 20:29:01 +000072 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000073 // If we could not resolve some functions at function compilation time
74 // (calls to functions before they are defined), resolve them now... Types
75 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000076 //
Chris Lattner00950542001-06-06 20:29:01 +000077 ResolveDefinitions(LateResolveValues);
78
Chris Lattner2079fde2001-10-13 06:41:08 +000079 // Check to make sure that all global value forward references have been
80 // resolved!
81 //
82 if (!GlobalRefs.empty()) {
Chris Lattner749ce032002-03-11 22:12:39 +000083 string UndefinedReferences = "Unresolved global references exist:\n";
84
85 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
86 I != E; ++I) {
87 UndefinedReferences += " " + I->first.first->getDescription() + " " +
88 I->first.second.getName() + "\n";
89 }
90 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000091 }
92
Chris Lattner7e708292002-06-25 16:13:24 +000093 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000094 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000095 CurrentModule = 0;
96 }
Chris Lattner2079fde2001-10-13 06:41:08 +000097
98
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000099 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000100 // is used to remove things from the forward declaration map, resolving them
101 // to the correct thing as needed.
102 //
103 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
104 // Check to see if there is a forward reference to this global variable...
105 // if there is, eliminate it and patch the reference to use the new def'n.
106 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
107
108 if (I != GlobalRefs.end()) {
109 GlobalVariable *OldGV = I->second; // Get the placeholder...
110 I->first.second.destroy(); // Free string memory if neccesary
111
112 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000113 // allowed to be is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000114 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
115 while (!OldGV->use_empty()) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000116 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
117 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
118 assert(CPR->getValue() == OldGV && "Something isn't happy");
119
120 // Change the const pool reference to point to the real global variable
121 // now. This should drop a use from the OldGV.
122 CPR->mutateReferences(OldGV, GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000123 }
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000124
125 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000126 CurrentModule->getGlobalList().remove(OldGV);
127 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000128
Chris Lattner2079fde2001-10-13 06:41:08 +0000129 // Remove the map entry for the global now that it has been created...
130 GlobalRefs.erase(I);
131 }
132 }
133
Chris Lattner00950542001-06-06 20:29:01 +0000134} CurModule;
135
Chris Lattner79df7c02002-03-26 18:01:55 +0000136static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000137 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000138
Chris Lattnere1815642001-07-15 06:35:53 +0000139 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000140 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000141 vector<PATypeHolder> Types;
142 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000143 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000144
Chris Lattner79df7c02002-03-26 18:01:55 +0000145 inline PerFunctionInfo() {
146 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000147 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000148 }
149
Chris Lattner79df7c02002-03-26 18:01:55 +0000150 inline ~PerFunctionInfo() {}
Chris Lattner00950542001-06-06 20:29:01 +0000151
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 inline void FunctionStart(Function *M) {
153 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000154 }
155
Chris Lattner79df7c02002-03-26 18:01:55 +0000156 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000157 // If we could not resolve some blocks at parsing time (forward branches)
158 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000159 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000160
Chris Lattner7e708292002-06-25 16:13:24 +0000161 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000162 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000163 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000164 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000165 }
Chris Lattner7e708292002-06-25 16:13:24 +0000166} CurMeth; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000167
Chris Lattner79df7c02002-03-26 18:01:55 +0000168static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000169
Chris Lattner00950542001-06-06 20:29:01 +0000170
171//===----------------------------------------------------------------------===//
172// Code to handle definitions of all the types
173//===----------------------------------------------------------------------===//
174
Chris Lattner2079fde2001-10-13 06:41:08 +0000175static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
176 if (D->hasName()) return -1; // Is this a numbered definition?
177
178 // Yes, insert the value into the value table...
179 unsigned type = D->getType()->getUniqueID();
180 if (ValueTab.size() <= type)
181 ValueTab.resize(type+1, ValueList());
182 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
183 ValueTab[type].push_back(D);
184 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000185}
186
Chris Lattner30c89792001-09-07 16:35:17 +0000187// TODO: FIXME when Type are not const
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000188static void InsertType(const Type *Ty, vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000189 Types.push_back(Ty);
190}
191
192static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000193 switch (D.Type) {
194 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000195 unsigned Num = (unsigned)D.Num;
196
197 // Module constants occupy the lowest numbered slots...
198 if (Num < CurModule.Types.size())
199 return CurModule.Types[Num];
200
201 Num -= CurModule.Types.size();
202
203 // Check that the number is within bounds...
204 if (Num <= CurMeth.Types.size())
205 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000206 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000207 }
208 case 1: { // Is it a named definition?
209 string Name(D.Name);
210 SymbolTable *SymTab = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000211 if (inFunctionScope()) SymTab = CurMeth.CurrentFunction->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000212 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
213
214 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000215 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000216 // hasn't been added to the module...
217 //
218 SymTab = CurModule.CurrentModule->getSymbolTable();
219 if (SymTab)
220 N = SymTab->lookup(Type::TypeTy, Name);
221 if (N == 0) break;
222 }
223
224 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000225 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000226 }
227 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000228 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000229 }
230
231 // If we reached here, we referenced either a symbol that we don't know about
232 // or an id number that hasn't been read yet. We may be referencing something
233 // forward, so just create an entry to be resolved later and get to it...
234 //
235 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
236
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000237 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000238 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
239
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000240 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000241 if (I != LateResolver.end()) {
242 return I->second;
243 }
Chris Lattner30c89792001-09-07 16:35:17 +0000244
Chris Lattner82269592001-10-22 06:01:08 +0000245 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000246 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000247 return Typ;
248}
249
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000250static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
251 SymbolTable *SymTab =
Chris Lattner9705a152002-05-02 19:27:42 +0000252 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
253 CurModule.CurrentModule->getSymbolTable();
Chris Lattner924025e2002-04-29 18:25:33 +0000254 return SymTab ? SymTab->lookup(Ty, Name) : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000255}
256
Chris Lattner2079fde2001-10-13 06:41:08 +0000257// getValNonImprovising - Look up the value specified by the provided type and
258// the provided ValID. If the value exists and has already been defined, return
259// it. Otherwise return null.
260//
261static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000262 if (isa<FunctionType>(Ty))
263 ThrowException("Functions are not values and "
264 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000265
Chris Lattner30c89792001-09-07 16:35:17 +0000266 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000267 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000268 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000269 unsigned Num = (unsigned)D.Num;
270
271 // Module constants occupy the lowest numbered slots...
272 if (type < CurModule.Values.size()) {
273 if (Num < CurModule.Values[type].size())
274 return CurModule.Values[type][Num];
275
276 Num -= CurModule.Values[type].size();
277 }
278
279 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000280 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000281
282 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000283 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000284
285 return CurMeth.Values[type][Num];
286 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000287
Chris Lattner1a1cb112001-09-30 22:46:54 +0000288 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000289 Value *N = lookupInSymbolTable(Ty, string(D.Name));
290 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000291
292 D.destroy(); // Free old strdup'd memory...
293 return N;
294 }
295
Chris Lattner2079fde2001-10-13 06:41:08 +0000296 // Check to make sure that "Ty" is an integral type, and that our
297 // value will fit into the specified type...
298 case ValID::ConstSIntVal: // Is it a constant pool reference??
299 if (Ty == Type::BoolTy) { // Special handling for boolean data
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000300 return ConstantBool::get(D.ConstPool64 != 0);
Chris Lattner2079fde2001-10-13 06:41:08 +0000301 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000302 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner2079fde2001-10-13 06:41:08 +0000303 ThrowException("Symbolic constant pool value '" +
304 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000305 Ty->getDescription() + "'!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000306 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000307 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000308
309 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000310 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
311 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000312 ThrowException("Integral constant pool reference is invalid!");
313 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000314 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000315 }
316 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000317 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000318 }
319
Chris Lattner2079fde2001-10-13 06:41:08 +0000320 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000321 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000322 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000323 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000324
325 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000326 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000327 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000328 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000329
Chris Lattner30c89792001-09-07 16:35:17 +0000330 default:
331 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000332 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000333 } // End of switch
334
Chris Lattner2079fde2001-10-13 06:41:08 +0000335 assert(0 && "Unhandled case!");
336 return 0;
337}
338
339
340// getVal - This function is identical to getValNonImprovising, except that if a
341// value is not already defined, it "improvises" by creating a placeholder var
342// that looks and acts just like the requested variable. When the value is
343// defined later, all uses of the placeholder variable are replaced with the
344// real thing.
345//
346static Value *getVal(const Type *Ty, const ValID &D) {
347 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
348
349 // See if the value has already been defined...
350 Value *V = getValNonImprovising(Ty, D);
351 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000352
353 // If we reached here, we referenced either a symbol that we don't know about
354 // or an id number that hasn't been read yet. We may be referencing something
355 // forward, so just create an entry to be resolved later and get to it...
356 //
Chris Lattner00950542001-06-06 20:29:01 +0000357 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000358 switch (Ty->getPrimitiveID()) {
359 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000360 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000361 }
362
363 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000364 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000365 InsertValue(d, CurMeth.LateResolveValues);
366 else
367 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000368 return d;
369}
370
371
372//===----------------------------------------------------------------------===//
373// Code to handle forward references in instructions
374//===----------------------------------------------------------------------===//
375//
376// This code handles the late binding needed with statements that reference
377// values not defined yet... for example, a forward branch, or the PHI node for
378// a loop body.
379//
380// This keeps a table (CurMeth.LateResolveValues) of all such forward references
381// and back patchs after we are done.
382//
383
384// ResolveDefinitions - If we could not resolve some defs at parsing
385// time (forward branches, phi functions for loops, etc...) resolve the
386// defs now...
387//
Chris Lattner386a3b72001-10-16 19:54:17 +0000388static void ResolveDefinitions(vector<ValueList> &LateResolvers,
389 vector<ValueList> *FutureLateResolvers = 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000390 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
391 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
392 while (!LateResolvers[ty].empty()) {
393 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000394 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
395
Chris Lattner00950542001-06-06 20:29:01 +0000396 LateResolvers[ty].pop_back();
397 ValID &DID = getValIDFromPlaceHolder(V);
398
Chris Lattner2079fde2001-10-13 06:41:08 +0000399 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000400 if (TheRealValue) {
401 V->replaceAllUsesWith(TheRealValue);
402 delete V;
403 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000404 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000405 // resolver table
406 InsertValue(V, *FutureLateResolvers);
407 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000408 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000409 ThrowException("Reference to an invalid definition: '" +DID.getName()+
410 "' of type '" + V->getType()->getDescription() + "'",
411 getLineNumFromPlaceHolder(V));
412 else
413 ThrowException("Reference to an invalid definition: #" +
414 itostr(DID.Num) + " of type '" +
415 V->getType()->getDescription() + "'",
416 getLineNumFromPlaceHolder(V));
417 }
Chris Lattner00950542001-06-06 20:29:01 +0000418 }
419 }
420
421 LateResolvers.clear();
422}
423
Chris Lattner4a42e902001-10-22 05:56:09 +0000424// ResolveTypeTo - A brand new type was just declared. This means that (if
425// name is not null) things referencing Name can be resolved. Otherwise, things
426// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000427//
Chris Lattner4a42e902001-10-22 05:56:09 +0000428static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000429 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000430 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000431
Chris Lattner4a42e902001-10-22 05:56:09 +0000432 ValID D;
433 if (Name) D = ValID::create(Name);
434 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000435
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000436 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000437 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
438
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000439 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000440 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000441 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000442 LateResolver.erase(I);
443 }
444}
445
446// ResolveTypes - At this point, all types should be resolved. Any that aren't
447// are errors.
448//
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000449static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000450 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000451 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000452
453 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000454 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000455 else
Chris Lattner82269592001-10-22 06:01:08 +0000456 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000457 }
458}
459
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000460
Chris Lattner1781aca2001-09-18 04:00:54 +0000461// setValueName - Set the specified value to the name given. The name may be
462// null potentially, in which case this is a noop. The string passed in is
463// assumed to be a malloc'd string buffer, and is freed by this function.
464//
Chris Lattnerb7474512001-10-03 15:39:04 +0000465// This function returns true if the value has already been defined, but is
466// allowed to be redefined in the specified context. If the name is a new name
467// for the typeplane, false is returned.
468//
469static bool setValueName(Value *V, char *NameStr) {
470 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000471
Chris Lattner1781aca2001-09-18 04:00:54 +0000472 string Name(NameStr); // Copy string
473 free(NameStr); // Free old string
474
Chris Lattner2079fde2001-10-13 06:41:08 +0000475 if (V->getType() == Type::VoidTy)
476 ThrowException("Can't assign name '" + Name +
477 "' to a null valued instruction!");
478
Chris Lattner79df7c02002-03-26 18:01:55 +0000479 SymbolTable *ST = inFunctionScope() ?
480 CurMeth.CurrentFunction->getSymbolTableSure() :
Chris Lattner30c89792001-09-07 16:35:17 +0000481 CurModule.CurrentModule->getSymbolTableSure();
482
483 Value *Existing = ST->lookup(V->getType(), Name);
484 if (Existing) { // Inserting a name that is already defined???
485 // There is only one case where this is allowed: when we are refining an
486 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000487 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000488 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000489 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000490 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000491 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000492 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000493 }
Chris Lattner30c89792001-09-07 16:35:17 +0000494
Chris Lattner9636a912001-10-01 16:18:37 +0000495 // Otherwise, we are a simple redefinition of a value, check to see if it
496 // is defined the same as the old one...
497 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000498 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
499 // cerr << "Type: " << Ty->getDescription() << " != "
500 // << cast<const Type>(V)->getDescription() << "!\n";
501 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000502 // We are allowed to redefine a global variable in two circumstances:
503 // 1. If at least one of the globals is uninitialized or
504 // 2. If both initializers have the same value.
505 //
506 // This can only be done if the const'ness of the vars is the same.
507 //
Chris Lattner89219832001-10-03 19:35:04 +0000508 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
509 if (EGV->isConstant() == GV->isConstant() &&
510 (!EGV->hasInitializer() || !GV->hasInitializer() ||
511 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000512
Chris Lattner89219832001-10-03 19:35:04 +0000513 // Make sure the existing global version gets the initializer!
514 if (GV->hasInitializer() && !EGV->hasInitializer())
515 EGV->setInitializer(GV->getInitializer());
516
Chris Lattner2079fde2001-10-13 06:41:08 +0000517 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000518 return true; // They are equivalent!
519 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000520 }
Chris Lattner9636a912001-10-01 16:18:37 +0000521 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000522 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000523 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000524 }
Chris Lattner00950542001-06-06 20:29:01 +0000525
Chris Lattner30c89792001-09-07 16:35:17 +0000526 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000527 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000528}
529
Chris Lattner8896eda2001-07-09 19:38:36 +0000530
Chris Lattner30c89792001-09-07 16:35:17 +0000531//===----------------------------------------------------------------------===//
532// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000533//
Chris Lattner8896eda2001-07-09 19:38:36 +0000534
Chris Lattner30c89792001-09-07 16:35:17 +0000535// TypeContains - Returns true if Ty contains E in it.
536//
537static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000538 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000539}
Chris Lattner698b56e2001-07-20 19:15:08 +0000540
Chris Lattner30c89792001-09-07 16:35:17 +0000541
542static vector<pair<unsigned, OpaqueType *> > UpRefs;
543
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000544static PATypeHolder HandleUpRefs(const Type *ty) {
545 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000546 UR_OUT("Type '" << ty->getDescription() <<
547 "' newly formed. Resolving upreferences.\n" <<
548 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000549 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000550 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000551 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000552 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000553 if (TypeContains(Ty, UpRefs[i].second)) {
554 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000555 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000556 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000557 UR_OUT(" * Resolving upreference for "
558 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000559 string OldName = UpRefs[i].second->getDescription());
560 UpRefs[i].second->refineAbstractTypeTo(Ty);
561 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000562 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000563 << (const void*)Ty << ", " << Ty->getDescription() << endl);
564 continue;
565 }
566 }
567
568 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000569 }
Chris Lattner30c89792001-09-07 16:35:17 +0000570 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000571 return Ty;
572}
573
Chris Lattner30c89792001-09-07 16:35:17 +0000574
Chris Lattner00950542001-06-06 20:29:01 +0000575//===----------------------------------------------------------------------===//
576// RunVMAsmParser - Define an interface to this parser
577//===----------------------------------------------------------------------===//
578//
Chris Lattnera2850432001-07-22 18:36:00 +0000579Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000580 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000581 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000582 llvmAsmlineno = 1; // Reset the current line number...
583
584 CurModule.CurrentModule = new Module(); // Allocate a new module to read
585 yyparse(); // Parse the file.
586 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000587 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
588 ParserResult = 0;
589
590 return Result;
591}
592
593%}
594
595%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000596 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000597 Function *FunctionVal;
Chris Lattner46748042002-04-09 19:41:42 +0000598 std::pair<Argument*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000599 BasicBlock *BasicBlockVal;
600 TerminatorInst *TermInstVal;
601 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000602 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000603
Chris Lattner30c89792001-09-07 16:35:17 +0000604 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000605 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000606 Value *ValueVal;
607
Chris Lattner46748042002-04-09 19:41:42 +0000608 std::list<std::pair<Argument*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000609 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000610 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000611 std::list<std::pair<Value*,
612 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000613 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000614 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000615
Chris Lattner30c89792001-09-07 16:35:17 +0000616 int64_t SInt64Val;
617 uint64_t UInt64Val;
618 int SIntVal;
619 unsigned UIntVal;
620 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000621 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000622
Chris Lattner30c89792001-09-07 16:35:17 +0000623 char *StrVal; // This memory is strdup'd!
624 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000625
Chris Lattner30c89792001-09-07 16:35:17 +0000626 Instruction::UnaryOps UnaryOpVal;
627 Instruction::BinaryOps BinaryOpVal;
628 Instruction::TermOps TermOpVal;
629 Instruction::MemoryOps MemOpVal;
630 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000631}
632
Chris Lattner79df7c02002-03-26 18:01:55 +0000633%type <ModuleVal> Module FunctionList
634%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000635%type <BasicBlockVal> BasicBlock InstructionList
636%type <TermInstVal> BBTerminatorInst
637%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000638%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000639%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000640%type <ArgList> ArgList ArgListH
641%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000642%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000643%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000644%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000645%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000646%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000647%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000648
Chris Lattner2079fde2001-10-13 06:41:08 +0000649// ValueRef - Unresolved reference to a definition or BB
650%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000651%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000652// Tokens and types for handling constant integer values
653//
654// ESINT64VAL - A negative number within long long range
655%token <SInt64Val> ESINT64VAL
656
657// EUINT64VAL - A positive number within uns. long long range
658%token <UInt64Val> EUINT64VAL
659%type <SInt64Val> EINT64VAL
660
661%token <SIntVal> SINTVAL // Signed 32 bit ints...
662%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
663%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000664%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000665
666// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000667%type <TypeVal> Types TypesV UpRTypes UpRTypesV
668%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000669%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
670%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000671
672%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000673%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000674
675
Chris Lattner9b02cc32002-05-03 18:23:48 +0000676%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT UNINIT
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000677%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL OPAQUE
Chris Lattner00950542001-06-06 20:29:01 +0000678
679// Basic Block Terminating Operators
680%token <TermOpVal> RET BR SWITCH
681
682// Unary Operators
683%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000684%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000685
686// Binary Operators
687%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000688%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000689%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000690
691// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000692%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000693
Chris Lattner027dcc52001-07-08 21:10:27 +0000694// Other Operators
695%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000696%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000697
Chris Lattner00950542001-06-06 20:29:01 +0000698%start Module
699%%
700
701// Handle constant integer size restriction and conversion...
702//
703
Chris Lattner51727be2002-06-04 21:58:56 +0000704INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000705INTVAL : UINTVAL {
706 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
707 ThrowException("Value too large for type!");
708 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000709};
Chris Lattner00950542001-06-06 20:29:01 +0000710
711
Chris Lattner51727be2002-06-04 21:58:56 +0000712EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000713EINT64VAL : EUINT64VAL {
714 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
715 ThrowException("Value too large for type!");
716 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000717};
Chris Lattner00950542001-06-06 20:29:01 +0000718
Chris Lattner00950542001-06-06 20:29:01 +0000719// Operations that are notably excluded from this list include:
720// RET, BR, & SWITCH because they end basic blocks and are treated specially.
721//
Chris Lattner51727be2002-06-04 21:58:56 +0000722UnaryOps : NOT;
723BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR;
724BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
725ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000726
Chris Lattnere98dda62001-07-14 06:10:16 +0000727// These are some types that allow classification if we only want a particular
728// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000729SIntType : LONG | INT | SHORT | SBYTE;
730UIntType : ULONG | UINT | USHORT | UBYTE;
731IntType : SIntType | UIntType;
732FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000733
Chris Lattnere98dda62001-07-14 06:10:16 +0000734// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000735OptAssign : VAR_ID '=' {
736 $$ = $1;
737 }
738 | /*empty*/ {
739 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000740 };
Chris Lattner00950542001-06-06 20:29:01 +0000741
Chris Lattner51727be2002-06-04 21:58:56 +0000742OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
Chris Lattner30c89792001-09-07 16:35:17 +0000743
744//===----------------------------------------------------------------------===//
745// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000746// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000747// access to it, a user must explicitly use TypesV.
748//
749
750// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000751TypesV : Types | VOID { $$ = new PATypeHolder($1); };
752UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000753
754Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000755 if (UpRefs.size())
756 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
757 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000758 };
Chris Lattner30c89792001-09-07 16:35:17 +0000759
760
761// Derived types are added later...
762//
Chris Lattner51727be2002-06-04 21:58:56 +0000763PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
764PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000765UpRTypes : OPAQUE {
766 $$ = new PATypeHolder(OpaqueType::get());
767 }
768 | PrimType {
769 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000770 };
Chris Lattner30c89792001-09-07 16:35:17 +0000771UpRTypes : ValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000772 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000773};
Chris Lattner30c89792001-09-07 16:35:17 +0000774
Chris Lattner30c89792001-09-07 16:35:17 +0000775// Include derived types in the Types production.
776//
777UpRTypes : '\\' EUINT64VAL { // Type UpReference
778 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
779 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
780 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000781 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000782 UR_OUT("New Upreference!\n");
783 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000784 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000785 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000786 mapto($3->begin(), $3->end(), std::back_inserter(Params),
787 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000788 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
789 if (isVarArg) Params.pop_back();
790
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000791 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000792 delete $3; // Delete the argument list
793 delete $1; // Delete the old type handle
794 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000795 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000796 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000797 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000798 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000799 | '{' TypeListI '}' { // Structure type?
800 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000801 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
802 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000803
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000804 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000805 delete $2;
806 }
807 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000808 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000809 }
810 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000811 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000812 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000813 };
Chris Lattner30c89792001-09-07 16:35:17 +0000814
Chris Lattner7e708292002-06-25 16:13:24 +0000815// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000816// declaration type lists
817//
818TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000819 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000820 $$->push_back(*$1); delete $1;
821 }
822 | TypeListI ',' UpRTypes {
823 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000824 };
Chris Lattner30c89792001-09-07 16:35:17 +0000825
Chris Lattner7e708292002-06-25 16:13:24 +0000826// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000827ArgTypeListI : TypeListI
828 | TypeListI ',' DOTDOTDOT {
829 ($$=$1)->push_back(Type::VoidTy);
830 }
831 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000832 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000833 }
834 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000835 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000836 };
Chris Lattner30c89792001-09-07 16:35:17 +0000837
Chris Lattnere98dda62001-07-14 06:10:16 +0000838// ConstVal - The various declarations that go into the constant pool. This
839// includes all forward declarations of types, constants, and functions.
840//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000841ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
842 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
843 if (ATy == 0)
844 ThrowException("Cannot make array constant with type: '" +
845 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000846 const Type *ETy = ATy->getElementType();
847 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000848
Chris Lattner30c89792001-09-07 16:35:17 +0000849 // Verify that we have the correct size...
850 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000851 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000852 utostr($3->size()) + " arguments, but has size of " +
853 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000854
Chris Lattner30c89792001-09-07 16:35:17 +0000855 // Verify all elements are correct type!
856 for (unsigned i = 0; i < $3->size(); i++) {
857 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000858 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000859 ETy->getDescription() +"' as required!\nIt is of type '"+
860 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000861 }
862
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000863 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000864 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000865 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000866 | Types '[' ']' {
867 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
868 if (ATy == 0)
869 ThrowException("Cannot make array constant with type: '" +
870 (*$1)->getDescription() + "'!");
871
872 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000873 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000874 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000875 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000876 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000877 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000878 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000879 | Types 'c' STRINGCONSTANT {
880 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
881 if (ATy == 0)
882 ThrowException("Cannot make array constant with type: '" +
883 (*$1)->getDescription() + "'!");
884
Chris Lattner30c89792001-09-07 16:35:17 +0000885 int NumElements = ATy->getNumElements();
886 const Type *ETy = ATy->getElementType();
887 char *EndStr = UnEscapeLexed($3, true);
888 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000889 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000890 itostr((int)(EndStr-$3)) +
891 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000892 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000893 if (ETy == Type::SByteTy) {
894 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000895 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000896 } else if (ETy == Type::UByteTy) {
897 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000898 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000899 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000900 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000901 ThrowException("Cannot build string arrays of non byte sized elements!");
902 }
Chris Lattner30c89792001-09-07 16:35:17 +0000903 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000904 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000905 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000906 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000907 | Types '{' ConstVector '}' {
908 const StructType *STy = dyn_cast<const StructType>($1->get());
909 if (STy == 0)
910 ThrowException("Cannot make struct constant with type: '" +
911 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000912 // FIXME: TODO: Check to see that the constants are compatible with the type
913 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000914 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000915 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000916 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000917 | Types NULL_TOK {
918 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
919 if (PTy == 0)
920 ThrowException("Cannot make null pointer constant with type: '" +
921 (*$1)->getDescription() + "'!");
922
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000923 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000924 delete $1;
925 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000926 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000927 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
928 if (Ty == 0)
929 ThrowException("Global const reference must be a pointer type!");
930
Chris Lattner2079fde2001-10-13 06:41:08 +0000931 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000932
Chris Lattner2079fde2001-10-13 06:41:08 +0000933 // If this is an initializer for a constant pointer, which is referencing a
934 // (currently) undefined variable, create a stub now that shall be replaced
935 // in the future with the right type of variable.
936 //
937 if (V == 0) {
938 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
939 const PointerType *PT = cast<PointerType>(Ty);
940
941 // First check to see if the forward references value is already created!
942 PerModuleInfo::GlobalRefsType::iterator I =
943 CurModule.GlobalRefs.find(make_pair(PT, $2));
944
945 if (I != CurModule.GlobalRefs.end()) {
946 V = I->second; // Placeholder already exists, use it...
947 } else {
948 // TODO: Include line number info by creating a subclass of
949 // TODO: GlobalVariable here that includes the said information!
950
951 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000952 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
953 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000954 // Keep track of the fact that we have a forward ref to recycle it
955 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
956
957 // Must temporarily push this value into the module table...
958 CurModule.CurrentModule->getGlobalList().push_back(GV);
959 V = GV;
960 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000961 }
962
Chris Lattner2079fde2001-10-13 06:41:08 +0000963 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000964 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000965 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000966 }
967 | ConstExpr {
968 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000969 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000970
Chris Lattner00950542001-06-06 20:29:01 +0000971
Chris Lattnercc4b6ec2002-07-18 00:14:27 +0000972// FIXME: ConstExpr::get never return null!
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000973ConstExpr: Types CAST ConstVal {
974 ConstantExpr* CPE = ConstantExpr::get($2, $3, $1->get());
975 if (CPE == 0) ThrowException("constant expression builder returned null!");
976 $$ = CPE;
977 }
978 | Types GETELEMENTPTR '(' ConstVal IndexList ')' {
Chris Lattnercc4b6ec2002-07-18 00:14:27 +0000979 vector<Constant*> IdxVec;
980 for (unsigned i = 0, e = $5->size(); i != e; ++i)
981 if (Constant *C = dyn_cast<Constant>((*$5)[i]))
982 IdxVec.push_back(C);
983 else
984 ThrowException("Arguments to getelementptr must be constants!");
985
986 delete $5;
987
988 ConstantExpr* CPE = ConstantExpr::get($2, $4, IdxVec, $1->get());
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000989 if (CPE == 0) ThrowException("constant expression builder returned null!");
990 $$ = CPE;
991 }
992 | Types UnaryOps ConstVal {
993 ConstantExpr* CPE = ConstantExpr::get($2, $3, $1->get());
994 if (CPE == 0) ThrowException("constant expression builder returned null!");
995 $$ = CPE;
996 }
997 | Types BinaryOps ConstVal ',' ConstVal {
998 ConstantExpr* CPE = ConstantExpr::get($2, $3, $5, $1->get());
999 if (CPE == 0) ThrowException("constant expression builder returned null!");
1000 $$ = CPE;
1001 }
1002 | Types ShiftOps ConstVal ',' ConstVal {
1003 ConstantExpr* CPE = ConstantExpr::get($2, $3, $5, $1->get());
1004 if (CPE == 0) ThrowException("constant expression builder returned null!");
1005 $$ = CPE;
1006 }
1007 ;
1008
1009
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001010ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001011 if (!ConstantSInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001012 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001013 $$ = ConstantSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001014 }
1015 | UIntType EUINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001016 if (!ConstantUInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001017 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001018 $$ = ConstantUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001019 }
1020 | BOOL TRUE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001021 $$ = ConstantBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001022 }
1023 | BOOL FALSE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001024 $$ = ConstantBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001025 }
1026 | FPType FPVAL { // Float & Double constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001027 $$ = ConstantFP::get($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001028 };
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001029
Chris Lattnere98dda62001-07-14 06:10:16 +00001030// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001031ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001032 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001033 }
1034 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001035 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001036 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001037 };
Chris Lattner00950542001-06-06 20:29:01 +00001038
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001039
Chris Lattner1781aca2001-09-18 04:00:54 +00001040// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001041GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001042
Chris Lattner00950542001-06-06 20:29:01 +00001043
Chris Lattner0e73ce62002-05-02 19:11:13 +00001044//===----------------------------------------------------------------------===//
1045// Rules to match Modules
1046//===----------------------------------------------------------------------===//
1047
1048// Module rule: Capture the result of parsing the whole file into a result
1049// variable...
1050//
1051Module : FunctionList {
1052 $$ = ParserResult = $1;
1053 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001054};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001055
Chris Lattner7e708292002-06-25 16:13:24 +00001056// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001057//
1058FunctionList : FunctionList Function {
1059 $$ = $1;
1060 assert($2->getParent() == 0 && "Function already in module!");
1061 $1->getFunctionList().push_back($2);
1062 CurMeth.FunctionDone();
1063 }
1064 | FunctionList FunctionProto {
1065 $$ = $1;
1066 }
1067 | FunctionList IMPLEMENTATION {
1068 $$ = $1;
1069 }
1070 | ConstPool {
1071 $$ = CurModule.CurrentModule;
1072 // Resolve circular types before we parse the body of the module
1073 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001074 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001075
Chris Lattnere98dda62001-07-14 06:10:16 +00001076// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001077ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001078 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001079 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001080 }
Chris Lattner30c89792001-09-07 16:35:17 +00001081 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001082 // Eagerly resolve types. This is not an optimization, this is a
1083 // requirement that is due to the fact that we could have this:
1084 //
1085 // %list = type { %list * }
1086 // %list = type { %list * } ; repeated type decl
1087 //
1088 // If types are not resolved eagerly, then the two types will not be
1089 // determined to be the same type!
1090 //
1091 ResolveTypeTo($2, $4->get());
1092
Chris Lattner1781aca2001-09-18 04:00:54 +00001093 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001094 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1095 // If this is not a redefinition of a type...
1096 if (!$2) {
1097 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001098 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001099 }
Chris Lattner30c89792001-09-07 16:35:17 +00001100 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001101
1102 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001103 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001104 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001105 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001106 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1107 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001108 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001109 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001110 if (Initializer == 0)
1111 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001112
Chris Lattnerdda71962001-11-26 18:54:16 +00001113 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001114 if (!setValueName(GV, $2)) { // If not redefining...
1115 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001116 int Slot = InsertValue(GV, CurModule.Values);
1117
1118 if (Slot != -1) {
1119 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1120 } else {
1121 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1122 (char*)GV->getName().c_str()));
1123 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001124 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001125 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001126 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1127 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001128 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001129 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001130 if (!setValueName(GV, $2)) { // If not redefining...
1131 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001132 int Slot = InsertValue(GV, CurModule.Values);
1133
1134 if (Slot != -1) {
1135 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1136 } else {
1137 assert(GV->hasName() && "Not named and not numbered!?");
1138 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1139 (char*)GV->getName().c_str()));
1140 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001141 }
Chris Lattner09c07532002-03-31 07:16:49 +00001142 delete $6;
Chris Lattnere98dda62001-07-14 06:10:16 +00001143 }
Chris Lattner00950542001-06-06 20:29:01 +00001144 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001145 };
Chris Lattner00950542001-06-06 20:29:01 +00001146
1147
1148//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001149// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001150//===----------------------------------------------------------------------===//
1151
Chris Lattner51727be2002-06-04 21:58:56 +00001152OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001153
1154ArgVal : Types OptVAR_ID {
Chris Lattner46748042002-04-09 19:41:42 +00001155 $$ = new pair<Argument*, char*>(new Argument(*$1), $2);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001156 delete $1; // Delete the type handle..
Chris Lattner51727be2002-06-04 21:58:56 +00001157};
Chris Lattner00950542001-06-06 20:29:01 +00001158
1159ArgListH : ArgVal ',' ArgListH {
1160 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001161 $3->push_front(*$1);
1162 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001163 }
1164 | ArgVal {
Chris Lattner46748042002-04-09 19:41:42 +00001165 $$ = new list<pair<Argument*,char*> >();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001166 $$->push_front(*$1);
1167 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001168 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001169 | DOTDOTDOT {
Chris Lattner46748042002-04-09 19:41:42 +00001170 $$ = new list<pair<Argument*, char*> >();
1171 $$->push_front(pair<Argument*,char*>(new Argument(Type::VoidTy), 0));
Chris Lattner51727be2002-06-04 21:58:56 +00001172 };
Chris Lattner00950542001-06-06 20:29:01 +00001173
1174ArgList : ArgListH {
1175 $$ = $1;
1176 }
1177 | /* empty */ {
1178 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001179 };
Chris Lattner00950542001-06-06 20:29:01 +00001180
Chris Lattner8ebccb72002-05-22 22:33:00 +00001181FuncName : VAR_ID | STRINGCONSTANT;
1182
1183FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
Chris Lattnerdda71962001-11-26 18:54:16 +00001184 UnEscapeLexed($3);
Chris Lattner79df7c02002-03-26 18:01:55 +00001185 string FunctionName($3);
Chris Lattnerdda71962001-11-26 18:54:16 +00001186
Chris Lattner30c89792001-09-07 16:35:17 +00001187 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001188 if ($5)
Chris Lattner46748042002-04-09 19:41:42 +00001189 for (list<pair<Argument*,char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001190 I != $5->end(); ++I)
1191 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001192
Chris Lattner2079fde2001-10-13 06:41:08 +00001193 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1194 if (isVarArg) ParamTypeList.pop_back();
1195
Chris Lattner79df7c02002-03-26 18:01:55 +00001196 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001197 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001198 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001199
Chris Lattner79df7c02002-03-26 18:01:55 +00001200 Function *M = 0;
Chris Lattnere1815642001-07-15 06:35:53 +00001201 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner79df7c02002-03-26 18:01:55 +00001202 // Is the function already in symtab?
1203 if (Value *V = ST->lookup(PMT, FunctionName)) {
1204 M = cast<Function>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001205
Chris Lattnere1815642001-07-15 06:35:53 +00001206 // Yes it is. If this is the case, either we need to be a forward decl,
1207 // or it needs to be.
1208 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner7e708292002-06-25 16:13:24 +00001209 ThrowException("Redefinition of function '" + FunctionName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001210
Chris Lattner5659dd12002-07-15 00:10:33 +00001211 // Make sure that we keep track of the internal marker, even if there was
1212 // a previous "declare".
1213 if ($1)
1214 M->setInternalLinkage(true);
1215
Chris Lattner7e708292002-06-25 16:13:24 +00001216 // If we found a preexisting function prototype, remove it from the
1217 // module, so that we don't get spurious conflicts with global & local
1218 // variables.
Chris Lattner34538142002-03-08 19:11:42 +00001219 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001220 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001221 }
1222 }
1223
1224 if (M == 0) { // Not already defined?
Chris Lattner79df7c02002-03-26 18:01:55 +00001225 M = new Function(MT, $1, FunctionName);
Chris Lattnere1815642001-07-15 06:35:53 +00001226 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001227 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001228 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001229 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001230
Chris Lattner79df7c02002-03-26 18:01:55 +00001231 CurMeth.FunctionStart(M);
Chris Lattner00950542001-06-06 20:29:01 +00001232
Chris Lattner7e708292002-06-25 16:13:24 +00001233 // Add all of the arguments we parsed to the function...
Chris Lattnerdda71962001-11-26 18:54:16 +00001234 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner46748042002-04-09 19:41:42 +00001235 for (list<pair<Argument*, char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001236 I != $5->end(); ++I) {
1237 if (setValueName(I->first, I->second)) { // Insert into symtab...
1238 assert(0 && "No arg redef allowed!");
1239 }
1240
1241 InsertValue(I->first);
Chris Lattner7e708292002-06-25 16:13:24 +00001242 M->getArgumentList().push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001243 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001244 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001245 } else if ($5) {
1246 // If we are a declaration, we should free the memory for the argument list!
Chris Lattner46748042002-04-09 19:41:42 +00001247 for (list<pair<Argument*, char*> >::iterator I = $5->begin(), E = $5->end();
1248 I != E; ++I) {
Chris Lattner9176fe42002-03-08 18:57:56 +00001249 if (I->second) free(I->second); // Free the memory for the name...
Chris Lattner09c07532002-03-31 07:16:49 +00001250 delete I->first; // Free the unused function argument
1251 }
Chris Lattner9176fe42002-03-08 18:57:56 +00001252 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001253 }
Chris Lattner51727be2002-06-04 21:58:56 +00001254};
Chris Lattner00950542001-06-06 20:29:01 +00001255
Chris Lattner9b02cc32002-05-03 18:23:48 +00001256BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1257
1258FunctionHeader : FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001259 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001260
Chris Lattner7e708292002-06-25 16:13:24 +00001261 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001262 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001263};
Chris Lattner00950542001-06-06 20:29:01 +00001264
Chris Lattner9b02cc32002-05-03 18:23:48 +00001265END : ENDTOK | '}'; // Allow end of '}' to end a function
1266
Chris Lattner79df7c02002-03-26 18:01:55 +00001267Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001268 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001269};
Chris Lattner00950542001-06-06 20:29:01 +00001270
Chris Lattner79df7c02002-03-26 18:01:55 +00001271FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1272 $$ = CurMeth.CurrentFunction;
1273 assert($$->getParent() == 0 && "Function already in module!");
1274 CurModule.CurrentModule->getFunctionList().push_back($$);
1275 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001276};
Chris Lattner00950542001-06-06 20:29:01 +00001277
1278//===----------------------------------------------------------------------===//
1279// Rules to match Basic Blocks
1280//===----------------------------------------------------------------------===//
1281
1282ConstValueRef : ESINT64VAL { // A reference to a direct constant
1283 $$ = ValID::create($1);
1284 }
1285 | EUINT64VAL {
1286 $$ = ValID::create($1);
1287 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001288 | FPVAL { // Perhaps it's an FP constant?
1289 $$ = ValID::create($1);
1290 }
Chris Lattner00950542001-06-06 20:29:01 +00001291 | TRUE {
1292 $$ = ValID::create((int64_t)1);
1293 }
1294 | FALSE {
1295 $$ = ValID::create((int64_t)0);
1296 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001297 | NULL_TOK {
1298 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001299 }
1300 ;
Chris Lattner1a1cb112001-09-30 22:46:54 +00001301
Chris Lattner2079fde2001-10-13 06:41:08 +00001302// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1303// another value.
1304//
1305SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001306 $$ = ValID::create($1);
1307 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001308 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001309 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001310 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001311
1312// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001313ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001314
Chris Lattner00950542001-06-06 20:29:01 +00001315
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001316// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1317// type immediately preceeds the value reference, and allows complex constant
1318// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001319ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001320 $$ = getVal(*$1, $2); delete $1;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001321 }
1322 | ConstExpr {
1323 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001324 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001325
Chris Lattner00950542001-06-06 20:29:01 +00001326BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001327 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001328 }
Chris Lattner7e708292002-06-25 16:13:24 +00001329 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1330 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001331 };
Chris Lattner00950542001-06-06 20:29:01 +00001332
1333
1334// Basic blocks are terminated by branching instructions:
1335// br, br/cc, switch, ret
1336//
Chris Lattner2079fde2001-10-13 06:41:08 +00001337BasicBlock : InstructionList OptAssign BBTerminatorInst {
1338 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1339 InsertValue($3);
1340
1341 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001342 InsertValue($1);
1343 $$ = $1;
1344 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001345 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1346 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1347 InsertValue($4);
1348
1349 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001350 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001351
1352 InsertValue($2);
1353 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001354 };
Chris Lattner00950542001-06-06 20:29:01 +00001355
1356InstructionList : InstructionList Inst {
1357 $1->getInstList().push_back($2);
1358 $$ = $1;
1359 }
1360 | /* empty */ {
1361 $$ = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001362 };
Chris Lattner00950542001-06-06 20:29:01 +00001363
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001364BBTerminatorInst : RET ResolvedVal { // Return with a result...
1365 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001366 }
1367 | RET VOID { // Return with no result...
1368 $$ = new ReturnInst();
1369 }
1370 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001371 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001372 } // Conditional Branch...
1373 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001374 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1375 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001376 getVal(Type::BoolTy, $3));
1377 }
1378 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1379 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001380 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001381 $$ = S;
1382
Chris Lattner46748042002-04-09 19:41:42 +00001383 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1384 E = $8->end();
1385 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001386 S->dest_push_back(I->first, I->second);
1387 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001388 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1389 EXCEPT ResolvedVal {
1390 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001391 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001392
1393 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001394 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001395 // Pull out the types of all of the arguments...
1396 vector<const Type*> ParamTypes;
1397 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001398 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001399 ParamTypes.push_back((*I)->getType());
1400 }
1401
1402 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1403 if (isVarArg) ParamTypes.pop_back();
1404
Chris Lattner79df7c02002-03-26 18:01:55 +00001405 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner2079fde2001-10-13 06:41:08 +00001406 PMTy = PointerType::get(Ty);
1407 }
1408 delete $2;
1409
Chris Lattner7e708292002-06-25 16:13:24 +00001410 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001411
1412 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1413 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1414
1415 if (Normal == 0 || Except == 0)
1416 ThrowException("Invoke instruction without label destinations!");
1417
1418 // Create the call node...
1419 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001420 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001421 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001422 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001423 // correctly!
1424 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001425 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1426 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001427 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001428
1429 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1430 if ((*ArgI)->getType() != *I)
1431 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001432 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001433
1434 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1435 ThrowException("Invalid number of parameters detected!");
1436
Chris Lattner6cdb0112001-11-26 16:54:11 +00001437 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001438 }
1439 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001440 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001441
1442
Chris Lattner00950542001-06-06 20:29:01 +00001443
1444JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1445 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001446 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001447 if (V == 0)
1448 ThrowException("May only switch on a constant pool value!");
1449
Chris Lattner9636a912001-10-01 16:18:37 +00001450 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001451 }
1452 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001453 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001454 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001455
1456 if (V == 0)
1457 ThrowException("May only switch on a constant pool value!");
1458
Chris Lattner9636a912001-10-01 16:18:37 +00001459 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001460 };
Chris Lattner00950542001-06-06 20:29:01 +00001461
1462Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001463 // Is this definition named?? if so, assign the name...
1464 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001465 InsertValue($2);
1466 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001467};
Chris Lattner00950542001-06-06 20:29:01 +00001468
Chris Lattnerc24d2082001-06-11 15:04:20 +00001469PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1470 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001471 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001472 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001473 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001474 }
1475 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1476 $$ = $1;
1477 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001478 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001479 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001480
1481
Chris Lattner30c89792001-09-07 16:35:17 +00001482ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001483 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001484 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001485 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001486 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001487 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001488 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001489 };
Chris Lattner00950542001-06-06 20:29:01 +00001490
1491// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001492ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001493
1494InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001495 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001496 if ($$ == 0)
1497 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001498 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001499 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001500 | UnaryOps ResolvedVal {
1501 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001502 if ($$ == 0)
1503 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001504 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001505 | ShiftOps ResolvedVal ',' ResolvedVal {
1506 if ($4->getType() != Type::UByteTy)
1507 ThrowException("Shift amount must be ubyte!");
1508 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001509 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001510 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001511 $$ = new CastInst($2, *$4);
1512 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001513 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001514 | PHI PHIList {
1515 const Type *Ty = $2->front().first->getType();
1516 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001517 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001518 if ($2->front().first->getType() != Ty)
1519 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001520 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001521 $2->pop_front();
1522 }
1523 delete $2; // Free the list...
1524 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001525 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001526 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001527 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001528
Chris Lattneref9c23f2001-10-03 14:53:21 +00001529 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001530 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001531 // Pull out the types of all of the arguments...
1532 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001533 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001534 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001535 ParamTypes.push_back((*I)->getType());
1536 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001537
1538 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1539 if (isVarArg) ParamTypes.pop_back();
1540
Chris Lattner79df7c02002-03-26 18:01:55 +00001541 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001542 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001543 }
Chris Lattner30c89792001-09-07 16:35:17 +00001544 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001545
Chris Lattner7e708292002-06-25 16:13:24 +00001546 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001547
Chris Lattner8b81bf52001-07-25 22:47:46 +00001548 // Create the call node...
1549 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001550 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001551 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001552 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001553 // correctly!
1554 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001555 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1556 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001557 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001558
1559 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1560 if ((*ArgI)->getType() != *I)
1561 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001562 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001563
Chris Lattner8b81bf52001-07-25 22:47:46 +00001564 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001565 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001566
Chris Lattner6cdb0112001-11-26 16:54:11 +00001567 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001568 }
1569 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001570 }
1571 | MemoryInst {
1572 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001573 };
Chris Lattner00950542001-06-06 20:29:01 +00001574
Chris Lattner6cdb0112001-11-26 16:54:11 +00001575
1576// IndexList - List of indices for GEP based instructions...
1577IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001578 $$ = $2;
1579} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001580 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001581};
Chris Lattner027dcc52001-07-08 21:10:27 +00001582
Chris Lattner00950542001-06-06 20:29:01 +00001583MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001584 $$ = new MallocInst(PointerType::get(*$2));
1585 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001586 }
1587 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001588 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001589 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001590 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001591 }
1592 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001593 $$ = new AllocaInst(PointerType::get(*$2));
1594 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001595 }
1596 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001597 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001598 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001599 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001600 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001601 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001602 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001603 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001604 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001605 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001606 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001607 }
1608
Chris Lattner6cdb0112001-11-26 16:54:11 +00001609 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001610 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001611 ThrowException("Can't load from nonpointer type: " +
1612 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001613 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001614 ThrowException("Invalid indices for load instruction!");
1615
Chris Lattner30c89792001-09-07 16:35:17 +00001616 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001617 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001618 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001619 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001620 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001621 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001622 ThrowException("Can't store to a nonpointer type: " +
1623 (*$4)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001624 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001625 if (ElTy == 0)
1626 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001627 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001628 ThrowException("Can't store '" + $2->getType()->getDescription() +
1629 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001630 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1631 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001632 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001633 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001634 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001635 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001636 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001637 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001638 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1639 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001640 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001641
Chris Lattner00950542001-06-06 20:29:01 +00001642%%
Chris Lattner09083092001-07-08 04:57:15 +00001643int yyerror(const char *ErrorMsg) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001644 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1645 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1646 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1647 if (yychar == YYEMPTY)
1648 errMsg += "end-of-file.";
1649 else
1650 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1651 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001652 return 0;
1653}