blob: 4b9f36fe71c35b668b5d46af672d1bf4ddc34e31 [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 Lattner00950542001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000011#include "llvm/GlobalVariable.h"
Chris Lattner00950542001-06-06 20:29:01 +000012#include "llvm/iTerminators.h"
13#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
Chris Lattner46748042002-04-09 19:41:42 +000015#include "llvm/Argument.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000016#include "Support/STLExtras.h"
17#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include <list>
19#include <utility> // Get definition of pair class
Chris Lattner30c89792001-09-07 16:35:17 +000020#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000021#include <iostream>
22using std::list;
23using std::vector;
24using std::pair;
25using std::map;
26using std::pair;
27using std::make_pair;
28using std::cerr;
29using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000030
Chris Lattner386a3b72001-10-16 19:54:17 +000031int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000032int 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 Lattner7e708292002-06-25 16:13:24 +000048// This contains info used when building the body of a function. It is
49// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000050//
51typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000052static void ResolveDefinitions(vector<ValueList> &LateResolvers,
53 vector<ValueList> *FutureLateResolvers = 0);
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;
Chris Lattner8b88b3b2002-04-04 19:23:55 +000059 vector<PATypeHolder> Types;
60 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000061
Chris Lattner2079fde2001-10-13 06:41:08 +000062 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
63 // references to global values. Global values may be referenced before they
64 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000065 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000066 //
67 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
68 GlobalRefsType GlobalRefs;
69
Chris Lattner00950542001-06-06 20:29:01 +000070 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000071 // If we could not resolve some functions at function compilation time
72 // (calls to functions before they are defined), resolve them now... Types
73 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000074 //
Chris Lattner00950542001-06-06 20:29:01 +000075 ResolveDefinitions(LateResolveValues);
76
Chris Lattner2079fde2001-10-13 06:41:08 +000077 // Check to make sure that all global value forward references have been
78 // resolved!
79 //
80 if (!GlobalRefs.empty()) {
Chris Lattner749ce032002-03-11 22:12:39 +000081 string UndefinedReferences = "Unresolved global references exist:\n";
82
83 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
84 I != E; ++I) {
85 UndefinedReferences += " " + I->first.first->getDescription() + " " +
86 I->first.second.getName() + "\n";
87 }
88 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000089 }
90
Chris Lattner7e708292002-06-25 16:13:24 +000091 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000092 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000093 CurrentModule = 0;
94 }
Chris Lattner2079fde2001-10-13 06:41:08 +000095
96
97 // DeclareNewGlobalValue - Called every type a new GV has been defined. This
98 // is used to remove things from the forward declaration map, resolving them
99 // to the correct thing as needed.
100 //
101 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
102 // Check to see if there is a forward reference to this global variable...
103 // if there is, eliminate it and patch the reference to use the new def'n.
104 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
105
106 if (I != GlobalRefs.end()) {
107 GlobalVariable *OldGV = I->second; // Get the placeholder...
108 I->first.second.destroy(); // Free string memory if neccesary
109
110 // Loop over all of the uses of the GlobalValue. The only thing they are
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000111 // allowed to be at this point is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000112 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
113 while (!OldGV->use_empty()) {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000114 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
115 ConstantPointerRef *CPPR = cast<ConstantPointerRef>(U);
Chris Lattner2079fde2001-10-13 06:41:08 +0000116 assert(CPPR->getValue() == OldGV && "Something isn't happy");
117
118 // Change the const pool reference to point to the real global variable
119 // now. This should drop a use from the OldGV.
120 CPPR->mutateReference(GV);
121 }
122
123 // Remove GV from the module...
124 CurrentModule->getGlobalList().remove(OldGV);
125 delete OldGV; // Delete the old placeholder
126
127 // Remove the map entry for the global now that it has been created...
128 GlobalRefs.erase(I);
129 }
130 }
131
Chris Lattner00950542001-06-06 20:29:01 +0000132} CurModule;
133
Chris Lattner79df7c02002-03-26 18:01:55 +0000134static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000135 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000136
Chris Lattnere1815642001-07-15 06:35:53 +0000137 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000138 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000139 vector<PATypeHolder> Types;
140 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000141 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000142
Chris Lattner79df7c02002-03-26 18:01:55 +0000143 inline PerFunctionInfo() {
144 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000145 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000146 }
147
Chris Lattner79df7c02002-03-26 18:01:55 +0000148 inline ~PerFunctionInfo() {}
Chris Lattner00950542001-06-06 20:29:01 +0000149
Chris Lattner79df7c02002-03-26 18:01:55 +0000150 inline void FunctionStart(Function *M) {
151 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000152 }
153
Chris Lattner79df7c02002-03-26 18:01:55 +0000154 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000155 // If we could not resolve some blocks at parsing time (forward branches)
156 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000157 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000158
Chris Lattner7e708292002-06-25 16:13:24 +0000159 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000160 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000161 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000162 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000163 }
Chris Lattner7e708292002-06-25 16:13:24 +0000164} CurMeth; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000165
Chris Lattner79df7c02002-03-26 18:01:55 +0000166static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000167
Chris Lattner00950542001-06-06 20:29:01 +0000168
169//===----------------------------------------------------------------------===//
170// Code to handle definitions of all the types
171//===----------------------------------------------------------------------===//
172
Chris Lattner2079fde2001-10-13 06:41:08 +0000173static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
174 if (D->hasName()) return -1; // Is this a numbered definition?
175
176 // Yes, insert the value into the value table...
177 unsigned type = D->getType()->getUniqueID();
178 if (ValueTab.size() <= type)
179 ValueTab.resize(type+1, ValueList());
180 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
181 ValueTab[type].push_back(D);
182 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000183}
184
Chris Lattner30c89792001-09-07 16:35:17 +0000185// TODO: FIXME when Type are not const
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000186static void InsertType(const Type *Ty, vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000187 Types.push_back(Ty);
188}
189
190static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000191 switch (D.Type) {
192 case 0: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000193 unsigned Num = (unsigned)D.Num;
194
195 // Module constants occupy the lowest numbered slots...
196 if (Num < CurModule.Types.size())
197 return CurModule.Types[Num];
198
199 Num -= CurModule.Types.size();
200
201 // Check that the number is within bounds...
202 if (Num <= CurMeth.Types.size())
203 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000204 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000205 }
206 case 1: { // Is it a named definition?
207 string Name(D.Name);
208 SymbolTable *SymTab = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000209 if (inFunctionScope()) SymTab = CurMeth.CurrentFunction->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000210 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
211
212 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000213 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000214 // hasn't been added to the module...
215 //
216 SymTab = CurModule.CurrentModule->getSymbolTable();
217 if (SymTab)
218 N = SymTab->lookup(Type::TypeTy, Name);
219 if (N == 0) break;
220 }
221
222 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000223 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000224 }
225 default:
226 ThrowException("Invalid symbol type reference!");
227 }
228
229 // If we reached here, we referenced either a symbol that we don't know about
230 // or an id number that hasn't been read yet. We may be referencing something
231 // forward, so just create an entry to be resolved later and get to it...
232 //
233 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
234
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000235 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000236 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
237
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000238 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000239 if (I != LateResolver.end()) {
240 return I->second;
241 }
Chris Lattner30c89792001-09-07 16:35:17 +0000242
Chris Lattner82269592001-10-22 06:01:08 +0000243 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000244 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000245 return Typ;
246}
247
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000248static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
249 SymbolTable *SymTab =
Chris Lattner9705a152002-05-02 19:27:42 +0000250 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
251 CurModule.CurrentModule->getSymbolTable();
Chris Lattner924025e2002-04-29 18:25:33 +0000252 return SymTab ? SymTab->lookup(Ty, Name) : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000253}
254
Chris Lattner2079fde2001-10-13 06:41:08 +0000255// getValNonImprovising - Look up the value specified by the provided type and
256// the provided ValID. If the value exists and has already been defined, return
257// it. Otherwise return null.
258//
259static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000260 if (isa<FunctionType>(Ty))
261 ThrowException("Functions are not values and "
262 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000263
Chris Lattner30c89792001-09-07 16:35:17 +0000264 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000265 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000266 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000267 unsigned Num = (unsigned)D.Num;
268
269 // Module constants occupy the lowest numbered slots...
270 if (type < CurModule.Values.size()) {
271 if (Num < CurModule.Values[type].size())
272 return CurModule.Values[type][Num];
273
274 Num -= CurModule.Values[type].size();
275 }
276
277 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000278 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000279
280 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000281 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000282
283 return CurMeth.Values[type][Num];
284 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000285
Chris Lattner1a1cb112001-09-30 22:46:54 +0000286 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000287 Value *N = lookupInSymbolTable(Ty, string(D.Name));
288 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000289
290 D.destroy(); // Free old strdup'd memory...
291 return N;
292 }
293
Chris Lattner2079fde2001-10-13 06:41:08 +0000294 // Check to make sure that "Ty" is an integral type, and that our
295 // value will fit into the specified type...
296 case ValID::ConstSIntVal: // Is it a constant pool reference??
297 if (Ty == Type::BoolTy) { // Special handling for boolean data
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000298 return ConstantBool::get(D.ConstPool64 != 0);
Chris Lattner2079fde2001-10-13 06:41:08 +0000299 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000300 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattner2079fde2001-10-13 06:41:08 +0000301 ThrowException("Symbolic constant pool value '" +
302 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000303 Ty->getDescription() + "'!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000304 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000305 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000306
307 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000308 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
309 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000310 ThrowException("Integral constant pool reference is invalid!");
311 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000312 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000313 }
314 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000315 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000316 }
317
Chris Lattner2079fde2001-10-13 06:41:08 +0000318 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000319 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000320 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000321 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000322
323 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000324 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000325 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000326 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000327
Chris Lattner30c89792001-09-07 16:35:17 +0000328 default:
329 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000330 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000331 } // End of switch
332
Chris Lattner2079fde2001-10-13 06:41:08 +0000333 assert(0 && "Unhandled case!");
334 return 0;
335}
336
337
338// getVal - This function is identical to getValNonImprovising, except that if a
339// value is not already defined, it "improvises" by creating a placeholder var
340// that looks and acts just like the requested variable. When the value is
341// defined later, all uses of the placeholder variable are replaced with the
342// real thing.
343//
344static Value *getVal(const Type *Ty, const ValID &D) {
345 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
346
347 // See if the value has already been defined...
348 Value *V = getValNonImprovising(Ty, D);
349 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000350
351 // If we reached here, we referenced either a symbol that we don't know about
352 // or an id number that hasn't been read yet. We may be referencing something
353 // forward, so just create an entry to be resolved later and get to it...
354 //
Chris Lattner00950542001-06-06 20:29:01 +0000355 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000356 switch (Ty->getPrimitiveID()) {
357 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000358 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000359 }
360
361 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000362 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000363 InsertValue(d, CurMeth.LateResolveValues);
364 else
365 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000366 return d;
367}
368
369
370//===----------------------------------------------------------------------===//
371// Code to handle forward references in instructions
372//===----------------------------------------------------------------------===//
373//
374// This code handles the late binding needed with statements that reference
375// values not defined yet... for example, a forward branch, or the PHI node for
376// a loop body.
377//
378// This keeps a table (CurMeth.LateResolveValues) of all such forward references
379// and back patchs after we are done.
380//
381
382// ResolveDefinitions - If we could not resolve some defs at parsing
383// time (forward branches, phi functions for loops, etc...) resolve the
384// defs now...
385//
Chris Lattner386a3b72001-10-16 19:54:17 +0000386static void ResolveDefinitions(vector<ValueList> &LateResolvers,
387 vector<ValueList> *FutureLateResolvers = 0) {
Chris Lattner00950542001-06-06 20:29:01 +0000388 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
389 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
390 while (!LateResolvers[ty].empty()) {
391 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000392 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
393
Chris Lattner00950542001-06-06 20:29:01 +0000394 LateResolvers[ty].pop_back();
395 ValID &DID = getValIDFromPlaceHolder(V);
396
Chris Lattner2079fde2001-10-13 06:41:08 +0000397 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000398 if (TheRealValue) {
399 V->replaceAllUsesWith(TheRealValue);
400 delete V;
401 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000402 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000403 // resolver table
404 InsertValue(V, *FutureLateResolvers);
405 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000406 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000407 ThrowException("Reference to an invalid definition: '" +DID.getName()+
408 "' of type '" + V->getType()->getDescription() + "'",
409 getLineNumFromPlaceHolder(V));
410 else
411 ThrowException("Reference to an invalid definition: #" +
412 itostr(DID.Num) + " of type '" +
413 V->getType()->getDescription() + "'",
414 getLineNumFromPlaceHolder(V));
415 }
Chris Lattner00950542001-06-06 20:29:01 +0000416 }
417 }
418
419 LateResolvers.clear();
420}
421
Chris Lattner4a42e902001-10-22 05:56:09 +0000422// ResolveTypeTo - A brand new type was just declared. This means that (if
423// name is not null) things referencing Name can be resolved. Otherwise, things
424// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000425//
Chris Lattner4a42e902001-10-22 05:56:09 +0000426static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000427 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000428 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000429
Chris Lattner4a42e902001-10-22 05:56:09 +0000430 ValID D;
431 if (Name) D = ValID::create(Name);
432 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000433
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000434 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000435 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
436
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000437 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000438 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000439 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000440 LateResolver.erase(I);
441 }
442}
443
444// ResolveTypes - At this point, all types should be resolved. Any that aren't
445// are errors.
446//
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000447static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000448 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000449 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000450
451 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000452 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000453 else
Chris Lattner82269592001-10-22 06:01:08 +0000454 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000455 }
456}
457
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000458
Chris Lattner1781aca2001-09-18 04:00:54 +0000459// setValueName - Set the specified value to the name given. The name may be
460// null potentially, in which case this is a noop. The string passed in is
461// assumed to be a malloc'd string buffer, and is freed by this function.
462//
Chris Lattnerb7474512001-10-03 15:39:04 +0000463// This function returns true if the value has already been defined, but is
464// allowed to be redefined in the specified context. If the name is a new name
465// for the typeplane, false is returned.
466//
467static bool setValueName(Value *V, char *NameStr) {
468 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000469
Chris Lattner1781aca2001-09-18 04:00:54 +0000470 string Name(NameStr); // Copy string
471 free(NameStr); // Free old string
472
Chris Lattner2079fde2001-10-13 06:41:08 +0000473 if (V->getType() == Type::VoidTy)
474 ThrowException("Can't assign name '" + Name +
475 "' to a null valued instruction!");
476
Chris Lattner79df7c02002-03-26 18:01:55 +0000477 SymbolTable *ST = inFunctionScope() ?
478 CurMeth.CurrentFunction->getSymbolTableSure() :
Chris Lattner30c89792001-09-07 16:35:17 +0000479 CurModule.CurrentModule->getSymbolTableSure();
480
481 Value *Existing = ST->lookup(V->getType(), Name);
482 if (Existing) { // Inserting a name that is already defined???
483 // There is only one case where this is allowed: when we are refining an
484 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000485 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000486 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000487 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000488 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000489 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000490 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000491 }
Chris Lattner30c89792001-09-07 16:35:17 +0000492
Chris Lattner9636a912001-10-01 16:18:37 +0000493 // Otherwise, we are a simple redefinition of a value, check to see if it
494 // is defined the same as the old one...
495 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000496 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
497 // cerr << "Type: " << Ty->getDescription() << " != "
498 // << cast<const Type>(V)->getDescription() << "!\n";
499 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000500 // We are allowed to redefine a global variable in two circumstances:
501 // 1. If at least one of the globals is uninitialized or
502 // 2. If both initializers have the same value.
503 //
504 // This can only be done if the const'ness of the vars is the same.
505 //
Chris Lattner89219832001-10-03 19:35:04 +0000506 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
507 if (EGV->isConstant() == GV->isConstant() &&
508 (!EGV->hasInitializer() || !GV->hasInitializer() ||
509 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000510
Chris Lattner89219832001-10-03 19:35:04 +0000511 // Make sure the existing global version gets the initializer!
512 if (GV->hasInitializer() && !EGV->hasInitializer())
513 EGV->setInitializer(GV->getInitializer());
514
Chris Lattner2079fde2001-10-13 06:41:08 +0000515 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000516 return true; // They are equivalent!
517 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000518 }
Chris Lattner9636a912001-10-01 16:18:37 +0000519 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000520 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000521 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000522 }
Chris Lattner00950542001-06-06 20:29:01 +0000523
Chris Lattner30c89792001-09-07 16:35:17 +0000524 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000525 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000526}
527
Chris Lattner8896eda2001-07-09 19:38:36 +0000528
Chris Lattner30c89792001-09-07 16:35:17 +0000529//===----------------------------------------------------------------------===//
530// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000531//
Chris Lattner8896eda2001-07-09 19:38:36 +0000532
Chris Lattner30c89792001-09-07 16:35:17 +0000533// TypeContains - Returns true if Ty contains E in it.
534//
535static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000536 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000537}
Chris Lattner698b56e2001-07-20 19:15:08 +0000538
Chris Lattner30c89792001-09-07 16:35:17 +0000539
540static vector<pair<unsigned, OpaqueType *> > UpRefs;
541
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000542static PATypeHolder HandleUpRefs(const Type *ty) {
543 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000544 UR_OUT("Type '" << ty->getDescription() <<
545 "' newly formed. Resolving upreferences.\n" <<
546 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000547 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000548 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000549 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000550 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000551 if (TypeContains(Ty, UpRefs[i].second)) {
552 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000553 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000554 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000555 UR_OUT(" * Resolving upreference for "
556 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000557 string OldName = UpRefs[i].second->getDescription());
558 UpRefs[i].second->refineAbstractTypeTo(Ty);
559 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000560 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000561 << (const void*)Ty << ", " << Ty->getDescription() << endl);
562 continue;
563 }
564 }
565
566 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000567 }
Chris Lattner30c89792001-09-07 16:35:17 +0000568 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000569 return Ty;
570}
571
Chris Lattner30c89792001-09-07 16:35:17 +0000572
Chris Lattner00950542001-06-06 20:29:01 +0000573//===----------------------------------------------------------------------===//
574// RunVMAsmParser - Define an interface to this parser
575//===----------------------------------------------------------------------===//
576//
Chris Lattnera2850432001-07-22 18:36:00 +0000577Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000578 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000579 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000580 llvmAsmlineno = 1; // Reset the current line number...
581
582 CurModule.CurrentModule = new Module(); // Allocate a new module to read
583 yyparse(); // Parse the file.
584 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000585 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
586 ParserResult = 0;
587
588 return Result;
589}
590
591%}
592
593%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000594 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000595 Function *FunctionVal;
Chris Lattner46748042002-04-09 19:41:42 +0000596 std::pair<Argument*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000597 BasicBlock *BasicBlockVal;
598 TerminatorInst *TermInstVal;
599 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000600 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000601
Chris Lattner30c89792001-09-07 16:35:17 +0000602 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000603 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000604 Value *ValueVal;
605
Chris Lattner46748042002-04-09 19:41:42 +0000606 std::list<std::pair<Argument*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000607 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000608 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000609 std::list<std::pair<Value*,
610 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000611 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000612 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000613
Chris Lattner30c89792001-09-07 16:35:17 +0000614 int64_t SInt64Val;
615 uint64_t UInt64Val;
616 int SIntVal;
617 unsigned UIntVal;
618 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000619 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000620
Chris Lattner30c89792001-09-07 16:35:17 +0000621 char *StrVal; // This memory is strdup'd!
622 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000623
Chris Lattner30c89792001-09-07 16:35:17 +0000624 Instruction::UnaryOps UnaryOpVal;
625 Instruction::BinaryOps BinaryOpVal;
626 Instruction::TermOps TermOpVal;
627 Instruction::MemoryOps MemOpVal;
628 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000629}
630
Chris Lattner79df7c02002-03-26 18:01:55 +0000631%type <ModuleVal> Module FunctionList
632%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000633%type <BasicBlockVal> BasicBlock InstructionList
634%type <TermInstVal> BBTerminatorInst
635%type <InstVal> Inst InstVal MemoryInst
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000636%type <ConstVal> ConstVal
Chris Lattner6cdb0112001-11-26 16:54:11 +0000637%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000638%type <ArgList> ArgList ArgListH
639%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000640%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000641%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000642%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000643%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000644%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000645%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000646
Chris Lattner2079fde2001-10-13 06:41:08 +0000647// ValueRef - Unresolved reference to a definition or BB
648%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000649%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000650// Tokens and types for handling constant integer values
651//
652// ESINT64VAL - A negative number within long long range
653%token <SInt64Val> ESINT64VAL
654
655// EUINT64VAL - A positive number within uns. long long range
656%token <UInt64Val> EUINT64VAL
657%type <SInt64Val> EINT64VAL
658
659%token <SIntVal> SINTVAL // Signed 32 bit ints...
660%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
661%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000662%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000663
664// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000665%type <TypeVal> Types TypesV UpRTypes UpRTypesV
666%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000667%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
668%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000669
670%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000671%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000672
673
Chris Lattner9b02cc32002-05-03 18:23:48 +0000674%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT UNINIT
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000675%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL OPAQUE
Chris Lattner00950542001-06-06 20:29:01 +0000676
677// Basic Block Terminating Operators
678%token <TermOpVal> RET BR SWITCH
679
680// Unary Operators
681%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000682%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000683
684// Binary Operators
685%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner42c9e772001-10-20 09:32:59 +0000686%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000687%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000688
689// Memory Instructions
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000690%token <MemoryOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000691
Chris Lattner027dcc52001-07-08 21:10:27 +0000692// Other Operators
693%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000694%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000695
Chris Lattner00950542001-06-06 20:29:01 +0000696%start Module
697%%
698
699// Handle constant integer size restriction and conversion...
700//
701
Chris Lattner51727be2002-06-04 21:58:56 +0000702INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000703INTVAL : UINTVAL {
704 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
705 ThrowException("Value too large for type!");
706 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000707};
Chris Lattner00950542001-06-06 20:29:01 +0000708
709
Chris Lattner51727be2002-06-04 21:58:56 +0000710EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000711EINT64VAL : EUINT64VAL {
712 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
713 ThrowException("Value too large for type!");
714 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000715};
Chris Lattner00950542001-06-06 20:29:01 +0000716
Chris Lattner00950542001-06-06 20:29:01 +0000717// Operations that are notably excluded from this list include:
718// RET, BR, & SWITCH because they end basic blocks and are treated specially.
719//
Chris Lattner51727be2002-06-04 21:58:56 +0000720UnaryOps : NOT;
721BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR;
722BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
723ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000724
Chris Lattnere98dda62001-07-14 06:10:16 +0000725// These are some types that allow classification if we only want a particular
726// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000727SIntType : LONG | INT | SHORT | SBYTE;
728UIntType : ULONG | UINT | USHORT | UBYTE;
729IntType : SIntType | UIntType;
730FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000731
Chris Lattnere98dda62001-07-14 06:10:16 +0000732// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000733OptAssign : VAR_ID '=' {
734 $$ = $1;
735 }
736 | /*empty*/ {
737 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000738 };
Chris Lattner00950542001-06-06 20:29:01 +0000739
Chris Lattner51727be2002-06-04 21:58:56 +0000740OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
Chris Lattner30c89792001-09-07 16:35:17 +0000741
742//===----------------------------------------------------------------------===//
743// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000744// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000745// access to it, a user must explicitly use TypesV.
746//
747
748// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000749TypesV : Types | VOID { $$ = new PATypeHolder($1); };
750UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000751
752Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000753 if (UpRefs.size())
754 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
755 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000756 };
Chris Lattner30c89792001-09-07 16:35:17 +0000757
758
759// Derived types are added later...
760//
Chris Lattner51727be2002-06-04 21:58:56 +0000761PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
762PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000763UpRTypes : OPAQUE {
764 $$ = new PATypeHolder(OpaqueType::get());
765 }
766 | PrimType {
767 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000768 };
Chris Lattner30c89792001-09-07 16:35:17 +0000769UpRTypes : ValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000770 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000771};
Chris Lattner30c89792001-09-07 16:35:17 +0000772
Chris Lattner30c89792001-09-07 16:35:17 +0000773// Include derived types in the Types production.
774//
775UpRTypes : '\\' EUINT64VAL { // Type UpReference
776 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
777 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
778 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000779 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000780 UR_OUT("New Upreference!\n");
781 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000782 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000783 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000784 mapto($3->begin(), $3->end(), std::back_inserter(Params),
785 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000786 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
787 if (isVarArg) Params.pop_back();
788
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000789 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000790 delete $3; // Delete the argument list
791 delete $1; // Delete the old type handle
792 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000793 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000794 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000795 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000796 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000797 | '{' TypeListI '}' { // Structure type?
798 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000799 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
800 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000801
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000802 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000803 delete $2;
804 }
805 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000806 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000807 }
808 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000809 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000810 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000811 };
Chris Lattner30c89792001-09-07 16:35:17 +0000812
Chris Lattner7e708292002-06-25 16:13:24 +0000813// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000814// declaration type lists
815//
816TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000817 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000818 $$->push_back(*$1); delete $1;
819 }
820 | TypeListI ',' UpRTypes {
821 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000822 };
Chris Lattner30c89792001-09-07 16:35:17 +0000823
Chris Lattner7e708292002-06-25 16:13:24 +0000824// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000825ArgTypeListI : TypeListI
826 | TypeListI ',' DOTDOTDOT {
827 ($$=$1)->push_back(Type::VoidTy);
828 }
829 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000830 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000831 }
832 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000833 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000834 };
Chris Lattner30c89792001-09-07 16:35:17 +0000835
836
Chris Lattnere98dda62001-07-14 06:10:16 +0000837// ConstVal - The various declarations that go into the constant pool. This
838// includes all forward declarations of types, constants, and functions.
839//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000840ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
841 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
842 if (ATy == 0)
843 ThrowException("Cannot make array constant with type: '" +
844 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000845 const Type *ETy = ATy->getElementType();
846 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000847
Chris Lattner30c89792001-09-07 16:35:17 +0000848 // Verify that we have the correct size...
849 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000850 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000851 utostr($3->size()) + " arguments, but has size of " +
852 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000853
Chris Lattner30c89792001-09-07 16:35:17 +0000854 // Verify all elements are correct type!
855 for (unsigned i = 0; i < $3->size(); i++) {
856 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000857 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000858 ETy->getDescription() +"' as required!\nIt is of type '"+
859 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000860 }
861
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000862 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000863 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000864 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000865 | Types '[' ']' {
866 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
867 if (ATy == 0)
868 ThrowException("Cannot make array constant with type: '" +
869 (*$1)->getDescription() + "'!");
870
871 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000872 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000873 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000874 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000875 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000876 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000877 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000878 | Types 'c' STRINGCONSTANT {
879 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
880 if (ATy == 0)
881 ThrowException("Cannot make array constant with type: '" +
882 (*$1)->getDescription() + "'!");
883
Chris Lattner30c89792001-09-07 16:35:17 +0000884 int NumElements = ATy->getNumElements();
885 const Type *ETy = ATy->getElementType();
886 char *EndStr = UnEscapeLexed($3, true);
887 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000888 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000889 itostr((int)(EndStr-$3)) +
890 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000891 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000892 if (ETy == Type::SByteTy) {
893 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000894 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000895 } else if (ETy == Type::UByteTy) {
896 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000897 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000898 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000899 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000900 ThrowException("Cannot build string arrays of non byte sized elements!");
901 }
Chris Lattner30c89792001-09-07 16:35:17 +0000902 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000903 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000904 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000905 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000906 | Types '{' ConstVector '}' {
907 const StructType *STy = dyn_cast<const StructType>($1->get());
908 if (STy == 0)
909 ThrowException("Cannot make struct constant with type: '" +
910 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000911 // FIXME: TODO: Check to see that the constants are compatible with the type
912 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000913 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000914 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000915 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000916 | Types NULL_TOK {
917 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
918 if (PTy == 0)
919 ThrowException("Cannot make null pointer constant with type: '" +
920 (*$1)->getDescription() + "'!");
921
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000922 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000923 delete $1;
924 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000925 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000926 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
927 if (Ty == 0)
928 ThrowException("Global const reference must be a pointer type!");
929
Chris Lattner2079fde2001-10-13 06:41:08 +0000930 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000931
Chris Lattner2079fde2001-10-13 06:41:08 +0000932 // If this is an initializer for a constant pointer, which is referencing a
933 // (currently) undefined variable, create a stub now that shall be replaced
934 // in the future with the right type of variable.
935 //
936 if (V == 0) {
937 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
938 const PointerType *PT = cast<PointerType>(Ty);
939
940 // First check to see if the forward references value is already created!
941 PerModuleInfo::GlobalRefsType::iterator I =
942 CurModule.GlobalRefs.find(make_pair(PT, $2));
943
944 if (I != CurModule.GlobalRefs.end()) {
945 V = I->second; // Placeholder already exists, use it...
946 } else {
947 // TODO: Include line number info by creating a subclass of
948 // TODO: GlobalVariable here that includes the said information!
949
950 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000951 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
952 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000953 // Keep track of the fact that we have a forward ref to recycle it
954 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
955
956 // Must temporarily push this value into the module table...
957 CurModule.CurrentModule->getGlobalList().push_back(GV);
958 V = GV;
959 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000960 }
961
Chris Lattner2079fde2001-10-13 06:41:08 +0000962 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000963 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000964 delete $1; // Free the type handle
Chris Lattner51727be2002-06-04 21:58:56 +0000965 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000966
Chris Lattner00950542001-06-06 20:29:01 +0000967
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000968ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000969 if (!ConstantSInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000970 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000971 $$ = ConstantSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000972 }
973 | UIntType EUINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000974 if (!ConstantUInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000975 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000976 $$ = ConstantUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000977 }
978 | BOOL TRUE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000979 $$ = ConstantBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000980 }
981 | BOOL FALSE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000982 $$ = ConstantBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000983 }
984 | FPType FPVAL { // Float & Double constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000985 $$ = ConstantFP::get($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +0000986 };
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000987
Chris Lattnere98dda62001-07-14 06:10:16 +0000988// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +0000989ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +0000990 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +0000991 }
992 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000993 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +0000994 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000995 };
Chris Lattner00950542001-06-06 20:29:01 +0000996
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000997
Chris Lattner1781aca2001-09-18 04:00:54 +0000998// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +0000999GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001000
Chris Lattner00950542001-06-06 20:29:01 +00001001
Chris Lattner0e73ce62002-05-02 19:11:13 +00001002//===----------------------------------------------------------------------===//
1003// Rules to match Modules
1004//===----------------------------------------------------------------------===//
1005
1006// Module rule: Capture the result of parsing the whole file into a result
1007// variable...
1008//
1009Module : FunctionList {
1010 $$ = ParserResult = $1;
1011 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001012};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001013
Chris Lattner7e708292002-06-25 16:13:24 +00001014// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001015//
1016FunctionList : FunctionList Function {
1017 $$ = $1;
1018 assert($2->getParent() == 0 && "Function already in module!");
1019 $1->getFunctionList().push_back($2);
1020 CurMeth.FunctionDone();
1021 }
1022 | FunctionList FunctionProto {
1023 $$ = $1;
1024 }
1025 | FunctionList IMPLEMENTATION {
1026 $$ = $1;
1027 }
1028 | ConstPool {
1029 $$ = CurModule.CurrentModule;
1030 // Resolve circular types before we parse the body of the module
1031 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001032 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001033
Chris Lattnere98dda62001-07-14 06:10:16 +00001034// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001035ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001036 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001037 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001038 }
Chris Lattner30c89792001-09-07 16:35:17 +00001039 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001040 // Eagerly resolve types. This is not an optimization, this is a
1041 // requirement that is due to the fact that we could have this:
1042 //
1043 // %list = type { %list * }
1044 // %list = type { %list * } ; repeated type decl
1045 //
1046 // If types are not resolved eagerly, then the two types will not be
1047 // determined to be the same type!
1048 //
1049 ResolveTypeTo($2, $4->get());
1050
Chris Lattner1781aca2001-09-18 04:00:54 +00001051 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001052 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1053 // If this is not a redefinition of a type...
1054 if (!$2) {
1055 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001056 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001057 }
Chris Lattner30c89792001-09-07 16:35:17 +00001058 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001059
1060 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001061 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001062 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001063 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001064 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1065 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001066 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001067 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001068 if (Initializer == 0)
1069 ThrowException("Global value initializer is not a constant!");
1070
Chris Lattnerdda71962001-11-26 18:54:16 +00001071 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001072 if (!setValueName(GV, $2)) { // If not redefining...
1073 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001074 int Slot = InsertValue(GV, CurModule.Values);
1075
1076 if (Slot != -1) {
1077 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1078 } else {
1079 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1080 (char*)GV->getName().c_str()));
1081 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001082 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001083 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001084 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1085 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001086 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001087 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001088 if (!setValueName(GV, $2)) { // If not redefining...
1089 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001090 int Slot = InsertValue(GV, CurModule.Values);
1091
1092 if (Slot != -1) {
1093 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1094 } else {
1095 assert(GV->hasName() && "Not named and not numbered!?");
1096 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1097 (char*)GV->getName().c_str()));
1098 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001099 }
Chris Lattner09c07532002-03-31 07:16:49 +00001100 delete $6;
Chris Lattnere98dda62001-07-14 06:10:16 +00001101 }
Chris Lattner00950542001-06-06 20:29:01 +00001102 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001103 };
Chris Lattner00950542001-06-06 20:29:01 +00001104
1105
1106//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001107// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001108//===----------------------------------------------------------------------===//
1109
Chris Lattner51727be2002-06-04 21:58:56 +00001110OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001111
1112ArgVal : Types OptVAR_ID {
Chris Lattner46748042002-04-09 19:41:42 +00001113 $$ = new pair<Argument*, char*>(new Argument(*$1), $2);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001114 delete $1; // Delete the type handle..
Chris Lattner51727be2002-06-04 21:58:56 +00001115};
Chris Lattner00950542001-06-06 20:29:01 +00001116
1117ArgListH : ArgVal ',' ArgListH {
1118 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001119 $3->push_front(*$1);
1120 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001121 }
1122 | ArgVal {
Chris Lattner46748042002-04-09 19:41:42 +00001123 $$ = new list<pair<Argument*,char*> >();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001124 $$->push_front(*$1);
1125 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001126 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001127 | DOTDOTDOT {
Chris Lattner46748042002-04-09 19:41:42 +00001128 $$ = new list<pair<Argument*, char*> >();
1129 $$->push_front(pair<Argument*,char*>(new Argument(Type::VoidTy), 0));
Chris Lattner51727be2002-06-04 21:58:56 +00001130 };
Chris Lattner00950542001-06-06 20:29:01 +00001131
1132ArgList : ArgListH {
1133 $$ = $1;
1134 }
1135 | /* empty */ {
1136 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001137 };
Chris Lattner00950542001-06-06 20:29:01 +00001138
Chris Lattner8ebccb72002-05-22 22:33:00 +00001139FuncName : VAR_ID | STRINGCONSTANT;
1140
1141FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
Chris Lattnerdda71962001-11-26 18:54:16 +00001142 UnEscapeLexed($3);
Chris Lattner79df7c02002-03-26 18:01:55 +00001143 string FunctionName($3);
Chris Lattnerdda71962001-11-26 18:54:16 +00001144
Chris Lattner30c89792001-09-07 16:35:17 +00001145 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001146 if ($5)
Chris Lattner46748042002-04-09 19:41:42 +00001147 for (list<pair<Argument*,char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001148 I != $5->end(); ++I)
1149 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001150
Chris Lattner2079fde2001-10-13 06:41:08 +00001151 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1152 if (isVarArg) ParamTypeList.pop_back();
1153
Chris Lattner79df7c02002-03-26 18:01:55 +00001154 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001155 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001156 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001157
Chris Lattner79df7c02002-03-26 18:01:55 +00001158 Function *M = 0;
Chris Lattnere1815642001-07-15 06:35:53 +00001159 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner79df7c02002-03-26 18:01:55 +00001160 // Is the function already in symtab?
1161 if (Value *V = ST->lookup(PMT, FunctionName)) {
1162 M = cast<Function>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001163
Chris Lattnere1815642001-07-15 06:35:53 +00001164 // Yes it is. If this is the case, either we need to be a forward decl,
1165 // or it needs to be.
1166 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner7e708292002-06-25 16:13:24 +00001167 ThrowException("Redefinition of function '" + FunctionName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001168
Chris Lattner7e708292002-06-25 16:13:24 +00001169 // If we found a preexisting function prototype, remove it from the
1170 // module, so that we don't get spurious conflicts with global & local
1171 // variables.
Chris Lattner34538142002-03-08 19:11:42 +00001172 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001173 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001174 }
1175 }
1176
1177 if (M == 0) { // Not already defined?
Chris Lattner79df7c02002-03-26 18:01:55 +00001178 M = new Function(MT, $1, FunctionName);
Chris Lattnere1815642001-07-15 06:35:53 +00001179 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001180 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001181 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001182 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001183
Chris Lattner79df7c02002-03-26 18:01:55 +00001184 CurMeth.FunctionStart(M);
Chris Lattner00950542001-06-06 20:29:01 +00001185
Chris Lattner7e708292002-06-25 16:13:24 +00001186 // Add all of the arguments we parsed to the function...
Chris Lattnerdda71962001-11-26 18:54:16 +00001187 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner46748042002-04-09 19:41:42 +00001188 for (list<pair<Argument*, char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001189 I != $5->end(); ++I) {
1190 if (setValueName(I->first, I->second)) { // Insert into symtab...
1191 assert(0 && "No arg redef allowed!");
1192 }
1193
1194 InsertValue(I->first);
Chris Lattner7e708292002-06-25 16:13:24 +00001195 M->getArgumentList().push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001196 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001197 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001198 } else if ($5) {
1199 // If we are a declaration, we should free the memory for the argument list!
Chris Lattner46748042002-04-09 19:41:42 +00001200 for (list<pair<Argument*, char*> >::iterator I = $5->begin(), E = $5->end();
1201 I != E; ++I) {
Chris Lattner9176fe42002-03-08 18:57:56 +00001202 if (I->second) free(I->second); // Free the memory for the name...
Chris Lattner09c07532002-03-31 07:16:49 +00001203 delete I->first; // Free the unused function argument
1204 }
Chris Lattner9176fe42002-03-08 18:57:56 +00001205 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001206 }
Chris Lattner51727be2002-06-04 21:58:56 +00001207};
Chris Lattner00950542001-06-06 20:29:01 +00001208
Chris Lattner9b02cc32002-05-03 18:23:48 +00001209BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1210
1211FunctionHeader : FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001212 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001213
Chris Lattner7e708292002-06-25 16:13:24 +00001214 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001215 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001216};
Chris Lattner00950542001-06-06 20:29:01 +00001217
Chris Lattner9b02cc32002-05-03 18:23:48 +00001218END : ENDTOK | '}'; // Allow end of '}' to end a function
1219
Chris Lattner79df7c02002-03-26 18:01:55 +00001220Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001221 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001222};
Chris Lattner00950542001-06-06 20:29:01 +00001223
Chris Lattner79df7c02002-03-26 18:01:55 +00001224FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1225 $$ = CurMeth.CurrentFunction;
1226 assert($$->getParent() == 0 && "Function already in module!");
1227 CurModule.CurrentModule->getFunctionList().push_back($$);
1228 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001229};
Chris Lattner00950542001-06-06 20:29:01 +00001230
1231//===----------------------------------------------------------------------===//
1232// Rules to match Basic Blocks
1233//===----------------------------------------------------------------------===//
1234
1235ConstValueRef : ESINT64VAL { // A reference to a direct constant
1236 $$ = ValID::create($1);
1237 }
1238 | EUINT64VAL {
1239 $$ = ValID::create($1);
1240 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001241 | FPVAL { // Perhaps it's an FP constant?
1242 $$ = ValID::create($1);
1243 }
Chris Lattner00950542001-06-06 20:29:01 +00001244 | TRUE {
1245 $$ = ValID::create((int64_t)1);
1246 }
1247 | FALSE {
1248 $$ = ValID::create((int64_t)0);
1249 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001250 | NULL_TOK {
1251 $$ = ValID::createNull();
Chris Lattner51727be2002-06-04 21:58:56 +00001252 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001253
Chris Lattner2079fde2001-10-13 06:41:08 +00001254// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1255// another value.
1256//
1257SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001258 $$ = ValID::create($1);
1259 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001260 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001261 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001262 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001263
1264// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001265ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001266
Chris Lattner00950542001-06-06 20:29:01 +00001267
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001268// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1269// type immediately preceeds the value reference, and allows complex constant
1270// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001271ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001272 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001273 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001274
Chris Lattner00950542001-06-06 20:29:01 +00001275
1276BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001277 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001278 }
Chris Lattner7e708292002-06-25 16:13:24 +00001279 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1280 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001281 };
Chris Lattner00950542001-06-06 20:29:01 +00001282
1283
1284// Basic blocks are terminated by branching instructions:
1285// br, br/cc, switch, ret
1286//
Chris Lattner2079fde2001-10-13 06:41:08 +00001287BasicBlock : InstructionList OptAssign BBTerminatorInst {
1288 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1289 InsertValue($3);
1290
1291 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001292 InsertValue($1);
1293 $$ = $1;
1294 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001295 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1296 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1297 InsertValue($4);
1298
1299 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001300 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001301
1302 InsertValue($2);
1303 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001304 };
Chris Lattner00950542001-06-06 20:29:01 +00001305
1306InstructionList : InstructionList Inst {
1307 $1->getInstList().push_back($2);
1308 $$ = $1;
1309 }
1310 | /* empty */ {
1311 $$ = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001312 };
Chris Lattner00950542001-06-06 20:29:01 +00001313
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001314BBTerminatorInst : RET ResolvedVal { // Return with a result...
1315 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001316 }
1317 | RET VOID { // Return with no result...
1318 $$ = new ReturnInst();
1319 }
1320 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001321 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001322 } // Conditional Branch...
1323 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001324 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1325 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001326 getVal(Type::BoolTy, $3));
1327 }
1328 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1329 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001330 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001331 $$ = S;
1332
Chris Lattner46748042002-04-09 19:41:42 +00001333 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1334 E = $8->end();
1335 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001336 S->dest_push_back(I->first, I->second);
1337 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001338 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1339 EXCEPT ResolvedVal {
1340 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001341 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001342
1343 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001344 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001345 // Pull out the types of all of the arguments...
1346 vector<const Type*> ParamTypes;
1347 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001348 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001349 ParamTypes.push_back((*I)->getType());
1350 }
1351
1352 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1353 if (isVarArg) ParamTypes.pop_back();
1354
Chris Lattner79df7c02002-03-26 18:01:55 +00001355 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner2079fde2001-10-13 06:41:08 +00001356 PMTy = PointerType::get(Ty);
1357 }
1358 delete $2;
1359
Chris Lattner7e708292002-06-25 16:13:24 +00001360 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001361
1362 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1363 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1364
1365 if (Normal == 0 || Except == 0)
1366 ThrowException("Invoke instruction without label destinations!");
1367
1368 // Create the call node...
1369 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001370 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001371 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001372 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001373 // correctly!
1374 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001375 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1376 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001377 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001378
1379 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1380 if ((*ArgI)->getType() != *I)
1381 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001382 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001383
1384 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1385 ThrowException("Invalid number of parameters detected!");
1386
Chris Lattner6cdb0112001-11-26 16:54:11 +00001387 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001388 }
1389 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001390 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001391
1392
Chris Lattner00950542001-06-06 20:29:01 +00001393
1394JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1395 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001396 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001397 if (V == 0)
1398 ThrowException("May only switch on a constant pool value!");
1399
Chris Lattner9636a912001-10-01 16:18:37 +00001400 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001401 }
1402 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001403 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001404 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001405
1406 if (V == 0)
1407 ThrowException("May only switch on a constant pool value!");
1408
Chris Lattner9636a912001-10-01 16:18:37 +00001409 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001410 };
Chris Lattner00950542001-06-06 20:29:01 +00001411
1412Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001413 // Is this definition named?? if so, assign the name...
1414 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001415 InsertValue($2);
1416 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001417};
Chris Lattner00950542001-06-06 20:29:01 +00001418
Chris Lattnerc24d2082001-06-11 15:04:20 +00001419PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1420 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001421 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001422 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001423 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001424 }
1425 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1426 $$ = $1;
1427 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001428 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001429 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001430
1431
Chris Lattner30c89792001-09-07 16:35:17 +00001432ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001433 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001434 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001435 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001436 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001437 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001438 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001439 };
Chris Lattner00950542001-06-06 20:29:01 +00001440
1441// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001442ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001443
1444InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001445 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001446 if ($$ == 0)
1447 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001448 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001449 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001450 | UnaryOps ResolvedVal {
1451 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001452 if ($$ == 0)
1453 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001454 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001455 | ShiftOps ResolvedVal ',' ResolvedVal {
1456 if ($4->getType() != Type::UByteTy)
1457 ThrowException("Shift amount must be ubyte!");
1458 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001459 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001460 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001461 $$ = new CastInst($2, *$4);
1462 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001463 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001464 | PHI PHIList {
1465 const Type *Ty = $2->front().first->getType();
1466 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001467 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001468 if ($2->front().first->getType() != Ty)
1469 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001470 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001471 $2->pop_front();
1472 }
1473 delete $2; // Free the list...
1474 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001475 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001476 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001477 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001478
Chris Lattneref9c23f2001-10-03 14:53:21 +00001479 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001480 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001481 // Pull out the types of all of the arguments...
1482 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001483 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001484 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001485 ParamTypes.push_back((*I)->getType());
1486 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001487
1488 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1489 if (isVarArg) ParamTypes.pop_back();
1490
Chris Lattner79df7c02002-03-26 18:01:55 +00001491 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001492 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001493 }
Chris Lattner30c89792001-09-07 16:35:17 +00001494 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001495
Chris Lattner7e708292002-06-25 16:13:24 +00001496 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001497
Chris Lattner8b81bf52001-07-25 22:47:46 +00001498 // Create the call node...
1499 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001500 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001501 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001502 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001503 // correctly!
1504 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001505 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1506 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001507 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001508
1509 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1510 if ((*ArgI)->getType() != *I)
1511 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001512 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001513
Chris Lattner8b81bf52001-07-25 22:47:46 +00001514 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001515 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001516
Chris Lattner6cdb0112001-11-26 16:54:11 +00001517 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001518 }
1519 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001520 }
1521 | MemoryInst {
1522 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001523 };
Chris Lattner00950542001-06-06 20:29:01 +00001524
Chris Lattner6cdb0112001-11-26 16:54:11 +00001525
1526// IndexList - List of indices for GEP based instructions...
1527IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001528 $$ = $2;
1529} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001530 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001531};
Chris Lattner027dcc52001-07-08 21:10:27 +00001532
Chris Lattner00950542001-06-06 20:29:01 +00001533MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001534 $$ = new MallocInst(PointerType::get(*$2));
1535 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001536 }
1537 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001538 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001539 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001540 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001541 }
1542 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001543 $$ = new AllocaInst(PointerType::get(*$2));
1544 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001545 }
1546 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001547 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001548 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001549 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001550 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001551 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001552 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001553 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001554 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001555 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001556 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001557 }
1558
Chris Lattner6cdb0112001-11-26 16:54:11 +00001559 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001560 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001561 ThrowException("Can't load from nonpointer type: " +
1562 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001563 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001564 ThrowException("Invalid indices for load instruction!");
1565
Chris Lattner30c89792001-09-07 16:35:17 +00001566 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001567 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001568 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001569 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001570 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001571 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001572 ThrowException("Can't store to a nonpointer type: " +
1573 (*$4)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001574 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001575 if (ElTy == 0)
1576 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001577 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001578 ThrowException("Can't store '" + $2->getType()->getDescription() +
1579 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001580 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1581 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001582 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001583 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001584 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001585 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001586 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001587 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001588 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1589 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001590 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001591
Chris Lattner00950542001-06-06 20:29:01 +00001592%%
Chris Lattner09083092001-07-08 04:57:15 +00001593int yyerror(const char *ErrorMsg) {
Chris Lattner00950542001-06-06 20:29:01 +00001594 ThrowException(string("Parse error: ") + ErrorMsg);
1595 return 0;
1596}