blob: 9d6c99d41ee3542909fd8ffcd4a237330492ce50 [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 Lattner70cc3392001-09-10 07:58:01 +00009#include "llvm/Assembly/Parser.h"
Chris Lattner00950542001-06-06 20:29:01 +000010#include "llvm/SymbolTable.h"
11#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000012#include "llvm/GlobalVariable.h"
13#include "llvm/Method.h"
14#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000015#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/iTerminators.h"
17#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000018#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/STLExtras.h"
20#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include <list>
22#include <utility> // Get definition of pair class
Chris Lattner30c89792001-09-07 16:35:17 +000023#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000024#include <stdio.h> // This embarasment is due to our flex lexer...
Chris Lattner697954c2002-01-20 22:54:45 +000025#include <iostream>
26using std::list;
27using std::vector;
28using std::pair;
29using std::map;
30using std::pair;
31using std::make_pair;
32using std::cerr;
33using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner386a3b72001-10-16 19:54:17 +000035int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000036int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000037int yyparse();
38
39static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000040string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000041
Chris Lattner30c89792001-09-07 16:35:17 +000042// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
43// relating to upreferences in the input stream.
44//
45//#define DEBUG_UPREFS 1
46#ifdef DEBUG_UPREFS
47#define UR_OUT(X) cerr << X
48#else
49#define UR_OUT(X)
50#endif
51
Chris Lattner00950542001-06-06 20:29:01 +000052// This contains info used when building the body of a method. It is destroyed
53// when the method is completed.
54//
55typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000056static void ResolveDefinitions(vector<ValueList> &LateResolvers,
57 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000058
59static struct PerModuleInfo {
60 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000061 vector<ValueList> Values; // Module level numbered definitions
62 vector<ValueList> LateResolveValues;
Chris Lattner4a42e902001-10-22 05:56:09 +000063 vector<PATypeHolder<Type> > Types;
64 map<ValID, PATypeHolder<Type> > LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000065
Chris Lattner2079fde2001-10-13 06:41:08 +000066 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
67 // references to global values. Global values may be referenced before they
68 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000069 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000070 //
71 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
72 GlobalRefsType GlobalRefs;
73
Chris Lattner00950542001-06-06 20:29:01 +000074 void ModuleDone() {
Chris Lattner30c89792001-09-07 16:35:17 +000075 // If we could not resolve some methods at method compilation time (calls to
76 // methods before they are defined), resolve them now... Types are resolved
77 // when the constant pool has been completely parsed.
78 //
Chris Lattner00950542001-06-06 20:29:01 +000079 ResolveDefinitions(LateResolveValues);
80
Chris Lattner2079fde2001-10-13 06:41:08 +000081 // Check to make sure that all global value forward references have been
82 // resolved!
83 //
84 if (!GlobalRefs.empty()) {
85 // TODO: Make this more detailed! Loop over each undef value and print
86 // info
Chris Lattner4a42e902001-10-22 05:56:09 +000087 ThrowException("TODO: Make better error - Unresolved forward constant "
88 "references exist!");
Chris Lattner2079fde2001-10-13 06:41:08 +000089 }
90
Chris Lattner00950542001-06-06 20:29:01 +000091 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000092 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000093 CurrentModule = 0;
94 }
Chris Lattner2079fde2001-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 Lattnere9bb2df2001-12-03 22:26:30 +0000111 // allowed to be at this point is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000112 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
113 while (!OldGV->use_empty()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000114 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
115 ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
Chris Lattner2079fde2001-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 Lattner00950542001-06-06 20:29:01 +0000132} CurModule;
133
134static struct PerMethodInfo {
135 Method *CurrentMethod; // Pointer to current method being created
136
Chris Lattnere1815642001-07-15 06:35:53 +0000137 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000138 vector<ValueList> LateResolveValues;
Chris Lattner4a42e902001-10-22 05:56:09 +0000139 vector<PATypeHolder<Type> > Types;
140 map<ValID, PATypeHolder<Type> > LateResolveTypes;
Chris Lattnere1815642001-07-15 06:35:53 +0000141 bool isDeclare; // Is this method a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000142
143 inline PerMethodInfo() {
144 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000145 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000146 }
147
148 inline ~PerMethodInfo() {}
149
150 inline void MethodStart(Method *M) {
151 CurrentMethod = M;
152 }
153
154 void MethodDone() {
155 // If we could not resolve some blocks at parsing time (forward branches)
156 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000157 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000158
159 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000160 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000161 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000162 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000163 }
164} CurMeth; // Info for the current method...
165
Chris Lattnerb7474512001-10-03 15:39:04 +0000166static bool inMethodScope() { return CurMeth.CurrentMethod != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000167
Chris Lattner00950542001-06-06 20:29:01 +0000168
169//===----------------------------------------------------------------------===//
170// Code to handle definitions of all the types
171//===----------------------------------------------------------------------===//
172
Chris Lattner2079fde2001-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 Lattner00950542001-06-06 20:29:01 +0000183}
184
Chris Lattner30c89792001-09-07 16:35:17 +0000185// TODO: FIXME when Type are not const
186static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
187 Types.push_back(Ty);
188}
189
190static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000191 switch (D.Type) {
192 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-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 Lattner42c9e772001-10-20 09:32:59 +0000204 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000205 }
206 case 1: { // Is it a named definition?
207 string Name(D.Name);
208 SymbolTable *SymTab = 0;
Chris Lattnerb7474512001-10-03 15:39:04 +0000209 if (inMethodScope()) SymTab = CurMeth.CurrentMethod->getSymbolTable();
Chris Lattner30c89792001-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 Lattnercfe26c92001-10-01 18:26:53 +0000223 return cast<const Type>(N);
Chris Lattner30c89792001-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 Lattner4a42e902001-10-22 05:56:09 +0000235 map<ValID, PATypeHolder<Type> > &LateResolver = inMethodScope() ?
236 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
237
238 map<ValID, PATypeHolder<Type> >::iterator I = LateResolver.find(D);
239 if (I != LateResolver.end()) {
240 return I->second;
241 }
Chris Lattner30c89792001-09-07 16:35:17 +0000242
Chris Lattner82269592001-10-22 06:01:08 +0000243 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000244 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000245 return Typ;
246}
247
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000248static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
249 SymbolTable *SymTab =
Chris Lattnerb7474512001-10-03 15:39:04 +0000250 inMethodScope() ? CurMeth.CurrentMethod->getSymbolTable() : 0;
Chris Lattnerf4ba6c72001-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 Lattner2079fde2001-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 Lattner386a3b72001-10-16 19:54:17 +0000270 if (isa<MethodType>(Ty))
271 ThrowException("Methods are not values and must be referenced as pointers");
272
Chris Lattner30c89792001-09-07 16:35:17 +0000273 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000274 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000275 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000276 unsigned Num = (unsigned)D.Num;
277
278 // Module constants occupy the lowest numbered slots...
279 if (type < CurModule.Values.size()) {
280 if (Num < CurModule.Values[type].size())
281 return CurModule.Values[type][Num];
282
283 Num -= CurModule.Values[type].size();
284 }
285
286 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000287 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000288
289 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000290 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000291
292 return CurMeth.Values[type][Num];
293 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000294
Chris Lattner1a1cb112001-09-30 22:46:54 +0000295 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000296 Value *N = lookupInSymbolTable(Ty, string(D.Name));
297 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000298
299 D.destroy(); // Free old strdup'd memory...
300 return N;
301 }
302
Chris Lattner2079fde2001-10-13 06:41:08 +0000303 // Check to make sure that "Ty" is an integral type, and that our
304 // value will fit into the specified type...
305 case ValID::ConstSIntVal: // Is it a constant pool reference??
306 if (Ty == Type::BoolTy) { // Special handling for boolean data
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000307 return ConstantBool::get(D.ConstPool64 != 0);
Chris Lattner2079fde2001-10-13 06:41:08 +0000308 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000309 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner2079fde2001-10-13 06:41:08 +0000310 ThrowException("Symbolic constant pool value '" +
311 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000312 Ty->getDescription() + "'!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000313 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000314 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000315
316 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000317 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
318 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000319 ThrowException("Integral constant pool reference is invalid!");
320 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000321 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000322 }
323 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000324 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000325 }
326
327 case ValID::ConstStringVal: // Is it a string const pool reference?
328 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
329 abort();
330 return 0;
331
332 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000333 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000334 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000335 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000336
337 case ValID::ConstNullVal: // Is it a null value?
338 if (!Ty->isPointerType())
339 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000340 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000341
Chris Lattner30c89792001-09-07 16:35:17 +0000342 default:
343 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000344 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000345 } // End of switch
346
Chris Lattner2079fde2001-10-13 06:41:08 +0000347 assert(0 && "Unhandled case!");
348 return 0;
349}
350
351
352// getVal - This function is identical to getValNonImprovising, except that if a
353// value is not already defined, it "improvises" by creating a placeholder var
354// that looks and acts just like the requested variable. When the value is
355// defined later, all uses of the placeholder variable are replaced with the
356// real thing.
357//
358static Value *getVal(const Type *Ty, const ValID &D) {
359 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
360
361 // See if the value has already been defined...
362 Value *V = getValNonImprovising(Ty, D);
363 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000364
365 // If we reached here, we referenced either a symbol that we don't know about
366 // or an id number that hasn't been read yet. We may be referencing something
367 // forward, so just create an entry to be resolved later and get to it...
368 //
Chris Lattner00950542001-06-06 20:29:01 +0000369 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000370 switch (Ty->getPrimitiveID()) {
371 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000372 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000373 }
374
375 assert(d != 0 && "How did we not make something?");
Chris Lattner386a3b72001-10-16 19:54:17 +0000376 if (inMethodScope())
377 InsertValue(d, CurMeth.LateResolveValues);
378 else
379 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000380 return d;
381}
382
383
384//===----------------------------------------------------------------------===//
385// Code to handle forward references in instructions
386//===----------------------------------------------------------------------===//
387//
388// This code handles the late binding needed with statements that reference
389// values not defined yet... for example, a forward branch, or the PHI node for
390// a loop body.
391//
392// This keeps a table (CurMeth.LateResolveValues) of all such forward references
393// and back patchs after we are done.
394//
395
396// ResolveDefinitions - If we could not resolve some defs at parsing
397// time (forward branches, phi functions for loops, etc...) resolve the
398// defs now...
399//
Chris Lattner386a3b72001-10-16 19:54:17 +0000400static void ResolveDefinitions(vector<ValueList> &LateResolvers,
401 vector<ValueList> *FutureLateResolvers = 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000402 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
403 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
404 while (!LateResolvers[ty].empty()) {
405 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000406 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
407
Chris Lattner00950542001-06-06 20:29:01 +0000408 LateResolvers[ty].pop_back();
409 ValID &DID = getValIDFromPlaceHolder(V);
410
Chris Lattner2079fde2001-10-13 06:41:08 +0000411 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000412 if (TheRealValue) {
413 V->replaceAllUsesWith(TheRealValue);
414 delete V;
415 } else if (FutureLateResolvers) {
416 // Methods have their unresolved items forwarded to the module late
417 // resolver table
418 InsertValue(V, *FutureLateResolvers);
419 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000420 if (DID.Type == 1)
421 ThrowException("Reference to an invalid definition: '" +DID.getName()+
422 "' of type '" + V->getType()->getDescription() + "'",
423 getLineNumFromPlaceHolder(V));
424 else
425 ThrowException("Reference to an invalid definition: #" +
426 itostr(DID.Num) + " of type '" +
427 V->getType()->getDescription() + "'",
428 getLineNumFromPlaceHolder(V));
429 }
Chris Lattner00950542001-06-06 20:29:01 +0000430 }
431 }
432
433 LateResolvers.clear();
434}
435
Chris Lattner4a42e902001-10-22 05:56:09 +0000436// ResolveTypeTo - A brand new type was just declared. This means that (if
437// name is not null) things referencing Name can be resolved. Otherwise, things
438// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000439//
Chris Lattner4a42e902001-10-22 05:56:09 +0000440static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattnerdda71962001-11-26 18:54:16 +0000441 vector<PATypeHolder<Type> > &Types = inMethodScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000442 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000443
Chris Lattner4a42e902001-10-22 05:56:09 +0000444 ValID D;
445 if (Name) D = ValID::create(Name);
446 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000447
Chris Lattner4a42e902001-10-22 05:56:09 +0000448 map<ValID, PATypeHolder<Type> > &LateResolver = inMethodScope() ?
449 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
450
451 map<ValID, PATypeHolder<Type> >::iterator I = LateResolver.find(D);
452 if (I != LateResolver.end()) {
453 cast<DerivedType>(I->second.get())->refineAbstractTypeTo(ToTy);
454 LateResolver.erase(I);
455 }
456}
457
458// ResolveTypes - At this point, all types should be resolved. Any that aren't
459// are errors.
460//
461static void ResolveTypes(map<ValID, PATypeHolder<Type> > &LateResolveTypes) {
462 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000463 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000464
465 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000466 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000467 else
Chris Lattner82269592001-10-22 06:01:08 +0000468 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000469 }
470}
471
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000472
Chris Lattner1781aca2001-09-18 04:00:54 +0000473// setValueName - Set the specified value to the name given. The name may be
474// null potentially, in which case this is a noop. The string passed in is
475// assumed to be a malloc'd string buffer, and is freed by this function.
476//
Chris Lattnerb7474512001-10-03 15:39:04 +0000477// This function returns true if the value has already been defined, but is
478// allowed to be redefined in the specified context. If the name is a new name
479// for the typeplane, false is returned.
480//
481static bool setValueName(Value *V, char *NameStr) {
482 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000483
Chris Lattner1781aca2001-09-18 04:00:54 +0000484 string Name(NameStr); // Copy string
485 free(NameStr); // Free old string
486
Chris Lattner2079fde2001-10-13 06:41:08 +0000487 if (V->getType() == Type::VoidTy)
488 ThrowException("Can't assign name '" + Name +
489 "' to a null valued instruction!");
490
Chris Lattnerb7474512001-10-03 15:39:04 +0000491 SymbolTable *ST = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000492 CurMeth.CurrentMethod->getSymbolTableSure() :
493 CurModule.CurrentModule->getSymbolTableSure();
494
495 Value *Existing = ST->lookup(V->getType(), Name);
496 if (Existing) { // Inserting a name that is already defined???
497 // There is only one case where this is allowed: when we are refining an
498 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000499 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000500 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000501 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000502 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000503 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000504 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000505 }
Chris Lattner30c89792001-09-07 16:35:17 +0000506
Chris Lattner9636a912001-10-01 16:18:37 +0000507 // Otherwise, we are a simple redefinition of a value, check to see if it
508 // is defined the same as the old one...
509 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000510 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
511 // cerr << "Type: " << Ty->getDescription() << " != "
512 // << cast<const Type>(V)->getDescription() << "!\n";
513 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000514 // We are allowed to redefine a global variable in two circumstances:
515 // 1. If at least one of the globals is uninitialized or
516 // 2. If both initializers have the same value.
517 //
518 // This can only be done if the const'ness of the vars is the same.
519 //
Chris Lattner89219832001-10-03 19:35:04 +0000520 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
521 if (EGV->isConstant() == GV->isConstant() &&
522 (!EGV->hasInitializer() || !GV->hasInitializer() ||
523 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000524
Chris Lattner89219832001-10-03 19:35:04 +0000525 // Make sure the existing global version gets the initializer!
526 if (GV->hasInitializer() && !EGV->hasInitializer())
527 EGV->setInitializer(GV->getInitializer());
528
Chris Lattner2079fde2001-10-13 06:41:08 +0000529 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000530 return true; // They are equivalent!
531 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000532 }
Chris Lattner9636a912001-10-01 16:18:37 +0000533 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000534 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000535 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000536 }
Chris Lattner00950542001-06-06 20:29:01 +0000537
Chris Lattner30c89792001-09-07 16:35:17 +0000538 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000539 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000540}
541
Chris Lattner8896eda2001-07-09 19:38:36 +0000542
Chris Lattner30c89792001-09-07 16:35:17 +0000543//===----------------------------------------------------------------------===//
544// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000545//
Chris Lattner8896eda2001-07-09 19:38:36 +0000546
Chris Lattner30c89792001-09-07 16:35:17 +0000547// TypeContains - Returns true if Ty contains E in it.
548//
549static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000550 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000551}
Chris Lattner698b56e2001-07-20 19:15:08 +0000552
Chris Lattner30c89792001-09-07 16:35:17 +0000553
554static vector<pair<unsigned, OpaqueType *> > UpRefs;
555
556static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
557 PATypeHolder<Type> Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000558 UR_OUT("Type '" << ty->getDescription() <<
559 "' newly formed. Resolving upreferences.\n" <<
560 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000561 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000562 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000563 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000564 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000565 if (TypeContains(Ty, UpRefs[i].second)) {
566 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000567 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000568 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000569 UR_OUT(" * Resolving upreference for "
570 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000571 string OldName = UpRefs[i].second->getDescription());
572 UpRefs[i].second->refineAbstractTypeTo(Ty);
573 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000574 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000575 << (const void*)Ty << ", " << Ty->getDescription() << endl);
576 continue;
577 }
578 }
579
580 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000581 }
Chris Lattner30c89792001-09-07 16:35:17 +0000582 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000583 return Ty;
584}
585
Chris Lattner30c89792001-09-07 16:35:17 +0000586template <class TypeTy>
587inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
588 if (UpRefs.size())
589 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
590}
591
592// newTH - Allocate a new type holder for the specified type
593template <class TypeTy>
594inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
595 return new PATypeHolder<TypeTy>(Ty);
596}
597template <class TypeTy>
598inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
599 return new PATypeHolder<TypeTy>(TH);
600}
601
602
Chris Lattner00950542001-06-06 20:29:01 +0000603//===----------------------------------------------------------------------===//
604// RunVMAsmParser - Define an interface to this parser
605//===----------------------------------------------------------------------===//
606//
Chris Lattnera2850432001-07-22 18:36:00 +0000607Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000608 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000609 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000610 llvmAsmlineno = 1; // Reset the current line number...
611
612 CurModule.CurrentModule = new Module(); // Allocate a new module to read
613 yyparse(); // Parse the file.
614 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000615 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
616 ParserResult = 0;
617
618 return Result;
619}
620
621%}
622
623%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000624 Module *ModuleVal;
625 Method *MethodVal;
Chris Lattnerf28d6c92002-03-08 18:41:32 +0000626 std::pair<MethodArgument*,char*> *MethArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000627 BasicBlock *BasicBlockVal;
628 TerminatorInst *TermInstVal;
629 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000630 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000631
Chris Lattner30c89792001-09-07 16:35:17 +0000632 const Type *PrimType;
633 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000634 Value *ValueVal;
635
Chris Lattnerf28d6c92002-03-08 18:41:32 +0000636 std::list<std::pair<MethodArgument*,char*> > *MethodArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000637 std::vector<Value*> *ValueList;
638 std::list<PATypeHolder<Type> > *TypeList;
639 std::list<std::pair<Value*,
640 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
641 std::list<std::pair<Constant*, BasicBlock*> > *JumpTable;
642 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000643
Chris Lattner30c89792001-09-07 16:35:17 +0000644 int64_t SInt64Val;
645 uint64_t UInt64Val;
646 int SIntVal;
647 unsigned UIntVal;
648 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000649 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000650
Chris Lattner30c89792001-09-07 16:35:17 +0000651 char *StrVal; // This memory is strdup'd!
652 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000653
Chris Lattner30c89792001-09-07 16:35:17 +0000654 Instruction::UnaryOps UnaryOpVal;
655 Instruction::BinaryOps BinaryOpVal;
656 Instruction::TermOps TermOpVal;
657 Instruction::MemoryOps MemOpVal;
658 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000659}
660
661%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000662%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000663%type <BasicBlockVal> BasicBlock InstructionList
664%type <TermInstVal> BBTerminatorInst
665%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000666%type <ConstVal> ConstVal
Chris Lattner6cdb0112001-11-26 16:54:11 +0000667%type <ConstVector> ConstVector
Chris Lattner00950542001-06-06 20:29:01 +0000668%type <MethodArgList> ArgList ArgListH
669%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000670%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000671%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000672%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000673%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000674%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000675%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000676
Chris Lattner2079fde2001-10-13 06:41:08 +0000677// ValueRef - Unresolved reference to a definition or BB
678%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000679%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000680// Tokens and types for handling constant integer values
681//
682// ESINT64VAL - A negative number within long long range
683%token <SInt64Val> ESINT64VAL
684
685// EUINT64VAL - A positive number within uns. long long range
686%token <UInt64Val> EUINT64VAL
687%type <SInt64Val> EINT64VAL
688
689%token <SIntVal> SINTVAL // Signed 32 bit ints...
690%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
691%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000692%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000693
694// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000695%type <TypeVal> Types TypesV UpRTypes UpRTypesV
696%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
697%token <TypeVal> OPAQUE
698%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
699%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000700
701%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
702%type <StrVal> OptVAR_ID OptAssign
703
704
Chris Lattner1781aca2001-09-18 04:00:54 +0000705%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerdda71962001-11-26 18:54:16 +0000706%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL
Chris Lattner00950542001-06-06 20:29:01 +0000707
708// Basic Block Terminating Operators
709%token <TermOpVal> RET BR SWITCH
710
711// Unary Operators
712%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000713%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000714
715// Binary Operators
716%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000717%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000718%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000719
720// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000721%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000722
Chris Lattner027dcc52001-07-08 21:10:27 +0000723// Other Operators
724%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000725%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000726
Chris Lattner00950542001-06-06 20:29:01 +0000727%start Module
728%%
729
730// Handle constant integer size restriction and conversion...
731//
732
733INTVAL : SINTVAL
734INTVAL : UINTVAL {
735 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
736 ThrowException("Value too large for type!");
737 $$ = (int32_t)$1;
738}
739
740
741EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
742EINT64VAL : EUINT64VAL {
743 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
744 ThrowException("Value too large for type!");
745 $$ = (int64_t)$1;
746}
747
Chris Lattner00950542001-06-06 20:29:01 +0000748// Operations that are notably excluded from this list include:
749// RET, BR, & SWITCH because they end basic blocks and are treated specially.
750//
Chris Lattner09083092001-07-08 04:57:15 +0000751UnaryOps : NOT
Chris Lattner42c9e772001-10-20 09:32:59 +0000752BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR
Chris Lattner00950542001-06-06 20:29:01 +0000753BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000754ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000755
Chris Lattnere98dda62001-07-14 06:10:16 +0000756// These are some types that allow classification if we only want a particular
757// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000758SIntType : LONG | INT | SHORT | SBYTE
759UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000760IntType : SIntType | UIntType
761FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000762
Chris Lattnere98dda62001-07-14 06:10:16 +0000763// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000764OptAssign : VAR_ID '=' {
765 $$ = $1;
766 }
767 | /*empty*/ {
768 $$ = 0;
769 }
770
Chris Lattnerdda71962001-11-26 18:54:16 +0000771OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; }
Chris Lattner30c89792001-09-07 16:35:17 +0000772
773//===----------------------------------------------------------------------===//
774// Types includes all predefined types... except void, because it can only be
775// used in specific contexts (method returning void for example). To have
776// access to it, a user must explicitly use TypesV.
777//
778
779// TypesV includes all of 'Types', but it also includes the void type.
780TypesV : Types | VOID { $$ = newTH($1); }
781UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
782
783Types : UpRTypes {
784 TypeDone($$ = $1);
785 }
786
787
788// Derived types are added later...
789//
790PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
791PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
792UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
793UpRTypes : ValueRef { // Named types are also simple types...
794 $$ = newTH(getTypeVal($1));
795}
796
Chris Lattner30c89792001-09-07 16:35:17 +0000797// Include derived types in the Types production.
798//
799UpRTypes : '\\' EUINT64VAL { // Type UpReference
800 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
801 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
802 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
803 $$ = newTH<Type>(OT);
804 UR_OUT("New Upreference!\n");
805 }
806 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
807 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000808 mapto($3->begin(), $3->end(), std::back_inserter(Params),
809 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000810 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
811 if (isVarArg) Params.pop_back();
812
813 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params, isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000814 delete $3; // Delete the argument list
815 delete $1; // Delete the old type handle
816 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000817 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner72e00252001-12-14 16:28:42 +0000818 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000819 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000820 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000821 | '{' TypeListI '}' { // Structure type?
822 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000823 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
824 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000825
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000826 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
827 delete $2;
828 }
829 | '{' '}' { // Empty structure type?
830 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
831 }
832 | UpRTypes '*' { // Pointer type?
833 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
834 delete $1;
835 }
Chris Lattner30c89792001-09-07 16:35:17 +0000836
837// TypeList - Used for struct declarations and as a basis for method type
838// declaration type lists
839//
840TypeListI : UpRTypes {
841 $$ = new list<PATypeHolder<Type> >();
842 $$->push_back(*$1); delete $1;
843 }
844 | TypeListI ',' UpRTypes {
845 ($$=$1)->push_back(*$3); delete $3;
846 }
847
848// ArgTypeList - List of types for a method type declaration...
849ArgTypeListI : TypeListI
850 | TypeListI ',' DOTDOTDOT {
851 ($$=$1)->push_back(Type::VoidTy);
852 }
853 | DOTDOTDOT {
854 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
855 }
856 | /*empty*/ {
857 $$ = new list<PATypeHolder<Type> >();
858 }
859
860
Chris Lattnere98dda62001-07-14 06:10:16 +0000861// ConstVal - The various declarations that go into the constant pool. This
862// includes all forward declarations of types, constants, and functions.
863//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000864ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
865 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
866 if (ATy == 0)
867 ThrowException("Cannot make array constant with type: '" +
868 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000869 const Type *ETy = ATy->getElementType();
870 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000871
Chris Lattner30c89792001-09-07 16:35:17 +0000872 // Verify that we have the correct size...
873 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000874 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000875 utostr($3->size()) + " arguments, but has size of " +
876 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000877
Chris Lattner30c89792001-09-07 16:35:17 +0000878 // Verify all elements are correct type!
879 for (unsigned i = 0; i < $3->size(); i++) {
880 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000881 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000882 ETy->getDescription() +"' as required!\nIt is of type '"+
883 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000884 }
885
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000886 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000887 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000888 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000889 | Types '[' ']' {
890 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
891 if (ATy == 0)
892 ThrowException("Cannot make array constant with type: '" +
893 (*$1)->getDescription() + "'!");
894
895 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000896 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000897 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000898 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000899 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000900 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000901 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000902 | Types 'c' STRINGCONSTANT {
903 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
904 if (ATy == 0)
905 ThrowException("Cannot make array constant with type: '" +
906 (*$1)->getDescription() + "'!");
907
Chris Lattner30c89792001-09-07 16:35:17 +0000908 int NumElements = ATy->getNumElements();
909 const Type *ETy = ATy->getElementType();
910 char *EndStr = UnEscapeLexed($3, true);
911 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000912 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000913 itostr((int)(EndStr-$3)) +
914 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000915 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000916 if (ETy == Type::SByteTy) {
917 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000918 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000919 } else if (ETy == Type::UByteTy) {
920 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000921 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000922 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000923 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000924 ThrowException("Cannot build string arrays of non byte sized elements!");
925 }
Chris Lattner30c89792001-09-07 16:35:17 +0000926 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000927 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000928 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000929 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000930 | Types '{' ConstVector '}' {
931 const StructType *STy = dyn_cast<const StructType>($1->get());
932 if (STy == 0)
933 ThrowException("Cannot make struct constant with type: '" +
934 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000935 // FIXME: TODO: Check to see that the constants are compatible with the type
936 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000937 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000938 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000939 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000940 | Types NULL_TOK {
941 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
942 if (PTy == 0)
943 ThrowException("Cannot make null pointer constant with type: '" +
944 (*$1)->getDescription() + "'!");
945
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000946 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000947 delete $1;
948 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000949 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000950 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
951 if (Ty == 0)
952 ThrowException("Global const reference must be a pointer type!");
953
Chris Lattner2079fde2001-10-13 06:41:08 +0000954 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000955
Chris Lattner2079fde2001-10-13 06:41:08 +0000956 // If this is an initializer for a constant pointer, which is referencing a
957 // (currently) undefined variable, create a stub now that shall be replaced
958 // in the future with the right type of variable.
959 //
960 if (V == 0) {
961 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
962 const PointerType *PT = cast<PointerType>(Ty);
963
964 // First check to see if the forward references value is already created!
965 PerModuleInfo::GlobalRefsType::iterator I =
966 CurModule.GlobalRefs.find(make_pair(PT, $2));
967
968 if (I != CurModule.GlobalRefs.end()) {
969 V = I->second; // Placeholder already exists, use it...
970 } else {
971 // TODO: Include line number info by creating a subclass of
972 // TODO: GlobalVariable here that includes the said information!
973
974 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000975 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
976 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000977 // Keep track of the fact that we have a forward ref to recycle it
978 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
979
980 // Must temporarily push this value into the module table...
981 CurModule.CurrentModule->getGlobalList().push_back(GV);
982 V = GV;
983 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000984 }
985
Chris Lattner2079fde2001-10-13 06:41:08 +0000986 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000987 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000988 delete $1; // Free the type handle
Chris Lattner00950542001-06-06 20:29:01 +0000989 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000990
Chris Lattner00950542001-06-06 20:29:01 +0000991
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000992ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000993 if (!ConstantSInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000994 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000995 $$ = ConstantSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000996 }
997 | UIntType EUINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000998 if (!ConstantUInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000999 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001000 $$ = ConstantUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001001 }
1002 | BOOL TRUE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001003 $$ = ConstantBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001004 }
1005 | BOOL FALSE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001006 $$ = ConstantBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001007 }
1008 | FPType FPVAL { // Float & Double constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001009 $$ = ConstantFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001010 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001011
Chris Lattnere98dda62001-07-14 06:10:16 +00001012// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001013ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001014 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001015 }
1016 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001017 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001018 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001019 }
1020
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001021
Chris Lattner1781aca2001-09-18 04:00:54 +00001022// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1023GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
1024
Chris Lattner00950542001-06-06 20:29:01 +00001025
Chris Lattnere98dda62001-07-14 06:10:16 +00001026// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001027ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001028 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001029 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001030 }
Chris Lattner30c89792001-09-07 16:35:17 +00001031 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001032 // Eagerly resolve types. This is not an optimization, this is a
1033 // requirement that is due to the fact that we could have this:
1034 //
1035 // %list = type { %list * }
1036 // %list = type { %list * } ; repeated type decl
1037 //
1038 // If types are not resolved eagerly, then the two types will not be
1039 // determined to be the same type!
1040 //
1041 ResolveTypeTo($2, $4->get());
1042
Chris Lattner1781aca2001-09-18 04:00:54 +00001043 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001044 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1045 // If this is not a redefinition of a type...
1046 if (!$2) {
1047 InsertType($4->get(),
1048 inMethodScope() ? CurMeth.Types : CurModule.Types);
1049 }
Chris Lattner30c89792001-09-07 16:35:17 +00001050 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001051
1052 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001053 }
1054 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001055 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001056 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1057 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001058 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001059 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001060 if (Initializer == 0)
1061 ThrowException("Global value initializer is not a constant!");
1062
Chris Lattnerdda71962001-11-26 18:54:16 +00001063 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001064 if (!setValueName(GV, $2)) { // If not redefining...
1065 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001066 int Slot = InsertValue(GV, CurModule.Values);
1067
1068 if (Slot != -1) {
1069 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1070 } else {
1071 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1072 (char*)GV->getName().c_str()));
1073 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001074 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001075 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001076 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1077 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001078 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001079 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001080 if (!setValueName(GV, $2)) { // If not redefining...
1081 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001082 int Slot = InsertValue(GV, CurModule.Values);
1083
1084 if (Slot != -1) {
1085 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1086 } else {
1087 assert(GV->hasName() && "Not named and not numbered!?");
1088 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1089 (char*)GV->getName().c_str()));
1090 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001091 }
Chris Lattnere98dda62001-07-14 06:10:16 +00001092 }
Chris Lattner00950542001-06-06 20:29:01 +00001093 | /* empty: end of list */ {
1094 }
1095
1096
1097//===----------------------------------------------------------------------===//
1098// Rules to match Modules
1099//===----------------------------------------------------------------------===//
1100
1101// Module rule: Capture the result of parsing the whole file into a result
1102// variable...
1103//
1104Module : MethodList {
1105 $$ = ParserResult = $1;
1106 CurModule.ModuleDone();
1107}
1108
Chris Lattnere98dda62001-07-14 06:10:16 +00001109// MethodList - A list of methods, preceeded by a constant pool.
1110//
Chris Lattner00950542001-06-06 20:29:01 +00001111MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +00001112 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001113 if (!$2->getParent())
1114 $1->getMethodList().push_back($2);
1115 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +00001116 }
Chris Lattnere1815642001-07-15 06:35:53 +00001117 | MethodList MethodProto {
1118 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001119 }
Chris Lattner00950542001-06-06 20:29:01 +00001120 | ConstPool IMPLEMENTATION {
1121 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +00001122 // Resolve circular types before we parse the body of the module
1123 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001124 }
1125
1126
1127//===----------------------------------------------------------------------===//
1128// Rules to match Method Headers
1129//===----------------------------------------------------------------------===//
1130
1131OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1132
1133ArgVal : Types OptVAR_ID {
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001134 $$ = new pair<MethodArgument*,char*>(new MethodArgument(*$1), $2);
1135 delete $1; // Delete the type handle..
Chris Lattner00950542001-06-06 20:29:01 +00001136}
1137
1138ArgListH : ArgVal ',' ArgListH {
1139 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001140 $3->push_front(*$1);
1141 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001142 }
1143 | ArgVal {
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001144 $$ = new list<pair<MethodArgument*,char*> >();
1145 $$->push_front(*$1);
1146 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001147 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001148 | DOTDOTDOT {
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001149 $$ = new list<pair<MethodArgument*, char*> >();
1150 $$->push_front(pair<MethodArgument*,char*>(
1151 new MethodArgument(Type::VoidTy), 0));
Chris Lattner8b81bf52001-07-25 22:47:46 +00001152 }
Chris Lattner00950542001-06-06 20:29:01 +00001153
1154ArgList : ArgListH {
1155 $$ = $1;
1156 }
1157 | /* empty */ {
1158 $$ = 0;
1159 }
1160
Chris Lattnerdda71962001-11-26 18:54:16 +00001161MethodHeaderH : OptInternal TypesV STRINGCONSTANT '(' ArgList ')' {
1162 UnEscapeLexed($3);
1163 string MethodName($3);
1164
Chris Lattner30c89792001-09-07 16:35:17 +00001165 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001166 if ($5)
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001167 for (list<pair<MethodArgument*,char*> >::iterator I = $5->begin();
1168 I != $5->end(); ++I)
1169 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001170
Chris Lattner2079fde2001-10-13 06:41:08 +00001171 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1172 if (isVarArg) ParamTypeList.pop_back();
1173
Chris Lattnerdda71962001-11-26 18:54:16 +00001174 const MethodType *MT = MethodType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001175 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001176 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001177
Chris Lattnere1815642001-07-15 06:35:53 +00001178 Method *M = 0;
1179 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattnerdda71962001-11-26 18:54:16 +00001180 if (Value *V = ST->lookup(PMT, MethodName)) { // Method already in symtab?
Chris Lattneref9c23f2001-10-03 14:53:21 +00001181 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001182
Chris Lattnere1815642001-07-15 06:35:53 +00001183 // Yes it is. If this is the case, either we need to be a forward decl,
1184 // or it needs to be.
1185 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattnerdda71962001-11-26 18:54:16 +00001186 ThrowException("Redefinition of method '" + MethodName + "'!");
Chris Lattnere1815642001-07-15 06:35:53 +00001187 }
1188 }
1189
1190 if (M == 0) { // Not already defined?
Chris Lattnerdda71962001-11-26 18:54:16 +00001191 M = new Method(MT, $1, MethodName);
Chris Lattnere1815642001-07-15 06:35:53 +00001192 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001193 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001194 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001195 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001196
1197 CurMeth.MethodStart(M);
1198
1199 // Add all of the arguments we parsed to the method...
Chris Lattnerdda71962001-11-26 18:54:16 +00001200 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001201 Method::ArgumentListType &ArgList = M->getArgumentList();
1202
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001203 for (list<pair<MethodArgument*, char*> >::iterator I = $5->begin();
1204 I != $5->end(); ++I) {
1205 if (setValueName(I->first, I->second)) { // Insert into symtab...
1206 assert(0 && "No arg redef allowed!");
1207 }
1208
1209 InsertValue(I->first);
1210 ArgList.push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001211 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001212 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001213 } else if ($5) {
1214 // If we are a declaration, we should free the memory for the argument list!
1215 for (list<pair<MethodArgument*, char*> >::iterator I = $5->begin();
1216 I != $5->end(); ++I)
1217 if (I->second) free(I->second); // Free the memory for the name...
1218 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001219 }
1220}
1221
1222MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1223 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001224
1225 // Resolve circular types before we parse the body of the method.
1226 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001227}
1228
1229Method : BasicBlockList END {
1230 $$ = $1;
1231}
1232
Chris Lattnere1815642001-07-15 06:35:53 +00001233MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1234 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001235 if (!$$->getParent())
1236 CurModule.CurrentModule->getMethodList().push_back($$);
1237 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001238}
Chris Lattner00950542001-06-06 20:29:01 +00001239
1240//===----------------------------------------------------------------------===//
1241// Rules to match Basic Blocks
1242//===----------------------------------------------------------------------===//
1243
1244ConstValueRef : ESINT64VAL { // A reference to a direct constant
1245 $$ = ValID::create($1);
1246 }
1247 | EUINT64VAL {
1248 $$ = ValID::create($1);
1249 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001250 | FPVAL { // Perhaps it's an FP constant?
1251 $$ = ValID::create($1);
1252 }
Chris Lattner00950542001-06-06 20:29:01 +00001253 | TRUE {
1254 $$ = ValID::create((int64_t)1);
1255 }
1256 | FALSE {
1257 $$ = ValID::create((int64_t)0);
1258 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001259 | NULL_TOK {
1260 $$ = ValID::createNull();
1261 }
1262
Chris Lattner93750fa2001-07-28 17:48:55 +00001263/*
Chris Lattner00950542001-06-06 20:29:01 +00001264 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1265 $$ = ValID::create_conststr($1);
1266 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001267*/
Chris Lattner00950542001-06-06 20:29:01 +00001268
Chris Lattner2079fde2001-10-13 06:41:08 +00001269// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1270// another value.
1271//
1272SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001273 $$ = ValID::create($1);
1274 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001275 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001276 $$ = ValID::create($1);
1277 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001278
1279// ValueRef - A reference to a definition... either constant or symbolic
1280ValueRef : SymbolicValueRef | ConstValueRef
1281
Chris Lattner00950542001-06-06 20:29:01 +00001282
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001283// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1284// type immediately preceeds the value reference, and allows complex constant
1285// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001286ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001287 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001288 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001289
Chris Lattner00950542001-06-06 20:29:01 +00001290
1291BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner89219832001-10-03 19:35:04 +00001292 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001293 }
1294 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
Chris Lattner89219832001-10-03 19:35:04 +00001295 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001296 }
1297
1298
1299// Basic blocks are terminated by branching instructions:
1300// br, br/cc, switch, ret
1301//
Chris Lattner2079fde2001-10-13 06:41:08 +00001302BasicBlock : InstructionList OptAssign BBTerminatorInst {
1303 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1304 InsertValue($3);
1305
1306 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001307 InsertValue($1);
1308 $$ = $1;
1309 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001310 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1311 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1312 InsertValue($4);
1313
1314 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001315 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001316
1317 InsertValue($2);
1318 $$ = $2;
1319 }
1320
1321InstructionList : InstructionList Inst {
1322 $1->getInstList().push_back($2);
1323 $$ = $1;
1324 }
1325 | /* empty */ {
1326 $$ = new BasicBlock();
1327 }
1328
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001329BBTerminatorInst : RET ResolvedVal { // Return with a result...
1330 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001331 }
1332 | RET VOID { // Return with no result...
1333 $$ = new ReturnInst();
1334 }
1335 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001336 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001337 } // Conditional Branch...
1338 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001339 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1340 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001341 getVal(Type::BoolTy, $3));
1342 }
1343 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1344 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001345 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001346 $$ = S;
1347
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001348 list<pair<Constant*, BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner00950542001-06-06 20:29:01 +00001349 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001350 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001351 S->dest_push_back(I->first, I->second);
1352 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001353 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1354 EXCEPT ResolvedVal {
1355 const PointerType *PMTy;
1356 const MethodType *Ty;
1357
1358 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner7a176752001-12-04 00:03:30 +00001359 !(Ty = dyn_cast<MethodType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001360 // Pull out the types of all of the arguments...
1361 vector<const Type*> ParamTypes;
1362 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001363 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001364 ParamTypes.push_back((*I)->getType());
1365 }
1366
1367 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1368 if (isVarArg) ParamTypes.pop_back();
1369
1370 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
1371 PMTy = PointerType::get(Ty);
1372 }
1373 delete $2;
1374
1375 Value *V = getVal(PMTy, $3); // Get the method we're calling...
1376
1377 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1378 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1379
1380 if (Normal == 0 || Except == 0)
1381 ThrowException("Invoke instruction without label destinations!");
1382
1383 // Create the call node...
1384 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001385 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001386 } else { // Has arguments?
1387 // Loop through MethodType's arguments and ensure they are specified
1388 // correctly!
1389 //
1390 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1391 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001392 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001393
1394 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1395 if ((*ArgI)->getType() != *I)
1396 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001397 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001398
1399 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1400 ThrowException("Invalid number of parameters detected!");
1401
Chris Lattner6cdb0112001-11-26 16:54:11 +00001402 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001403 }
1404 delete $5;
1405 }
1406
1407
Chris Lattner00950542001-06-06 20:29:01 +00001408
1409JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1410 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001411 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001412 if (V == 0)
1413 ThrowException("May only switch on a constant pool value!");
1414
Chris Lattner9636a912001-10-01 16:18:37 +00001415 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001416 }
1417 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001418 $$ = new list<pair<Constant*, BasicBlock*> >();
1419 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001420
1421 if (V == 0)
1422 ThrowException("May only switch on a constant pool value!");
1423
Chris Lattner9636a912001-10-01 16:18:37 +00001424 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001425 }
1426
1427Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001428 // Is this definition named?? if so, assign the name...
1429 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001430 InsertValue($2);
1431 $$ = $2;
1432}
1433
Chris Lattnerc24d2082001-06-11 15:04:20 +00001434PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1435 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001436 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001437 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001438 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001439 }
1440 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1441 $$ = $1;
1442 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001443 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001444 }
1445
1446
Chris Lattner30c89792001-09-07 16:35:17 +00001447ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001448 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001449 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001450 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001451 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001452 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001453 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001454 }
1455
1456// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1457ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1458
1459InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001460 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001461 if ($$ == 0)
1462 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001463 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001464 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001465 | UnaryOps ResolvedVal {
1466 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001467 if ($$ == 0)
1468 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001469 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001470 | ShiftOps ResolvedVal ',' ResolvedVal {
1471 if ($4->getType() != Type::UByteTy)
1472 ThrowException("Shift amount must be ubyte!");
1473 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001474 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001475 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001476 $$ = new CastInst($2, *$4);
1477 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001478 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001479 | PHI PHIList {
1480 const Type *Ty = $2->front().first->getType();
1481 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001482 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001483 if ($2->front().first->getType() != Ty)
1484 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001485 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001486 $2->pop_front();
1487 }
1488 delete $2; // Free the list...
1489 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001490 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001491 const PointerType *PMTy;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001492 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001493
Chris Lattneref9c23f2001-10-03 14:53:21 +00001494 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner7a176752001-12-04 00:03:30 +00001495 !(Ty = dyn_cast<MethodType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001496 // Pull out the types of all of the arguments...
1497 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001498 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001499 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001500 ParamTypes.push_back((*I)->getType());
1501 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001502
1503 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1504 if (isVarArg) ParamTypes.pop_back();
1505
1506 Ty = MethodType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001507 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001508 }
Chris Lattner30c89792001-09-07 16:35:17 +00001509 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001510
Chris Lattneref9c23f2001-10-03 14:53:21 +00001511 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001512
Chris Lattner8b81bf52001-07-25 22:47:46 +00001513 // Create the call node...
1514 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001515 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001516 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001517 // Loop through MethodType's arguments and ensure they are specified
1518 // correctly!
1519 //
1520 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001521 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001522 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001523
1524 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1525 if ((*ArgI)->getType() != *I)
1526 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001527 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001528
Chris Lattner8b81bf52001-07-25 22:47:46 +00001529 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001530 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001531
Chris Lattner6cdb0112001-11-26 16:54:11 +00001532 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001533 }
1534 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001535 }
1536 | MemoryInst {
1537 $$ = $1;
1538 }
1539
Chris Lattner6cdb0112001-11-26 16:54:11 +00001540
1541// IndexList - List of indices for GEP based instructions...
1542IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001543 $$ = $2;
1544} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001545 $$ = new vector<Value*>();
Chris Lattner027dcc52001-07-08 21:10:27 +00001546}
1547
Chris Lattner00950542001-06-06 20:29:01 +00001548MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001549 $$ = new MallocInst(PointerType::get(*$2));
1550 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001551 }
1552 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001553 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001554 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001555 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001556 }
1557 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001558 $$ = new AllocaInst(PointerType::get(*$2));
1559 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001560 }
1561 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001562 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001563 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001564 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001565 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001566 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001567 | FREE ResolvedVal {
1568 if (!$2->getType()->isPointerType())
1569 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001570 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001571 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001572 }
1573
Chris Lattner6cdb0112001-11-26 16:54:11 +00001574 | LOAD Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001575 if (!(*$2)->isPointerType())
Chris Lattner2079fde2001-10-13 06:41:08 +00001576 ThrowException("Can't load from nonpointer type: " +
1577 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001578 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001579 ThrowException("Invalid indices for load instruction!");
1580
Chris Lattner30c89792001-09-07 16:35:17 +00001581 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001582 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001583 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001584 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001585 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001586 if (!(*$4)->isPointerType())
Chris Lattner72e00252001-12-14 16:28:42 +00001587 ThrowException("Can't store to a nonpointer type: " +
1588 (*$4)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001589 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001590 if (ElTy == 0)
1591 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001592 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001593 ThrowException("Can't store '" + $2->getType()->getDescription() +
1594 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001595 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1596 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001597 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001598 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001599 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001600 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001601 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001602 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001603 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1604 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001605 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001606
Chris Lattner00950542001-06-06 20:29:01 +00001607%%
Chris Lattner09083092001-07-08 04:57:15 +00001608int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001609 ThrowException(string("Parse error: ") + ErrorMsg);
1610 return 0;
1611}