blob: 91f4574a192e7586c5e013ba76a173e385a2bc95 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
Chris Lattner00950542001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner70cc3392001-09-10 07:58:01 +00009#include "llvm/Assembly/Parser.h"
Chris Lattner00950542001-06-06 20:29:01 +000010#include "llvm/SymbolTable.h"
11#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000012#include "llvm/GlobalVariable.h"
Chris Lattner79df7c02002-03-26 18:01:55 +000013#include "llvm/Function.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000014#include "llvm/BasicBlock.h"
Chris Lattner00950542001-06-06 20:29:01 +000015#include "llvm/DerivedTypes.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/iTerminators.h"
17#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000018#include "llvm/iPHINode.h"
Chris Lattner46748042002-04-09 19:41:42 +000019#include "llvm/Argument.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/STLExtras.h"
21#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000022#include <list>
23#include <utility> // Get definition of pair class
Chris Lattner30c89792001-09-07 16:35:17 +000024#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000025#include <stdio.h> // This embarasment is due to our flex lexer...
Chris Lattner697954c2002-01-20 22:54:45 +000026#include <iostream>
27using std::list;
28using std::vector;
29using std::pair;
30using std::map;
31using std::pair;
32using std::make_pair;
33using std::cerr;
34using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000035
Chris Lattner386a3b72001-10-16 19:54:17 +000036int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000037int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000038int yyparse();
39
40static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000041string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000042
Chris Lattner30c89792001-09-07 16:35:17 +000043// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
44// relating to upreferences in the input stream.
45//
46//#define DEBUG_UPREFS 1
47#ifdef DEBUG_UPREFS
48#define UR_OUT(X) cerr << X
49#else
50#define UR_OUT(X)
51#endif
52
Chris Lattner00950542001-06-06 20:29:01 +000053// This contains info used when building the body of a method. It is destroyed
54// when the method is completed.
55//
56typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000057static void ResolveDefinitions(vector<ValueList> &LateResolvers,
58 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000059
60static struct PerModuleInfo {
61 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000062 vector<ValueList> Values; // Module level numbered definitions
63 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +000064 vector<PATypeHolder> Types;
65 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000066
Chris Lattner2079fde2001-10-13 06:41:08 +000067 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
68 // references to global values. Global values may be referenced before they
69 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000070 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000071 //
72 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
73 GlobalRefsType GlobalRefs;
74
Chris Lattner00950542001-06-06 20:29:01 +000075 void ModuleDone() {
Chris Lattner30c89792001-09-07 16:35:17 +000076 // If we could not resolve some methods at method compilation time (calls to
77 // methods before they are defined), resolve them now... Types are resolved
78 // when the constant pool has been completely parsed.
79 //
Chris Lattner00950542001-06-06 20:29:01 +000080 ResolveDefinitions(LateResolveValues);
81
Chris Lattner2079fde2001-10-13 06:41:08 +000082 // Check to make sure that all global value forward references have been
83 // resolved!
84 //
85 if (!GlobalRefs.empty()) {
Chris Lattner749ce032002-03-11 22:12:39 +000086 string UndefinedReferences = "Unresolved global references exist:\n";
87
88 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
89 I != E; ++I) {
90 UndefinedReferences += " " + I->first.first->getDescription() + " " +
91 I->first.second.getName() + "\n";
92 }
93 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000094 }
95
Chris Lattner00950542001-06-06 20:29:01 +000096 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000097 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000098 CurrentModule = 0;
99 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000100
101
102 // DeclareNewGlobalValue - Called every type a new GV has been defined. This
103 // is used to remove things from the forward declaration map, resolving them
104 // to the correct thing as needed.
105 //
106 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
107 // Check to see if there is a forward reference to this global variable...
108 // if there is, eliminate it and patch the reference to use the new def'n.
109 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
110
111 if (I != GlobalRefs.end()) {
112 GlobalVariable *OldGV = I->second; // Get the placeholder...
113 I->first.second.destroy(); // Free string memory if neccesary
114
115 // Loop over all of the uses of the GlobalValue. The only thing they are
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000116 // allowed to be at this point is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000117 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
118 while (!OldGV->use_empty()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000119 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
120 ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
Chris Lattner2079fde2001-10-13 06:41:08 +0000121 assert(CPPR->getValue() == OldGV && "Something isn't happy");
122
123 // Change the const pool reference to point to the real global variable
124 // now. This should drop a use from the OldGV.
125 CPPR->mutateReference(GV);
126 }
127
128 // Remove GV from the module...
129 CurrentModule->getGlobalList().remove(OldGV);
130 delete OldGV; // Delete the old placeholder
131
132 // Remove the map entry for the global now that it has been created...
133 GlobalRefs.erase(I);
134 }
135 }
136
Chris Lattner00950542001-06-06 20:29:01 +0000137} CurModule;
138
Chris Lattner79df7c02002-03-26 18:01:55 +0000139static struct PerFunctionInfo {
140 Function *CurrentFunction; // Pointer to current method being created
Chris Lattner00950542001-06-06 20:29:01 +0000141
Chris Lattnere1815642001-07-15 06:35:53 +0000142 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000143 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000144 vector<PATypeHolder> Types;
145 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattnere1815642001-07-15 06:35:53 +0000146 bool isDeclare; // Is this method a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000147
Chris Lattner79df7c02002-03-26 18:01:55 +0000148 inline PerFunctionInfo() {
149 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000150 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000151 }
152
Chris Lattner79df7c02002-03-26 18:01:55 +0000153 inline ~PerFunctionInfo() {}
Chris Lattner00950542001-06-06 20:29:01 +0000154
Chris Lattner79df7c02002-03-26 18:01:55 +0000155 inline void FunctionStart(Function *M) {
156 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000157 }
158
Chris Lattner79df7c02002-03-26 18:01:55 +0000159 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000160 // If we could not resolve some blocks at parsing time (forward branches)
161 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000162 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000163
164 Values.clear(); // Clear out method local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000165 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000166 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000167 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000168 }
169} CurMeth; // Info for the current method...
170
Chris Lattner79df7c02002-03-26 18:01:55 +0000171static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000172
Chris Lattner00950542001-06-06 20:29:01 +0000173
174//===----------------------------------------------------------------------===//
175// Code to handle definitions of all the types
176//===----------------------------------------------------------------------===//
177
Chris Lattner2079fde2001-10-13 06:41:08 +0000178static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
179 if (D->hasName()) return -1; // Is this a numbered definition?
180
181 // Yes, insert the value into the value table...
182 unsigned type = D->getType()->getUniqueID();
183 if (ValueTab.size() <= type)
184 ValueTab.resize(type+1, ValueList());
185 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
186 ValueTab[type].push_back(D);
187 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000188}
189
Chris Lattner30c89792001-09-07 16:35:17 +0000190// TODO: FIXME when Type are not const
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000191static void InsertType(const Type *Ty, vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000192 Types.push_back(Ty);
193}
194
195static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000196 switch (D.Type) {
197 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000198 unsigned Num = (unsigned)D.Num;
199
200 // Module constants occupy the lowest numbered slots...
201 if (Num < CurModule.Types.size())
202 return CurModule.Types[Num];
203
204 Num -= CurModule.Types.size();
205
206 // Check that the number is within bounds...
207 if (Num <= CurMeth.Types.size())
208 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000209 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000210 }
211 case 1: { // Is it a named definition?
212 string Name(D.Name);
213 SymbolTable *SymTab = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000214 if (inFunctionScope()) SymTab = CurMeth.CurrentFunction->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000215 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
216
217 if (N == 0) {
218 // Symbol table doesn't automatically chain yet... because the method
219 // hasn't been added to the module...
220 //
221 SymTab = CurModule.CurrentModule->getSymbolTable();
222 if (SymTab)
223 N = SymTab->lookup(Type::TypeTy, Name);
224 if (N == 0) break;
225 }
226
227 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000228 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000229 }
230 default:
231 ThrowException("Invalid symbol type reference!");
232 }
233
234 // If we reached here, we referenced either a symbol that we don't know about
235 // or an id number that hasn't been read yet. We may be referencing something
236 // forward, so just create an entry to be resolved later and get to it...
237 //
238 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
239
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000240 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000241 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
242
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000243 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000244 if (I != LateResolver.end()) {
245 return I->second;
246 }
Chris Lattner30c89792001-09-07 16:35:17 +0000247
Chris Lattner82269592001-10-22 06:01:08 +0000248 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000249 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000250 return Typ;
251}
252
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000253static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
254 SymbolTable *SymTab =
Chris Lattner79df7c02002-03-26 18:01:55 +0000255 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000256 Value *N = SymTab ? SymTab->lookup(Ty, Name) : 0;
257
258 if (N == 0) {
259 // Symbol table doesn't automatically chain yet... because the method
260 // hasn't been added to the module...
261 //
262 SymTab = CurModule.CurrentModule->getSymbolTable();
263 if (SymTab)
264 N = SymTab->lookup(Ty, Name);
265 }
266
267 return N;
268}
269
Chris Lattner2079fde2001-10-13 06:41:08 +0000270// getValNonImprovising - Look up the value specified by the provided type and
271// the provided ValID. If the value exists and has already been defined, return
272// it. Otherwise return null.
273//
274static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000275 if (isa<FunctionType>(Ty))
276 ThrowException("Functions are not values and "
277 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000278
Chris Lattner30c89792001-09-07 16:35:17 +0000279 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000280 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000281 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000282 unsigned Num = (unsigned)D.Num;
283
284 // Module constants occupy the lowest numbered slots...
285 if (type < CurModule.Values.size()) {
286 if (Num < CurModule.Values[type].size())
287 return CurModule.Values[type][Num];
288
289 Num -= CurModule.Values[type].size();
290 }
291
292 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000293 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000294
295 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000296 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000297
298 return CurMeth.Values[type][Num];
299 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000300
Chris Lattner1a1cb112001-09-30 22:46:54 +0000301 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000302 Value *N = lookupInSymbolTable(Ty, string(D.Name));
303 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000304
305 D.destroy(); // Free old strdup'd memory...
306 return N;
307 }
308
Chris Lattner2079fde2001-10-13 06:41:08 +0000309 // Check to make sure that "Ty" is an integral type, and that our
310 // value will fit into the specified type...
311 case ValID::ConstSIntVal: // Is it a constant pool reference??
312 if (Ty == Type::BoolTy) { // Special handling for boolean data
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000313 return ConstantBool::get(D.ConstPool64 != 0);
Chris Lattner2079fde2001-10-13 06:41:08 +0000314 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000315 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner2079fde2001-10-13 06:41:08 +0000316 ThrowException("Symbolic constant pool value '" +
317 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000318 Ty->getDescription() + "'!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000319 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000320 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000321
322 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000323 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
324 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000325 ThrowException("Integral constant pool reference is invalid!");
326 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000327 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000328 }
329 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000330 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000331 }
332
333 case ValID::ConstStringVal: // Is it a string const pool reference?
334 cerr << "FIXME: TODO: String constants [sbyte] not implemented yet!\n";
335 abort();
336 return 0;
337
338 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000339 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000340 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000341 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000342
343 case ValID::ConstNullVal: // Is it a null value?
344 if (!Ty->isPointerType())
345 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000346 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000347
Chris Lattner30c89792001-09-07 16:35:17 +0000348 default:
349 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000350 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000351 } // End of switch
352
Chris Lattner2079fde2001-10-13 06:41:08 +0000353 assert(0 && "Unhandled case!");
354 return 0;
355}
356
357
358// getVal - This function is identical to getValNonImprovising, except that if a
359// value is not already defined, it "improvises" by creating a placeholder var
360// that looks and acts just like the requested variable. When the value is
361// defined later, all uses of the placeholder variable are replaced with the
362// real thing.
363//
364static Value *getVal(const Type *Ty, const ValID &D) {
365 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
366
367 // See if the value has already been defined...
368 Value *V = getValNonImprovising(Ty, D);
369 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000370
371 // If we reached here, we referenced either a symbol that we don't know about
372 // or an id number that hasn't been read yet. We may be referencing something
373 // forward, so just create an entry to be resolved later and get to it...
374 //
Chris Lattner00950542001-06-06 20:29:01 +0000375 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000376 switch (Ty->getPrimitiveID()) {
377 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000378 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000379 }
380
381 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000382 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000383 InsertValue(d, CurMeth.LateResolveValues);
384 else
385 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000386 return d;
387}
388
389
390//===----------------------------------------------------------------------===//
391// Code to handle forward references in instructions
392//===----------------------------------------------------------------------===//
393//
394// This code handles the late binding needed with statements that reference
395// values not defined yet... for example, a forward branch, or the PHI node for
396// a loop body.
397//
398// This keeps a table (CurMeth.LateResolveValues) of all such forward references
399// and back patchs after we are done.
400//
401
402// ResolveDefinitions - If we could not resolve some defs at parsing
403// time (forward branches, phi functions for loops, etc...) resolve the
404// defs now...
405//
Chris Lattner386a3b72001-10-16 19:54:17 +0000406static void ResolveDefinitions(vector<ValueList> &LateResolvers,
407 vector<ValueList> *FutureLateResolvers = 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000408 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
409 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
410 while (!LateResolvers[ty].empty()) {
411 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000412 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
413
Chris Lattner00950542001-06-06 20:29:01 +0000414 LateResolvers[ty].pop_back();
415 ValID &DID = getValIDFromPlaceHolder(V);
416
Chris Lattner2079fde2001-10-13 06:41:08 +0000417 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000418 if (TheRealValue) {
419 V->replaceAllUsesWith(TheRealValue);
420 delete V;
421 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000422 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000423 // resolver table
424 InsertValue(V, *FutureLateResolvers);
425 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000426 if (DID.Type == 1)
427 ThrowException("Reference to an invalid definition: '" +DID.getName()+
428 "' of type '" + V->getType()->getDescription() + "'",
429 getLineNumFromPlaceHolder(V));
430 else
431 ThrowException("Reference to an invalid definition: #" +
432 itostr(DID.Num) + " of type '" +
433 V->getType()->getDescription() + "'",
434 getLineNumFromPlaceHolder(V));
435 }
Chris Lattner00950542001-06-06 20:29:01 +0000436 }
437 }
438
439 LateResolvers.clear();
440}
441
Chris Lattner4a42e902001-10-22 05:56:09 +0000442// ResolveTypeTo - A brand new type was just declared. This means that (if
443// name is not null) things referencing Name can be resolved. Otherwise, things
444// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000445//
Chris Lattner4a42e902001-10-22 05:56:09 +0000446static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000447 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000448 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000449
Chris Lattner4a42e902001-10-22 05:56:09 +0000450 ValID D;
451 if (Name) D = ValID::create(Name);
452 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000453
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000454 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000455 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
456
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000457 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000458 if (I != LateResolver.end()) {
459 cast<DerivedType>(I->second.get())->refineAbstractTypeTo(ToTy);
460 LateResolver.erase(I);
461 }
462}
463
464// ResolveTypes - At this point, all types should be resolved. Any that aren't
465// are errors.
466//
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000467static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000468 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000469 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000470
471 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000472 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000473 else
Chris Lattner82269592001-10-22 06:01:08 +0000474 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000475 }
476}
477
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000478
Chris Lattner1781aca2001-09-18 04:00:54 +0000479// setValueName - Set the specified value to the name given. The name may be
480// null potentially, in which case this is a noop. The string passed in is
481// assumed to be a malloc'd string buffer, and is freed by this function.
482//
Chris Lattnerb7474512001-10-03 15:39:04 +0000483// This function returns true if the value has already been defined, but is
484// allowed to be redefined in the specified context. If the name is a new name
485// for the typeplane, false is returned.
486//
487static bool setValueName(Value *V, char *NameStr) {
488 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000489
Chris Lattner1781aca2001-09-18 04:00:54 +0000490 string Name(NameStr); // Copy string
491 free(NameStr); // Free old string
492
Chris Lattner2079fde2001-10-13 06:41:08 +0000493 if (V->getType() == Type::VoidTy)
494 ThrowException("Can't assign name '" + Name +
495 "' to a null valued instruction!");
496
Chris Lattner79df7c02002-03-26 18:01:55 +0000497 SymbolTable *ST = inFunctionScope() ?
498 CurMeth.CurrentFunction->getSymbolTableSure() :
Chris Lattner30c89792001-09-07 16:35:17 +0000499 CurModule.CurrentModule->getSymbolTableSure();
500
501 Value *Existing = ST->lookup(V->getType(), Name);
502 if (Existing) { // Inserting a name that is already defined???
503 // There is only one case where this is allowed: when we are refining an
504 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000505 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb00c5822001-10-02 03:41:24 +0000506 if (OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000507 // We ARE replacing an opaque type!
Chris Lattnerb00c5822001-10-02 03:41:24 +0000508 OpTy->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000509 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000510 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000511 }
Chris Lattner30c89792001-09-07 16:35:17 +0000512
Chris Lattner9636a912001-10-01 16:18:37 +0000513 // Otherwise, we are a simple redefinition of a value, check to see if it
514 // is defined the same as the old one...
515 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000516 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
517 // cerr << "Type: " << Ty->getDescription() << " != "
518 // << cast<const Type>(V)->getDescription() << "!\n";
519 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000520 // We are allowed to redefine a global variable in two circumstances:
521 // 1. If at least one of the globals is uninitialized or
522 // 2. If both initializers have the same value.
523 //
524 // This can only be done if the const'ness of the vars is the same.
525 //
Chris Lattner89219832001-10-03 19:35:04 +0000526 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
527 if (EGV->isConstant() == GV->isConstant() &&
528 (!EGV->hasInitializer() || !GV->hasInitializer() ||
529 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000530
Chris Lattner89219832001-10-03 19:35:04 +0000531 // Make sure the existing global version gets the initializer!
532 if (GV->hasInitializer() && !EGV->hasInitializer())
533 EGV->setInitializer(GV->getInitializer());
534
Chris Lattner2079fde2001-10-13 06:41:08 +0000535 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000536 return true; // They are equivalent!
537 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000538 }
Chris Lattner9636a912001-10-01 16:18:37 +0000539 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000540 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000541 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000542 }
Chris Lattner00950542001-06-06 20:29:01 +0000543
Chris Lattner30c89792001-09-07 16:35:17 +0000544 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000545 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000546}
547
Chris Lattner8896eda2001-07-09 19:38:36 +0000548
Chris Lattner30c89792001-09-07 16:35:17 +0000549//===----------------------------------------------------------------------===//
550// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000551//
Chris Lattner8896eda2001-07-09 19:38:36 +0000552
Chris Lattner30c89792001-09-07 16:35:17 +0000553// TypeContains - Returns true if Ty contains E in it.
554//
555static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000556 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000557}
Chris Lattner698b56e2001-07-20 19:15:08 +0000558
Chris Lattner30c89792001-09-07 16:35:17 +0000559
560static vector<pair<unsigned, OpaqueType *> > UpRefs;
561
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000562static PATypeHolder HandleUpRefs(const Type *ty) {
563 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000564 UR_OUT("Type '" << ty->getDescription() <<
565 "' newly formed. Resolving upreferences.\n" <<
566 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000567 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000568 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000569 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000570 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000571 if (TypeContains(Ty, UpRefs[i].second)) {
572 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000573 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000574 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000575 UR_OUT(" * Resolving upreference for "
576 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000577 string OldName = UpRefs[i].second->getDescription());
578 UpRefs[i].second->refineAbstractTypeTo(Ty);
579 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000580 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000581 << (const void*)Ty << ", " << Ty->getDescription() << endl);
582 continue;
583 }
584 }
585
586 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000587 }
Chris Lattner30c89792001-09-07 16:35:17 +0000588 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000589 return Ty;
590}
591
Chris Lattner30c89792001-09-07 16:35:17 +0000592
Chris Lattner00950542001-06-06 20:29:01 +0000593//===----------------------------------------------------------------------===//
594// RunVMAsmParser - Define an interface to this parser
595//===----------------------------------------------------------------------===//
596//
Chris Lattnera2850432001-07-22 18:36:00 +0000597Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000598 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000599 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000600 llvmAsmlineno = 1; // Reset the current line number...
601
602 CurModule.CurrentModule = new Module(); // Allocate a new module to read
603 yyparse(); // Parse the file.
604 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000605 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
606 ParserResult = 0;
607
608 return Result;
609}
610
611%}
612
613%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000614 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000615 Function *FunctionVal;
Chris Lattner46748042002-04-09 19:41:42 +0000616 std::pair<Argument*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000617 BasicBlock *BasicBlockVal;
618 TerminatorInst *TermInstVal;
619 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000620 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000621
Chris Lattner30c89792001-09-07 16:35:17 +0000622 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000623 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000624 Value *ValueVal;
625
Chris Lattner46748042002-04-09 19:41:42 +0000626 std::list<std::pair<Argument*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000627 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000628 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000629 std::list<std::pair<Value*,
630 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000631 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000632 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000633
Chris Lattner30c89792001-09-07 16:35:17 +0000634 int64_t SInt64Val;
635 uint64_t UInt64Val;
636 int SIntVal;
637 unsigned UIntVal;
638 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000639 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000640
Chris Lattner30c89792001-09-07 16:35:17 +0000641 char *StrVal; // This memory is strdup'd!
642 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000643
Chris Lattner30c89792001-09-07 16:35:17 +0000644 Instruction::UnaryOps UnaryOpVal;
645 Instruction::BinaryOps BinaryOpVal;
646 Instruction::TermOps TermOpVal;
647 Instruction::MemoryOps MemOpVal;
648 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000649}
650
Chris Lattner79df7c02002-03-26 18:01:55 +0000651%type <ModuleVal> Module FunctionList
652%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000653%type <BasicBlockVal> BasicBlock InstructionList
654%type <TermInstVal> BBTerminatorInst
655%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000656%type <ConstVal> ConstVal
Chris Lattner6cdb0112001-11-26 16:54:11 +0000657%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000658%type <ArgList> ArgList ArgListH
659%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000660%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000661%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000662%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000663%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000664%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000665%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000666
Chris Lattner2079fde2001-10-13 06:41:08 +0000667// ValueRef - Unresolved reference to a definition or BB
668%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000669%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000670// Tokens and types for handling constant integer values
671//
672// ESINT64VAL - A negative number within long long range
673%token <SInt64Val> ESINT64VAL
674
675// EUINT64VAL - A positive number within uns. long long range
676%token <UInt64Val> EUINT64VAL
677%type <SInt64Val> EINT64VAL
678
679%token <SIntVal> SINTVAL // Signed 32 bit ints...
680%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
681%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000682%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000683
684// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000685%type <TypeVal> Types TypesV UpRTypes UpRTypesV
686%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000687%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
688%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000689
690%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
691%type <StrVal> OptVAR_ID OptAssign
692
693
Chris Lattner1781aca2001-09-18 04:00:54 +0000694%token IMPLEMENTATION TRUE FALSE BEGINTOK END DECLARE GLOBAL CONSTANT UNINIT
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000695%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL OPAQUE
Chris Lattner00950542001-06-06 20:29:01 +0000696
697// Basic Block Terminating Operators
698%token <TermOpVal> RET BR SWITCH
699
700// Unary Operators
701%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000702%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000703
704// Binary Operators
705%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000706%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000707%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000708
709// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000710%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000711
Chris Lattner027dcc52001-07-08 21:10:27 +0000712// Other Operators
713%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000714%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000715
Chris Lattner00950542001-06-06 20:29:01 +0000716%start Module
717%%
718
719// Handle constant integer size restriction and conversion...
720//
721
722INTVAL : SINTVAL
723INTVAL : UINTVAL {
724 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
725 ThrowException("Value too large for type!");
726 $$ = (int32_t)$1;
727}
728
729
730EINT64VAL : ESINT64VAL // These have same type and can't cause problems...
731EINT64VAL : EUINT64VAL {
732 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
733 ThrowException("Value too large for type!");
734 $$ = (int64_t)$1;
735}
736
Chris Lattner00950542001-06-06 20:29:01 +0000737// Operations that are notably excluded from this list include:
738// RET, BR, & SWITCH because they end basic blocks and are treated specially.
739//
Chris Lattner09083092001-07-08 04:57:15 +0000740UnaryOps : NOT
Chris Lattner42c9e772001-10-20 09:32:59 +0000741BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR
Chris Lattner00950542001-06-06 20:29:01 +0000742BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE
Chris Lattner027dcc52001-07-08 21:10:27 +0000743ShiftOps : SHL | SHR
Chris Lattner00950542001-06-06 20:29:01 +0000744
Chris Lattnere98dda62001-07-14 06:10:16 +0000745// These are some types that allow classification if we only want a particular
746// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner00950542001-06-06 20:29:01 +0000747SIntType : LONG | INT | SHORT | SBYTE
748UIntType : ULONG | UINT | USHORT | UBYTE
Chris Lattner30c89792001-09-07 16:35:17 +0000749IntType : SIntType | UIntType
750FPType : FLOAT | DOUBLE
Chris Lattner00950542001-06-06 20:29:01 +0000751
Chris Lattnere98dda62001-07-14 06:10:16 +0000752// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000753OptAssign : VAR_ID '=' {
754 $$ = $1;
755 }
756 | /*empty*/ {
757 $$ = 0;
758 }
759
Chris Lattnerdda71962001-11-26 18:54:16 +0000760OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; }
Chris Lattner30c89792001-09-07 16:35:17 +0000761
762//===----------------------------------------------------------------------===//
763// Types includes all predefined types... except void, because it can only be
764// used in specific contexts (method returning void for example). To have
765// access to it, a user must explicitly use TypesV.
766//
767
768// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000769TypesV : Types | VOID { $$ = new PATypeHolder($1); }
770UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); }
Chris Lattner30c89792001-09-07 16:35:17 +0000771
772Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000773 if (UpRefs.size())
774 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
775 $$ = $1;
Chris Lattner30c89792001-09-07 16:35:17 +0000776 }
777
778
779// Derived types are added later...
780//
781PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT
782PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000783UpRTypes : OPAQUE {
784 $$ = new PATypeHolder(OpaqueType::get());
785 }
786 | PrimType {
787 $$ = new PATypeHolder($1);
788 }
Chris Lattner30c89792001-09-07 16:35:17 +0000789UpRTypes : ValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000790 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner30c89792001-09-07 16:35:17 +0000791}
792
Chris Lattner30c89792001-09-07 16:35:17 +0000793// Include derived types in the Types production.
794//
795UpRTypes : '\\' EUINT64VAL { // Type UpReference
796 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
797 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
798 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000799 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000800 UR_OUT("New Upreference!\n");
801 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000802 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000803 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000804 mapto($3->begin(), $3->end(), std::back_inserter(Params),
805 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000806 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
807 if (isVarArg) Params.pop_back();
808
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000809 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000810 delete $3; // Delete the argument list
811 delete $1; // Delete the old type handle
812 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000813 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000814 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000815 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000816 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000817 | '{' TypeListI '}' { // Structure type?
818 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000819 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
820 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000821
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000822 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000823 delete $2;
824 }
825 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000826 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000827 }
828 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000829 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000830 delete $1;
831 }
Chris Lattner30c89792001-09-07 16:35:17 +0000832
833// TypeList - Used for struct declarations and as a basis for method type
834// declaration type lists
835//
836TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000837 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000838 $$->push_back(*$1); delete $1;
839 }
840 | TypeListI ',' UpRTypes {
841 ($$=$1)->push_back(*$3); delete $3;
842 }
843
844// ArgTypeList - List of types for a method type declaration...
845ArgTypeListI : TypeListI
846 | TypeListI ',' DOTDOTDOT {
847 ($$=$1)->push_back(Type::VoidTy);
848 }
849 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000850 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000851 }
852 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000853 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000854 }
855
856
Chris Lattnere98dda62001-07-14 06:10:16 +0000857// ConstVal - The various declarations that go into the constant pool. This
858// includes all forward declarations of types, constants, and functions.
859//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000860ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
861 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
862 if (ATy == 0)
863 ThrowException("Cannot make array constant with type: '" +
864 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000865 const Type *ETy = ATy->getElementType();
866 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000867
Chris Lattner30c89792001-09-07 16:35:17 +0000868 // Verify that we have the correct size...
869 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000870 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000871 utostr($3->size()) + " arguments, but has size of " +
872 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000873
Chris Lattner30c89792001-09-07 16:35:17 +0000874 // Verify all elements are correct type!
875 for (unsigned i = 0; i < $3->size(); i++) {
876 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000877 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000878 ETy->getDescription() +"' as required!\nIt is of type '"+
879 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000880 }
881
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000882 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000883 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000884 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000885 | Types '[' ']' {
886 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
887 if (ATy == 0)
888 ThrowException("Cannot make array constant with type: '" +
889 (*$1)->getDescription() + "'!");
890
891 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000892 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000893 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000894 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000895 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000896 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000897 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000898 | Types 'c' STRINGCONSTANT {
899 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
900 if (ATy == 0)
901 ThrowException("Cannot make array constant with type: '" +
902 (*$1)->getDescription() + "'!");
903
Chris Lattner30c89792001-09-07 16:35:17 +0000904 int NumElements = ATy->getNumElements();
905 const Type *ETy = ATy->getElementType();
906 char *EndStr = UnEscapeLexed($3, true);
907 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000908 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000909 itostr((int)(EndStr-$3)) +
910 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000911 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000912 if (ETy == Type::SByteTy) {
913 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000914 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000915 } else if (ETy == Type::UByteTy) {
916 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000917 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000918 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000919 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000920 ThrowException("Cannot build string arrays of non byte sized elements!");
921 }
Chris Lattner30c89792001-09-07 16:35:17 +0000922 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000923 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000924 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000925 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000926 | Types '{' ConstVector '}' {
927 const StructType *STy = dyn_cast<const StructType>($1->get());
928 if (STy == 0)
929 ThrowException("Cannot make struct constant with type: '" +
930 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000931 // FIXME: TODO: Check to see that the constants are compatible with the type
932 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000933 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000934 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000935 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000936 | Types NULL_TOK {
937 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
938 if (PTy == 0)
939 ThrowException("Cannot make null pointer constant with type: '" +
940 (*$1)->getDescription() + "'!");
941
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000942 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000943 delete $1;
944 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000945 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000946 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
947 if (Ty == 0)
948 ThrowException("Global const reference must be a pointer type!");
949
Chris Lattner2079fde2001-10-13 06:41:08 +0000950 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000951
Chris Lattner2079fde2001-10-13 06:41:08 +0000952 // If this is an initializer for a constant pointer, which is referencing a
953 // (currently) undefined variable, create a stub now that shall be replaced
954 // in the future with the right type of variable.
955 //
956 if (V == 0) {
957 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
958 const PointerType *PT = cast<PointerType>(Ty);
959
960 // First check to see if the forward references value is already created!
961 PerModuleInfo::GlobalRefsType::iterator I =
962 CurModule.GlobalRefs.find(make_pair(PT, $2));
963
964 if (I != CurModule.GlobalRefs.end()) {
965 V = I->second; // Placeholder already exists, use it...
966 } else {
967 // TODO: Include line number info by creating a subclass of
968 // TODO: GlobalVariable here that includes the said information!
969
970 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000971 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
972 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000973 // Keep track of the fact that we have a forward ref to recycle it
974 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
975
976 // Must temporarily push this value into the module table...
977 CurModule.CurrentModule->getGlobalList().push_back(GV);
978 V = GV;
979 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000980 }
981
Chris Lattner2079fde2001-10-13 06:41:08 +0000982 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000983 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000984 delete $1; // Free the type handle
Chris Lattner00950542001-06-06 20:29:01 +0000985 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000986
Chris Lattner00950542001-06-06 20:29:01 +0000987
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000988ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000989 if (!ConstantSInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000990 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000991 $$ = ConstantSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000992 }
993 | UIntType EUINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000994 if (!ConstantUInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000995 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000996 $$ = ConstantUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000997 }
998 | BOOL TRUE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000999 $$ = ConstantBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001000 }
1001 | BOOL FALSE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001002 $$ = ConstantBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001003 }
1004 | FPType FPVAL { // Float & Double constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001005 $$ = ConstantFP::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001006 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001007
Chris Lattnere98dda62001-07-14 06:10:16 +00001008// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001009ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001010 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001011 }
1012 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001013 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001014 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001015 }
1016
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001017
Chris Lattner1781aca2001-09-18 04:00:54 +00001018// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1019GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; }
1020
Chris Lattner00950542001-06-06 20:29:01 +00001021
Chris Lattnere98dda62001-07-14 06:10:16 +00001022// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001023ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001024 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001025 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001026 }
Chris Lattner30c89792001-09-07 16:35:17 +00001027 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001028 // Eagerly resolve types. This is not an optimization, this is a
1029 // requirement that is due to the fact that we could have this:
1030 //
1031 // %list = type { %list * }
1032 // %list = type { %list * } ; repeated type decl
1033 //
1034 // If types are not resolved eagerly, then the two types will not be
1035 // determined to be the same type!
1036 //
1037 ResolveTypeTo($2, $4->get());
1038
Chris Lattner1781aca2001-09-18 04:00:54 +00001039 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001040 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1041 // If this is not a redefinition of a type...
1042 if (!$2) {
1043 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001044 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001045 }
Chris Lattner30c89792001-09-07 16:35:17 +00001046 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001047
1048 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001049 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001050 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001051 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001052 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1053 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001054 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001055 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001056 if (Initializer == 0)
1057 ThrowException("Global value initializer is not a constant!");
1058
Chris Lattnerdda71962001-11-26 18:54:16 +00001059 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001060 if (!setValueName(GV, $2)) { // If not redefining...
1061 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001062 int Slot = InsertValue(GV, CurModule.Values);
1063
1064 if (Slot != -1) {
1065 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1066 } else {
1067 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1068 (char*)GV->getName().c_str()));
1069 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001070 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001071 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001072 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1073 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001074 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001075 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001076 if (!setValueName(GV, $2)) { // If not redefining...
1077 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001078 int Slot = InsertValue(GV, CurModule.Values);
1079
1080 if (Slot != -1) {
1081 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1082 } else {
1083 assert(GV->hasName() && "Not named and not numbered!?");
1084 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1085 (char*)GV->getName().c_str()));
1086 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001087 }
Chris Lattner09c07532002-03-31 07:16:49 +00001088 delete $6;
Chris Lattnere98dda62001-07-14 06:10:16 +00001089 }
Chris Lattner00950542001-06-06 20:29:01 +00001090 | /* empty: end of list */ {
1091 }
1092
1093
1094//===----------------------------------------------------------------------===//
1095// Rules to match Modules
1096//===----------------------------------------------------------------------===//
1097
1098// Module rule: Capture the result of parsing the whole file into a result
1099// variable...
1100//
Chris Lattner79df7c02002-03-26 18:01:55 +00001101Module : FunctionList {
Chris Lattner00950542001-06-06 20:29:01 +00001102 $$ = ParserResult = $1;
1103 CurModule.ModuleDone();
1104}
1105
Chris Lattner79df7c02002-03-26 18:01:55 +00001106// FunctionList - A list of methods, preceeded by a constant pool.
Chris Lattnere98dda62001-07-14 06:10:16 +00001107//
Chris Lattner79df7c02002-03-26 18:01:55 +00001108FunctionList : FunctionList Function {
Chris Lattner00950542001-06-06 20:29:01 +00001109 $$ = $1;
Chris Lattner79df7c02002-03-26 18:01:55 +00001110 assert($2->getParent() == 0 && "Function already in module!");
1111 $1->getFunctionList().push_back($2);
1112 CurMeth.FunctionDone();
Chris Lattner00950542001-06-06 20:29:01 +00001113 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001114 | FunctionList FunctionProto {
Chris Lattnere1815642001-07-15 06:35:53 +00001115 $$ = $1;
Chris Lattnere1815642001-07-15 06:35:53 +00001116 }
Chris Lattner00950542001-06-06 20:29:01 +00001117 | ConstPool IMPLEMENTATION {
1118 $$ = CurModule.CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +00001119 // Resolve circular types before we parse the body of the module
1120 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001121 }
1122
1123
1124//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001125// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001126//===----------------------------------------------------------------------===//
1127
1128OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; }
1129
1130ArgVal : Types OptVAR_ID {
Chris Lattner46748042002-04-09 19:41:42 +00001131 $$ = new pair<Argument*, char*>(new Argument(*$1), $2);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001132 delete $1; // Delete the type handle..
Chris Lattner00950542001-06-06 20:29:01 +00001133}
1134
1135ArgListH : ArgVal ',' ArgListH {
1136 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001137 $3->push_front(*$1);
1138 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001139 }
1140 | ArgVal {
Chris Lattner46748042002-04-09 19:41:42 +00001141 $$ = new list<pair<Argument*,char*> >();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001142 $$->push_front(*$1);
1143 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001144 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001145 | DOTDOTDOT {
Chris Lattner46748042002-04-09 19:41:42 +00001146 $$ = new list<pair<Argument*, char*> >();
1147 $$->push_front(pair<Argument*,char*>(new Argument(Type::VoidTy), 0));
Chris Lattner8b81bf52001-07-25 22:47:46 +00001148 }
Chris Lattner00950542001-06-06 20:29:01 +00001149
1150ArgList : ArgListH {
1151 $$ = $1;
1152 }
1153 | /* empty */ {
1154 $$ = 0;
1155 }
1156
Chris Lattner79df7c02002-03-26 18:01:55 +00001157FunctionHeaderH : OptInternal TypesV STRINGCONSTANT '(' ArgList ')' {
Chris Lattnerdda71962001-11-26 18:54:16 +00001158 UnEscapeLexed($3);
Chris Lattner79df7c02002-03-26 18:01:55 +00001159 string FunctionName($3);
Chris Lattnerdda71962001-11-26 18:54:16 +00001160
Chris Lattner30c89792001-09-07 16:35:17 +00001161 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001162 if ($5)
Chris Lattner46748042002-04-09 19:41:42 +00001163 for (list<pair<Argument*,char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001164 I != $5->end(); ++I)
1165 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001166
Chris Lattner2079fde2001-10-13 06:41:08 +00001167 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1168 if (isVarArg) ParamTypeList.pop_back();
1169
Chris Lattner79df7c02002-03-26 18:01:55 +00001170 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001171 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001172 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001173
Chris Lattner79df7c02002-03-26 18:01:55 +00001174 Function *M = 0;
Chris Lattnere1815642001-07-15 06:35:53 +00001175 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner79df7c02002-03-26 18:01:55 +00001176 // Is the function already in symtab?
1177 if (Value *V = ST->lookup(PMT, FunctionName)) {
1178 M = cast<Function>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001179
Chris Lattnere1815642001-07-15 06:35:53 +00001180 // Yes it is. If this is the case, either we need to be a forward decl,
1181 // or it needs to be.
1182 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner79df7c02002-03-26 18:01:55 +00001183 ThrowException("Redefinition of method '" + FunctionName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001184
1185 // If we found a preexisting method prototype, remove it from the module,
1186 // so that we don't get spurious conflicts with global & local variables.
1187 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001188 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001189 }
1190 }
1191
1192 if (M == 0) { // Not already defined?
Chris Lattner79df7c02002-03-26 18:01:55 +00001193 M = new Function(MT, $1, FunctionName);
Chris Lattnere1815642001-07-15 06:35:53 +00001194 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001195 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001196 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001197 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001198
Chris Lattner79df7c02002-03-26 18:01:55 +00001199 CurMeth.FunctionStart(M);
Chris Lattner00950542001-06-06 20:29:01 +00001200
1201 // Add all of the arguments we parsed to the method...
Chris Lattnerdda71962001-11-26 18:54:16 +00001202 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner79df7c02002-03-26 18:01:55 +00001203 Function::ArgumentListType &ArgList = M->getArgumentList();
Chris Lattner00950542001-06-06 20:29:01 +00001204
Chris Lattner46748042002-04-09 19:41:42 +00001205 for (list<pair<Argument*, char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001206 I != $5->end(); ++I) {
1207 if (setValueName(I->first, I->second)) { // Insert into symtab...
1208 assert(0 && "No arg redef allowed!");
1209 }
1210
1211 InsertValue(I->first);
1212 ArgList.push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001213 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001214 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001215 } else if ($5) {
1216 // If we are a declaration, we should free the memory for the argument list!
Chris Lattner46748042002-04-09 19:41:42 +00001217 for (list<pair<Argument*, char*> >::iterator I = $5->begin(), E = $5->end();
1218 I != E; ++I) {
Chris Lattner9176fe42002-03-08 18:57:56 +00001219 if (I->second) free(I->second); // Free the memory for the name...
Chris Lattner09c07532002-03-31 07:16:49 +00001220 delete I->first; // Free the unused function argument
1221 }
Chris Lattner9176fe42002-03-08 18:57:56 +00001222 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001223 }
1224}
1225
Chris Lattner79df7c02002-03-26 18:01:55 +00001226FunctionHeader : FunctionHeaderH ConstPool BEGINTOK {
1227 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001228
1229 // Resolve circular types before we parse the body of the method.
1230 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner00950542001-06-06 20:29:01 +00001231}
1232
Chris Lattner79df7c02002-03-26 18:01:55 +00001233Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001234 $$ = $1;
1235}
1236
Chris Lattner79df7c02002-03-26 18:01:55 +00001237FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1238 $$ = CurMeth.CurrentFunction;
1239 assert($$->getParent() == 0 && "Function already in module!");
1240 CurModule.CurrentModule->getFunctionList().push_back($$);
1241 CurMeth.FunctionDone();
Chris Lattnere1815642001-07-15 06:35:53 +00001242}
Chris Lattner00950542001-06-06 20:29:01 +00001243
1244//===----------------------------------------------------------------------===//
1245// Rules to match Basic Blocks
1246//===----------------------------------------------------------------------===//
1247
1248ConstValueRef : ESINT64VAL { // A reference to a direct constant
1249 $$ = ValID::create($1);
1250 }
1251 | EUINT64VAL {
1252 $$ = ValID::create($1);
1253 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001254 | FPVAL { // Perhaps it's an FP constant?
1255 $$ = ValID::create($1);
1256 }
Chris Lattner00950542001-06-06 20:29:01 +00001257 | TRUE {
1258 $$ = ValID::create((int64_t)1);
1259 }
1260 | FALSE {
1261 $$ = ValID::create((int64_t)0);
1262 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001263 | NULL_TOK {
1264 $$ = ValID::createNull();
1265 }
1266
Chris Lattner93750fa2001-07-28 17:48:55 +00001267/*
Chris Lattner00950542001-06-06 20:29:01 +00001268 | STRINGCONSTANT { // Quoted strings work too... especially for methods
1269 $$ = ValID::create_conststr($1);
1270 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001271*/
Chris Lattner00950542001-06-06 20:29:01 +00001272
Chris Lattner2079fde2001-10-13 06:41:08 +00001273// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1274// another value.
1275//
1276SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001277 $$ = ValID::create($1);
1278 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001279 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001280 $$ = ValID::create($1);
1281 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001282
1283// ValueRef - A reference to a definition... either constant or symbolic
1284ValueRef : SymbolicValueRef | ConstValueRef
1285
Chris Lattner00950542001-06-06 20:29:01 +00001286
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001287// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1288// type immediately preceeds the value reference, and allows complex constant
1289// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001290ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001291 $$ = getVal(*$1, $2); delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001292 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001293
Chris Lattner00950542001-06-06 20:29:01 +00001294
1295BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner89219832001-10-03 19:35:04 +00001296 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001297 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001298 | FunctionHeader BasicBlock { // Do not allow methods with 0 basic blocks
Chris Lattner89219832001-10-03 19:35:04 +00001299 ($$ = $1)->getBasicBlocks().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001300 }
1301
1302
1303// Basic blocks are terminated by branching instructions:
1304// br, br/cc, switch, ret
1305//
Chris Lattner2079fde2001-10-13 06:41:08 +00001306BasicBlock : InstructionList OptAssign BBTerminatorInst {
1307 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1308 InsertValue($3);
1309
1310 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001311 InsertValue($1);
1312 $$ = $1;
1313 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001314 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1315 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1316 InsertValue($4);
1317
1318 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001319 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001320
1321 InsertValue($2);
1322 $$ = $2;
1323 }
1324
1325InstructionList : InstructionList Inst {
1326 $1->getInstList().push_back($2);
1327 $$ = $1;
1328 }
1329 | /* empty */ {
1330 $$ = new BasicBlock();
1331 }
1332
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001333BBTerminatorInst : RET ResolvedVal { // Return with a result...
1334 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001335 }
1336 | RET VOID { // Return with no result...
1337 $$ = new ReturnInst();
1338 }
1339 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001340 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001341 } // Conditional Branch...
1342 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001343 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1344 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001345 getVal(Type::BoolTy, $3));
1346 }
1347 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1348 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001349 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001350 $$ = S;
1351
Chris Lattner46748042002-04-09 19:41:42 +00001352 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1353 E = $8->end();
1354 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001355 S->dest_push_back(I->first, I->second);
1356 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001357 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1358 EXCEPT ResolvedVal {
1359 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001360 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001361
1362 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001363 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001364 // Pull out the types of all of the arguments...
1365 vector<const Type*> ParamTypes;
1366 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001367 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001368 ParamTypes.push_back((*I)->getType());
1369 }
1370
1371 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1372 if (isVarArg) ParamTypes.pop_back();
1373
Chris Lattner79df7c02002-03-26 18:01:55 +00001374 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner2079fde2001-10-13 06:41:08 +00001375 PMTy = PointerType::get(Ty);
1376 }
1377 delete $2;
1378
1379 Value *V = getVal(PMTy, $3); // Get the method we're calling...
1380
1381 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1382 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1383
1384 if (Normal == 0 || Except == 0)
1385 ThrowException("Invoke instruction without label destinations!");
1386
1387 // Create the call node...
1388 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001389 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001390 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001391 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001392 // correctly!
1393 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001394 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1395 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001396 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001397
1398 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1399 if ((*ArgI)->getType() != *I)
1400 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001401 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001402
1403 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1404 ThrowException("Invalid number of parameters detected!");
1405
Chris Lattner6cdb0112001-11-26 16:54:11 +00001406 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001407 }
1408 delete $5;
1409 }
1410
1411
Chris Lattner00950542001-06-06 20:29:01 +00001412
1413JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1414 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001415 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001416 if (V == 0)
1417 ThrowException("May only switch on a constant pool value!");
1418
Chris Lattner9636a912001-10-01 16:18:37 +00001419 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001420 }
1421 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001422 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001423 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001424
1425 if (V == 0)
1426 ThrowException("May only switch on a constant pool value!");
1427
Chris Lattner9636a912001-10-01 16:18:37 +00001428 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner00950542001-06-06 20:29:01 +00001429 }
1430
1431Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001432 // Is this definition named?? if so, assign the name...
1433 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001434 InsertValue($2);
1435 $$ = $2;
1436}
1437
Chris Lattnerc24d2082001-06-11 15:04:20 +00001438PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1439 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001440 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001441 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001442 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001443 }
1444 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1445 $$ = $1;
1446 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001447 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattnerc24d2082001-06-11 15:04:20 +00001448 }
1449
1450
Chris Lattner30c89792001-09-07 16:35:17 +00001451ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001452 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001453 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001454 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001455 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001456 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001457 $1->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001458 }
1459
1460// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1461ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; }
1462
1463InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001464 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001465 if ($$ == 0)
1466 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001467 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001468 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001469 | UnaryOps ResolvedVal {
1470 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001471 if ($$ == 0)
1472 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001473 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001474 | ShiftOps ResolvedVal ',' ResolvedVal {
1475 if ($4->getType() != Type::UByteTy)
1476 ThrowException("Shift amount must be ubyte!");
1477 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001478 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001479 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001480 $$ = new CastInst($2, *$4);
1481 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001482 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001483 | PHI PHIList {
1484 const Type *Ty = $2->front().first->getType();
1485 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001486 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001487 if ($2->front().first->getType() != Ty)
1488 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001489 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001490 $2->pop_front();
1491 }
1492 delete $2; // Free the list...
1493 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001494 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001495 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001496 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001497
Chris Lattneref9c23f2001-10-03 14:53:21 +00001498 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001499 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001500 // Pull out the types of all of the arguments...
1501 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001502 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001503 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001504 ParamTypes.push_back((*I)->getType());
1505 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001506
1507 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1508 if (isVarArg) ParamTypes.pop_back();
1509
Chris Lattner79df7c02002-03-26 18:01:55 +00001510 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001511 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001512 }
Chris Lattner30c89792001-09-07 16:35:17 +00001513 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001514
Chris Lattneref9c23f2001-10-03 14:53:21 +00001515 Value *V = getVal(PMTy, $3); // Get the method we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001516
Chris Lattner8b81bf52001-07-25 22:47:46 +00001517 // Create the call node...
1518 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001519 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001520 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001521 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001522 // correctly!
1523 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001524 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1525 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001526 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001527
1528 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1529 if ((*ArgI)->getType() != *I)
1530 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001531 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001532
Chris Lattner8b81bf52001-07-25 22:47:46 +00001533 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001534 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001535
Chris Lattner6cdb0112001-11-26 16:54:11 +00001536 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001537 }
1538 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001539 }
1540 | MemoryInst {
1541 $$ = $1;
1542 }
1543
Chris Lattner6cdb0112001-11-26 16:54:11 +00001544
1545// IndexList - List of indices for GEP based instructions...
1546IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001547 $$ = $2;
1548} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001549 $$ = new vector<Value*>();
Chris Lattner027dcc52001-07-08 21:10:27 +00001550}
1551
Chris Lattner00950542001-06-06 20:29:01 +00001552MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001553 $$ = new MallocInst(PointerType::get(*$2));
1554 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001555 }
1556 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001557 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001558 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001559 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001560 }
1561 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001562 $$ = new AllocaInst(PointerType::get(*$2));
1563 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001564 }
1565 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001566 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001567 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001568 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001569 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001570 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001571 | FREE ResolvedVal {
1572 if (!$2->getType()->isPointerType())
1573 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001574 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001575 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001576 }
1577
Chris Lattner6cdb0112001-11-26 16:54:11 +00001578 | LOAD Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001579 if (!(*$2)->isPointerType())
Chris Lattner2079fde2001-10-13 06:41:08 +00001580 ThrowException("Can't load from nonpointer type: " +
1581 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001582 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001583 ThrowException("Invalid indices for load instruction!");
1584
Chris Lattner30c89792001-09-07 16:35:17 +00001585 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001586 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001587 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001588 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001589 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001590 if (!(*$4)->isPointerType())
Chris Lattner72e00252001-12-14 16:28:42 +00001591 ThrowException("Can't store to a nonpointer type: " +
1592 (*$4)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001593 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001594 if (ElTy == 0)
1595 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001596 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001597 ThrowException("Can't store '" + $2->getType()->getDescription() +
1598 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001599 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1600 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001601 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001602 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner30c89792001-09-07 16:35:17 +00001603 if (!(*$2)->isPointerType())
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001604 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001605 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001606 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001607 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1608 delete $2; delete $4;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001609 }
Chris Lattner027dcc52001-07-08 21:10:27 +00001610
Chris Lattner00950542001-06-06 20:29:01 +00001611%%
Chris Lattner09083092001-07-08 04:57:15 +00001612int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001613 ThrowException(string("Parse error: ") + ErrorMsg);
1614 return 0;
1615}