blob: 7a32813fc174cf3734f4b5f95629cb8a92d86ab3 [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
7//
8// TODO: Parse comments and add them to an internal node... so that they may
9// be saved in the bytecode format as well as everything else. Very important
10// for a general IR format.
11//
12
13%{
14#include "ParserInternals.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000015#include "llvm/Assembly/Parser.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000018#include "llvm/GlobalVariable.h"
19#include "llvm/Method.h"
20#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000021#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include "llvm/iTerminators.h"
23#include "llvm/iMemory.h"
Chris Lattner30c89792001-09-07 16:35:17 +000024#include "llvm/Support/STLExtras.h"
Chris Lattner3ff43872001-09-28 22:56:31 +000025#include "llvm/Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#include <list>
27#include <utility> // Get definition of pair class
Chris Lattner30c89792001-09-07 16:35:17 +000028#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000029#include <stdio.h> // This embarasment is due to our flex lexer...
30
Chris Lattner09083092001-07-08 04:57:15 +000031int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
32int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000033int yyparse();
34
35static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000036string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
43#define UR_OUT(X) cerr << X
44#else
45#define UR_OUT(X)
46#endif
47
Chris Lattner00950542001-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
52static void ResolveDefinitions(vector<ValueList> &LateResolvers);
Chris Lattner30c89792001-09-07 16:35:17 +000053static void ResolveTypes (vector<PATypeHolder<Type> > &LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +000054
55static struct PerModuleInfo {
56 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000057 vector<ValueList> Values; // Module level numbered definitions
58 vector<ValueList> LateResolveValues;
59 vector<PATypeHolder<Type> > Types, LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000060
61 void ModuleDone() {
Chris Lattner30c89792001-09-07 16:35:17 +000062 // If we could not resolve some methods at method compilation time (calls to
63 // methods before they are defined), resolve them now... Types are resolved
64 // when the constant pool has been completely parsed.
65 //
Chris Lattner00950542001-06-06 20:29:01 +000066 ResolveDefinitions(LateResolveValues);
67
68 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000069 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000070 CurrentModule = 0;
71 }
72} CurModule;
73
74static struct PerMethodInfo {
75 Method *CurrentMethod; // Pointer to current method being created
76
Chris Lattnere1815642001-07-15 06:35:53 +000077 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +000078 vector<ValueList> LateResolveValues;
Chris Lattner30c89792001-09-07 16:35:17 +000079 vector<PATypeHolder<Type> > Types, LateResolveTypes;
Chris Lattnere1815642001-07-15 06:35:53 +000080 bool isDeclare; // Is this method a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +000081
82 inline PerMethodInfo() {
83 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +000084 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +000085 }
86
87 inline ~PerMethodInfo() {}
88
89 inline void MethodStart(Method *M) {
90 CurrentMethod = M;
91 }
92
93 void MethodDone() {
94 // If we could not resolve some blocks at parsing time (forward branches)
95 // resolve the branches now...
96 ResolveDefinitions(LateResolveValues);
97
98 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000099 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000100 CurrentMethod = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000101 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000102 }
103} CurMeth; // Info for the current method...
104
105
106//===----------------------------------------------------------------------===//
107// Code to handle definitions of all the types
108//===----------------------------------------------------------------------===//
109
Chris Lattner93750fa2001-07-28 17:48:55 +0000110static void InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values){
Chris Lattner00950542001-06-06 20:29:01 +0000111 if (!D->hasName()) { // Is this a numbered definition?
112 unsigned type = D->getType()->getUniqueID();
113 if (ValueTab.size() <= type)
114 ValueTab.resize(type+1, ValueList());
115 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
116 ValueTab[type].push_back(D);
117 }
118}
119
Chris Lattner30c89792001-09-07 16:35:17 +0000120// TODO: FIXME when Type are not const
121static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
122 Types.push_back(Ty);
123}
124
125static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000126 switch (D.Type) {
127 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000128 unsigned Num = (unsigned)D.Num;
129
130 // Module constants occupy the lowest numbered slots...
131 if (Num < CurModule.Types.size())
132 return CurModule.Types[Num];
133
134 Num -= CurModule.Types.size();
135
136 // Check that the number is within bounds...
137 if (Num <= CurMeth.Types.size())
138 return CurMeth.Types[Num];
139 }
140 case 1: { // Is it a named definition?
141 string Name(D.Name);
142 SymbolTable *SymTab = 0;
143 if (CurMeth.CurrentMethod)
144 SymTab = CurMeth.CurrentMethod->getSymbolTable();
145 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
146
147 if (N == 0) {
148 // Symbol table doesn't automatically chain yet... because the method
149 // hasn't been added to the module...
150 //
151 SymTab = CurModule.CurrentModule->getSymbolTable();
152 if (SymTab)
153 N = SymTab->lookup(Type::TypeTy, Name);
154 if (N == 0) break;
155 }
156
157 D.destroy(); // Free old strdup'd memory...
158 return N->castTypeAsserting();
159 }
160 default:
161 ThrowException("Invalid symbol type reference!");
162 }
163
164 // If we reached here, we referenced either a symbol that we don't know about
165 // or an id number that hasn't been read yet. We may be referencing something
166 // forward, so just create an entry to be resolved later and get to it...
167 //
168 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
169
170 vector<PATypeHolder<Type> > *LateResolver = CurMeth.CurrentMethod ?
171 &CurMeth.LateResolveTypes : &CurModule.LateResolveTypes;
172
173 Type *Typ = new TypePlaceHolder(Type::TypeTy, D);
174 InsertType(Typ, *LateResolver);
175 return Typ;
176}
177
178static Value *getVal(const Type *Ty, const ValID &D,
179 bool DoNotImprovise = false) {
180 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
181
182 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000183 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000184 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000185 unsigned Num = (unsigned)D.Num;
186
187 // Module constants occupy the lowest numbered slots...
188 if (type < CurModule.Values.size()) {
189 if (Num < CurModule.Values[type].size())
190 return CurModule.Values[type][Num];
191
192 Num -= CurModule.Values[type].size();
193 }
194
195 // Make sure that our type is within bounds
196 if (CurMeth.Values.size() <= type)
197 break;
198
199 // Check that the number is within bounds...
200 if (CurMeth.Values[type].size() <= Num)
201 break;
202
203 return CurMeth.Values[type][Num];
204 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000205 case ValID::NameVal: { // Is it a named definition?
Chris Lattner00950542001-06-06 20:29:01 +0000206 string Name(D.Name);
207 SymbolTable *SymTab = 0;
208 if (CurMeth.CurrentMethod)
209 SymTab = CurMeth.CurrentMethod->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000210 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
Chris Lattner00950542001-06-06 20:29:01 +0000211
212 if (N == 0) {
Chris Lattner30c89792001-09-07 16:35:17 +0000213 // Symbol table doesn't automatically chain yet... because the method
214 // hasn't been added to the module...
215 //
Chris Lattner00950542001-06-06 20:29:01 +0000216 SymTab = CurModule.CurrentModule->getSymbolTable();
217 if (SymTab)
Chris Lattner30c89792001-09-07 16:35:17 +0000218 N = SymTab->lookup(Ty, Name);
Chris Lattner00950542001-06-06 20:29:01 +0000219 if (N == 0) break;
220 }
221
222 D.destroy(); // Free old strdup'd memory...
223 return N;
224 }
225
Chris Lattner1a1cb112001-09-30 22:46:54 +0000226 case ValID::ConstSIntVal: // Is it a constant pool reference??
227 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
228 case ValID::ConstStringVal: // Is it a string const pool reference?
229 case ValID::ConstFPVal: // Is it a floating point const pool reference?
230 case ValID::ConstNullVal: { // Is it a null value?
Chris Lattner00950542001-06-06 20:29:01 +0000231 ConstPoolVal *CPV = 0;
232
Chris Lattner30c89792001-09-07 16:35:17 +0000233 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner00950542001-06-06 20:29:01 +0000234 // value will fit into the specified type...
235 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000236 case ValID::ConstSIntVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000237 if (Ty == Type::BoolTy) { // Special handling for boolean data
238 CPV = ConstPoolBool::get(D.ConstPool64 != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000239 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000240 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000241 ThrowException("Symbolic constant pool value '" +
242 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000243 Ty->getName() + "'!");
244 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000245 }
246 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000247 case ValID::ConstUIntVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000248 if (!ConstPoolUInt::isValueValidForType(Ty, D.UConstPool64)) {
249 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000250 ThrowException("Integral constant pool reference is invalid!");
Chris Lattner00950542001-06-06 20:29:01 +0000251 } else { // This is really a signed reference. Transmogrify.
Chris Lattner30c89792001-09-07 16:35:17 +0000252 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000253 }
254 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000255 CPV = ConstPoolUInt::get(Ty, D.UConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000256 }
257 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000258 case ValID::ConstStringVal:
Chris Lattner00950542001-06-06 20:29:01 +0000259 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
260 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000261 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000262 case ValID::ConstFPVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000263 if (!ConstPoolFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000264 ThrowException("FP constant invalid for type!!");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000265 CPV = ConstPoolFP::get(Ty, D.ConstPoolFP);
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000266 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000267 case ValID::ConstNullVal:
268 if (!Ty->isPointerType())
269 ThrowException("Cannot create a a non pointer null!");
270 CPV = ConstPoolPointer::getNullPointer(Ty->castPointerType());
271 break;
272 default:
273 assert(0 && "Unhandled case!");
Chris Lattner00950542001-06-06 20:29:01 +0000274 }
275 assert(CPV && "How did we escape creating a constant??");
Chris Lattner00950542001-06-06 20:29:01 +0000276 return CPV;
277 } // End of case 2,3,4
Chris Lattner30c89792001-09-07 16:35:17 +0000278 default:
279 assert(0 && "Unhandled case!");
Chris Lattner00950542001-06-06 20:29:01 +0000280 } // End of switch
281
282
283 // If we reached here, we referenced either a symbol that we don't know about
284 // or an id number that hasn't been read yet. We may be referencing something
285 // forward, so just create an entry to be resolved later and get to it...
286 //
287 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
288
Chris Lattner00950542001-06-06 20:29:01 +0000289 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000290 vector<ValueList> *LateResolver = (CurMeth.CurrentMethod) ?
291 &CurMeth.LateResolveValues : &CurModule.LateResolveValues;
Chris Lattner93750fa2001-07-28 17:48:55 +0000292
Chris Lattner30c89792001-09-07 16:35:17 +0000293 switch (Ty->getPrimitiveID()) {
294 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
295 case Type::MethodTyID: d = new MethPlaceHolder(Ty, D);
Chris Lattner93750fa2001-07-28 17:48:55 +0000296 LateResolver = &CurModule.LateResolveValues; break;
Chris Lattner30c89792001-09-07 16:35:17 +0000297 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000298 }
299
300 assert(d != 0 && "How did we not make something?");
Chris Lattner93750fa2001-07-28 17:48:55 +0000301 InsertValue(d, *LateResolver);
Chris Lattner00950542001-06-06 20:29:01 +0000302 return d;
303}
304
305
306//===----------------------------------------------------------------------===//
307// Code to handle forward references in instructions
308//===----------------------------------------------------------------------===//
309//
310// This code handles the late binding needed with statements that reference
311// values not defined yet... for example, a forward branch, or the PHI node for
312// a loop body.
313//
314// This keeps a table (CurMeth.LateResolveValues) of all such forward references
315// and back patchs after we are done.
316//
317
318// ResolveDefinitions - If we could not resolve some defs at parsing
319// time (forward branches, phi functions for loops, etc...) resolve the
320// defs now...
321//
322static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
323 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
324 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
325 while (!LateResolvers[ty].empty()) {
326 Value *V = LateResolvers[ty].back();
327 LateResolvers[ty].pop_back();
328 ValID &DID = getValIDFromPlaceHolder(V);
329
330 Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true);
331
Chris Lattner30c89792001-09-07 16:35:17 +0000332 if (TheRealValue == 0) {
333 if (DID.Type == 1)
334 ThrowException("Reference to an invalid definition: '" +DID.getName()+
335 "' of type '" + V->getType()->getDescription() + "'",
336 getLineNumFromPlaceHolder(V));
337 else
338 ThrowException("Reference to an invalid definition: #" +
339 itostr(DID.Num) + " of type '" +
340 V->getType()->getDescription() + "'",
341 getLineNumFromPlaceHolder(V));
342 }
343
344 assert(!V->isType() && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000345
346 V->replaceAllUsesWith(TheRealValue);
Chris Lattner00950542001-06-06 20:29:01 +0000347 delete V;
348 }
349 }
350
351 LateResolvers.clear();
352}
353
Chris Lattner30c89792001-09-07 16:35:17 +0000354
355// ResolveTypes - This goes through the forward referenced type table and makes
356// sure that all type references are complete. This code is executed after the
357// constant pool of a method or module is completely parsed.
Chris Lattner00950542001-06-06 20:29:01 +0000358//
Chris Lattner30c89792001-09-07 16:35:17 +0000359static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
360 while (!LateResolveTypes.empty()) {
361 const Type *Ty = LateResolveTypes.back();
362 ValID &DID = getValIDFromPlaceHolder(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000363
Chris Lattner30c89792001-09-07 16:35:17 +0000364 const Type *TheRealType = getTypeVal(DID, true);
365 if (TheRealType == 0) {
366 if (DID.Type == 1)
367 ThrowException("Reference to an invalid type: '" +DID.getName(),
368 getLineNumFromPlaceHolder(Ty));
369 else
370 ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
371 getLineNumFromPlaceHolder(Ty));
Chris Lattner00950542001-06-06 20:29:01 +0000372 }
Chris Lattner30c89792001-09-07 16:35:17 +0000373
374 // FIXME: When types are not const
375 DerivedType *DTy = const_cast<DerivedType*>(Ty->castDerivedTypeAsserting());
376
377 // Refine the opaque type we had to the new type we are getting.
378 DTy->refineAbstractTypeTo(TheRealType);
379
380 // No need to delete type, refine does that for us.
381 LateResolveTypes.pop_back();
382 }
383}
384
Chris Lattner1781aca2001-09-18 04:00:54 +0000385// setValueName - Set the specified value to the name given. The name may be
386// null potentially, in which case this is a noop. The string passed in is
387// assumed to be a malloc'd string buffer, and is freed by this function.
388//
389static void setValueName(Value *V, char *NameStr) {
390 if (NameStr == 0) return;
391 string Name(NameStr); // Copy string
392 free(NameStr); // Free old string
393
Chris Lattner30c89792001-09-07 16:35:17 +0000394 SymbolTable *ST = CurMeth.CurrentMethod ?
395 CurMeth.CurrentMethod->getSymbolTableSure() :
396 CurModule.CurrentModule->getSymbolTableSure();
397
398 Value *Existing = ST->lookup(V->getType(), Name);
399 if (Existing) { // Inserting a name that is already defined???
400 // There is only one case where this is allowed: when we are refining an
401 // opaque type. In this case, Existing will be an opaque type.
402 if (const Type *Ty = Existing->castType())
403 if (Ty->isOpaqueType()) {
404 // We ARE replacing an opaque type!
405
406 // TODO: FIXME when types are not const!
407 const_cast<DerivedType*>(Ty->castDerivedTypeAsserting())->refineAbstractTypeTo(V->castTypeAsserting());
408 return;
409 }
410
Chris Lattner9636a912001-10-01 16:18:37 +0000411 // Otherwise, we are a simple redefinition of a value, check to see if it
412 // is defined the same as the old one...
413 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
414 if (Ty == cast<const Type>(V)) return; // Yes, it's equal.
415 } else {
416
417 }
Chris Lattner30c89792001-09-07 16:35:17 +0000418 ThrowException("Redefinition of value name '" + Name + "' in the '" +
419 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000420 }
Chris Lattner00950542001-06-06 20:29:01 +0000421
Chris Lattner30c89792001-09-07 16:35:17 +0000422 V->setName(Name, ST);
Chris Lattner00950542001-06-06 20:29:01 +0000423}
424
Chris Lattner8896eda2001-07-09 19:38:36 +0000425
Chris Lattner30c89792001-09-07 16:35:17 +0000426//===----------------------------------------------------------------------===//
427// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000428//
Chris Lattner8896eda2001-07-09 19:38:36 +0000429
Chris Lattner30c89792001-09-07 16:35:17 +0000430// TypeContains - Returns true if Ty contains E in it.
431//
432static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000433 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000434}
Chris Lattner698b56e2001-07-20 19:15:08 +0000435
Chris Lattner30c89792001-09-07 16:35:17 +0000436
437static vector<pair<unsigned, OpaqueType *> > UpRefs;
438
439static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
440 PATypeHolder<Type> Ty(ty);
441 UR_OUT(UpRefs.size() << " upreferences active!\n");
442 for (unsigned i = 0; i < UpRefs.size(); ) {
443 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
444 << UpRefs[i].second->getDescription() << ") = "
445 << TypeContains(Ty, UpRefs[i].second) << endl);
446 if (TypeContains(Ty, UpRefs[i].second)) {
447 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
448 UR_OUT("Uplevel Ref Level = " << Level << endl);
449 if (Level == 0) { // Upreference should be resolved!
450 UR_OUT("About to resolve upreference!\n";
451 string OldName = UpRefs[i].second->getDescription());
452 UpRefs[i].second->refineAbstractTypeTo(Ty);
453 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
454 UR_OUT("Type '" << OldName << "' refined upreference to: "
455 << (const void*)Ty << ", " << Ty->getDescription() << endl);
456 continue;
457 }
458 }
459
460 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000461 }
Chris Lattner30c89792001-09-07 16:35:17 +0000462 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000463 return Ty;
464}
465
Chris Lattner30c89792001-09-07 16:35:17 +0000466template <class TypeTy>
467inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
468 if (UpRefs.size())
469 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
470}
471
472// newTH - Allocate a new type holder for the specified type
473template <class TypeTy>
474inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
475 return new PATypeHolder<TypeTy>(Ty);
476}
477template <class TypeTy>
478inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
479 return new PATypeHolder<TypeTy>(TH);
480}
481
482
483// newTHC - Allocate a new type holder for the specified type that can be
484// casted to a new Type type.
485template <class TypeTy, class OldTy>
486inline static PATypeHolder<TypeTy> *newTHC(const PATypeHolder<OldTy> &Old) {
487 return new PATypeHolder<TypeTy>((const TypeTy*)Old.get());
488}
489
Chris Lattner8896eda2001-07-09 19:38:36 +0000490
Chris Lattner00950542001-06-06 20:29:01 +0000491//===----------------------------------------------------------------------===//
492// RunVMAsmParser - Define an interface to this parser
493//===----------------------------------------------------------------------===//
494//
Chris Lattnera2850432001-07-22 18:36:00 +0000495Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000496 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000497 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000498 llvmAsmlineno = 1; // Reset the current line number...
499
500 CurModule.CurrentModule = new Module(); // Allocate a new module to read
501 yyparse(); // Parse the file.
502 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000503 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
504 ParserResult = 0;
505
506 return Result;
507}
508
509%}
510
511%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000512 Module *ModuleVal;
513 Method *MethodVal;
514 MethodArgument *MethArgVal;
515 BasicBlock *BasicBlockVal;
516 TerminatorInst *TermInstVal;
517 Instruction *InstVal;
518 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000519
Chris Lattner30c89792001-09-07 16:35:17 +0000520 const Type *PrimType;
521 PATypeHolder<Type> *TypeVal;
522 PATypeHolder<ArrayType> *ArrayTypeTy;
523 PATypeHolder<StructType> *StructTypeTy;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000524 PATypeHolder<PointerType> *PointerTypeTy;
Chris Lattner30c89792001-09-07 16:35:17 +0000525 Value *ValueVal;
526
527 list<MethodArgument*> *MethodArgList;
528 list<Value*> *ValueList;
529 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000530 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000531 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000532 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000533
Chris Lattner30c89792001-09-07 16:35:17 +0000534 int64_t SInt64Val;
535 uint64_t UInt64Val;
536 int SIntVal;
537 unsigned UIntVal;
538 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000539 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000540
Chris Lattner30c89792001-09-07 16:35:17 +0000541 char *StrVal; // This memory is strdup'd!
542 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000543
Chris Lattner30c89792001-09-07 16:35:17 +0000544 Instruction::UnaryOps UnaryOpVal;
545 Instruction::BinaryOps BinaryOpVal;
546 Instruction::TermOps TermOpVal;
547 Instruction::MemoryOps MemOpVal;
548 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000549}
550
551%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000552%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000553%type <BasicBlockVal> BasicBlock InstructionList
554%type <TermInstVal> BBTerminatorInst
555%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000556%type <ConstVal> ConstVal ExtendedConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000557%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000558%type <MethodArgList> ArgList ArgListH
559%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000560%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000561%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000562%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000563%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000564%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000565
566%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000567%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000568// Tokens and types for handling constant integer values
569//
570// ESINT64VAL - A negative number within long long range
571%token <SInt64Val> ESINT64VAL
572
573// EUINT64VAL - A positive number within uns. long long range
574%token <UInt64Val> EUINT64VAL
575%type <SInt64Val> EINT64VAL
576
577%token <SIntVal> SINTVAL // Signed 32 bit ints...
578%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
579%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000580%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000581
582// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000583%type <TypeVal> Types TypesV UpRTypes UpRTypesV
584%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
585%token <TypeVal> OPAQUE
586%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
587%token <PrimType> FLOAT DOUBLE TYPE LABEL
588%type <ArrayTypeTy> ArrayType ArrayTypeI
589%type <StructTypeTy> StructType StructTypeI
Chris Lattner1a1cb112001-09-30 22:46:54 +0000590%type <PointerTypeTy> PointerType PointerTypeI
Chris Lattner00950542001-06-06 20:29:01 +0000591
592%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
593%type <StrVal> OptVAR_ID OptAssign
594
595
Chris Lattner1781aca2001-09-18 04:00:54 +0000596%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattner1a1cb112001-09-30 22:46:54 +0000597%token TO DOTDOTDOT STRING NULL_TOK
Chris Lattner00950542001-06-06 20:29:01 +0000598
599// Basic Block Terminating Operators
600%token <TermOpVal> RET BR SWITCH
601
602// Unary Operators
603%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000604%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000605
606// Binary Operators
607%type <BinaryOpVal> BinaryOps // all the binary operators
608%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000609%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000610
611// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000612%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000613
Chris Lattner027dcc52001-07-08 21:10:27 +0000614// Other Operators
615%type <OtherOpVal> ShiftOps
616%token <OtherOpVal> PHI CALL CAST SHL SHR
617
Chris Lattner00950542001-06-06 20:29:01 +0000618%start Module
619%%
620
621// Handle constant integer size restriction and conversion...
622//
623
624INTVAL : SINTVAL
625INTVAL : UINTVAL {
626 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
627 ThrowException("Value too large for type!");
628 $$ = (int32_t)$1;
629}
630
631
632EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
633EINT64VAL : EUINT64VAL {
634 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
635 ThrowException("Value too large for type!");
636 $$ = (int64_t)$1;
637}
638
Chris Lattner00950542001-06-06 20:29:01 +0000639// Operations that are notably excluded from this list include:
640// RET, BR, & SWITCH because they end basic blocks and are treated specially.
641//
Chris Lattner09083092001-07-08 04:57:15 +0000642UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000643BinaryOps : ADD | SUB | MUL | DIV | REM
644BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000645ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000646
Chris Lattnere98dda62001-07-14 06:10:16 +0000647// These are some types that allow classification if we only want a particular
648// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000649SIntType : LONG | INT | SHORT | SBYTE
650UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000651IntType : SIntType | UIntType
652FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000653
Chris Lattnere98dda62001-07-14 06:10:16 +0000654// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000655OptAssign : VAR_ID '=' {
656 $$ = $1;
657 }
658 | /*empty*/ {
659 $$ = 0;
660 }
661
Chris Lattner30c89792001-09-07 16:35:17 +0000662
663//===----------------------------------------------------------------------===//
664// Types includes all predefined types... except void, because it can only be
665// used in specific contexts (method returning void for example). To have
666// access to it, a user must explicitly use TypesV.
667//
668
669// TypesV includes all of 'Types', but it also includes the void type.
670TypesV : Types | VOID { $$ = newTH($1); }
671UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
672
673Types : UpRTypes {
674 TypeDone($$ = $1);
675 }
676
677
678// Derived types are added later...
679//
680PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
681PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
682UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
683UpRTypes : ValueRef { // Named types are also simple types...
684 $$ = newTH(getTypeVal($1));
685}
686
687// ArrayTypeI - Internal version of ArrayType that can have incomplete uprefs
688//
689ArrayTypeI : '[' UpRTypesV ']' { // Unsized array type?
690 $$ = newTHC<ArrayType>(HandleUpRefs(ArrayType::get(*$2)));
691 delete $2;
692 }
693 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
694 $$ = newTHC<ArrayType>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
695 delete $4;
696 }
697
698StructTypeI : '{' TypeListI '}' { // Structure type?
699 vector<const Type*> Elements;
700 mapto($2->begin(), $2->end(), back_inserter(Elements),
701 mem_fun_ref(&PATypeHandle<Type>::get));
702
703 $$ = newTHC<StructType>(HandleUpRefs(StructType::get(Elements)));
704 delete $2;
705 }
706 | '{' '}' { // Empty structure type?
707 $$ = newTH(StructType::get(vector<const Type*>()));
708 }
709
Chris Lattner1a1cb112001-09-30 22:46:54 +0000710PointerTypeI : UpRTypes '*' { // Pointer type?
711 $$ = newTHC<PointerType>(HandleUpRefs(PointerType::get(*$1)));
712 delete $1; // Delete the type handle
713 }
Chris Lattner30c89792001-09-07 16:35:17 +0000714
715// Include derived types in the Types production.
716//
717UpRTypes : '\\' EUINT64VAL { // Type UpReference
718 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
719 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
720 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
721 $$ = newTH<Type>(OT);
722 UR_OUT("New Upreference!\n");
723 }
724 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
725 vector<const Type*> Params;
726 mapto($3->begin(), $3->end(), back_inserter(Params),
727 mem_fun_ref(&PATypeHandle<Type>::get));
728 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params)));
729 delete $3; // Delete the argument list
730 delete $1; // Delete the old type handle
731 }
732 | ArrayTypeI { // [Un]sized array type?
733 $$ = newTHC<Type>(*$1); delete $1;
734 }
735 | StructTypeI { // Structure type?
736 $$ = newTHC<Type>(*$1); delete $1;
737 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000738 | PointerTypeI { // Pointer type?
739 $$ = newTHC<Type>(*$1); delete $1;
Chris Lattner30c89792001-09-07 16:35:17 +0000740 }
741
742// Define some helpful top level types that do not allow UpReferences to escape
743//
Chris Lattner1a1cb112001-09-30 22:46:54 +0000744ArrayType : ArrayTypeI { TypeDone($$ = $1); }
745StructType : StructTypeI { TypeDone($$ = $1); }
746PointerType : PointerTypeI { TypeDone($$ = $1); }
Chris Lattner30c89792001-09-07 16:35:17 +0000747
748
749// TypeList - Used for struct declarations and as a basis for method type
750// declaration type lists
751//
752TypeListI : UpRTypes {
753 $$ = new list<PATypeHolder<Type> >();
754 $$->push_back(*$1); delete $1;
755 }
756 | TypeListI ',' UpRTypes {
757 ($$=$1)->push_back(*$3); delete $3;
758 }
759
760// ArgTypeList - List of types for a method type declaration...
761ArgTypeListI : TypeListI
762 | TypeListI ',' DOTDOTDOT {
763 ($$=$1)->push_back(Type::VoidTy);
764 }
765 | DOTDOTDOT {
766 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
767 }
768 | /*empty*/ {
769 $$ = new list<PATypeHolder<Type> >();
770 }
771
772
Chris Lattnere98dda62001-07-14 06:10:16 +0000773// ConstVal - The various declarations that go into the constant pool. This
774// includes all forward declarations of types, constants, and functions.
775//
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000776// This is broken into two sections: ExtendedConstVal and ConstVal
777//
Chris Lattner30c89792001-09-07 16:35:17 +0000778ExtendedConstVal: ArrayType '[' ConstVector ']' { // Nonempty unsized arr
779 const ArrayType *ATy = *$1;
780 const Type *ETy = ATy->getElementType();
781 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000782
Chris Lattner30c89792001-09-07 16:35:17 +0000783 // Verify that we have the correct size...
784 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000785 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000786 utostr($3->size()) + " arguments, but has size of " +
787 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000788
Chris Lattner30c89792001-09-07 16:35:17 +0000789 // Verify all elements are correct type!
790 for (unsigned i = 0; i < $3->size(); i++) {
791 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000792 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000793 ETy->getName() + "' as required!\nIt is of type '" +
794 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000795 }
796
Chris Lattner30c89792001-09-07 16:35:17 +0000797 $$ = ConstPoolArray::get(ATy, *$3);
798 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000799 }
Chris Lattner30c89792001-09-07 16:35:17 +0000800 | ArrayType '[' ']' {
801 int NumElements = (*$1)->getNumElements();
802 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000803 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000804 " arguments, but has size of " + itostr(NumElements) +"!");
805 $$ = ConstPoolArray::get((*$1), vector<ConstPoolVal*>());
806 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000807 }
Chris Lattner30c89792001-09-07 16:35:17 +0000808 | ArrayType 'c' STRINGCONSTANT {
809 const ArrayType *ATy = *$1;
810 int NumElements = ATy->getNumElements();
811 const Type *ETy = ATy->getElementType();
812 char *EndStr = UnEscapeLexed($3, true);
813 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000814 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000815 itostr((int)(EndStr-$3)) +
816 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000817 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000818 if (ETy == Type::SByteTy) {
819 for (char *C = $3; C != EndStr; ++C)
820 Vals.push_back(ConstPoolSInt::get(ETy, *C));
821 } else if (ETy == Type::UByteTy) {
822 for (char *C = $3; C != EndStr; ++C)
823 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000824 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000825 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000826 ThrowException("Cannot build string arrays of non byte sized elements!");
827 }
Chris Lattner30c89792001-09-07 16:35:17 +0000828 free($3);
829 $$ = ConstPoolArray::get(ATy, Vals);
830 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000831 }
Chris Lattner30c89792001-09-07 16:35:17 +0000832 | StructType '{' ConstVector '}' {
833 // FIXME: TODO: Check to see that the constants are compatible with the type
834 // initializer!
835 $$ = ConstPoolStruct::get(*$1, *$3);
836 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000837 }
838/*
839 | Types '*' ConstVal {
840 assert(0);
841 $$ = 0;
842 }
843*/
844
Chris Lattner93750fa2001-07-28 17:48:55 +0000845ConstVal : ExtendedConstVal {
846 $$ = $1;
847 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000848 | SIntType EINT64VAL { // integral constants
849 if (!ConstPoolSInt::isValueValidForType($1, $2))
850 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000851 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000852 }
853 | UIntType EUINT64VAL { // integral constants
854 if (!ConstPoolUInt::isValueValidForType($1, $2))
855 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000856 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000857 }
858 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000859 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000860 }
861 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000862 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000863 }
864 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +0000865 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000866 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000867
Chris Lattnere98dda62001-07-14 06:10:16 +0000868// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000869ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +0000870 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +0000871 }
872 | ConstVal {
873 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +0000874 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +0000875 }
876
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000877
Chris Lattner1781aca2001-09-18 04:00:54 +0000878// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
879GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
880
Chris Lattner00950542001-06-06 20:29:01 +0000881
Chris Lattnere98dda62001-07-14 06:10:16 +0000882// ConstPool - Constants with optional names assigned to them.
Chris Lattner00950542001-06-06 20:29:01 +0000883ConstPool : ConstPool OptAssign ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +0000884 setValueName($3, $2);
Chris Lattner30c89792001-09-07 16:35:17 +0000885 InsertValue($3);
Chris Lattner00950542001-06-06 20:29:01 +0000886 }
Chris Lattner30c89792001-09-07 16:35:17 +0000887 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +0000888 // TODO: FIXME when Type are not const
889 setValueName(const_cast<Type*>($4->get()), $2);
890
891 if (!$2) {
Chris Lattner30c89792001-09-07 16:35:17 +0000892 InsertType($4->get(),
893 CurMeth.CurrentMethod ? CurMeth.Types : CurModule.Types);
894 }
895 delete $4;
896 }
897 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +0000898 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000899 | ConstPool OptAssign GlobalType ResolvedVal {
900 const Type *Ty = $4->getType();
901 // Global declarations appear in Constant Pool
Chris Lattner1781aca2001-09-18 04:00:54 +0000902 ConstPoolVal *Initializer = $4->castConstant();
903 if (Initializer == 0)
904 ThrowException("Global value initializer is not a constant!");
905
906 GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $3,
907 Initializer);
908 setValueName(GV, $2);
909
910 CurModule.CurrentModule->getGlobalList().push_back(GV);
911 InsertValue(GV, CurModule.Values);
912 }
913 | ConstPool OptAssign UNINIT GlobalType Types {
914 const Type *Ty = *$5;
915 // Global declarations appear in Constant Pool
916 if (Ty->isArrayType() && Ty->castArrayType()->isUnsized()) {
917 ThrowException("Type '" + Ty->getDescription() +
918 "' is not a sized type!");
Chris Lattner70cc3392001-09-10 07:58:01 +0000919 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000920
921 GlobalVariable *GV = new GlobalVariable(PointerType::get(Ty), $4);
922 setValueName(GV, $2);
923
Chris Lattner70cc3392001-09-10 07:58:01 +0000924 CurModule.CurrentModule->getGlobalList().push_back(GV);
925 InsertValue(GV, CurModule.Values);
Chris Lattnere98dda62001-07-14 06:10:16 +0000926 }
Chris Lattner00950542001-06-06 20:29:01 +0000927 | /* empty: end of list */ {
928 }
929
930
931//===----------------------------------------------------------------------===//
932// Rules to match Modules
933//===----------------------------------------------------------------------===//
934
935// Module rule: Capture the result of parsing the whole file into a result
936// variable...
937//
938Module : MethodList {
939 $$ = ParserResult = $1;
940 CurModule.ModuleDone();
941}
942
Chris Lattnere98dda62001-07-14 06:10:16 +0000943// MethodList - A list of methods, preceeded by a constant pool.
944//
Chris Lattner00950542001-06-06 20:29:01 +0000945MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +0000946 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000947 if (!$2->getParent())
948 $1->getMethodList().push_back($2);
949 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +0000950 }
Chris Lattnere1815642001-07-15 06:35:53 +0000951 | MethodList MethodProto {
952 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +0000953 }
Chris Lattner00950542001-06-06 20:29:01 +0000954 | ConstPool IMPLEMENTATION {
955 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +0000956 // Resolve circular types before we parse the body of the module
957 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +0000958 }
959
960
961//===----------------------------------------------------------------------===//
962// Rules to match Method Headers
963//===----------------------------------------------------------------------===//
964
965OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
966
967ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +0000968 $$ = new MethodArgument(*$1); delete $1;
Chris Lattner1781aca2001-09-18 04:00:54 +0000969 setValueName($$, $2);
Chris Lattner00950542001-06-06 20:29:01 +0000970}
971
972ArgListH : ArgVal ',' ArgListH {
973 $$ = $3;
974 $3->push_front($1);
975 }
976 | ArgVal {
977 $$ = new list<MethodArgument*>();
978 $$->push_front($1);
979 }
Chris Lattner8b81bf52001-07-25 22:47:46 +0000980 | DOTDOTDOT {
981 $$ = new list<MethodArgument*>();
982 $$->push_back(new MethodArgument(Type::VoidTy));
983 }
Chris Lattner00950542001-06-06 20:29:01 +0000984
985ArgList : ArgListH {
986 $$ = $1;
987 }
988 | /* empty */ {
989 $$ = 0;
990 }
991
992MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +0000993 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +0000994 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +0000995 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +0000996 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +0000997 ParamTypeList.push_back((*I)->getType());
998
Chris Lattner30c89792001-09-07 16:35:17 +0000999 const MethodType *MT = MethodType::get(*$1, ParamTypeList);
1000 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001001
Chris Lattnere1815642001-07-15 06:35:53 +00001002 Method *M = 0;
1003 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
1004 if (Value *V = ST->lookup(MT, $2)) { // Method already in symtab?
Chris Lattner9636a912001-10-01 16:18:37 +00001005 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001006
Chris Lattnere1815642001-07-15 06:35:53 +00001007 // Yes it is. If this is the case, either we need to be a forward decl,
1008 // or it needs to be.
1009 if (!CurMeth.isDeclare && !M->isExternal())
1010 ThrowException("Redefinition of method '" + string($2) + "'!");
1011 }
1012 }
1013
1014 if (M == 0) { // Not already defined?
1015 M = new Method(MT, $2);
1016 InsertValue(M, CurModule.Values);
1017 }
1018
1019 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001020
1021 CurMeth.MethodStart(M);
1022
1023 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001024 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001025 Method::ArgumentListType &ArgList = M->getArgumentList();
1026
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001027 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001028 InsertValue(*I);
1029 ArgList.push_back(*I);
1030 }
1031 delete $4; // We're now done with the argument list
1032 }
1033}
1034
1035MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1036 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001037
1038 // Resolve circular types before we parse the body of the method.
1039 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001040}
1041
1042Method : BasicBlockList END {
1043 $$ = $1;
1044}
1045
Chris Lattnere1815642001-07-15 06:35:53 +00001046MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1047 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001048 if (!$$->getParent())
1049 CurModule.CurrentModule->getMethodList().push_back($$);
1050 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001051}
Chris Lattner00950542001-06-06 20:29:01 +00001052
1053//===----------------------------------------------------------------------===//
1054// Rules to match Basic Blocks
1055//===----------------------------------------------------------------------===//
1056
1057ConstValueRef : ESINT64VAL { // A reference to a direct constant
1058 $$ = ValID::create($1);
1059 }
1060 | EUINT64VAL {
1061 $$ = ValID::create($1);
1062 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001063 | FPVAL { // Perhaps it's an FP constant?
1064 $$ = ValID::create($1);
1065 }
Chris Lattner00950542001-06-06 20:29:01 +00001066 | TRUE {
1067 $$ = ValID::create((int64_t)1);
1068 }
1069 | FALSE {
1070 $$ = ValID::create((int64_t)0);
1071 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001072 | NULL_TOK {
1073 $$ = ValID::createNull();
1074 }
1075
Chris Lattner93750fa2001-07-28 17:48:55 +00001076/*
Chris Lattner00950542001-06-06 20:29:01 +00001077 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1078 $$ = ValID::create_conststr($1);
1079 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001080*/
Chris Lattner00950542001-06-06 20:29:01 +00001081
1082// ValueRef - A reference to a definition...
1083ValueRef : INTVAL { // Is it an integer reference...?
1084 $$ = ValID::create($1);
1085 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001086 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001087 $$ = ValID::create($1);
1088 }
1089 | ConstValueRef {
1090 $$ = $1;
1091 }
1092
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001093// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1094// type immediately preceeds the value reference, and allows complex constant
1095// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1096ResolvedVal : ExtendedConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001097 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001098 }
1099 | Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001100 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001101 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001102
Chris Lattner00950542001-06-06 20:29:01 +00001103
1104BasicBlockList : BasicBlockList BasicBlock {
1105 $1->getBasicBlocks().push_back($2);
1106 $$ = $1;
1107 }
1108 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
1109 $$ = $1; // in them...
1110 $1->getBasicBlocks().push_back($2);
1111 }
1112
1113
1114// Basic blocks are terminated by branching instructions:
1115// br, br/cc, switch, ret
1116//
1117BasicBlock : InstructionList BBTerminatorInst {
1118 $1->getInstList().push_back($2);
1119 InsertValue($1);
1120 $$ = $1;
1121 }
1122 | LABELSTR InstructionList BBTerminatorInst {
1123 $2->getInstList().push_back($3);
Chris Lattner30c89792001-09-07 16:35:17 +00001124 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001125
1126 InsertValue($2);
1127 $$ = $2;
1128 }
1129
1130InstructionList : InstructionList Inst {
1131 $1->getInstList().push_back($2);
1132 $$ = $1;
1133 }
1134 | /* empty */ {
1135 $$ = new BasicBlock();
1136 }
1137
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001138BBTerminatorInst : RET ResolvedVal { // Return with a result...
1139 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001140 }
1141 | RET VOID { // Return with no result...
1142 $$ = new ReturnInst();
1143 }
1144 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001145 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001146 } // Conditional Branch...
1147 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001148 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1149 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001150 getVal(Type::BoolTy, $3));
1151 }
1152 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1153 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001154 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001155 $$ = S;
1156
1157 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1158 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001159 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001160 S->dest_push_back(I->first, I->second);
1161 }
1162
1163JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1164 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001165 ConstPoolVal *V = getVal($2, $3, true)->castConstantAsserting();
Chris Lattner00950542001-06-06 20:29:01 +00001166 if (V == 0)
1167 ThrowException("May only switch on a constant pool value!");
1168
Chris Lattner9636a912001-10-01 16:18:37 +00001169 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001170 }
1171 | IntType ConstValueRef ',' LABEL ValueRef {
1172 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001173 ConstPoolVal *V = getVal($1, $2, true)->castConstantAsserting();
Chris Lattner00950542001-06-06 20:29:01 +00001174
1175 if (V == 0)
1176 ThrowException("May only switch on a constant pool value!");
1177
Chris Lattner9636a912001-10-01 16:18:37 +00001178 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001179 }
1180
1181Inst : OptAssign InstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +00001182 setValueName($2, $1); // Is this definition named?? if so, assign the name...
Chris Lattner00950542001-06-06 20:29:01 +00001183
1184 InsertValue($2);
1185 $$ = $2;
1186}
1187
Chris Lattnerc24d2082001-06-11 15:04:20 +00001188PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1189 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001190 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001191 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001192 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001193 }
1194 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1195 $$ = $1;
1196 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001197 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001198 }
1199
1200
Chris Lattner30c89792001-09-07 16:35:17 +00001201ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001202 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001203 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001204 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001205 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001206 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001207 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001208 }
1209
1210// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1211ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1212
1213InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001214 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001215 if ($$ == 0)
1216 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001217 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001218 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001219 | UnaryOps ResolvedVal {
1220 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001221 if ($$ == 0)
1222 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001223 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001224 | ShiftOps ResolvedVal ',' ResolvedVal {
1225 if ($4->getType() != Type::UByteTy)
1226 ThrowException("Shift amount must be ubyte!");
1227 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001228 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001229 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001230 $$ = new CastInst($2, *$4);
1231 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001232 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001233 | PHI PHIList {
1234 const Type *Ty = $2->front().first->getType();
1235 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001236 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001237 if ($2->front().first->getType() != Ty)
1238 ThrowException("All elements of a PHI node must be of the same type!");
1239 ((PHINode*)$$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001240 $2->pop_front();
1241 }
1242 delete $2; // Free the list...
1243 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001244 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001245 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001246
Chris Lattner9636a912001-10-01 16:18:37 +00001247 if (!(Ty = dyn_cast<MethodType>($2->get()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001248 // Pull out the types of all of the arguments...
1249 vector<const Type*> ParamTypes;
1250 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1251 ParamTypes.push_back((*I)->getType());
Chris Lattner30c89792001-09-07 16:35:17 +00001252 Ty = MethodType::get(*$2, ParamTypes);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001253 }
Chris Lattner30c89792001-09-07 16:35:17 +00001254 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001255
Chris Lattner8b81bf52001-07-25 22:47:46 +00001256 Value *V = getVal(Ty, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001257
Chris Lattner8b81bf52001-07-25 22:47:46 +00001258 // Create the call node...
1259 if (!$5) { // Has no arguments?
Chris Lattner9636a912001-10-01 16:18:37 +00001260 $$ = new CallInst(cast<Method>(V), vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001261 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001262 // Loop through MethodType's arguments and ensure they are specified
1263 // correctly!
1264 //
1265 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001266 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1267 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1268
1269 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1270 if ((*ArgI)->getType() != *I)
1271 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001272 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001273
Chris Lattner8b81bf52001-07-25 22:47:46 +00001274 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001275 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001276
Chris Lattner9636a912001-10-01 16:18:37 +00001277 $$ = new CallInst(cast<Method>(V),
Chris Lattner8b81bf52001-07-25 22:47:46 +00001278 vector<Value*>($5->begin(), $5->end()));
1279 }
1280 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001281 }
1282 | MemoryInst {
1283 $$ = $1;
1284 }
1285
Chris Lattner027dcc52001-07-08 21:10:27 +00001286// UByteList - List of ubyte values for load and store instructions
1287UByteList : ',' ConstVector {
1288 $$ = $2;
1289} | /* empty */ {
1290 $$ = new vector<ConstPoolVal*>();
1291}
1292
Chris Lattner00950542001-06-06 20:29:01 +00001293MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001294 $$ = new MallocInst(PointerType::get(*$2));
1295 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001296 }
1297 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001298 if (!(*$2)->isArrayType() || ((const ArrayType*)$2->get())->isSized())
1299 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001300 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001301 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001302 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001303 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001304 }
1305 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001306 $$ = new AllocaInst(PointerType::get(*$2));
1307 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001308 }
1309 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001310 if (!(*$2)->isArrayType() || ((const ArrayType*)$2->get())->isSized())
1311 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001312 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001313 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001314 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001315 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001316 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001317 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001318 | FREE ResolvedVal {
1319 if (!$2->getType()->isPointerType())
1320 ThrowException("Trying to free nonpointer type " +
1321 $2->getType()->getName() + "!");
1322 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001323 }
1324
Chris Lattner027dcc52001-07-08 21:10:27 +00001325 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001326 if (!(*$2)->isPointerType())
1327 ThrowException("Can't load from nonpointer type: " + (*$2)->getName());
1328 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001329 ThrowException("Invalid indices for load instruction!");
1330
Chris Lattner30c89792001-09-07 16:35:17 +00001331 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001332 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001333 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001334 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001335 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001336 if (!(*$4)->isPointerType())
1337 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1338 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001339 if (ElTy == 0)
1340 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001341 if (ElTy != $2->getType())
1342 ThrowException("Can't store '" + $2->getType()->getName() +
1343 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001344 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1345 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001346 }
1347 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001348 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001349 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001350 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1351 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1352 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1353 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001354 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001355
Chris Lattner00950542001-06-06 20:29:01 +00001356%%
Chris Lattner09083092001-07-08 04:57:15 +00001357int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001358 ThrowException(string("Parse error: ") + ErrorMsg);
1359 return 0;
1360}