blob: f2ae016de3bf3b1a47ed66413bdbc6b3b1ae3bc3 [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
Chris Lattnerb7474512001-10-03 15:39:04 +0000105static bool inMethodScope() { return CurMeth.CurrentMethod != 0; }
106static bool inModuleScope() { return CurMeth.CurrentMethod == 0; }
107
Chris Lattner00950542001-06-06 20:29:01 +0000108
109//===----------------------------------------------------------------------===//
110// Code to handle definitions of all the types
111//===----------------------------------------------------------------------===//
112
Chris Lattner93750fa2001-07-28 17:48:55 +0000113static void InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values){
Chris Lattner00950542001-06-06 20:29:01 +0000114 if (!D->hasName()) { // Is this a numbered definition?
115 unsigned type = D->getType()->getUniqueID();
116 if (ValueTab.size() <= type)
117 ValueTab.resize(type+1, ValueList());
118 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
119 ValueTab[type].push_back(D);
120 }
121}
122
Chris Lattner30c89792001-09-07 16:35:17 +0000123// TODO: FIXME when Type are not const
124static void InsertType(const Type *Ty, vector<PATypeHolder<Type> > &Types) {
125 Types.push_back(Ty);
126}
127
128static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000129 switch (D.Type) {
130 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000131 unsigned Num = (unsigned)D.Num;
132
133 // Module constants occupy the lowest numbered slots...
134 if (Num < CurModule.Types.size())
135 return CurModule.Types[Num];
136
137 Num -= CurModule.Types.size();
138
139 // Check that the number is within bounds...
140 if (Num <= CurMeth.Types.size())
141 return CurMeth.Types[Num];
142 }
143 case 1: { // Is it a named definition?
144 string Name(D.Name);
145 SymbolTable *SymTab = 0;
Chris Lattnerb7474512001-10-03 15:39:04 +0000146 if (inMethodScope()) SymTab = CurMeth.CurrentMethod->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000147 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
148
149 if (N == 0) {
150 // Symbol table doesn't automatically chain yet... because the method
151 // hasn't been added to the module...
152 //
153 SymTab = CurModule.CurrentModule->getSymbolTable();
154 if (SymTab)
155 N = SymTab->lookup(Type::TypeTy, Name);
156 if (N == 0) break;
157 }
158
159 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000160 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000161 }
162 default:
163 ThrowException("Invalid symbol type reference!");
164 }
165
166 // If we reached here, we referenced either a symbol that we don't know about
167 // or an id number that hasn't been read yet. We may be referencing something
168 // forward, so just create an entry to be resolved later and get to it...
169 //
170 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
171
Chris Lattnerb7474512001-10-03 15:39:04 +0000172 vector<PATypeHolder<Type> > *LateResolver = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000173 &CurMeth.LateResolveTypes : &CurModule.LateResolveTypes;
174
175 Type *Typ = new TypePlaceHolder(Type::TypeTy, D);
176 InsertType(Typ, *LateResolver);
177 return Typ;
178}
179
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000180static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
181 SymbolTable *SymTab =
Chris Lattnerb7474512001-10-03 15:39:04 +0000182 inMethodScope() ? CurMeth.CurrentMethod->getSymbolTable() : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000183 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
184
185 if (N == 0) {
186 // Symbol table doesn't automatically chain yet... because the method
187 // hasn't been added to the module...
188 //
189 SymTab = CurModule.CurrentModule->getSymbolTable();
190 if (SymTab)
191 N = SymTab->lookup(Ty, Name);
192 }
193
194 return N;
195}
196
Chris Lattner30c89792001-09-07 16:35:17 +0000197static Value *getVal(const Type *Ty, const ValID &D,
198 bool DoNotImprovise = false) {
199 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
200
201 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000202 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000203 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000204 unsigned Num = (unsigned)D.Num;
205
206 // Module constants occupy the lowest numbered slots...
207 if (type < CurModule.Values.size()) {
208 if (Num < CurModule.Values[type].size())
209 return CurModule.Values[type][Num];
210
211 Num -= CurModule.Values[type].size();
212 }
213
214 // Make sure that our type is within bounds
215 if (CurMeth.Values.size() <= type)
216 break;
217
218 // Check that the number is within bounds...
219 if (CurMeth.Values[type].size() <= Num)
220 break;
221
222 return CurMeth.Values[type][Num];
223 }
Chris Lattner1a1cb112001-09-30 22:46:54 +0000224 case ValID::NameVal: { // Is it a named definition?
Chris Lattner00950542001-06-06 20:29:01 +0000225 string Name(D.Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000226 Value *N = lookupInSymbolTable(Ty, Name);
227 if (N == 0) break;
Chris Lattner00950542001-06-06 20:29:01 +0000228
229 D.destroy(); // Free old strdup'd memory...
230 return N;
231 }
232
Chris Lattner1a1cb112001-09-30 22:46:54 +0000233 case ValID::ConstSIntVal: // Is it a constant pool reference??
234 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
235 case ValID::ConstStringVal: // Is it a string const pool reference?
236 case ValID::ConstFPVal: // Is it a floating point const pool reference?
237 case ValID::ConstNullVal: { // Is it a null value?
Chris Lattner00950542001-06-06 20:29:01 +0000238 ConstPoolVal *CPV = 0;
239
Chris Lattner30c89792001-09-07 16:35:17 +0000240 // Check to make sure that "Ty" is an integral type, and that our
Chris Lattner00950542001-06-06 20:29:01 +0000241 // value will fit into the specified type...
242 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000243 case ValID::ConstSIntVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000244 if (Ty == Type::BoolTy) { // Special handling for boolean data
245 CPV = ConstPoolBool::get(D.ConstPool64 != 0);
Chris Lattner00950542001-06-06 20:29:01 +0000246 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000247 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000248 ThrowException("Symbolic constant pool value '" +
249 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000250 Ty->getName() + "'!");
251 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000252 }
253 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000254 case ValID::ConstUIntVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000255 if (!ConstPoolUInt::isValueValidForType(Ty, D.UConstPool64)) {
256 if (!ConstPoolSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000257 ThrowException("Integral constant pool reference is invalid!");
Chris Lattner00950542001-06-06 20:29:01 +0000258 } else { // This is really a signed reference. Transmogrify.
Chris Lattner30c89792001-09-07 16:35:17 +0000259 CPV = ConstPoolSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000260 }
261 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000262 CPV = ConstPoolUInt::get(Ty, D.UConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000263 }
264 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000265 case ValID::ConstStringVal:
Chris Lattner00950542001-06-06 20:29:01 +0000266 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
267 abort();
Chris Lattner00950542001-06-06 20:29:01 +0000268 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000269 case ValID::ConstFPVal:
Chris Lattner30c89792001-09-07 16:35:17 +0000270 if (!ConstPoolFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000271 ThrowException("FP constant invalid for type!!");
Chris Lattner1a1cb112001-09-30 22:46:54 +0000272 CPV = ConstPoolFP::get(Ty, D.ConstPoolFP);
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000273 break;
Chris Lattner1a1cb112001-09-30 22:46:54 +0000274 case ValID::ConstNullVal:
275 if (!Ty->isPointerType())
276 ThrowException("Cannot create a a non pointer null!");
Chris Lattnerb7474512001-10-03 15:39:04 +0000277 CPV = ConstPoolPointer::getNull(cast<PointerType>(Ty));
Chris Lattner1a1cb112001-09-30 22:46:54 +0000278 break;
279 default:
280 assert(0 && "Unhandled case!");
Chris Lattner00950542001-06-06 20:29:01 +0000281 }
282 assert(CPV && "How did we escape creating a constant??");
Chris Lattner00950542001-06-06 20:29:01 +0000283 return CPV;
284 } // End of case 2,3,4
Chris Lattner30c89792001-09-07 16:35:17 +0000285 default:
286 assert(0 && "Unhandled case!");
Chris Lattner00950542001-06-06 20:29:01 +0000287 } // End of switch
288
289
290 // If we reached here, we referenced either a symbol that we don't know about
291 // or an id number that hasn't been read yet. We may be referencing something
292 // forward, so just create an entry to be resolved later and get to it...
293 //
294 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
295
Chris Lattner00950542001-06-06 20:29:01 +0000296 Value *d = 0;
Chris Lattnerb7474512001-10-03 15:39:04 +0000297 vector<ValueList> *LateResolver = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000298 &CurMeth.LateResolveValues : &CurModule.LateResolveValues;
Chris Lattner93750fa2001-07-28 17:48:55 +0000299
Chris Lattnerb973dd72001-10-03 14:59:05 +0000300 if (isa<MethodType>(Ty))
301 ThrowException("Methods are not values and must be referenced as pointers");
302
Chris Lattneref9c23f2001-10-03 14:53:21 +0000303 if (const PointerType *PTy = dyn_cast<PointerType>(Ty))
304 if (const MethodType *MTy = dyn_cast<MethodType>(PTy->getValueType()))
305 Ty = MTy; // Convert pointer to method to method type
306
Chris Lattner30c89792001-09-07 16:35:17 +0000307 switch (Ty->getPrimitiveID()) {
308 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
309 case Type::MethodTyID: d = new MethPlaceHolder(Ty, D);
Chris Lattner93750fa2001-07-28 17:48:55 +0000310 LateResolver = &CurModule.LateResolveValues; break;
Chris Lattner30c89792001-09-07 16:35:17 +0000311 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000312 }
313
314 assert(d != 0 && "How did we not make something?");
Chris Lattner93750fa2001-07-28 17:48:55 +0000315 InsertValue(d, *LateResolver);
Chris Lattner00950542001-06-06 20:29:01 +0000316 return d;
317}
318
319
320//===----------------------------------------------------------------------===//
321// Code to handle forward references in instructions
322//===----------------------------------------------------------------------===//
323//
324// This code handles the late binding needed with statements that reference
325// values not defined yet... for example, a forward branch, or the PHI node for
326// a loop body.
327//
328// This keeps a table (CurMeth.LateResolveValues) of all such forward references
329// and back patchs after we are done.
330//
331
332// ResolveDefinitions - If we could not resolve some defs at parsing
333// time (forward branches, phi functions for loops, etc...) resolve the
334// defs now...
335//
336static void ResolveDefinitions(vector<ValueList> &LateResolvers) {
337 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
338 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
339 while (!LateResolvers[ty].empty()) {
340 Value *V = LateResolvers[ty].back();
341 LateResolvers[ty].pop_back();
342 ValID &DID = getValIDFromPlaceHolder(V);
343
344 Value *TheRealValue = getVal(Type::getUniqueIDType(ty), DID, true);
345
Chris Lattner30c89792001-09-07 16:35:17 +0000346 if (TheRealValue == 0) {
347 if (DID.Type == 1)
348 ThrowException("Reference to an invalid definition: '" +DID.getName()+
349 "' of type '" + V->getType()->getDescription() + "'",
350 getLineNumFromPlaceHolder(V));
351 else
352 ThrowException("Reference to an invalid definition: #" +
353 itostr(DID.Num) + " of type '" +
354 V->getType()->getDescription() + "'",
355 getLineNumFromPlaceHolder(V));
356 }
357
Chris Lattnercfe26c92001-10-01 18:26:53 +0000358 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000359
360 V->replaceAllUsesWith(TheRealValue);
Chris Lattner00950542001-06-06 20:29:01 +0000361 delete V;
362 }
363 }
364
365 LateResolvers.clear();
366}
367
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000368// ResolveType - Take a specified unresolved type and resolve it. If there is
369// nothing to resolve it to yet, return true. Otherwise resolve it and return
370// false.
371//
372static bool ResolveType(PATypeHolder<Type> &T) {
373 const Type *Ty = T;
374 ValID &DID = getValIDFromPlaceHolder(Ty);
375
376 const Type *TheRealType = getTypeVal(DID, true);
377 if (TheRealType == 0) return true;
378
379 // Refine the opaque type we had to the new type we are getting.
380 cast<DerivedType>(Ty)->refineAbstractTypeTo(TheRealType);
381 return false;
382}
383
Chris Lattner30c89792001-09-07 16:35:17 +0000384
385// ResolveTypes - This goes through the forward referenced type table and makes
386// sure that all type references are complete. This code is executed after the
387// constant pool of a method or module is completely parsed.
Chris Lattner00950542001-06-06 20:29:01 +0000388//
Chris Lattner30c89792001-09-07 16:35:17 +0000389static void ResolveTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
390 while (!LateResolveTypes.empty()) {
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000391 if (ResolveType(LateResolveTypes.back())) {
392 const Type *Ty = LateResolveTypes.back();
393 ValID &DID = getValIDFromPlaceHolder(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000394
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000395 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000396 ThrowException("Reference to an invalid type: '" +DID.getName(),
397 getLineNumFromPlaceHolder(Ty));
398 else
399 ThrowException("Reference to an invalid type: #" + itostr(DID.Num),
400 getLineNumFromPlaceHolder(Ty));
Chris Lattner00950542001-06-06 20:29:01 +0000401 }
Chris Lattner30c89792001-09-07 16:35:17 +0000402
Chris Lattner30c89792001-09-07 16:35:17 +0000403 // No need to delete type, refine does that for us.
404 LateResolveTypes.pop_back();
405 }
406}
407
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000408
409// ResolveSomeTypes - This goes through the forward referenced type table and
410// completes references that are now done. This is so that types are
411// immediately resolved to be as concrete as possible. This does not cause
412// thrown exceptions if not everything is resolved.
413//
414static void ResolveSomeTypes(vector<PATypeHolder<Type> > &LateResolveTypes) {
415 for (unsigned i = 0; i < LateResolveTypes.size(); ) {
416 if (ResolveType(LateResolveTypes[i]))
417 ++i; // Type didn't resolve
418 else
419 LateResolveTypes.erase(LateResolveTypes.begin()+i); // Type resolved!
420 }
421}
422
423
Chris Lattner1781aca2001-09-18 04:00:54 +0000424// setValueName - Set the specified value to the name given. The name may be
425// null potentially, in which case this is a noop. The string passed in is
426// assumed to be a malloc'd string buffer, and is freed by this function.
427//
Chris Lattnerb7474512001-10-03 15:39:04 +0000428// This function returns true if the value has already been defined, but is
429// allowed to be redefined in the specified context. If the name is a new name
430// for the typeplane, false is returned.
431//
432static bool setValueName(Value *V, char *NameStr) {
433 if (NameStr == 0) return false;
Chris Lattner1781aca2001-09-18 04:00:54 +0000434 string Name(NameStr); // Copy string
435 free(NameStr); // Free old string
436
Chris Lattnerb7474512001-10-03 15:39:04 +0000437 SymbolTable *ST = inMethodScope() ?
Chris Lattner30c89792001-09-07 16:35:17 +0000438 CurMeth.CurrentMethod->getSymbolTableSure() :
439 CurModule.CurrentModule->getSymbolTableSure();
440
441 Value *Existing = ST->lookup(V->getType(), Name);
442 if (Existing) { // Inserting a name that is already defined???
443 // There is only one case where this is allowed: when we are refining an
444 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000445 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000446 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000447 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000448 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000449 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000450 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000451 }
Chris Lattner30c89792001-09-07 16:35:17 +0000452
Chris Lattner9636a912001-10-01 16:18:37 +0000453 // Otherwise, we are a simple redefinition of a value, check to see if it
454 // is defined the same as the old one...
455 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000456 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
457 // cerr << "Type: " << Ty->getDescription() << " != "
458 // << cast<const Type>(V)->getDescription() << "!\n";
459 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
460 GlobalVariable *GV = cast<GlobalVariable>(V);
461
462 // We are allowed to redefine a global variable in two circumstances:
463 // 1. If at least one of the globals is uninitialized or
464 // 2. If both initializers have the same value.
465 //
466 // This can only be done if the const'ness of the vars is the same.
467 //
468 if (EGV->isConstant() == GV->isConstant() &&
469 (!EGV->hasInitializer() || !GV->hasInitializer() ||
470 EGV->getInitializer() == GV->getInitializer())) {
471
472 // Make sure the existing global version gets the initializer!
473 if (GV->hasInitializer() && !EGV->hasInitializer())
474 EGV->setInitializer(GV->getInitializer());
475
476 return true; // They are equivalent!
477 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000478
Chris Lattner9636a912001-10-01 16:18:37 +0000479 }
Chris Lattner30c89792001-09-07 16:35:17 +0000480 ThrowException("Redefinition of value name '" + Name + "' in the '" +
481 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000482 }
Chris Lattner00950542001-06-06 20:29:01 +0000483
Chris Lattner30c89792001-09-07 16:35:17 +0000484 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000485 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000486}
487
Chris Lattner8896eda2001-07-09 19:38:36 +0000488
Chris Lattner30c89792001-09-07 16:35:17 +0000489//===----------------------------------------------------------------------===//
490// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000491//
Chris Lattner8896eda2001-07-09 19:38:36 +0000492
Chris Lattner30c89792001-09-07 16:35:17 +0000493// TypeContains - Returns true if Ty contains E in it.
494//
495static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000496 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000497}
Chris Lattner698b56e2001-07-20 19:15:08 +0000498
Chris Lattner30c89792001-09-07 16:35:17 +0000499
500static vector<pair<unsigned, OpaqueType *> > UpRefs;
501
502static PATypeHolder<Type> HandleUpRefs(const Type *ty) {
503 PATypeHolder<Type> Ty(ty);
504 UR_OUT(UpRefs.size() << " upreferences active!\n");
505 for (unsigned i = 0; i < UpRefs.size(); ) {
506 UR_OUT("TypeContains(" << Ty->getDescription() << ", "
507 << UpRefs[i].second->getDescription() << ") = "
508 << TypeContains(Ty, UpRefs[i].second) << endl);
509 if (TypeContains(Ty, UpRefs[i].second)) {
510 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
511 UR_OUT("Uplevel Ref Level = " << Level << endl);
512 if (Level == 0) { // Upreference should be resolved!
513 UR_OUT("About to resolve upreference!\n";
514 string OldName = UpRefs[i].second->getDescription());
515 UpRefs[i].second->refineAbstractTypeTo(Ty);
516 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
517 UR_OUT("Type '" << OldName << "' refined upreference to: "
518 << (const void*)Ty << ", " << Ty->getDescription() << endl);
519 continue;
520 }
521 }
522
523 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000524 }
Chris Lattner30c89792001-09-07 16:35:17 +0000525 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000526 return Ty;
527}
528
Chris Lattner30c89792001-09-07 16:35:17 +0000529template <class TypeTy>
530inline static void TypeDone(PATypeHolder<TypeTy> *Ty) {
531 if (UpRefs.size())
532 ThrowException("Invalid upreference in type: " + (*Ty)->getDescription());
533}
534
535// newTH - Allocate a new type holder for the specified type
536template <class TypeTy>
537inline static PATypeHolder<TypeTy> *newTH(const TypeTy *Ty) {
538 return new PATypeHolder<TypeTy>(Ty);
539}
540template <class TypeTy>
541inline static PATypeHolder<TypeTy> *newTH(const PATypeHolder<TypeTy> &TH) {
542 return new PATypeHolder<TypeTy>(TH);
543}
544
545
Chris Lattner00950542001-06-06 20:29:01 +0000546//===----------------------------------------------------------------------===//
547// RunVMAsmParser - Define an interface to this parser
548//===----------------------------------------------------------------------===//
549//
Chris Lattnera2850432001-07-22 18:36:00 +0000550Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000551 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000552 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000553 llvmAsmlineno = 1; // Reset the current line number...
554
555 CurModule.CurrentModule = new Module(); // Allocate a new module to read
556 yyparse(); // Parse the file.
557 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000558 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
559 ParserResult = 0;
560
561 return Result;
562}
563
564%}
565
566%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000567 Module *ModuleVal;
568 Method *MethodVal;
569 MethodArgument *MethArgVal;
570 BasicBlock *BasicBlockVal;
571 TerminatorInst *TermInstVal;
572 Instruction *InstVal;
573 ConstPoolVal *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000574
Chris Lattner30c89792001-09-07 16:35:17 +0000575 const Type *PrimType;
576 PATypeHolder<Type> *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000577 Value *ValueVal;
578
579 list<MethodArgument*> *MethodArgList;
580 list<Value*> *ValueList;
581 list<PATypeHolder<Type> > *TypeList;
Chris Lattnerc24d2082001-06-11 15:04:20 +0000582 list<pair<Value*, BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner00950542001-06-06 20:29:01 +0000583 list<pair<ConstPoolVal*, BasicBlock*> > *JumpTable;
Chris Lattner30c89792001-09-07 16:35:17 +0000584 vector<ConstPoolVal*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000585
Chris Lattner30c89792001-09-07 16:35:17 +0000586 int64_t SInt64Val;
587 uint64_t UInt64Val;
588 int SIntVal;
589 unsigned UIntVal;
590 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000591 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000592
Chris Lattner30c89792001-09-07 16:35:17 +0000593 char *StrVal; // This memory is strdup'd!
594 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000595
Chris Lattner30c89792001-09-07 16:35:17 +0000596 Instruction::UnaryOps UnaryOpVal;
597 Instruction::BinaryOps BinaryOpVal;
598 Instruction::TermOps TermOpVal;
599 Instruction::MemoryOps MemOpVal;
600 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000601}
602
603%type <ModuleVal> Module MethodList
Chris Lattnere1815642001-07-15 06:35:53 +0000604%type <MethodVal> Method MethodProto MethodHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000605%type <BasicBlockVal> BasicBlock InstructionList
606%type <TermInstVal> BBTerminatorInst
607%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000608%type <ConstVal> ConstVal
Chris Lattner027dcc52001-07-08 21:10:27 +0000609%type <ConstVector> ConstVector UByteList
Chris Lattner00950542001-06-06 20:29:01 +0000610%type <MethodArgList> ArgList ArgListH
611%type <MethArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000612%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000613%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner30c89792001-09-07 16:35:17 +0000614%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000615%type <JumpTable> JumpTable
Chris Lattner1781aca2001-09-18 04:00:54 +0000616%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner00950542001-06-06 20:29:01 +0000617
618%type <ValIDVal> ValueRef ConstValueRef // Reference to a definition or BB
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000619%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000620// Tokens and types for handling constant integer values
621//
622// ESINT64VAL - A negative number within long long range
623%token <SInt64Val> ESINT64VAL
624
625// EUINT64VAL - A positive number within uns. long long range
626%token <UInt64Val> EUINT64VAL
627%type <SInt64Val> EINT64VAL
628
629%token <SIntVal> SINTVAL // Signed 32 bit ints...
630%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
631%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000632%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000633
634// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000635%type <TypeVal> Types TypesV UpRTypes UpRTypesV
636%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
637%token <TypeVal> OPAQUE
638%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
639%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000640
641%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
642%type <StrVal> OptVAR_ID OptAssign
643
644
Chris Lattner1781aca2001-09-18 04:00:54 +0000645%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000646%token TO DOTDOTDOT STRING NULL_TOK CONST
Chris Lattner00950542001-06-06 20:29:01 +0000647
648// Basic Block Terminating Operators
649%token <TermOpVal> RET BR SWITCH
650
651// Unary Operators
652%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000653%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000654
655// Binary Operators
656%type <BinaryOpVal> BinaryOps // all the binary operators
657%token <BinaryOpVal> ADD SUB MUL DIV REM
Chris Lattner027dcc52001-07-08 21:10:27 +0000658%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000659
660// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000661%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000662
Chris Lattner027dcc52001-07-08 21:10:27 +0000663// Other Operators
664%type <OtherOpVal> ShiftOps
665%token <OtherOpVal> PHI CALL CAST SHL SHR
666
Chris Lattner00950542001-06-06 20:29:01 +0000667%start Module
668%%
669
670// Handle constant integer size restriction and conversion...
671//
672
673INTVAL : SINTVAL
674INTVAL : UINTVAL {
675 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
676 ThrowException("Value too large for type!");
677 $$ = (int32_t)$1;
678}
679
680
681EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
682EINT64VAL : EUINT64VAL {
683 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
684 ThrowException("Value too large for type!");
685 $$ = (int64_t)$1;
686}
687
Chris Lattner00950542001-06-06 20:29:01 +0000688// Operations that are notably excluded from this list include:
689// RET, BR, & SWITCH because they end basic blocks and are treated specially.
690//
Chris Lattner09083092001-07-08 04:57:15 +0000691UnaryOps : NOT
Chris Lattner00950542001-06-06 20:29:01 +0000692BinaryOps : ADD | SUB | MUL | DIV | REM
693BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000694ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000695
Chris Lattnere98dda62001-07-14 06:10:16 +0000696// These are some types that allow classification if we only want a particular
697// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000698SIntType : LONG | INT | SHORT | SBYTE
699UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000700IntType : SIntType | UIntType
701FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000702
Chris Lattnere98dda62001-07-14 06:10:16 +0000703// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000704OptAssign : VAR_ID '=' {
705 $$ = $1;
706 }
707 | /*empty*/ {
708 $$ = 0;
709 }
710
Chris Lattner30c89792001-09-07 16:35:17 +0000711
712//===----------------------------------------------------------------------===//
713// Types includes all predefined types... except void, because it can only be
714// used in specific contexts (method returning void for example). To have
715// access to it, a user must explicitly use TypesV.
716//
717
718// TypesV includes all of 'Types', but it also includes the void type.
719TypesV : Types | VOID { $$ = newTH($1); }
720UpRTypesV : UpRTypes | VOID { $$ = newTH($1); }
721
722Types : UpRTypes {
723 TypeDone($$ = $1);
724 }
725
726
727// Derived types are added later...
728//
729PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
730PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
731UpRTypes : OPAQUE | PrimType { $$ = newTH($1); }
732UpRTypes : ValueRef { // Named types are also simple types...
733 $$ = newTH(getTypeVal($1));
734}
735
Chris Lattner30c89792001-09-07 16:35:17 +0000736// Include derived types in the Types production.
737//
738UpRTypes : '\\' EUINT64VAL { // Type UpReference
739 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
740 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
741 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
742 $$ = newTH<Type>(OT);
743 UR_OUT("New Upreference!\n");
744 }
745 | UpRTypesV '(' ArgTypeListI ')' { // Method derived type?
746 vector<const Type*> Params;
747 mapto($3->begin(), $3->end(), back_inserter(Params),
748 mem_fun_ref(&PATypeHandle<Type>::get));
749 $$ = newTH(HandleUpRefs(MethodType::get(*$1, Params)));
750 delete $3; // Delete the argument list
751 delete $1; // Delete the old type handle
752 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000753 | '[' UpRTypesV ']' { // Unsized array type?
754 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$2)));
755 delete $2;
Chris Lattner30c89792001-09-07 16:35:17 +0000756 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000757 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
758 $$ = newTH<Type>(HandleUpRefs(ArrayType::get(*$4, (int)$2)));
759 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000760 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000761 | '{' TypeListI '}' { // Structure type?
762 vector<const Type*> Elements;
763 mapto($2->begin(), $2->end(), back_inserter(Elements),
764 mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000765
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000766 $$ = newTH<Type>(HandleUpRefs(StructType::get(Elements)));
767 delete $2;
768 }
769 | '{' '}' { // Empty structure type?
770 $$ = newTH<Type>(StructType::get(vector<const Type*>()));
771 }
772 | UpRTypes '*' { // Pointer type?
773 $$ = newTH<Type>(HandleUpRefs(PointerType::get(*$1)));
774 delete $1;
775 }
Chris Lattner30c89792001-09-07 16:35:17 +0000776
777// TypeList - Used for struct declarations and as a basis for method type
778// declaration type lists
779//
780TypeListI : UpRTypes {
781 $$ = new list<PATypeHolder<Type> >();
782 $$->push_back(*$1); delete $1;
783 }
784 | TypeListI ',' UpRTypes {
785 ($$=$1)->push_back(*$3); delete $3;
786 }
787
788// ArgTypeList - List of types for a method type declaration...
789ArgTypeListI : TypeListI
790 | TypeListI ',' DOTDOTDOT {
791 ($$=$1)->push_back(Type::VoidTy);
792 }
793 | DOTDOTDOT {
794 ($$ = new list<PATypeHolder<Type> >())->push_back(Type::VoidTy);
795 }
796 | /*empty*/ {
797 $$ = new list<PATypeHolder<Type> >();
798 }
799
800
Chris Lattnere98dda62001-07-14 06:10:16 +0000801// ConstVal - The various declarations that go into the constant pool. This
802// includes all forward declarations of types, constants, and functions.
803//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000804ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
805 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
806 if (ATy == 0)
807 ThrowException("Cannot make array constant with type: '" +
808 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000809 const Type *ETy = ATy->getElementType();
810 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000811
Chris Lattner30c89792001-09-07 16:35:17 +0000812 // Verify that we have the correct size...
813 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000814 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000815 utostr($3->size()) + " arguments, but has size of " +
816 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000817
Chris Lattner30c89792001-09-07 16:35:17 +0000818 // Verify all elements are correct type!
819 for (unsigned i = 0; i < $3->size(); i++) {
820 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000821 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000822 ETy->getName() + "' as required!\nIt is of type '" +
823 (*$3)[i]->getType()->getName() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000824 }
825
Chris Lattner30c89792001-09-07 16:35:17 +0000826 $$ = ConstPoolArray::get(ATy, *$3);
827 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000828 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000829 | Types '[' ']' {
830 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
831 if (ATy == 0)
832 ThrowException("Cannot make array constant with type: '" +
833 (*$1)->getDescription() + "'!");
834
835 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000836 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000837 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000838 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000839 $$ = ConstPoolArray::get(ATy, vector<ConstPoolVal*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000840 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000841 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000842 | Types 'c' STRINGCONSTANT {
843 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
844 if (ATy == 0)
845 ThrowException("Cannot make array constant with type: '" +
846 (*$1)->getDescription() + "'!");
847
Chris Lattner30c89792001-09-07 16:35:17 +0000848 int NumElements = ATy->getNumElements();
849 const Type *ETy = ATy->getElementType();
850 char *EndStr = UnEscapeLexed($3, true);
851 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000852 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000853 itostr((int)(EndStr-$3)) +
854 " when array has size " + itostr(NumElements) + "!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000855 vector<ConstPoolVal*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000856 if (ETy == Type::SByteTy) {
857 for (char *C = $3; C != EndStr; ++C)
858 Vals.push_back(ConstPoolSInt::get(ETy, *C));
859 } else if (ETy == Type::UByteTy) {
860 for (char *C = $3; C != EndStr; ++C)
861 Vals.push_back(ConstPoolUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000862 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000863 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000864 ThrowException("Cannot build string arrays of non byte sized elements!");
865 }
Chris Lattner30c89792001-09-07 16:35:17 +0000866 free($3);
867 $$ = ConstPoolArray::get(ATy, Vals);
868 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000869 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000870 | Types '{' ConstVector '}' {
871 const StructType *STy = dyn_cast<const StructType>($1->get());
872 if (STy == 0)
873 ThrowException("Cannot make struct constant with type: '" +
874 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000875 // FIXME: TODO: Check to see that the constants are compatible with the type
876 // initializer!
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000877 $$ = ConstPoolStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000878 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000879 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000880 | Types NULL_TOK {
881 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
882 if (PTy == 0)
883 ThrowException("Cannot make null pointer constant with type: '" +
884 (*$1)->getDescription() + "'!");
885
Chris Lattnerb7474512001-10-03 15:39:04 +0000886 $$ = ConstPoolPointer::getNull(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000887 delete $1;
888 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000889 | Types VAR_ID {
890 string Name($2); free($2); // Change to a responsible mem manager
891 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
892 if (Ty == 0)
893 ThrowException("Global const reference must be a pointer type!");
894
895 Value *N = lookupInSymbolTable(Ty, Name);
896 if (N == 0)
897 ThrowException("Global pointer reference '%" + Name +
898 "' must be defined before use!");
899
900 // TODO FIXME: This should also allow methods... when common baseclass
901 // exists
902 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(N)) {
903 $$ = ConstPoolPointerReference::get(GV);
904 } else {
905 ThrowException("'%" + Name + "' is not a global value reference!");
906 }
907
908 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000909 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000910
Chris Lattner00950542001-06-06 20:29:01 +0000911
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000912ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000913 if (!ConstPoolSInt::isValueValidForType($1, $2))
914 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000915 $$ = ConstPoolSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000916 }
917 | UIntType EUINT64VAL { // integral constants
918 if (!ConstPoolUInt::isValueValidForType($1, $2))
919 ThrowException("Constant value doesn't fit in type!");
Chris Lattner30c89792001-09-07 16:35:17 +0000920 $$ = ConstPoolUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000921 }
922 | BOOL TRUE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000923 $$ = ConstPoolBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000924 }
925 | BOOL FALSE { // Boolean constants
Chris Lattner30c89792001-09-07 16:35:17 +0000926 $$ = ConstPoolBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000927 }
928 | FPType FPVAL { // Float & Double constants
Chris Lattner30c89792001-09-07 16:35:17 +0000929 $$ = ConstPoolFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000930 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000931
Chris Lattnere98dda62001-07-14 06:10:16 +0000932// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000933ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +0000934 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +0000935 }
936 | ConstVal {
937 $$ = new vector<ConstPoolVal*>();
Chris Lattner30c89792001-09-07 16:35:17 +0000938 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +0000939 }
940
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000941
Chris Lattner1781aca2001-09-18 04:00:54 +0000942// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
943GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
944
Chris Lattner00950542001-06-06 20:29:01 +0000945
Chris Lattnere98dda62001-07-14 06:10:16 +0000946// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000947ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +0000948 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000949 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +0000950 }
Chris Lattner30c89792001-09-07 16:35:17 +0000951 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner1781aca2001-09-18 04:00:54 +0000952 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +0000953 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
954 // If this is not a redefinition of a type...
955 if (!$2) {
956 InsertType($4->get(),
957 inMethodScope() ? CurMeth.Types : CurModule.Types);
958 }
959 delete $4;
Chris Lattner1781aca2001-09-18 04:00:54 +0000960
Chris Lattnerb7474512001-10-03 15:39:04 +0000961 ResolveSomeTypes(inMethodScope() ? CurMeth.LateResolveTypes :
962 CurModule.LateResolveTypes);
Chris Lattner30c89792001-09-07 16:35:17 +0000963 }
Chris Lattner30c89792001-09-07 16:35:17 +0000964 }
965 | ConstPool MethodProto { // Method prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +0000966 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000967 | ConstPool OptAssign GlobalType ConstVal {
Chris Lattner1781aca2001-09-18 04:00:54 +0000968 const Type *Ty = $4->getType();
969 // Global declarations appear in Constant Pool
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000970 ConstPoolVal *Initializer = $4;
Chris Lattner1781aca2001-09-18 04:00:54 +0000971 if (Initializer == 0)
972 ThrowException("Global value initializer is not a constant!");
973
Chris Lattneref9c23f2001-10-03 14:53:21 +0000974 GlobalVariable *GV = new GlobalVariable(Ty, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +0000975 if (!setValueName(GV, $2)) { // If not redefining...
976 CurModule.CurrentModule->getGlobalList().push_back(GV);
977 InsertValue(GV, CurModule.Values);
978 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000979 }
980 | ConstPool OptAssign UNINIT GlobalType Types {
981 const Type *Ty = *$5;
982 // Global declarations appear in Constant Pool
Chris Lattnercfe26c92001-10-01 18:26:53 +0000983 if (isa<ArrayType>(Ty) && cast<ArrayType>(Ty)->isUnsized()) {
Chris Lattner1781aca2001-09-18 04:00:54 +0000984 ThrowException("Type '" + Ty->getDescription() +
985 "' is not a sized type!");
Chris Lattner70cc3392001-09-10 07:58:01 +0000986 }
Chris Lattner1781aca2001-09-18 04:00:54 +0000987
Chris Lattneref9c23f2001-10-03 14:53:21 +0000988 GlobalVariable *GV = new GlobalVariable(Ty, $4);
Chris Lattnerb7474512001-10-03 15:39:04 +0000989 if (!setValueName(GV, $2)) { // If not redefining...
990 CurModule.CurrentModule->getGlobalList().push_back(GV);
991 InsertValue(GV, CurModule.Values);
992 }
Chris Lattnere98dda62001-07-14 06:10:16 +0000993 }
Chris Lattner00950542001-06-06 20:29:01 +0000994 | /* empty: end of list */ {
995 }
996
997
998//===----------------------------------------------------------------------===//
999// Rules to match Modules
1000//===----------------------------------------------------------------------===//
1001
1002// Module rule: Capture the result of parsing the whole file into a result
1003// variable...
1004//
1005Module : MethodList {
1006 $$ = ParserResult = $1;
1007 CurModule.ModuleDone();
1008}
1009
Chris Lattnere98dda62001-07-14 06:10:16 +00001010// MethodList - A list of methods, preceeded by a constant pool.
1011//
Chris Lattner00950542001-06-06 20:29:01 +00001012MethodList : MethodList Method {
Chris Lattner00950542001-06-06 20:29:01 +00001013 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001014 if (!$2->getParent())
1015 $1->getMethodList().push_back($2);
1016 CurMeth.MethodDone();
Chris Lattner00950542001-06-06 20:29:01 +00001017 }
Chris Lattnere1815642001-07-15 06:35:53 +00001018 | MethodList MethodProto {
1019 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001020 }
Chris Lattner00950542001-06-06 20:29:01 +00001021 | ConstPool IMPLEMENTATION {
1022 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +00001023 // Resolve circular types before we parse the body of the module
1024 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001025 }
1026
1027
1028//===----------------------------------------------------------------------===//
1029// Rules to match Method Headers
1030//===----------------------------------------------------------------------===//
1031
1032OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1033
1034ArgVal : Types OptVAR_ID {
Chris Lattner30c89792001-09-07 16:35:17 +00001035 $$ = new MethodArgument(*$1); delete $1;
Chris Lattnerb7474512001-10-03 15:39:04 +00001036 if (setValueName($$, $2)) { assert(0 && "No arg redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001037}
1038
1039ArgListH : ArgVal ',' ArgListH {
1040 $$ = $3;
1041 $3->push_front($1);
1042 }
1043 | ArgVal {
1044 $$ = new list<MethodArgument*>();
1045 $$->push_front($1);
1046 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001047 | DOTDOTDOT {
1048 $$ = new list<MethodArgument*>();
1049 $$->push_back(new MethodArgument(Type::VoidTy));
1050 }
Chris Lattner00950542001-06-06 20:29:01 +00001051
1052ArgList : ArgListH {
1053 $$ = $1;
1054 }
1055 | /* empty */ {
1056 $$ = 0;
1057 }
1058
1059MethodHeaderH : TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattner93750fa2001-07-28 17:48:55 +00001060 UnEscapeLexed($2);
Chris Lattner30c89792001-09-07 16:35:17 +00001061 vector<const Type*> ParamTypeList;
Chris Lattner00950542001-06-06 20:29:01 +00001062 if ($4)
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001063 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001064 ParamTypeList.push_back((*I)->getType());
1065
Chris Lattneref9c23f2001-10-03 14:53:21 +00001066 const MethodType *MT = MethodType::get(*$1, ParamTypeList);
1067 const PointerType *PMT = PointerType::get(MT);
Chris Lattner30c89792001-09-07 16:35:17 +00001068 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001069
Chris Lattnere1815642001-07-15 06:35:53 +00001070 Method *M = 0;
1071 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001072 if (Value *V = ST->lookup(PMT, $2)) { // Method already in symtab?
1073 M = cast<Method>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001074
Chris Lattnere1815642001-07-15 06:35:53 +00001075 // Yes it is. If this is the case, either we need to be a forward decl,
1076 // or it needs to be.
1077 if (!CurMeth.isDeclare && !M->isExternal())
1078 ThrowException("Redefinition of method '" + string($2) + "'!");
1079 }
1080 }
1081
1082 if (M == 0) { // Not already defined?
1083 M = new Method(MT, $2);
1084 InsertValue(M, CurModule.Values);
1085 }
1086
1087 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001088
1089 CurMeth.MethodStart(M);
1090
1091 // Add all of the arguments we parsed to the method...
Chris Lattnere1815642001-07-15 06:35:53 +00001092 if ($4 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner00950542001-06-06 20:29:01 +00001093 Method::ArgumentListType &ArgList = M->getArgumentList();
1094
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001095 for (list<MethodArgument*>::iterator I = $4->begin(); I != $4->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +00001096 InsertValue(*I);
1097 ArgList.push_back(*I);
1098 }
1099 delete $4; // We're now done with the argument list
1100 }
1101}
1102
1103MethodHeader : MethodHeaderH ConstPool BEGINTOK {
1104 $$ = CurMeth.CurrentMethod;
Chris Lattner30c89792001-09-07 16:35:17 +00001105
1106 // Resolve circular types before we parse the body of the method.
1107 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001108}
1109
1110Method : BasicBlockList END {
1111 $$ = $1;
1112}
1113
Chris Lattnere1815642001-07-15 06:35:53 +00001114MethodProto : DECLARE { CurMeth.isDeclare = true; } MethodHeaderH {
1115 $$ = CurMeth.CurrentMethod;
Chris Lattner93750fa2001-07-28 17:48:55 +00001116 if (!$$->getParent())
1117 CurModule.CurrentModule->getMethodList().push_back($$);
1118 CurMeth.MethodDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001119}
Chris Lattner00950542001-06-06 20:29:01 +00001120
1121//===----------------------------------------------------------------------===//
1122// Rules to match Basic Blocks
1123//===----------------------------------------------------------------------===//
1124
1125ConstValueRef : ESINT64VAL { // A reference to a direct constant
1126 $$ = ValID::create($1);
1127 }
1128 | EUINT64VAL {
1129 $$ = ValID::create($1);
1130 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001131 | FPVAL { // Perhaps it's an FP constant?
1132 $$ = ValID::create($1);
1133 }
Chris Lattner00950542001-06-06 20:29:01 +00001134 | TRUE {
1135 $$ = ValID::create((int64_t)1);
1136 }
1137 | FALSE {
1138 $$ = ValID::create((int64_t)0);
1139 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001140 | NULL_TOK {
1141 $$ = ValID::createNull();
1142 }
1143
Chris Lattner93750fa2001-07-28 17:48:55 +00001144/*
Chris Lattner00950542001-06-06 20:29:01 +00001145 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1146 $$ = ValID::create_conststr($1);
1147 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001148*/
Chris Lattner00950542001-06-06 20:29:01 +00001149
1150// ValueRef - A reference to a definition...
1151ValueRef : INTVAL { // Is it an integer reference...?
1152 $$ = ValID::create($1);
1153 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001154 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001155 $$ = ValID::create($1);
1156 }
1157 | ConstValueRef {
1158 $$ = $1;
1159 }
1160
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001161// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1162// type immediately preceeds the value reference, and allows complex constant
1163// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001164ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001165 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001166 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001167
Chris Lattner00950542001-06-06 20:29:01 +00001168
1169BasicBlockList : BasicBlockList BasicBlock {
1170 $1->getBasicBlocks().push_back($2);
1171 $$ = $1;
1172 }
1173 | MethodHeader BasicBlock { // Do not allow methods with 0 basic blocks
1174 $$ = $1; // in them...
1175 $1->getBasicBlocks().push_back($2);
1176 }
1177
1178
1179// Basic blocks are terminated by branching instructions:
1180// br, br/cc, switch, ret
1181//
1182BasicBlock : InstructionList BBTerminatorInst {
1183 $1->getInstList().push_back($2);
1184 InsertValue($1);
1185 $$ = $1;
1186 }
1187 | LABELSTR InstructionList BBTerminatorInst {
1188 $2->getInstList().push_back($3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001189 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001190
1191 InsertValue($2);
1192 $$ = $2;
1193 }
1194
1195InstructionList : InstructionList Inst {
1196 $1->getInstList().push_back($2);
1197 $$ = $1;
1198 }
1199 | /* empty */ {
1200 $$ = new BasicBlock();
1201 }
1202
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001203BBTerminatorInst : RET ResolvedVal { // Return with a result...
1204 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001205 }
1206 | RET VOID { // Return with no result...
1207 $$ = new ReturnInst();
1208 }
1209 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001210 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001211 } // Conditional Branch...
1212 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001213 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1214 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001215 getVal(Type::BoolTy, $3));
1216 }
1217 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1218 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001219 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001220 $$ = S;
1221
1222 list<pair<ConstPoolVal*, BasicBlock*> >::iterator I = $8->begin(),
1223 end = $8->end();
Chris Lattner7fc9fe32001-06-27 23:41:11 +00001224 for (; I != end; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001225 S->dest_push_back(I->first, I->second);
1226 }
1227
1228JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1229 $$ = $1;
Chris Lattnercfe26c92001-10-01 18:26:53 +00001230 ConstPoolVal *V = cast<ConstPoolVal>(getVal($2, $3, true));
Chris Lattner00950542001-06-06 20:29:01 +00001231 if (V == 0)
1232 ThrowException("May only switch on a constant pool value!");
1233
Chris Lattner9636a912001-10-01 16:18:37 +00001234 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001235 }
1236 | IntType ConstValueRef ',' LABEL ValueRef {
1237 $$ = new list<pair<ConstPoolVal*, BasicBlock*> >();
Chris Lattnercfe26c92001-10-01 18:26:53 +00001238 ConstPoolVal *V = cast<ConstPoolVal>(getVal($1, $2, true));
Chris Lattner00950542001-06-06 20:29:01 +00001239
1240 if (V == 0)
1241 ThrowException("May only switch on a constant pool value!");
1242
Chris Lattner9636a912001-10-01 16:18:37 +00001243 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001244 }
1245
1246Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001247 // Is this definition named?? if so, assign the name...
1248 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001249 InsertValue($2);
1250 $$ = $2;
1251}
1252
Chris Lattnerc24d2082001-06-11 15:04:20 +00001253PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1254 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001255 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001256 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001257 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001258 }
1259 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1260 $$ = $1;
1261 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001262 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001263 }
1264
1265
Chris Lattner30c89792001-09-07 16:35:17 +00001266ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner00950542001-06-06 20:29:01 +00001267 $$ = new list<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001268 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001269 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001270 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001271 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001272 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001273 }
1274
1275// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1276ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1277
1278InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001279 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001280 if ($$ == 0)
1281 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001282 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001283 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001284 | UnaryOps ResolvedVal {
1285 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001286 if ($$ == 0)
1287 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001288 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001289 | ShiftOps ResolvedVal ',' ResolvedVal {
1290 if ($4->getType() != Type::UByteTy)
1291 ThrowException("Shift amount must be ubyte!");
1292 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001293 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001294 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001295 $$ = new CastInst($2, *$4);
1296 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001297 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001298 | PHI PHIList {
1299 const Type *Ty = $2->front().first->getType();
1300 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001301 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001302 if ($2->front().first->getType() != Ty)
1303 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001304 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001305 $2->pop_front();
1306 }
1307 delete $2; // Free the list...
1308 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001309 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001310 const PointerType *PMTy;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001311 const MethodType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001312
Chris Lattneref9c23f2001-10-03 14:53:21 +00001313 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
1314 !(Ty = dyn_cast<MethodType>(PMTy->getValueType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001315 // Pull out the types of all of the arguments...
1316 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001317 if ($5) {
1318 for (list<Value*>::iterator I = $5->begin(), E = $5->end(); I != E; ++I)
1319 ParamTypes.push_back((*I)->getType());
1320 }
1321 Ty = MethodType::get($2->get(), ParamTypes);
1322 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001323 }
Chris Lattner30c89792001-09-07 16:35:17 +00001324 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001325
Chris Lattneref9c23f2001-10-03 14:53:21 +00001326 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001327
Chris Lattner8b81bf52001-07-25 22:47:46 +00001328 // Create the call node...
1329 if (!$5) { // Has no arguments?
Chris Lattner9636a912001-10-01 16:18:37 +00001330 $$ = new CallInst(cast<Method>(V), vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001331 } else { // Has arguments?
Chris Lattner00950542001-06-06 20:29:01 +00001332 // Loop through MethodType's arguments and ensure they are specified
1333 // correctly!
1334 //
1335 MethodType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001336 MethodType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
1337 list<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
1338
1339 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1340 if ((*ArgI)->getType() != *I)
1341 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner00950542001-06-06 20:29:01 +00001342 (*I)->getName() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001343
Chris Lattner8b81bf52001-07-25 22:47:46 +00001344 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001345 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001346
Chris Lattner9636a912001-10-01 16:18:37 +00001347 $$ = new CallInst(cast<Method>(V),
Chris Lattner8b81bf52001-07-25 22:47:46 +00001348 vector<Value*>($5->begin(), $5->end()));
1349 }
1350 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001351 }
1352 | MemoryInst {
1353 $$ = $1;
1354 }
1355
Chris Lattner027dcc52001-07-08 21:10:27 +00001356// UByteList - List of ubyte values for load and store instructions
1357UByteList : ',' ConstVector {
1358 $$ = $2;
1359} | /* empty */ {
1360 $$ = new vector<ConstPoolVal*>();
1361}
1362
Chris Lattner00950542001-06-06 20:29:01 +00001363MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001364 $$ = new MallocInst(PointerType::get(*$2));
1365 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001366 }
1367 | MALLOC Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001368 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001369 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001370 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001371 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001372 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001373 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001374 }
1375 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001376 $$ = new AllocaInst(PointerType::get(*$2));
1377 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001378 }
1379 | ALLOCA Types ',' UINT ValueRef {
Chris Lattnerb00c5822001-10-02 03:41:24 +00001380 if (!(*$2)->isArrayType() || cast<const ArrayType>($2->get())->isSized())
Chris Lattner30c89792001-09-07 16:35:17 +00001381 ThrowException("Trying to allocate " + (*$2)->getName() +
Chris Lattner00950542001-06-06 20:29:01 +00001382 " as unsized array!");
Chris Lattner30c89792001-09-07 16:35:17 +00001383 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001384 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001385 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001386 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001387 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001388 | FREE ResolvedVal {
1389 if (!$2->getType()->isPointerType())
1390 ThrowException("Trying to free nonpointer type " +
1391 $2->getType()->getName() + "!");
1392 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001393 }
1394
Chris Lattner027dcc52001-07-08 21:10:27 +00001395 | LOAD Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001396 if (!(*$2)->isPointerType())
1397 ThrowException("Can't load from nonpointer type: " + (*$2)->getName());
1398 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001399 ThrowException("Invalid indices for load instruction!");
1400
Chris Lattner30c89792001-09-07 16:35:17 +00001401 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001402 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001403 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001404 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001405 | STORE ResolvedVal ',' Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001406 if (!(*$4)->isPointerType())
1407 ThrowException("Can't store to a nonpointer type: " + (*$4)->getName());
1408 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001409 if (ElTy == 0)
1410 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001411 if (ElTy != $2->getType())
1412 ThrowException("Can't store '" + $2->getType()->getName() +
1413 "' into space of type '" + ElTy->getName() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001414 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1415 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001416 }
1417 | GETELEMENTPTR Types ValueRef UByteList {
Chris Lattner30c89792001-09-07 16:35:17 +00001418 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001419 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001420 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
1421 ThrowException("Can't get element ptr '" + (*$2)->getName() + "'!");
1422 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1423 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001424 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001425
Chris Lattner00950542001-06-06 20:29:01 +00001426%%
Chris Lattner09083092001-07-08 04:57:15 +00001427int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001428 ThrowException(string("Parse error: ") + ErrorMsg);
1429 return 0;
1430}