blob: ad5e57e04ae4f66d21b6c2a43dcdc84155e08cb0 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
Chris Lattner2f7c9632001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattnerda975502001-09-10 07:58:01 +000011#include "llvm/GlobalVariable.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000012#include "llvm/iTerminators.h"
13#include "llvm/iMemory.h"
Chris Lattnerfb5ae022001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
Chris Lattnerae234e12002-04-09 19:41:42 +000015#include "llvm/Argument.h"
Chris Lattner5de22042001-11-27 00:03:19 +000016#include "Support/STLExtras.h"
17#include "Support/DepthFirstIterator.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000018#include <list>
19#include <utility> // Get definition of pair class
Chris Lattneraa534bf2001-09-07 16:35:17 +000020#include <algorithm>
Chris Lattner7f74a562002-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 Lattner2f7c9632001-06-06 20:29:01 +000030
Chris Lattner322e49a2001-10-16 19:54:17 +000031int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattnera6821822001-07-08 04:57:15 +000032int yylex(); // declaration" of xxx warnings.
Chris Lattner2f7c9632001-06-06 20:29:01 +000033int yyparse();
34
35static Module *ParserResult;
Chris Lattnerf2d1e792001-07-22 18:36:00 +000036string CurFilename;
Chris Lattner2f7c9632001-06-06 20:29:01 +000037
Chris Lattneraa534bf2001-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
Chris Lattner2f7c9632001-06-06 20:29:01 +000048// This contains info used when building the body of a method. It is destroyed
49// when the method is completed.
50//
51typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner322e49a2001-10-16 19:54:17 +000052static void ResolveDefinitions(vector<ValueList> &LateResolvers,
53 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner2f7c9632001-06-06 20:29:01 +000054
55static struct PerModuleInfo {
56 Module *CurrentModule;
Chris Lattneraa534bf2001-09-07 16:35:17 +000057 vector<ValueList> Values; // Module level numbered definitions
58 vector<ValueList> LateResolveValues;
Chris Lattner30752bd92002-04-04 19:23:55 +000059 vector<PATypeHolder> Types;
60 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner2f7c9632001-06-06 20:29:01 +000061
Chris Lattnerf26e0d92001-10-13 06:41:08 +000062 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
63 // references to global values. Global values may be referenced before they
64 // are defined, and if so, the temporary object that they represent is held
Chris Lattner3462ae32001-12-03 22:26:30 +000065 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattnerf26e0d92001-10-13 06:41:08 +000066 //
67 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
68 GlobalRefsType GlobalRefs;
69
Chris Lattner2f7c9632001-06-06 20:29:01 +000070 void ModuleDone() {
Chris Lattneraa534bf2001-09-07 16:35:17 +000071 // If we could not resolve some methods at method compilation time (calls to
72 // methods before they are defined), resolve them now... Types are resolved
73 // when the constant pool has been completely parsed.
74 //
Chris Lattner2f7c9632001-06-06 20:29:01 +000075 ResolveDefinitions(LateResolveValues);
76
Chris Lattnerf26e0d92001-10-13 06:41:08 +000077 // Check to make sure that all global value forward references have been
78 // resolved!
79 //
80 if (!GlobalRefs.empty()) {
Chris Lattner1a8ea8a2002-03-11 22:12:39 +000081 string UndefinedReferences = "Unresolved global references exist:\n";
82
83 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
84 I != E; ++I) {
85 UndefinedReferences += " " + I->first.first->getDescription() + " " +
86 I->first.second.getName() + "\n";
87 }
88 ThrowException(UndefinedReferences);
Chris Lattnerf26e0d92001-10-13 06:41:08 +000089 }
90
Chris Lattner2f7c9632001-06-06 20:29:01 +000091 Values.clear(); // Clear out method local definitions
Chris Lattneraa534bf2001-09-07 16:35:17 +000092 Types.clear();
Chris Lattner2f7c9632001-06-06 20:29:01 +000093 CurrentModule = 0;
94 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +000095
96
97 // DeclareNewGlobalValue - Called every type a new GV has been defined. This
98 // is used to remove things from the forward declaration map, resolving them
99 // to the correct thing as needed.
100 //
101 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
102 // Check to see if there is a forward reference to this global variable...
103 // if there is, eliminate it and patch the reference to use the new def'n.
104 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
105
106 if (I != GlobalRefs.end()) {
107 GlobalVariable *OldGV = I->second; // Get the placeholder...
108 I->first.second.destroy(); // Free string memory if neccesary
109
110 // Loop over all of the uses of the GlobalValue. The only thing they are
Chris Lattner3462ae32001-12-03 22:26:30 +0000111 // allowed to be at this point is ConstantPointerRef's.
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000112 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
113 while (!OldGV->use_empty()) {
Chris Lattner3462ae32001-12-03 22:26:30 +0000114 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
115 ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000116 assert(CPPR->getValue() == OldGV && "Something isn't happy");
117
118 // Change the const pool reference to point to the real global variable
119 // now. This should drop a use from the OldGV.
120 CPPR->mutateReference(GV);
121 }
122
123 // Remove GV from the module...
124 CurrentModule->getGlobalList().remove(OldGV);
125 delete OldGV; // Delete the old placeholder
126
127 // Remove the map entry for the global now that it has been created...
128 GlobalRefs.erase(I);
129 }
130 }
131
Chris Lattner2f7c9632001-06-06 20:29:01 +0000132} CurModule;
133
Chris Lattner57698e22002-03-26 18:01:55 +0000134static struct PerFunctionInfo {
135 Function *CurrentFunction; // Pointer to current method being created
Chris Lattner2f7c9632001-06-06 20:29:01 +0000136
Chris Lattner17f729e2001-07-15 06:35:53 +0000137 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner2f7c9632001-06-06 20:29:01 +0000138 vector<ValueList> LateResolveValues;
Chris Lattner30752bd92002-04-04 19:23:55 +0000139 vector<PATypeHolder> Types;
140 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner17f729e2001-07-15 06:35:53 +0000141 bool isDeclare; // Is this method a forward declararation?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000142
Chris Lattner57698e22002-03-26 18:01:55 +0000143 inline PerFunctionInfo() {
144 CurrentFunction = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +0000145 isDeclare = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000146 }
147
Chris Lattner57698e22002-03-26 18:01:55 +0000148 inline ~PerFunctionInfo() {}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000149
Chris Lattner57698e22002-03-26 18:01:55 +0000150 inline void FunctionStart(Function *M) {
151 CurrentFunction = M;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000152 }
153
Chris Lattner57698e22002-03-26 18:01:55 +0000154 void FunctionDone() {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000155 // If we could not resolve some blocks at parsing time (forward branches)
156 // resolve the branches now...
Chris Lattner322e49a2001-10-16 19:54:17 +0000157 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000158
159 Values.clear(); // Clear out method local definitions
Chris Lattneraa534bf2001-09-07 16:35:17 +0000160 Types.clear();
Chris Lattner57698e22002-03-26 18:01:55 +0000161 CurrentFunction = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +0000162 isDeclare = false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000163 }
164} CurMeth; // Info for the current method...
165
Chris Lattner57698e22002-03-26 18:01:55 +0000166static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000167
Chris Lattner2f7c9632001-06-06 20:29:01 +0000168
169//===----------------------------------------------------------------------===//
170// Code to handle definitions of all the types
171//===----------------------------------------------------------------------===//
172
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000173static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
174 if (D->hasName()) return -1; // Is this a numbered definition?
175
176 // Yes, insert the value into the value table...
177 unsigned type = D->getType()->getUniqueID();
178 if (ValueTab.size() <= type)
179 ValueTab.resize(type+1, ValueList());
180 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
181 ValueTab[type].push_back(D);
182 return ValueTab[type].size()-1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000183}
184
Chris Lattneraa534bf2001-09-07 16:35:17 +0000185// TODO: FIXME when Type are not const
Chris Lattner30752bd92002-04-04 19:23:55 +0000186static void InsertType(const Type *Ty, vector<PATypeHolder> &Types) {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000187 Types.push_back(Ty);
188}
189
190static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000191 switch (D.Type) {
192 case 0: { // Is it a numbered definition?
Chris Lattneraa534bf2001-09-07 16:35:17 +0000193 unsigned Num = (unsigned)D.Num;
194
195 // Module constants occupy the lowest numbered slots...
196 if (Num < CurModule.Types.size())
197 return CurModule.Types[Num];
198
199 Num -= CurModule.Types.size();
200
201 // Check that the number is within bounds...
202 if (Num <= CurMeth.Types.size())
203 return CurMeth.Types[Num];
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000204 break;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000205 }
206 case 1: { // Is it a named definition?
207 string Name(D.Name);
208 SymbolTable *SymTab = 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000209 if (inFunctionScope()) SymTab = CurMeth.CurrentFunction->getSymbolTable();
Chris Lattneraa534bf2001-09-07 16:35:17 +0000210 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
211
212 if (N == 0) {
213 // Symbol table doesn't automatically chain yet... because the method
214 // hasn't been added to the module...
215 //
216 SymTab = CurModule.CurrentModule->getSymbolTable();
217 if (SymTab)
218 N = SymTab->lookup(Type::TypeTy, Name);
219 if (N == 0) break;
220 }
221
222 D.destroy(); // Free old strdup'd memory...
Chris Lattner8f191122001-10-01 18:26:53 +0000223 return cast<const Type>(N);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000224 }
225 default:
226 ThrowException("Invalid symbol type reference!");
227 }
228
229 // If we reached here, we referenced either a symbol that we don't know about
230 // or an id number that hasn't been read yet. We may be referencing something
231 // forward, so just create an entry to be resolved later and get to it...
232 //
233 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
234
Chris Lattner30752bd92002-04-04 19:23:55 +0000235 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000236 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
237
Chris Lattner30752bd92002-04-04 19:23:55 +0000238 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000239 if (I != LateResolver.end()) {
240 return I->second;
241 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000242
Chris Lattnere6b470c2001-10-22 06:01:08 +0000243 Type *Typ = OpaqueType::get();
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000244 LateResolver.insert(make_pair(D, Typ));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000245 return Typ;
246}
247
Chris Lattner60e0dd72001-10-03 06:12:09 +0000248static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
249 SymbolTable *SymTab =
Chris Lattner57698e22002-03-26 18:01:55 +0000250 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() : 0;
Chris Lattner60e0dd72001-10-03 06:12:09 +0000251 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
252
253 if (N == 0) {
254 // Symbol table doesn't automatically chain yet... because the method
255 // hasn't been added to the module...
256 //
257 SymTab = CurModule.CurrentModule->getSymbolTable();
258 if (SymTab)
259 N = SymTab->lookup(Ty, Name);
260 }
261
262 return N;
263}
264
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000265// getValNonImprovising - Look up the value specified by the provided type and
266// the provided ValID. If the value exists and has already been defined, return
267// it. Otherwise return null.
268//
269static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner57698e22002-03-26 18:01:55 +0000270 if (isa<FunctionType>(Ty))
271 ThrowException("Functions are not values and "
272 "must be referenced as pointers");
Chris Lattner322e49a2001-10-16 19:54:17 +0000273
Chris Lattneraa534bf2001-09-07 16:35:17 +0000274 switch (D.Type) {
Chris Lattnerfbdec252001-09-30 22:46:54 +0000275 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattneraa534bf2001-09-07 16:35:17 +0000276 unsigned type = Ty->getUniqueID();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000277 unsigned Num = (unsigned)D.Num;
278
279 // Module constants occupy the lowest numbered slots...
280 if (type < CurModule.Values.size()) {
281 if (Num < CurModule.Values[type].size())
282 return CurModule.Values[type][Num];
283
284 Num -= CurModule.Values[type].size();
285 }
286
287 // Make sure that our type is within bounds
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000288 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000289
290 // Check that the number is within bounds...
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000291 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000292
293 return CurMeth.Values[type][Num];
294 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000295
Chris Lattnerfbdec252001-09-30 22:46:54 +0000296 case ValID::NameVal: { // Is it a named definition?
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000297 Value *N = lookupInSymbolTable(Ty, string(D.Name));
298 if (N == 0) return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000299
300 D.destroy(); // Free old strdup'd memory...
301 return N;
302 }
303
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000304 // Check to make sure that "Ty" is an integral type, and that our
305 // value will fit into the specified type...
306 case ValID::ConstSIntVal: // Is it a constant pool reference??
307 if (Ty == Type::BoolTy) { // Special handling for boolean data
Chris Lattner3462ae32001-12-03 22:26:30 +0000308 return ConstantBool::get(D.ConstPool64 != 0);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000309 } else {
Chris Lattner3462ae32001-12-03 22:26:30 +0000310 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000311 ThrowException("Symbolic constant pool value '" +
312 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +0000313 Ty->getDescription() + "'!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000314 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000315 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000316
317 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattner3462ae32001-12-03 22:26:30 +0000318 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
319 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000320 ThrowException("Integral constant pool reference is invalid!");
321 } else { // This is really a signed reference. Transmogrify.
Chris Lattner3462ae32001-12-03 22:26:30 +0000322 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000323 }
324 } else {
Chris Lattner3462ae32001-12-03 22:26:30 +0000325 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000326 }
327
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000328 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattner3462ae32001-12-03 22:26:30 +0000329 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000330 ThrowException("FP constant invalid for type!!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000331 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000332
333 case ValID::ConstNullVal: // Is it a null value?
334 if (!Ty->isPointerType())
335 ThrowException("Cannot create a a non pointer null!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000336 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000337
Chris Lattneraa534bf2001-09-07 16:35:17 +0000338 default:
339 assert(0 && "Unhandled case!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000340 return 0;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000341 } // End of switch
342
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000343 assert(0 && "Unhandled case!");
344 return 0;
345}
346
347
348// getVal - This function is identical to getValNonImprovising, except that if a
349// value is not already defined, it "improvises" by creating a placeholder var
350// that looks and acts just like the requested variable. When the value is
351// defined later, all uses of the placeholder variable are replaced with the
352// real thing.
353//
354static Value *getVal(const Type *Ty, const ValID &D) {
355 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
356
357 // See if the value has already been defined...
358 Value *V = getValNonImprovising(Ty, D);
359 if (V) return V;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000360
361 // If we reached here, we referenced either a symbol that we don't know about
362 // or an id number that hasn't been read yet. We may be referencing something
363 // forward, so just create an entry to be resolved later and get to it...
364 //
Chris Lattner2f7c9632001-06-06 20:29:01 +0000365 Value *d = 0;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000366 switch (Ty->getPrimitiveID()) {
367 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000368 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000369 }
370
371 assert(d != 0 && "How did we not make something?");
Chris Lattner57698e22002-03-26 18:01:55 +0000372 if (inFunctionScope())
Chris Lattner322e49a2001-10-16 19:54:17 +0000373 InsertValue(d, CurMeth.LateResolveValues);
374 else
375 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner2f7c9632001-06-06 20:29:01 +0000376 return d;
377}
378
379
380//===----------------------------------------------------------------------===//
381// Code to handle forward references in instructions
382//===----------------------------------------------------------------------===//
383//
384// This code handles the late binding needed with statements that reference
385// values not defined yet... for example, a forward branch, or the PHI node for
386// a loop body.
387//
388// This keeps a table (CurMeth.LateResolveValues) of all such forward references
389// and back patchs after we are done.
390//
391
392// ResolveDefinitions - If we could not resolve some defs at parsing
393// time (forward branches, phi functions for loops, etc...) resolve the
394// defs now...
395//
Chris Lattner322e49a2001-10-16 19:54:17 +0000396static void ResolveDefinitions(vector<ValueList> &LateResolvers,
397 vector<ValueList> *FutureLateResolvers = 0) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000398 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
399 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
400 while (!LateResolvers[ty].empty()) {
401 Value *V = LateResolvers[ty].back();
Chris Lattner322e49a2001-10-16 19:54:17 +0000402 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
403
Chris Lattner2f7c9632001-06-06 20:29:01 +0000404 LateResolvers[ty].pop_back();
405 ValID &DID = getValIDFromPlaceHolder(V);
406
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000407 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner322e49a2001-10-16 19:54:17 +0000408 if (TheRealValue) {
409 V->replaceAllUsesWith(TheRealValue);
410 delete V;
411 } else if (FutureLateResolvers) {
Chris Lattner57698e22002-03-26 18:01:55 +0000412 // Functions have their unresolved items forwarded to the module late
Chris Lattner322e49a2001-10-16 19:54:17 +0000413 // resolver table
414 InsertValue(V, *FutureLateResolvers);
415 } else {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000416 if (DID.Type == 1)
417 ThrowException("Reference to an invalid definition: '" +DID.getName()+
418 "' of type '" + V->getType()->getDescription() + "'",
419 getLineNumFromPlaceHolder(V));
420 else
421 ThrowException("Reference to an invalid definition: #" +
422 itostr(DID.Num) + " of type '" +
423 V->getType()->getDescription() + "'",
424 getLineNumFromPlaceHolder(V));
425 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000426 }
427 }
428
429 LateResolvers.clear();
430}
431
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000432// ResolveTypeTo - A brand new type was just declared. This means that (if
433// name is not null) things referencing Name can be resolved. Otherwise, things
434// refering to the number can be resolved. Do this now.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000435//
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000436static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner30752bd92002-04-04 19:23:55 +0000437 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000438 CurMeth.Types : CurModule.Types;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000439
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000440 ValID D;
441 if (Name) D = ValID::create(Name);
442 else D = ValID::create((int)Types.size());
Chris Lattneraa534bf2001-09-07 16:35:17 +0000443
Chris Lattner30752bd92002-04-04 19:23:55 +0000444 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000445 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
446
Chris Lattner30752bd92002-04-04 19:23:55 +0000447 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000448 if (I != LateResolver.end()) {
449 cast<DerivedType>(I->second.get())->refineAbstractTypeTo(ToTy);
450 LateResolver.erase(I);
451 }
452}
453
454// ResolveTypes - At this point, all types should be resolved. Any that aren't
455// are errors.
456//
Chris Lattner30752bd92002-04-04 19:23:55 +0000457static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000458 if (!LateResolveTypes.empty()) {
Chris Lattnere6b470c2001-10-22 06:01:08 +0000459 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000460
461 if (DID.Type == ValID::NameVal)
Chris Lattnere6b470c2001-10-22 06:01:08 +0000462 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattnerd50fa5a2001-10-22 05:56:09 +0000463 else
Chris Lattnere6b470c2001-10-22 06:01:08 +0000464 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000465 }
466}
467
Chris Lattner571a6b02001-10-03 01:49:25 +0000468
Chris Lattner7fb14f52001-09-18 04:00:54 +0000469// setValueName - Set the specified value to the name given. The name may be
470// null potentially, in which case this is a noop. The string passed in is
471// assumed to be a malloc'd string buffer, and is freed by this function.
472//
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000473// This function returns true if the value has already been defined, but is
474// allowed to be redefined in the specified context. If the name is a new name
475// for the typeplane, false is returned.
476//
477static bool setValueName(Value *V, char *NameStr) {
478 if (NameStr == 0) return false;
Chris Lattner322e49a2001-10-16 19:54:17 +0000479
Chris Lattner7fb14f52001-09-18 04:00:54 +0000480 string Name(NameStr); // Copy string
481 free(NameStr); // Free old string
482
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000483 if (V->getType() == Type::VoidTy)
484 ThrowException("Can't assign name '" + Name +
485 "' to a null valued instruction!");
486
Chris Lattner57698e22002-03-26 18:01:55 +0000487 SymbolTable *ST = inFunctionScope() ?
488 CurMeth.CurrentFunction->getSymbolTableSure() :
Chris Lattneraa534bf2001-09-07 16:35:17 +0000489 CurModule.CurrentModule->getSymbolTableSure();
490
491 Value *Existing = ST->lookup(V->getType(), Name);
492 if (Existing) { // Inserting a name that is already defined???
493 // There is only one case where this is allowed: when we are refining an
494 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner571a6b02001-10-03 01:49:25 +0000495 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerda558102001-10-02 03:41:24 +0000496 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000497 // We ARE replacing an opaque type!
Chris Lattnerda558102001-10-02 03:41:24 +0000498 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000499 return true;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000500 }
Chris Lattner571a6b02001-10-03 01:49:25 +0000501 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000502
Chris Lattner4b717c02001-10-01 16:18:37 +0000503 // Otherwise, we are a simple redefinition of a value, check to see if it
504 // is defined the same as the old one...
505 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000506 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
507 // cerr << "Type: " << Ty->getDescription() << " != "
508 // << cast<const Type>(V)->getDescription() << "!\n";
509 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner78326d22001-10-03 19:35:57 +0000510 // We are allowed to redefine a global variable in two circumstances:
511 // 1. If at least one of the globals is uninitialized or
512 // 2. If both initializers have the same value.
513 //
514 // This can only be done if the const'ness of the vars is the same.
515 //
Chris Lattnere504b432001-10-03 19:35:04 +0000516 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
517 if (EGV->isConstant() == GV->isConstant() &&
518 (!EGV->hasInitializer() || !GV->hasInitializer() ||
519 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000520
Chris Lattnere504b432001-10-03 19:35:04 +0000521 // Make sure the existing global version gets the initializer!
522 if (GV->hasInitializer() && !EGV->hasInitializer())
523 EGV->setInitializer(GV->getInitializer());
524
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000525 delete GV; // Destroy the duplicate!
Chris Lattnere504b432001-10-03 19:35:04 +0000526 return true; // They are equivalent!
527 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000528 }
Chris Lattner4b717c02001-10-01 16:18:37 +0000529 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000530 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattneraa534bf2001-09-07 16:35:17 +0000531 V->getType()->getDescription() + "' type plane!");
Chris Lattner26e50dc2001-07-28 17:48:55 +0000532 }
Chris Lattner2f7c9632001-06-06 20:29:01 +0000533
Chris Lattneraa534bf2001-09-07 16:35:17 +0000534 V->setName(Name, ST);
Chris Lattnerfb3e5492001-10-03 15:39:04 +0000535 return false;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000536}
537
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000538
Chris Lattneraa534bf2001-09-07 16:35:17 +0000539//===----------------------------------------------------------------------===//
540// Code for handling upreferences in type names...
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000541//
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000542
Chris Lattneraa534bf2001-09-07 16:35:17 +0000543// TypeContains - Returns true if Ty contains E in it.
544//
545static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattnerbb09a102001-09-28 22:56:31 +0000546 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000547}
Chris Lattner1caf0bb2001-07-20 19:15:08 +0000548
Chris Lattneraa534bf2001-09-07 16:35:17 +0000549
550static vector<pair<unsigned, OpaqueType *> > UpRefs;
551
Chris Lattner30752bd92002-04-04 19:23:55 +0000552static PATypeHolder HandleUpRefs(const Type *ty) {
553 PATypeHolder Ty(ty);
Chris Lattnerf29b2312001-11-02 07:46:26 +0000554 UR_OUT("Type '" << ty->getDescription() <<
555 "' newly formed. Resolving upreferences.\n" <<
556 UpRefs.size() << " upreferences active!\n");
Chris Lattneraa534bf2001-09-07 16:35:17 +0000557 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattnerf29b2312001-11-02 07:46:26 +0000558 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattneraa534bf2001-09-07 16:35:17 +0000559 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerf29b2312001-11-02 07:46:26 +0000560 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000561 if (TypeContains(Ty, UpRefs[i].second)) {
562 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattnerf29b2312001-11-02 07:46:26 +0000563 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000564 if (Level == 0) { // Upreference should be resolved!
Chris Lattnerf29b2312001-11-02 07:46:26 +0000565 UR_OUT(" * Resolving upreference for "
566 << UpRefs[i].second->getDescription() << endl;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000567 string OldName = UpRefs[i].second->getDescription());
568 UpRefs[i].second->refineAbstractTypeTo(Ty);
569 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattnerf29b2312001-11-02 07:46:26 +0000570 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattneraa534bf2001-09-07 16:35:17 +0000571 << (const void*)Ty << ", " << Ty->getDescription() << endl);
572 continue;
573 }
574 }
575
576 ++i; // Otherwise, no resolve, move on...
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000577 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000578 // FIXME: TODO: this should return the updated type
Chris Lattnerd9c40e32001-07-09 19:38:36 +0000579 return Ty;
580}
581
Chris Lattneraa534bf2001-09-07 16:35:17 +0000582
Chris Lattner2f7c9632001-06-06 20:29:01 +0000583//===----------------------------------------------------------------------===//
584// RunVMAsmParser - Define an interface to this parser
585//===----------------------------------------------------------------------===//
586//
Chris Lattnerf2d1e792001-07-22 18:36:00 +0000587Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner2f7c9632001-06-06 20:29:01 +0000588 llvmAsmin = F;
Chris Lattnerf2d1e792001-07-22 18:36:00 +0000589 CurFilename = Filename;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000590 llvmAsmlineno = 1; // Reset the current line number...
591
592 CurModule.CurrentModule = new Module(); // Allocate a new module to read
593 yyparse(); // Parse the file.
594 Module *Result = ParserResult;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000595 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
596 ParserResult = 0;
597
598 return Result;
599}
600
601%}
602
603%union {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000604 Module *ModuleVal;
Chris Lattner57698e22002-03-26 18:01:55 +0000605 Function *FunctionVal;
Chris Lattnerae234e12002-04-09 19:41:42 +0000606 std::pair<Argument*, char*> *ArgVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000607 BasicBlock *BasicBlockVal;
608 TerminatorInst *TermInstVal;
609 Instruction *InstVal;
Chris Lattner3462ae32001-12-03 22:26:30 +0000610 Constant *ConstVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000611
Chris Lattneraa534bf2001-09-07 16:35:17 +0000612 const Type *PrimType;
Chris Lattner30752bd92002-04-04 19:23:55 +0000613 PATypeHolder *TypeVal;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000614 Value *ValueVal;
615
Chris Lattnerae234e12002-04-09 19:41:42 +0000616 std::list<std::pair<Argument*,char*> > *ArgList;
Chris Lattner7f74a562002-01-20 22:54:45 +0000617 std::vector<Value*> *ValueList;
Chris Lattner30752bd92002-04-04 19:23:55 +0000618 std::list<PATypeHolder> *TypeList;
Chris Lattner7f74a562002-01-20 22:54:45 +0000619 std::list<std::pair<Value*,
620 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattnerae234e12002-04-09 19:41:42 +0000621 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner7f74a562002-01-20 22:54:45 +0000622 std::vector<Constant*> *ConstVector;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000623
Chris Lattneraa534bf2001-09-07 16:35:17 +0000624 int64_t SInt64Val;
625 uint64_t UInt64Val;
626 int SIntVal;
627 unsigned UIntVal;
628 double FPVal;
Chris Lattner7fb14f52001-09-18 04:00:54 +0000629 bool BoolVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000630
Chris Lattneraa534bf2001-09-07 16:35:17 +0000631 char *StrVal; // This memory is strdup'd!
632 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner2f7c9632001-06-06 20:29:01 +0000633
Chris Lattneraa534bf2001-09-07 16:35:17 +0000634 Instruction::UnaryOps UnaryOpVal;
635 Instruction::BinaryOps BinaryOpVal;
636 Instruction::TermOps TermOpVal;
637 Instruction::MemoryOps MemOpVal;
638 Instruction::OtherOps OtherOpVal;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000639}
640
Chris Lattner57698e22002-03-26 18:01:55 +0000641%type <ModuleVal> Module FunctionList
642%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner2f7c9632001-06-06 20:29:01 +0000643%type <BasicBlockVal> BasicBlock InstructionList
644%type <TermInstVal> BBTerminatorInst
645%type <InstVal> Inst InstVal MemoryInst
Chris Lattner571a6b02001-10-03 01:49:25 +0000646%type <ConstVal> ConstVal
Chris Lattner476148f2001-11-26 16:54:11 +0000647%type <ConstVector> ConstVector
Chris Lattnerae234e12002-04-09 19:41:42 +0000648%type <ArgList> ArgList ArgListH
649%type <ArgVal> ArgVal
Chris Lattner931ef3b2001-06-11 15:04:20 +0000650%type <PHIList> PHIList
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000651%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner476148f2001-11-26 16:54:11 +0000652%type <ValueList> IndexList // For GEP derived indices
Chris Lattneraa534bf2001-09-07 16:35:17 +0000653%type <TypeList> TypeListI ArgTypeListI
Chris Lattner2f7c9632001-06-06 20:29:01 +0000654%type <JumpTable> JumpTable
Chris Lattner841d8b92001-11-26 18:54:16 +0000655%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner2f7c9632001-06-06 20:29:01 +0000656
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000657// ValueRef - Unresolved reference to a definition or BB
658%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattner252afba2001-07-26 16:29:15 +0000659%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner2f7c9632001-06-06 20:29:01 +0000660// Tokens and types for handling constant integer values
661//
662// ESINT64VAL - A negative number within long long range
663%token <SInt64Val> ESINT64VAL
664
665// EUINT64VAL - A positive number within uns. long long range
666%token <UInt64Val> EUINT64VAL
667%type <SInt64Val> EINT64VAL
668
669%token <SIntVal> SINTVAL // Signed 32 bit ints...
670%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
671%type <SIntVal> INTVAL
Chris Lattner212f70d2001-07-15 00:17:01 +0000672%token <FPVal> FPVAL // Float or Double constant
Chris Lattner2f7c9632001-06-06 20:29:01 +0000673
674// Built in types...
Chris Lattneraa534bf2001-09-07 16:35:17 +0000675%type <TypeVal> Types TypesV UpRTypes UpRTypesV
676%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattneraa534bf2001-09-07 16:35:17 +0000677%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
678%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner2f7c9632001-06-06 20:29:01 +0000679
680%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
681%type <StrVal> OptVAR_ID OptAssign
682
683
Chris Lattner7fb14f52001-09-18 04:00:54 +0000684%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattner30752bd92002-04-04 19:23:55 +0000685%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL OPAQUE
Chris Lattner2f7c9632001-06-06 20:29:01 +0000686
687// Basic Block Terminating Operators
688%token <TermOpVal> RET BR SWITCH
689
690// Unary Operators
691%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner49c64322001-07-08 19:03:27 +0000692%token <UnaryOpVal> NOT
Chris Lattner2f7c9632001-06-06 20:29:01 +0000693
694// Binary Operators
695%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000696%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000697%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner2f7c9632001-06-06 20:29:01 +0000698
699// Memory Instructions
Chris Lattner62ecb4a2001-07-08 23:22:50 +0000700%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner2f7c9632001-06-06 20:29:01 +0000701
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000702// Other Operators
703%type <OtherOpVal> ShiftOps
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000704%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000705
Chris Lattner2f7c9632001-06-06 20:29:01 +0000706%start Module
707%%
708
709// Handle constant integer size restriction and conversion...
710//
711
712INTVAL : SINTVAL
713INTVAL : UINTVAL {
714 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
715 ThrowException("Value too large for type!");
716 $$ = (int32_t)$1;
717}
718
719
720EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
721EINT64VAL : EUINT64VAL {
722 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
723 ThrowException("Value too large for type!");
724 $$ = (int64_t)$1;
725}
726
Chris Lattner2f7c9632001-06-06 20:29:01 +0000727// Operations that are notably excluded from this list include:
728// RET, BR, & SWITCH because they end basic blocks and are treated specially.
729//
Chris Lattnera6821822001-07-08 04:57:15 +0000730UnaryOps : NOT
Chris Lattnerc27b1d72001-10-20 09:32:59 +0000731BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR
Chris Lattner2f7c9632001-06-06 20:29:01 +0000732BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattnerd8bebcd2001-07-08 21:10:27 +0000733ShiftOps : SHL | SHR
Chris Lattner2f7c9632001-06-06 20:29:01 +0000734
Chris Lattner56f73d42001-07-14 06:10:16 +0000735// These are some types that allow classification if we only want a particular
736// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000737SIntType : LONG | INT | SHORT | SBYTE
738UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattneraa534bf2001-09-07 16:35:17 +0000739IntType : SIntType | UIntType
740FPType : FLOAT | DOUBLE
Chris Lattner2f7c9632001-06-06 20:29:01 +0000741
Chris Lattner56f73d42001-07-14 06:10:16 +0000742// OptAssign - Value producing statements have an optional assignment component
Chris Lattner2f7c9632001-06-06 20:29:01 +0000743OptAssign : VAR_ID '=' {
744 $$ = $1;
745 }
746 | /*empty*/ {
747 $$ = 0;
748 }
749
Chris Lattner841d8b92001-11-26 18:54:16 +0000750OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000751
752//===----------------------------------------------------------------------===//
753// Types includes all predefined types... except void, because it can only be
754// used in specific contexts (method returning void for example). To have
755// access to it, a user must explicitly use TypesV.
756//
757
758// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner30752bd92002-04-04 19:23:55 +0000759TypesV : Types | VOID { $$ = new PATypeHolder($1); }
760UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000761
762Types : UpRTypes {
Chris Lattner30752bd92002-04-04 19:23:55 +0000763 if (UpRefs.size())
764 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
765 $$ = $1;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000766 }
767
768
769// Derived types are added later...
770//
771PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
772PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
Chris Lattner30752bd92002-04-04 19:23:55 +0000773UpRTypes : OPAQUE {
774 $$ = new PATypeHolder(OpaqueType::get());
775 }
776 | PrimType {
777 $$ = new PATypeHolder($1);
778 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000779UpRTypes : ValueRef { // Named types are also simple types...
Chris Lattner30752bd92002-04-04 19:23:55 +0000780 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000781}
782
Chris Lattneraa534bf2001-09-07 16:35:17 +0000783// Include derived types in the Types production.
784//
785UpRTypes : '\\' EUINT64VAL { // Type UpReference
786 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
787 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
788 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner30752bd92002-04-04 19:23:55 +0000789 $$ = new PATypeHolder(OT);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000790 UR_OUT("New Upreference!\n");
791 }
Chris Lattner57698e22002-03-26 18:01:55 +0000792 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattneraa534bf2001-09-07 16:35:17 +0000793 vector<const Type*> Params;
Chris Lattner7f74a562002-01-20 22:54:45 +0000794 mapto($3->begin(), $3->end(), std::back_inserter(Params),
795 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000796 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
797 if (isVarArg) Params.pop_back();
798
Chris Lattner30752bd92002-04-04 19:23:55 +0000799 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000800 delete $3; // Delete the argument list
801 delete $1; // Delete the old type handle
802 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000803 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner30752bd92002-04-04 19:23:55 +0000804 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000805 delete $4;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000806 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000807 | '{' TypeListI '}' { // Structure type?
808 vector<const Type*> Elements;
Chris Lattner7f74a562002-01-20 22:54:45 +0000809 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
810 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000811
Chris Lattner30752bd92002-04-04 19:23:55 +0000812 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000813 delete $2;
814 }
815 | '{' '}' { // Empty structure type?
Chris Lattner30752bd92002-04-04 19:23:55 +0000816 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000817 }
818 | UpRTypes '*' { // Pointer type?
Chris Lattner30752bd92002-04-04 19:23:55 +0000819 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000820 delete $1;
821 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000822
823// TypeList - Used for struct declarations and as a basis for method type
824// declaration type lists
825//
826TypeListI : UpRTypes {
Chris Lattner30752bd92002-04-04 19:23:55 +0000827 $$ = new list<PATypeHolder>();
Chris Lattneraa534bf2001-09-07 16:35:17 +0000828 $$->push_back(*$1); delete $1;
829 }
830 | TypeListI ',' UpRTypes {
831 ($$=$1)->push_back(*$3); delete $3;
832 }
833
834// ArgTypeList - List of types for a method type declaration...
835ArgTypeListI : TypeListI
836 | TypeListI ',' DOTDOTDOT {
837 ($$=$1)->push_back(Type::VoidTy);
838 }
839 | DOTDOTDOT {
Chris Lattner30752bd92002-04-04 19:23:55 +0000840 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000841 }
842 | /*empty*/ {
Chris Lattner30752bd92002-04-04 19:23:55 +0000843 $$ = new list<PATypeHolder>();
Chris Lattneraa534bf2001-09-07 16:35:17 +0000844 }
845
846
Chris Lattner56f73d42001-07-14 06:10:16 +0000847// ConstVal - The various declarations that go into the constant pool. This
848// includes all forward declarations of types, constants, and functions.
849//
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000850ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
851 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
852 if (ATy == 0)
853 ThrowException("Cannot make array constant with type: '" +
854 (*$1)->getDescription() + "'!");
Chris Lattneraa534bf2001-09-07 16:35:17 +0000855 const Type *ETy = ATy->getElementType();
856 int NumElements = ATy->getNumElements();
Chris Lattner2f7c9632001-06-06 20:29:01 +0000857
Chris Lattneraa534bf2001-09-07 16:35:17 +0000858 // Verify that we have the correct size...
859 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner2f7c9632001-06-06 20:29:01 +0000860 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattneraa534bf2001-09-07 16:35:17 +0000861 utostr($3->size()) + " arguments, but has size of " +
862 itostr(NumElements) + "!");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000863
Chris Lattneraa534bf2001-09-07 16:35:17 +0000864 // Verify all elements are correct type!
865 for (unsigned i = 0; i < $3->size(); i++) {
866 if (ETy != (*$3)[i]->getType())
Chris Lattner2f7c9632001-06-06 20:29:01 +0000867 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +0000868 ETy->getDescription() +"' as required!\nIt is of type '"+
869 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner2f7c9632001-06-06 20:29:01 +0000870 }
871
Chris Lattner3462ae32001-12-03 22:26:30 +0000872 $$ = ConstantArray::get(ATy, *$3);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000873 delete $1; delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000874 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000875 | Types '[' ']' {
876 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
877 if (ATy == 0)
878 ThrowException("Cannot make array constant with type: '" +
879 (*$1)->getDescription() + "'!");
880
881 int NumElements = ATy->getNumElements();
Chris Lattneraa534bf2001-09-07 16:35:17 +0000882 if (NumElements != -1 && NumElements != 0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000883 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattneraa534bf2001-09-07 16:35:17 +0000884 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000885 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattneraa534bf2001-09-07 16:35:17 +0000886 delete $1;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000887 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000888 | Types 'c' STRINGCONSTANT {
889 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
890 if (ATy == 0)
891 ThrowException("Cannot make array constant with type: '" +
892 (*$1)->getDescription() + "'!");
893
Chris Lattneraa534bf2001-09-07 16:35:17 +0000894 int NumElements = ATy->getNumElements();
895 const Type *ETy = ATy->getElementType();
896 char *EndStr = UnEscapeLexed($3, true);
897 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner26e50dc2001-07-28 17:48:55 +0000898 ThrowException("Can't build string constant of size " +
Chris Lattneraa534bf2001-09-07 16:35:17 +0000899 itostr((int)(EndStr-$3)) +
900 " when array has size " + itostr(NumElements) + "!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000901 vector<Constant*> Vals;
Chris Lattneraa534bf2001-09-07 16:35:17 +0000902 if (ETy == Type::SByteTy) {
903 for (char *C = $3; C != EndStr; ++C)
Chris Lattner3462ae32001-12-03 22:26:30 +0000904 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattneraa534bf2001-09-07 16:35:17 +0000905 } else if (ETy == Type::UByteTy) {
906 for (char *C = $3; C != EndStr; ++C)
Chris Lattner3462ae32001-12-03 22:26:30 +0000907 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner26e50dc2001-07-28 17:48:55 +0000908 } else {
Chris Lattneraa534bf2001-09-07 16:35:17 +0000909 free($3);
Chris Lattner26e50dc2001-07-28 17:48:55 +0000910 ThrowException("Cannot build string arrays of non byte sized elements!");
911 }
Chris Lattneraa534bf2001-09-07 16:35:17 +0000912 free($3);
Chris Lattner3462ae32001-12-03 22:26:30 +0000913 $$ = ConstantArray::get(ATy, Vals);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000914 delete $1;
Chris Lattner26e50dc2001-07-28 17:48:55 +0000915 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000916 | Types '{' ConstVector '}' {
917 const StructType *STy = dyn_cast<const StructType>($1->get());
918 if (STy == 0)
919 ThrowException("Cannot make struct constant with type: '" +
920 (*$1)->getDescription() + "'!");
Chris Lattneraa534bf2001-09-07 16:35:17 +0000921 // FIXME: TODO: Check to see that the constants are compatible with the type
922 // initializer!
Chris Lattner3462ae32001-12-03 22:26:30 +0000923 $$ = ConstantStruct::get(STy, *$3);
Chris Lattneraa534bf2001-09-07 16:35:17 +0000924 delete $1; delete $3;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000925 }
Chris Lattnere63ddd8df2001-10-03 03:19:33 +0000926 | Types NULL_TOK {
927 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
928 if (PTy == 0)
929 ThrowException("Cannot make null pointer constant with type: '" +
930 (*$1)->getDescription() + "'!");
931
Chris Lattner3462ae32001-12-03 22:26:30 +0000932 $$ = ConstantPointerNull::get(PTy);
Chris Lattner571a6b02001-10-03 01:49:25 +0000933 delete $1;
934 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000935 | Types SymbolicValueRef {
Chris Lattner60e0dd72001-10-03 06:12:09 +0000936 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
937 if (Ty == 0)
938 ThrowException("Global const reference must be a pointer type!");
939
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000940 Value *V = getValNonImprovising(Ty, $2);
Chris Lattner60e0dd72001-10-03 06:12:09 +0000941
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000942 // If this is an initializer for a constant pointer, which is referencing a
943 // (currently) undefined variable, create a stub now that shall be replaced
944 // in the future with the right type of variable.
945 //
946 if (V == 0) {
947 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
948 const PointerType *PT = cast<PointerType>(Ty);
949
950 // First check to see if the forward references value is already created!
951 PerModuleInfo::GlobalRefsType::iterator I =
952 CurModule.GlobalRefs.find(make_pair(PT, $2));
953
954 if (I != CurModule.GlobalRefs.end()) {
955 V = I->second; // Placeholder already exists, use it...
956 } else {
957 // TODO: Include line number info by creating a subclass of
958 // TODO: GlobalVariable here that includes the said information!
959
960 // Create a placeholder for the global variable reference...
Chris Lattner2413b162001-12-04 00:03:30 +0000961 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
962 false, true);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000963 // Keep track of the fact that we have a forward ref to recycle it
964 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
965
966 // Must temporarily push this value into the module table...
967 CurModule.CurrentModule->getGlobalList().push_back(GV);
968 V = GV;
969 }
Chris Lattner60e0dd72001-10-03 06:12:09 +0000970 }
971
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000972 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattner3462ae32001-12-03 22:26:30 +0000973 $$ = ConstantPointerRef::get(GV);
Chris Lattnerf26e0d92001-10-13 06:41:08 +0000974 delete $1; // Free the type handle
Chris Lattner2f7c9632001-06-06 20:29:01 +0000975 }
Chris Lattner60e0dd72001-10-03 06:12:09 +0000976
Chris Lattner2f7c9632001-06-06 20:29:01 +0000977
Chris Lattner571a6b02001-10-03 01:49:25 +0000978ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattner3462ae32001-12-03 22:26:30 +0000979 if (!ConstantSInt::isValueValidForType($1, $2))
Chris Lattner252afba2001-07-26 16:29:15 +0000980 ThrowException("Constant value doesn't fit in type!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000981 $$ = ConstantSInt::get($1, $2);
Chris Lattner252afba2001-07-26 16:29:15 +0000982 }
983 | UIntType EUINT64VAL { // integral constants
Chris Lattner3462ae32001-12-03 22:26:30 +0000984 if (!ConstantUInt::isValueValidForType($1, $2))
Chris Lattner252afba2001-07-26 16:29:15 +0000985 ThrowException("Constant value doesn't fit in type!");
Chris Lattner3462ae32001-12-03 22:26:30 +0000986 $$ = ConstantUInt::get($1, $2);
Chris Lattner252afba2001-07-26 16:29:15 +0000987 }
988 | BOOL TRUE { // Boolean constants
Chris Lattner3462ae32001-12-03 22:26:30 +0000989 $$ = ConstantBool::True;
Chris Lattner252afba2001-07-26 16:29:15 +0000990 }
991 | BOOL FALSE { // Boolean constants
Chris Lattner3462ae32001-12-03 22:26:30 +0000992 $$ = ConstantBool::False;
Chris Lattner252afba2001-07-26 16:29:15 +0000993 }
994 | FPType FPVAL { // Float & Double constants
Chris Lattner3462ae32001-12-03 22:26:30 +0000995 $$ = ConstantFP::get($1, $2);
Chris Lattner252afba2001-07-26 16:29:15 +0000996 }
Chris Lattner252afba2001-07-26 16:29:15 +0000997
Chris Lattner56f73d42001-07-14 06:10:16 +0000998// ConstVector - A list of comma seperated constants.
Chris Lattner2f7c9632001-06-06 20:29:01 +0000999ConstVector : ConstVector ',' ConstVal {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001000 ($$ = $1)->push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001001 }
1002 | ConstVal {
Chris Lattner3462ae32001-12-03 22:26:30 +00001003 $$ = new vector<Constant*>();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001004 $$->push_back($1);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001005 }
1006
Chris Lattner252afba2001-07-26 16:29:15 +00001007
Chris Lattner7fb14f52001-09-18 04:00:54 +00001008// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1009GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
1010
Chris Lattner2f7c9632001-06-06 20:29:01 +00001011
Chris Lattner56f73d42001-07-14 06:10:16 +00001012// ConstPool - Constants with optional names assigned to them.
Chris Lattner571a6b02001-10-03 01:49:25 +00001013ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001014 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattner571a6b02001-10-03 01:49:25 +00001015 InsertValue($4);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001016 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001017 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattnerd50fa5a2001-10-22 05:56:09 +00001018 // Eagerly resolve types. This is not an optimization, this is a
1019 // requirement that is due to the fact that we could have this:
1020 //
1021 // %list = type { %list * }
1022 // %list = type { %list * } ; repeated type decl
1023 //
1024 // If types are not resolved eagerly, then the two types will not be
1025 // determined to be the same type!
1026 //
1027 ResolveTypeTo($2, $4->get());
1028
Chris Lattner7fb14f52001-09-18 04:00:54 +00001029 // TODO: FIXME when Type are not const
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001030 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1031 // If this is not a redefinition of a type...
1032 if (!$2) {
1033 InsertType($4->get(),
Chris Lattner57698e22002-03-26 18:01:55 +00001034 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001035 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001036 }
Chris Lattner6265d792001-10-21 23:02:41 +00001037
1038 delete $4;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001039 }
Chris Lattner57698e22002-03-26 18:01:55 +00001040 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner26e50dc2001-07-28 17:48:55 +00001041 }
Chris Lattner841d8b92001-11-26 18:54:16 +00001042 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1043 const Type *Ty = $5->getType();
Chris Lattner7fb14f52001-09-18 04:00:54 +00001044 // Global declarations appear in Constant Pool
Chris Lattner3462ae32001-12-03 22:26:30 +00001045 Constant *Initializer = $5;
Chris Lattner7fb14f52001-09-18 04:00:54 +00001046 if (Initializer == 0)
1047 ThrowException("Global value initializer is not a constant!");
1048
Chris Lattner841d8b92001-11-26 18:54:16 +00001049 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001050 if (!setValueName(GV, $2)) { // If not redefining...
1051 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001052 int Slot = InsertValue(GV, CurModule.Values);
1053
1054 if (Slot != -1) {
1055 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1056 } else {
1057 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1058 (char*)GV->getName().c_str()));
1059 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001060 }
Chris Lattner7fb14f52001-09-18 04:00:54 +00001061 }
Chris Lattner841d8b92001-11-26 18:54:16 +00001062 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1063 const Type *Ty = *$6;
Chris Lattner7fb14f52001-09-18 04:00:54 +00001064 // Global declarations appear in Constant Pool
Chris Lattner841d8b92001-11-26 18:54:16 +00001065 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001066 if (!setValueName(GV, $2)) { // If not redefining...
1067 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001068 int Slot = InsertValue(GV, CurModule.Values);
1069
1070 if (Slot != -1) {
1071 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1072 } else {
1073 assert(GV->hasName() && "Not named and not numbered!?");
1074 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1075 (char*)GV->getName().c_str()));
1076 }
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001077 }
Chris Lattner041468f2002-03-31 07:16:49 +00001078 delete $6;
Chris Lattner56f73d42001-07-14 06:10:16 +00001079 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001080 | /* empty: end of list */ {
1081 }
1082
1083
1084//===----------------------------------------------------------------------===//
1085// Rules to match Modules
1086//===----------------------------------------------------------------------===//
1087
1088// Module rule: Capture the result of parsing the whole file into a result
1089// variable...
1090//
Chris Lattner57698e22002-03-26 18:01:55 +00001091Module : FunctionList {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001092 $$ = ParserResult = $1;
1093 CurModule.ModuleDone();
1094}
1095
Chris Lattner57698e22002-03-26 18:01:55 +00001096// FunctionList - A list of methods, preceeded by a constant pool.
Chris Lattner56f73d42001-07-14 06:10:16 +00001097//
Chris Lattner57698e22002-03-26 18:01:55 +00001098FunctionList : FunctionList Function {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001099 $$ = $1;
Chris Lattner57698e22002-03-26 18:01:55 +00001100 assert($2->getParent() == 0 && "Function already in module!");
1101 $1->getFunctionList().push_back($2);
1102 CurMeth.FunctionDone();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001103 }
Chris Lattner57698e22002-03-26 18:01:55 +00001104 | FunctionList FunctionProto {
Chris Lattner17f729e2001-07-15 06:35:53 +00001105 $$ = $1;
Chris Lattner17f729e2001-07-15 06:35:53 +00001106 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001107 | ConstPool IMPLEMENTATION {
1108 $$ = CurModule.CurrentModule;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001109 // Resolve circular types before we parse the body of the module
1110 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001111 }
1112
1113
1114//===----------------------------------------------------------------------===//
Chris Lattner57698e22002-03-26 18:01:55 +00001115// Rules to match Function Headers
Chris Lattner2f7c9632001-06-06 20:29:01 +00001116//===----------------------------------------------------------------------===//
1117
1118OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1119
1120ArgVal : Types OptVAR_ID {
Chris Lattnerae234e12002-04-09 19:41:42 +00001121 $$ = new pair<Argument*, char*>(new Argument(*$1), $2);
Chris Lattner29f67092002-03-08 18:41:32 +00001122 delete $1; // Delete the type handle..
Chris Lattner2f7c9632001-06-06 20:29:01 +00001123}
1124
1125ArgListH : ArgVal ',' ArgListH {
1126 $$ = $3;
Chris Lattner29f67092002-03-08 18:41:32 +00001127 $3->push_front(*$1);
1128 delete $1;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001129 }
1130 | ArgVal {
Chris Lattnerae234e12002-04-09 19:41:42 +00001131 $$ = new list<pair<Argument*,char*> >();
Chris Lattner29f67092002-03-08 18:41:32 +00001132 $$->push_front(*$1);
1133 delete $1;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001134 }
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001135 | DOTDOTDOT {
Chris Lattnerae234e12002-04-09 19:41:42 +00001136 $$ = new list<pair<Argument*, char*> >();
1137 $$->push_front(pair<Argument*,char*>(new Argument(Type::VoidTy), 0));
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001138 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001139
1140ArgList : ArgListH {
1141 $$ = $1;
1142 }
1143 | /* empty */ {
1144 $$ = 0;
1145 }
1146
Chris Lattner57698e22002-03-26 18:01:55 +00001147FunctionHeaderH : OptInternal TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner841d8b92001-11-26 18:54:16 +00001148 UnEscapeLexed($3);
Chris Lattner57698e22002-03-26 18:01:55 +00001149 string FunctionName($3);
Chris Lattner841d8b92001-11-26 18:54:16 +00001150
Chris Lattneraa534bf2001-09-07 16:35:17 +00001151 vector<const Type*> ParamTypeList;
Chris Lattner841d8b92001-11-26 18:54:16 +00001152 if ($5)
Chris Lattnerae234e12002-04-09 19:41:42 +00001153 for (list<pair<Argument*,char*> >::iterator I = $5->begin();
Chris Lattner29f67092002-03-08 18:41:32 +00001154 I != $5->end(); ++I)
1155 ParamTypeList.push_back(I->first->getType());
Chris Lattner2f7c9632001-06-06 20:29:01 +00001156
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001157 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1158 if (isVarArg) ParamTypeList.pop_back();
1159
Chris Lattner57698e22002-03-26 18:01:55 +00001160 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattner7fac0702001-10-03 14:53:21 +00001161 const PointerType *PMT = PointerType::get(MT);
Chris Lattner841d8b92001-11-26 18:54:16 +00001162 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001163
Chris Lattner57698e22002-03-26 18:01:55 +00001164 Function *M = 0;
Chris Lattner17f729e2001-07-15 06:35:53 +00001165 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner57698e22002-03-26 18:01:55 +00001166 // Is the function already in symtab?
1167 if (Value *V = ST->lookup(PMT, FunctionName)) {
1168 M = cast<Function>(V);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001169
Chris Lattner17f729e2001-07-15 06:35:53 +00001170 // Yes it is. If this is the case, either we need to be a forward decl,
1171 // or it needs to be.
1172 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner57698e22002-03-26 18:01:55 +00001173 ThrowException("Redefinition of method '" + FunctionName + "'!");
Chris Lattner108308a2002-03-08 19:11:42 +00001174
1175 // If we found a preexisting method prototype, remove it from the module,
1176 // so that we don't get spurious conflicts with global & local variables.
1177 //
Chris Lattner57698e22002-03-26 18:01:55 +00001178 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattner17f729e2001-07-15 06:35:53 +00001179 }
1180 }
1181
1182 if (M == 0) { // Not already defined?
Chris Lattner57698e22002-03-26 18:01:55 +00001183 M = new Function(MT, $1, FunctionName);
Chris Lattner17f729e2001-07-15 06:35:53 +00001184 InsertValue(M, CurModule.Values);
Chris Lattner841d8b92001-11-26 18:54:16 +00001185 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattner17f729e2001-07-15 06:35:53 +00001186 }
Chris Lattner841d8b92001-11-26 18:54:16 +00001187 free($3); // Free strdup'd memory!
Chris Lattner2f7c9632001-06-06 20:29:01 +00001188
Chris Lattner57698e22002-03-26 18:01:55 +00001189 CurMeth.FunctionStart(M);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001190
1191 // Add all of the arguments we parsed to the method...
Chris Lattner841d8b92001-11-26 18:54:16 +00001192 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner57698e22002-03-26 18:01:55 +00001193 Function::ArgumentListType &ArgList = M->getArgumentList();
Chris Lattner2f7c9632001-06-06 20:29:01 +00001194
Chris Lattnerae234e12002-04-09 19:41:42 +00001195 for (list<pair<Argument*, char*> >::iterator I = $5->begin();
Chris Lattner29f67092002-03-08 18:41:32 +00001196 I != $5->end(); ++I) {
1197 if (setValueName(I->first, I->second)) { // Insert into symtab...
1198 assert(0 && "No arg redef allowed!");
1199 }
1200
1201 InsertValue(I->first);
1202 ArgList.push_back(I->first);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001203 }
Chris Lattner841d8b92001-11-26 18:54:16 +00001204 delete $5; // We're now done with the argument list
Chris Lattnerf7bfab52002-03-08 18:57:56 +00001205 } else if ($5) {
1206 // If we are a declaration, we should free the memory for the argument list!
Chris Lattnerae234e12002-04-09 19:41:42 +00001207 for (list<pair<Argument*, char*> >::iterator I = $5->begin(), E = $5->end();
1208 I != E; ++I) {
Chris Lattnerf7bfab52002-03-08 18:57:56 +00001209 if (I->second) free(I->second); // Free the memory for the name...
Chris Lattner041468f2002-03-31 07:16:49 +00001210 delete I->first; // Free the unused function argument
1211 }
Chris Lattnerf7bfab52002-03-08 18:57:56 +00001212 delete $5; // Free the memory for the list itself
Chris Lattner2f7c9632001-06-06 20:29:01 +00001213 }
1214}
1215
Chris Lattner57698e22002-03-26 18:01:55 +00001216FunctionHeader : FunctionHeaderH ConstPool BEGINTOK {
1217 $$ = CurMeth.CurrentFunction;
Chris Lattneraa534bf2001-09-07 16:35:17 +00001218
1219 // Resolve circular types before we parse the body of the method.
1220 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001221}
1222
Chris Lattner57698e22002-03-26 18:01:55 +00001223Function : BasicBlockList END {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001224 $$ = $1;
1225}
1226
Chris Lattner57698e22002-03-26 18:01:55 +00001227FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1228 $$ = CurMeth.CurrentFunction;
1229 assert($$->getParent() == 0 && "Function already in module!");
1230 CurModule.CurrentModule->getFunctionList().push_back($$);
1231 CurMeth.FunctionDone();
Chris Lattner17f729e2001-07-15 06:35:53 +00001232}
Chris Lattner2f7c9632001-06-06 20:29:01 +00001233
1234//===----------------------------------------------------------------------===//
1235// Rules to match Basic Blocks
1236//===----------------------------------------------------------------------===//
1237
1238ConstValueRef : ESINT64VAL { // A reference to a direct constant
1239 $$ = ValID::create($1);
1240 }
1241 | EUINT64VAL {
1242 $$ = ValID::create($1);
1243 }
Chris Lattner212f70d2001-07-15 00:17:01 +00001244 | FPVAL { // Perhaps it's an FP constant?
1245 $$ = ValID::create($1);
1246 }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001247 | TRUE {
1248 $$ = ValID::create((int64_t)1);
1249 }
1250 | FALSE {
1251 $$ = ValID::create((int64_t)0);
1252 }
Chris Lattnerfbdec252001-09-30 22:46:54 +00001253 | NULL_TOK {
1254 $$ = ValID::createNull();
1255 }
1256
Chris Lattner26e50dc2001-07-28 17:48:55 +00001257/*
Chris Lattner2f7c9632001-06-06 20:29:01 +00001258 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1259 $$ = ValID::create_conststr($1);
1260 }
Chris Lattner26e50dc2001-07-28 17:48:55 +00001261*/
Chris Lattner2f7c9632001-06-06 20:29:01 +00001262
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001263// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1264// another value.
1265//
1266SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner2f7c9632001-06-06 20:29:01 +00001267 $$ = ValID::create($1);
1268 }
Chris Lattner212f70d2001-07-15 00:17:01 +00001269 | VAR_ID { // Is it a named reference...?
Chris Lattner2f7c9632001-06-06 20:29:01 +00001270 $$ = ValID::create($1);
1271 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001272
1273// ValueRef - A reference to a definition... either constant or symbolic
1274ValueRef : SymbolicValueRef | ConstValueRef
1275
Chris Lattner2f7c9632001-06-06 20:29:01 +00001276
Chris Lattner252afba2001-07-26 16:29:15 +00001277// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1278// type immediately preceeds the value reference, and allows complex constant
1279// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattner571a6b02001-10-03 01:49:25 +00001280ResolvedVal : Types ValueRef {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001281 $$ = getVal(*$1, $2); delete $1;
Chris Lattner26e50dc2001-07-28 17:48:55 +00001282 }
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001283
Chris Lattner2f7c9632001-06-06 20:29:01 +00001284
1285BasicBlockList : BasicBlockList BasicBlock {
Chris Lattnere504b432001-10-03 19:35:04 +00001286 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001287 }
Chris Lattner57698e22002-03-26 18:01:55 +00001288 | FunctionHeader BasicBlock { // Do not allow methods with 0 basic blocks
Chris Lattnere504b432001-10-03 19:35:04 +00001289 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001290 }
1291
1292
1293// Basic blocks are terminated by branching instructions:
1294// br, br/cc, switch, ret
1295//
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001296BasicBlock : InstructionList OptAssign BBTerminatorInst {
1297 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1298 InsertValue($3);
1299
1300 $1->getInstList().push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001301 InsertValue($1);
1302 $$ = $1;
1303 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001304 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1305 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1306 InsertValue($4);
1307
1308 $2->getInstList().push_back($4);
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001309 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001310
1311 InsertValue($2);
1312 $$ = $2;
1313 }
1314
1315InstructionList : InstructionList Inst {
1316 $1->getInstList().push_back($2);
1317 $$ = $1;
1318 }
1319 | /* empty */ {
1320 $$ = new BasicBlock();
1321 }
1322
Chris Lattner252afba2001-07-26 16:29:15 +00001323BBTerminatorInst : RET ResolvedVal { // Return with a result...
1324 $$ = new ReturnInst($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001325 }
1326 | RET VOID { // Return with no result...
1327 $$ = new ReturnInst();
1328 }
1329 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner4b717c02001-10-01 16:18:37 +00001330 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001331 } // Conditional Branch...
1332 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner4b717c02001-10-01 16:18:37 +00001333 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1334 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner2f7c9632001-06-06 20:29:01 +00001335 getVal(Type::BoolTy, $3));
1336 }
1337 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1338 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner4b717c02001-10-01 16:18:37 +00001339 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001340 $$ = S;
1341
Chris Lattnerae234e12002-04-09 19:41:42 +00001342 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1343 E = $8->end();
1344 for (; I != E; ++I)
Chris Lattner2f7c9632001-06-06 20:29:01 +00001345 S->dest_push_back(I->first, I->second);
1346 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001347 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1348 EXCEPT ResolvedVal {
1349 const PointerType *PMTy;
Chris Lattner57698e22002-03-26 18:01:55 +00001350 const FunctionType *Ty;
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001351
1352 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner57698e22002-03-26 18:01:55 +00001353 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001354 // Pull out the types of all of the arguments...
1355 vector<const Type*> ParamTypes;
1356 if ($5) {
Chris Lattner476148f2001-11-26 16:54:11 +00001357 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001358 ParamTypes.push_back((*I)->getType());
1359 }
1360
1361 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1362 if (isVarArg) ParamTypes.pop_back();
1363
Chris Lattner57698e22002-03-26 18:01:55 +00001364 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001365 PMTy = PointerType::get(Ty);
1366 }
1367 delete $2;
1368
1369 Value *V = getVal(PMTy, $3); // Get the method we're calling...
1370
1371 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1372 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1373
1374 if (Normal == 0 || Except == 0)
1375 ThrowException("Invoke instruction without label destinations!");
1376
1377 // Create the call node...
1378 if (!$5) { // Has no arguments?
Chris Lattner322e49a2001-10-16 19:54:17 +00001379 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001380 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00001381 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001382 // correctly!
1383 //
Chris Lattner57698e22002-03-26 18:01:55 +00001384 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1385 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner476148f2001-11-26 16:54:11 +00001386 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001387
1388 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1389 if ((*ArgI)->getType() != *I)
1390 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001391 (*I)->getDescription() + "'!");
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001392
1393 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1394 ThrowException("Invalid number of parameters detected!");
1395
Chris Lattner476148f2001-11-26 16:54:11 +00001396 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001397 }
1398 delete $5;
1399 }
1400
1401
Chris Lattner2f7c9632001-06-06 20:29:01 +00001402
1403JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1404 $$ = $1;
Chris Lattner3462ae32001-12-03 22:26:30 +00001405 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001406 if (V == 0)
1407 ThrowException("May only switch on a constant pool value!");
1408
Chris Lattner4b717c02001-10-01 16:18:37 +00001409 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001410 }
1411 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattnerae234e12002-04-09 19:41:42 +00001412 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattner3462ae32001-12-03 22:26:30 +00001413 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001414
1415 if (V == 0)
1416 ThrowException("May only switch on a constant pool value!");
1417
Chris Lattner4b717c02001-10-01 16:18:37 +00001418 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001419 }
1420
1421Inst : OptAssign InstVal {
Chris Lattnerfb3e5492001-10-03 15:39:04 +00001422 // Is this definition named?? if so, assign the name...
1423 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner2f7c9632001-06-06 20:29:01 +00001424 InsertValue($2);
1425 $$ = $2;
1426}
1427
Chris Lattner931ef3b2001-06-11 15:04:20 +00001428PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1429 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattneraa534bf2001-09-07 16:35:17 +00001430 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner4b717c02001-10-01 16:18:37 +00001431 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattneraa534bf2001-09-07 16:35:17 +00001432 delete $1;
Chris Lattner931ef3b2001-06-11 15:04:20 +00001433 }
1434 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1435 $$ = $1;
1436 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner4b717c02001-10-01 16:18:37 +00001437 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner931ef3b2001-06-11 15:04:20 +00001438 }
1439
1440
Chris Lattneraa534bf2001-09-07 16:35:17 +00001441ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner476148f2001-11-26 16:54:11 +00001442 $$ = new vector<Value*>();
Chris Lattner252afba2001-07-26 16:29:15 +00001443 $$->push_back($1);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001444 }
Chris Lattner252afba2001-07-26 16:29:15 +00001445 | ValueRefList ',' ResolvedVal {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001446 $$ = $1;
Chris Lattner252afba2001-07-26 16:29:15 +00001447 $1->push_back($3);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001448 }
1449
1450// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1451ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1452
1453InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001454 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner2f7c9632001-06-06 20:29:01 +00001455 if ($$ == 0)
1456 ThrowException("binary operator returned null!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001457 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001458 }
Chris Lattner252afba2001-07-26 16:29:15 +00001459 | UnaryOps ResolvedVal {
1460 $$ = UnaryOperator::create($1, $2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001461 if ($$ == 0)
1462 ThrowException("unary operator returned null!");
Chris Lattnera6821822001-07-08 04:57:15 +00001463 }
Chris Lattner252afba2001-07-26 16:29:15 +00001464 | ShiftOps ResolvedVal ',' ResolvedVal {
1465 if ($4->getType() != Type::UByteTy)
1466 ThrowException("Shift amount must be ubyte!");
1467 $$ = new ShiftInst($1, $2, $4);
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001468 }
Chris Lattner252afba2001-07-26 16:29:15 +00001469 | CAST ResolvedVal TO Types {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001470 $$ = new CastInst($2, *$4);
1471 delete $4;
Chris Lattnera6821822001-07-08 04:57:15 +00001472 }
Chris Lattner931ef3b2001-06-11 15:04:20 +00001473 | PHI PHIList {
1474 const Type *Ty = $2->front().first->getType();
1475 $$ = new PHINode(Ty);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001476 while ($2->begin() != $2->end()) {
Chris Lattner931ef3b2001-06-11 15:04:20 +00001477 if ($2->front().first->getType() != Ty)
1478 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerda558102001-10-02 03:41:24 +00001479 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001480 $2->pop_front();
1481 }
1482 delete $2; // Free the list...
1483 }
Chris Lattner26e50dc2001-07-28 17:48:55 +00001484 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattner7fac0702001-10-03 14:53:21 +00001485 const PointerType *PMTy;
Chris Lattner57698e22002-03-26 18:01:55 +00001486 const FunctionType *Ty;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001487
Chris Lattner7fac0702001-10-03 14:53:21 +00001488 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner57698e22002-03-26 18:01:55 +00001489 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001490 // Pull out the types of all of the arguments...
1491 vector<const Type*> ParamTypes;
Chris Lattner7fac0702001-10-03 14:53:21 +00001492 if ($5) {
Chris Lattner476148f2001-11-26 16:54:11 +00001493 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner7fac0702001-10-03 14:53:21 +00001494 ParamTypes.push_back((*I)->getType());
1495 }
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001496
1497 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1498 if (isVarArg) ParamTypes.pop_back();
1499
Chris Lattner57698e22002-03-26 18:01:55 +00001500 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner7fac0702001-10-03 14:53:21 +00001501 PMTy = PointerType::get(Ty);
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001502 }
Chris Lattneraa534bf2001-09-07 16:35:17 +00001503 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001504
Chris Lattner7fac0702001-10-03 14:53:21 +00001505 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner2f7c9632001-06-06 20:29:01 +00001506
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001507 // Create the call node...
1508 if (!$5) { // Has no arguments?
Chris Lattner322e49a2001-10-16 19:54:17 +00001509 $$ = new CallInst(V, vector<Value*>());
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001510 } else { // Has arguments?
Chris Lattner57698e22002-03-26 18:01:55 +00001511 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2f7c9632001-06-06 20:29:01 +00001512 // correctly!
1513 //
Chris Lattner57698e22002-03-26 18:01:55 +00001514 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1515 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner476148f2001-11-26 16:54:11 +00001516 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001517
1518 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1519 if ((*ArgI)->getType() != *I)
1520 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001521 (*I)->getDescription() + "'!");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001522
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001523 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner2f7c9632001-06-06 20:29:01 +00001524 ThrowException("Invalid number of parameters detected!");
Chris Lattner2f7c9632001-06-06 20:29:01 +00001525
Chris Lattner476148f2001-11-26 16:54:11 +00001526 $$ = new CallInst(V, *$5);
Chris Lattner42b5a8a2001-07-25 22:47:46 +00001527 }
1528 delete $5;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001529 }
1530 | MemoryInst {
1531 $$ = $1;
1532 }
1533
Chris Lattner476148f2001-11-26 16:54:11 +00001534
1535// IndexList - List of indices for GEP based instructions...
1536IndexList : ',' ValueRefList {
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001537 $$ = $2;
1538} | /* empty */ {
Chris Lattner476148f2001-11-26 16:54:11 +00001539 $$ = new vector<Value*>();
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001540}
1541
Chris Lattner2f7c9632001-06-06 20:29:01 +00001542MemoryInst : MALLOC Types {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001543 $$ = new MallocInst(PointerType::get(*$2));
1544 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001545 }
1546 | MALLOC Types ',' UINT ValueRef {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001547 const Type *Ty = PointerType::get(*$2);
Chris Lattnerd9c40e32001-07-09 19:38:36 +00001548 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattneraa534bf2001-09-07 16:35:17 +00001549 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001550 }
1551 | ALLOCA Types {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001552 $$ = new AllocaInst(PointerType::get(*$2));
1553 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001554 }
1555 | ALLOCA Types ',' UINT ValueRef {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001556 const Type *Ty = PointerType::get(*$2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001557 Value *ArrSize = getVal($4, $5);
Chris Lattner37099992001-07-07 08:36:30 +00001558 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattneraa534bf2001-09-07 16:35:17 +00001559 delete $2;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001560 }
Chris Lattner252afba2001-07-26 16:29:15 +00001561 | FREE ResolvedVal {
1562 if (!$2->getType()->isPointerType())
1563 ThrowException("Trying to free nonpointer type " +
Chris Lattneref99d3c2001-12-14 16:28:42 +00001564 $2->getType()->getDescription() + "!");
Chris Lattner252afba2001-07-26 16:29:15 +00001565 $$ = new FreeInst($2);
Chris Lattner2f7c9632001-06-06 20:29:01 +00001566 }
1567
Chris Lattner476148f2001-11-26 16:54:11 +00001568 | LOAD Types ValueRef IndexList {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001569 if (!(*$2)->isPointerType())
Chris Lattnerf26e0d92001-10-13 06:41:08 +00001570 ThrowException("Can't load from nonpointer type: " +
1571 (*$2)->getDescription());
Chris Lattneraa534bf2001-09-07 16:35:17 +00001572 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001573 ThrowException("Invalid indices for load instruction!");
1574
Chris Lattneraa534bf2001-09-07 16:35:17 +00001575 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001576 delete $4; // Free the vector...
Chris Lattneraa534bf2001-09-07 16:35:17 +00001577 delete $2;
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001578 }
Chris Lattner476148f2001-11-26 16:54:11 +00001579 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001580 if (!(*$4)->isPointerType())
Chris Lattneref99d3c2001-12-14 16:28:42 +00001581 ThrowException("Can't store to a nonpointer type: " +
1582 (*$4)->getDescription());
Chris Lattneraa534bf2001-09-07 16:35:17 +00001583 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001584 if (ElTy == 0)
1585 ThrowException("Can't store into that field list!");
Chris Lattner252afba2001-07-26 16:29:15 +00001586 if (ElTy != $2->getType())
Chris Lattneref99d3c2001-12-14 16:28:42 +00001587 ThrowException("Can't store '" + $2->getType()->getDescription() +
1588 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001589 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1590 delete $4; delete $6;
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001591 }
Chris Lattner476148f2001-11-26 16:54:11 +00001592 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattneraa534bf2001-09-07 16:35:17 +00001593 if (!(*$2)->isPointerType())
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001594 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001595 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattneref99d3c2001-12-14 16:28:42 +00001596 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattneraa534bf2001-09-07 16:35:17 +00001597 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1598 delete $2; delete $4;
Chris Lattner62ecb4a2001-07-08 23:22:50 +00001599 }
Chris Lattnerd8bebcd2001-07-08 21:10:27 +00001600
Chris Lattner2f7c9632001-06-06 20:29:01 +00001601%%
Chris Lattnera6821822001-07-08 04:57:15 +00001602int yyerror(const char *ErrorMsg) {
Chris Lattner2f7c9632001-06-06 20:29:01 +00001603 ThrowException(string("Parse error: ") + ErrorMsg);
1604 return 0;
1605}