blob: 71da575080a060dfa8440dad4be42c260f46d8d7 [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 Lattner00950542001-06-06 20:29:01 +000011#include "llvm/iTerminators.h"
12#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000013#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000015#include "Support/STLExtras.h"
16#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000018#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000019#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000020using std::list;
21using std::vector;
22using std::pair;
23using std::map;
24using std::pair;
25using std::make_pair;
Chris Lattner697954c2002-01-20 22:54:45 +000026using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner386a3b72001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000030int yyparse();
31
32static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000033string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000034
Chris Lattner30c89792001-09-07 16:35:17 +000035// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
36// relating to upreferences in the input stream.
37//
38//#define DEBUG_UPREFS 1
39#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000040#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000041#else
42#define UR_OUT(X)
43#endif
44
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000045#define YYERROR_VERBOSE 1
46
Chris Lattner0383cc42002-08-21 23:51:21 +000047// HACK ALERT: This variable is used to implement the automatic conversion of
48// load/store instructions with indexes into a load/store + getelementptr pair
49// of instructions. When this compatiblity "Feature" is removed, this should be
50// too.
51//
52static BasicBlock *CurBB;
53
54
Chris Lattner7e708292002-06-25 16:13:24 +000055// This contains info used when building the body of a function. It is
56// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000057//
58typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000059static void ResolveDefinitions(vector<ValueList> &LateResolvers,
60 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000061
62static struct PerModuleInfo {
63 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000064 vector<ValueList> Values; // Module level numbered definitions
65 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +000066 vector<PATypeHolder> Types;
67 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000068
Chris Lattner2079fde2001-10-13 06:41:08 +000069 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
70 // references to global values. Global values may be referenced before they
71 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000072 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000073 //
74 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
75 GlobalRefsType GlobalRefs;
76
Chris Lattner00950542001-06-06 20:29:01 +000077 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000078 // If we could not resolve some functions at function compilation time
79 // (calls to functions before they are defined), resolve them now... Types
80 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000081 //
Chris Lattner00950542001-06-06 20:29:01 +000082 ResolveDefinitions(LateResolveValues);
83
Chris Lattner2079fde2001-10-13 06:41:08 +000084 // Check to make sure that all global value forward references have been
85 // resolved!
86 //
87 if (!GlobalRefs.empty()) {
Chris Lattner749ce032002-03-11 22:12:39 +000088 string UndefinedReferences = "Unresolved global references exist:\n";
89
90 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
91 I != E; ++I) {
92 UndefinedReferences += " " + I->first.first->getDescription() + " " +
93 I->first.second.getName() + "\n";
94 }
95 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000096 }
97
Chris Lattner7e708292002-06-25 16:13:24 +000098 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000099 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000100 CurrentModule = 0;
101 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000102
103
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000104 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000105 // is used to remove things from the forward declaration map, resolving them
106 // to the correct thing as needed.
107 //
108 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
109 // Check to see if there is a forward reference to this global variable...
110 // if there is, eliminate it and patch the reference to use the new def'n.
111 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
112
113 if (I != GlobalRefs.end()) {
114 GlobalVariable *OldGV = I->second; // Get the placeholder...
115 I->first.second.destroy(); // Free string memory if neccesary
116
117 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000118 // allowed to be is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000119 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000120 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
121 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000122
Chris Lattner9e932bd2002-10-14 03:28:42 +0000123 // Change the const pool reference to point to the real global variable
124 // now. This should drop a use from the OldGV.
125 CPR->mutateReferences(OldGV, GV);
126 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000127
128 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000129 CurrentModule->getGlobalList().remove(OldGV);
130 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000131
Chris Lattner2079fde2001-10-13 06:41:08 +0000132 // 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 {
Chris Lattner7e708292002-06-25 16:13:24 +0000140 Function *CurrentFunction; // Pointer to current function 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 Lattner7e708292002-06-25 16:13:24 +0000146 bool isDeclare; // Is this function 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
Chris Lattner7e708292002-06-25 16:13:24 +0000164 Values.clear(); // Clear out function 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 }
Chris Lattner7e708292002-06-25 16:13:24 +0000169} CurMeth; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000170
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) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000197 case ValID::NumberVal: { // 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 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000211 case ValID::NameVal: { // Is it a named definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000212 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) {
Chris Lattner7e708292002-06-25 16:13:24 +0000218 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000219 // 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:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000231 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000232 }
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 Lattner9705a152002-05-02 19:27:42 +0000255 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
256 CurModule.CurrentModule->getSymbolTable();
Chris Lattner924025e2002-04-29 18:25:33 +0000257 return SymTab ? SymTab->lookup(Ty, Name) : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000258}
259
Chris Lattner2079fde2001-10-13 06:41:08 +0000260// getValNonImprovising - Look up the value specified by the provided type and
261// the provided ValID. If the value exists and has already been defined, return
262// it. Otherwise return null.
263//
264static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000265 if (isa<FunctionType>(Ty))
266 ThrowException("Functions are not values and "
267 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000268
Chris Lattner30c89792001-09-07 16:35:17 +0000269 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000270 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000271 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000272 unsigned Num = (unsigned)D.Num;
273
274 // Module constants occupy the lowest numbered slots...
275 if (type < CurModule.Values.size()) {
276 if (Num < CurModule.Values[type].size())
277 return CurModule.Values[type][Num];
278
279 Num -= CurModule.Values[type].size();
280 }
281
282 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000283 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000284
285 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000286 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000287
288 return CurMeth.Values[type][Num];
289 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000290
Chris Lattner1a1cb112001-09-30 22:46:54 +0000291 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000292 Value *N = lookupInSymbolTable(Ty, string(D.Name));
293 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000294
295 D.destroy(); // Free old strdup'd memory...
296 return N;
297 }
298
Chris Lattner2079fde2001-10-13 06:41:08 +0000299 // Check to make sure that "Ty" is an integral type, and that our
300 // value will fit into the specified type...
301 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000302 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
303 ThrowException("Signed integral constant '" +
304 itostr(D.ConstPool64) + "' is invalid for type '" +
305 Ty->getDescription() + "'!");
306 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000307
308 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000309 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
310 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000311 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
312 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000313 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000314 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000315 }
316 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000317 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000318 }
319
Chris Lattner2079fde2001-10-13 06:41:08 +0000320 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000321 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000322 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000323 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000324
325 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000326 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000327 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000328 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000329
Chris Lattnerd78700d2002-08-16 21:14:40 +0000330 case ValID::ConstantVal: // Fully resolved constant?
331 if (D.ConstantValue->getType() != Ty)
332 ThrowException("Constant expression type different from required type!");
333 return D.ConstantValue;
334
Chris Lattner30c89792001-09-07 16:35:17 +0000335 default:
336 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000337 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000338 } // End of switch
339
Chris Lattner2079fde2001-10-13 06:41:08 +0000340 assert(0 && "Unhandled case!");
341 return 0;
342}
343
344
345// getVal - This function is identical to getValNonImprovising, except that if a
346// value is not already defined, it "improvises" by creating a placeholder var
347// that looks and acts just like the requested variable. When the value is
348// defined later, all uses of the placeholder variable are replaced with the
349// real thing.
350//
351static Value *getVal(const Type *Ty, const ValID &D) {
352 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
353
354 // See if the value has already been defined...
355 Value *V = getValNonImprovising(Ty, D);
356 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000357
358 // If we reached here, we referenced either a symbol that we don't know about
359 // or an id number that hasn't been read yet. We may be referencing something
360 // forward, so just create an entry to be resolved later and get to it...
361 //
Chris Lattner00950542001-06-06 20:29:01 +0000362 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000363 switch (Ty->getPrimitiveID()) {
364 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000365 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000366 }
367
368 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000369 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000370 InsertValue(d, CurMeth.LateResolveValues);
371 else
372 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000373 return d;
374}
375
376
377//===----------------------------------------------------------------------===//
378// Code to handle forward references in instructions
379//===----------------------------------------------------------------------===//
380//
381// This code handles the late binding needed with statements that reference
382// values not defined yet... for example, a forward branch, or the PHI node for
383// a loop body.
384//
385// This keeps a table (CurMeth.LateResolveValues) of all such forward references
386// and back patchs after we are done.
387//
388
389// ResolveDefinitions - If we could not resolve some defs at parsing
390// time (forward branches, phi functions for loops, etc...) resolve the
391// defs now...
392//
Chris Lattner386a3b72001-10-16 19:54:17 +0000393static void ResolveDefinitions(vector<ValueList> &LateResolvers,
Chris Lattnerbcafcce2002-07-25 06:17:42 +0000394 vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000395 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
396 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
397 while (!LateResolvers[ty].empty()) {
398 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000399 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
400
Chris Lattner00950542001-06-06 20:29:01 +0000401 LateResolvers[ty].pop_back();
402 ValID &DID = getValIDFromPlaceHolder(V);
403
Chris Lattner2079fde2001-10-13 06:41:08 +0000404 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000405 if (TheRealValue) {
406 V->replaceAllUsesWith(TheRealValue);
407 delete V;
408 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000409 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000410 // resolver table
411 InsertValue(V, *FutureLateResolvers);
412 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000413 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000414 ThrowException("Reference to an invalid definition: '" +DID.getName()+
415 "' of type '" + V->getType()->getDescription() + "'",
416 getLineNumFromPlaceHolder(V));
417 else
418 ThrowException("Reference to an invalid definition: #" +
419 itostr(DID.Num) + " of type '" +
420 V->getType()->getDescription() + "'",
421 getLineNumFromPlaceHolder(V));
422 }
Chris Lattner00950542001-06-06 20:29:01 +0000423 }
424 }
425
426 LateResolvers.clear();
427}
428
Chris Lattner4a42e902001-10-22 05:56:09 +0000429// ResolveTypeTo - A brand new type was just declared. This means that (if
430// name is not null) things referencing Name can be resolved. Otherwise, things
431// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000432//
Chris Lattner4a42e902001-10-22 05:56:09 +0000433static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000434 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000435 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000436
Chris Lattner4a42e902001-10-22 05:56:09 +0000437 ValID D;
438 if (Name) D = ValID::create(Name);
439 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000440
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000441 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000442 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
443
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000444 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000445 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000446 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000447 LateResolver.erase(I);
448 }
449}
450
451// ResolveTypes - At this point, all types should be resolved. Any that aren't
452// are errors.
453//
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000454static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000455 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000456 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000457
458 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000459 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000460 else
Chris Lattner82269592001-10-22 06:01:08 +0000461 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000462 }
463}
464
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000465
Chris Lattner1781aca2001-09-18 04:00:54 +0000466// setValueName - Set the specified value to the name given. The name may be
467// null potentially, in which case this is a noop. The string passed in is
468// assumed to be a malloc'd string buffer, and is freed by this function.
469//
Chris Lattnerb7474512001-10-03 15:39:04 +0000470// This function returns true if the value has already been defined, but is
471// allowed to be redefined in the specified context. If the name is a new name
472// for the typeplane, false is returned.
473//
474static bool setValueName(Value *V, char *NameStr) {
475 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000476
Chris Lattner1781aca2001-09-18 04:00:54 +0000477 string Name(NameStr); // Copy string
478 free(NameStr); // Free old string
479
Chris Lattner2079fde2001-10-13 06:41:08 +0000480 if (V->getType() == Type::VoidTy)
481 ThrowException("Can't assign name '" + Name +
482 "' to a null valued instruction!");
483
Chris Lattner79df7c02002-03-26 18:01:55 +0000484 SymbolTable *ST = inFunctionScope() ?
485 CurMeth.CurrentFunction->getSymbolTableSure() :
Chris Lattner30c89792001-09-07 16:35:17 +0000486 CurModule.CurrentModule->getSymbolTableSure();
487
488 Value *Existing = ST->lookup(V->getType(), Name);
489 if (Existing) { // Inserting a name that is already defined???
490 // There is only one case where this is allowed: when we are refining an
491 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000492 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000493 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000494 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000495 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000496 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000497 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000498 }
Chris Lattner30c89792001-09-07 16:35:17 +0000499
Chris Lattner9636a912001-10-01 16:18:37 +0000500 // Otherwise, we are a simple redefinition of a value, check to see if it
501 // is defined the same as the old one...
502 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000503 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000504 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattnerb7474512001-10-03 15:39:04 +0000505 // << cast<const Type>(V)->getDescription() << "!\n";
506 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000507 // We are allowed to redefine a global variable in two circumstances:
508 // 1. If at least one of the globals is uninitialized or
509 // 2. If both initializers have the same value.
510 //
511 // This can only be done if the const'ness of the vars is the same.
512 //
Chris Lattner89219832001-10-03 19:35:04 +0000513 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
514 if (EGV->isConstant() == GV->isConstant() &&
515 (!EGV->hasInitializer() || !GV->hasInitializer() ||
516 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000517
Chris Lattner89219832001-10-03 19:35:04 +0000518 // Make sure the existing global version gets the initializer!
519 if (GV->hasInitializer() && !EGV->hasInitializer())
520 EGV->setInitializer(GV->getInitializer());
521
Chris Lattner2079fde2001-10-13 06:41:08 +0000522 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000523 return true; // They are equivalent!
524 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000525 }
Chris Lattner9636a912001-10-01 16:18:37 +0000526 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000527 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000528 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000529 }
Chris Lattner00950542001-06-06 20:29:01 +0000530
Chris Lattner30c89792001-09-07 16:35:17 +0000531 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000532 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000533}
534
Chris Lattner8896eda2001-07-09 19:38:36 +0000535
Chris Lattner30c89792001-09-07 16:35:17 +0000536//===----------------------------------------------------------------------===//
537// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000538//
Chris Lattner8896eda2001-07-09 19:38:36 +0000539
Chris Lattner30c89792001-09-07 16:35:17 +0000540// TypeContains - Returns true if Ty contains E in it.
541//
542static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000543 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000544}
Chris Lattner698b56e2001-07-20 19:15:08 +0000545
Chris Lattner30c89792001-09-07 16:35:17 +0000546
547static vector<pair<unsigned, OpaqueType *> > UpRefs;
548
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000549static PATypeHolder HandleUpRefs(const Type *ty) {
550 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000551 UR_OUT("Type '" << ty->getDescription() <<
552 "' newly formed. Resolving upreferences.\n" <<
553 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000554 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000555 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000556 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000557 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000558 if (TypeContains(Ty, UpRefs[i].second)) {
559 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000560 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000561 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000562 UR_OUT(" * Resolving upreference for "
563 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000564 string OldName = UpRefs[i].second->getDescription());
565 UpRefs[i].second->refineAbstractTypeTo(Ty);
566 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000567 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000568 << (const void*)Ty << ", " << Ty->getDescription() << endl);
569 continue;
570 }
571 }
572
573 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000574 }
Chris Lattner30c89792001-09-07 16:35:17 +0000575 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000576 return Ty;
577}
578
Chris Lattner30c89792001-09-07 16:35:17 +0000579
Chris Lattner00950542001-06-06 20:29:01 +0000580//===----------------------------------------------------------------------===//
581// RunVMAsmParser - Define an interface to this parser
582//===----------------------------------------------------------------------===//
583//
Chris Lattnera2850432001-07-22 18:36:00 +0000584Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000585 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000586 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000587 llvmAsmlineno = 1; // Reset the current line number...
588
589 CurModule.CurrentModule = new Module(); // Allocate a new module to read
590 yyparse(); // Parse the file.
591 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000592 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
593 ParserResult = 0;
594
595 return Result;
596}
597
598%}
599
600%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000601 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000602 Function *FunctionVal;
Chris Lattner69da5cf2002-10-13 20:57:00 +0000603 std::pair<PATypeHolder*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000604 BasicBlock *BasicBlockVal;
605 TerminatorInst *TermInstVal;
606 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000607 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000608
Chris Lattner30c89792001-09-07 16:35:17 +0000609 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000610 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000611 Value *ValueVal;
612
Chris Lattner69da5cf2002-10-13 20:57:00 +0000613 std::vector<std::pair<PATypeHolder*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000614 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000615 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000616 std::list<std::pair<Value*,
617 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000618 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000619 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000620
Chris Lattner30c89792001-09-07 16:35:17 +0000621 int64_t SInt64Val;
622 uint64_t UInt64Val;
623 int SIntVal;
624 unsigned UIntVal;
625 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000626 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000627
Chris Lattner30c89792001-09-07 16:35:17 +0000628 char *StrVal; // This memory is strdup'd!
629 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000630
Chris Lattner30c89792001-09-07 16:35:17 +0000631 Instruction::BinaryOps BinaryOpVal;
632 Instruction::TermOps TermOpVal;
633 Instruction::MemoryOps MemOpVal;
634 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000635}
636
Chris Lattner79df7c02002-03-26 18:01:55 +0000637%type <ModuleVal> Module FunctionList
638%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000639%type <BasicBlockVal> BasicBlock InstructionList
640%type <TermInstVal> BBTerminatorInst
641%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000642%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000643%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000644%type <ArgList> ArgList ArgListH
645%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000646%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000647%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000648%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000649%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000650%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000651%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000652
Chris Lattner2079fde2001-10-13 06:41:08 +0000653// ValueRef - Unresolved reference to a definition or BB
654%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000655%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000656// Tokens and types for handling constant integer values
657//
658// ESINT64VAL - A negative number within long long range
659%token <SInt64Val> ESINT64VAL
660
661// EUINT64VAL - A positive number within uns. long long range
662%token <UInt64Val> EUINT64VAL
663%type <SInt64Val> EINT64VAL
664
665%token <SIntVal> SINTVAL // Signed 32 bit ints...
666%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
667%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000668%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000669
670// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000671%type <TypeVal> Types TypesV UpRTypes UpRTypesV
672%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000673%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
674%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000675
676%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000677%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000678
679
Chris Lattner08c0e6a2002-10-06 22:45:09 +0000680%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT
681%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL OPAQUE NOT EXTERNAL
Chris Lattner00950542001-06-06 20:29:01 +0000682
683// Basic Block Terminating Operators
684%token <TermOpVal> RET BR SWITCH
685
Chris Lattner00950542001-06-06 20:29:01 +0000686// Binary Operators
687%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000688%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000689%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000690%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000691
692// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000693%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000694
Chris Lattner027dcc52001-07-08 21:10:27 +0000695// Other Operators
696%type <OtherOpVal> ShiftOps
Chris Lattner2079fde2001-10-13 06:41:08 +0000697%token <OtherOpVal> PHI CALL INVOKE CAST SHL SHR
Chris Lattner027dcc52001-07-08 21:10:27 +0000698
Chris Lattner00950542001-06-06 20:29:01 +0000699%start Module
700%%
701
702// Handle constant integer size restriction and conversion...
703//
704
Chris Lattner51727be2002-06-04 21:58:56 +0000705INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000706INTVAL : UINTVAL {
707 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
708 ThrowException("Value too large for type!");
709 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000710};
Chris Lattner00950542001-06-06 20:29:01 +0000711
712
Chris Lattner51727be2002-06-04 21:58:56 +0000713EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000714EINT64VAL : EUINT64VAL {
715 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
716 ThrowException("Value too large for type!");
717 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000718};
Chris Lattner00950542001-06-06 20:29:01 +0000719
Chris Lattner00950542001-06-06 20:29:01 +0000720// Operations that are notably excluded from this list include:
721// RET, BR, & SWITCH because they end basic blocks and are treated specially.
722//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000723ArithmeticOps: ADD | SUB | MUL | DIV | REM;
724LogicalOps : AND | OR | XOR;
725SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
726BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
727
Chris Lattner51727be2002-06-04 21:58:56 +0000728ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000729
Chris Lattnere98dda62001-07-14 06:10:16 +0000730// These are some types that allow classification if we only want a particular
731// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000732SIntType : LONG | INT | SHORT | SBYTE;
733UIntType : ULONG | UINT | USHORT | UBYTE;
734IntType : SIntType | UIntType;
735FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000736
Chris Lattnere98dda62001-07-14 06:10:16 +0000737// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000738OptAssign : VAR_ID '=' {
739 $$ = $1;
740 }
741 | /*empty*/ {
742 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000743 };
Chris Lattner00950542001-06-06 20:29:01 +0000744
Chris Lattner51727be2002-06-04 21:58:56 +0000745OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
Chris Lattner30c89792001-09-07 16:35:17 +0000746
747//===----------------------------------------------------------------------===//
748// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000749// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000750// access to it, a user must explicitly use TypesV.
751//
752
753// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000754TypesV : Types | VOID { $$ = new PATypeHolder($1); };
755UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000756
757Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000758 if (UpRefs.size())
759 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
760 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000761 };
Chris Lattner30c89792001-09-07 16:35:17 +0000762
763
764// Derived types are added later...
765//
Chris Lattner51727be2002-06-04 21:58:56 +0000766PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
767PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000768UpRTypes : OPAQUE {
769 $$ = new PATypeHolder(OpaqueType::get());
770 }
771 | PrimType {
772 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000773 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000774UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000775 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000776};
Chris Lattner30c89792001-09-07 16:35:17 +0000777
Chris Lattner30c89792001-09-07 16:35:17 +0000778// Include derived types in the Types production.
779//
780UpRTypes : '\\' EUINT64VAL { // Type UpReference
781 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
782 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
783 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000784 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000785 UR_OUT("New Upreference!\n");
786 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000787 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000788 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000789 mapto($3->begin(), $3->end(), std::back_inserter(Params),
790 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000791 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
792 if (isVarArg) Params.pop_back();
793
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000794 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000795 delete $3; // Delete the argument list
796 delete $1; // Delete the old type handle
797 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000798 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000799 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000800 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000801 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000802 | '{' TypeListI '}' { // Structure type?
803 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000804 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
805 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000806
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000807 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000808 delete $2;
809 }
810 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000811 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000812 }
813 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000814 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000815 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000816 };
Chris Lattner30c89792001-09-07 16:35:17 +0000817
Chris Lattner7e708292002-06-25 16:13:24 +0000818// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000819// declaration type lists
820//
821TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000822 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000823 $$->push_back(*$1); delete $1;
824 }
825 | TypeListI ',' UpRTypes {
826 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000827 };
Chris Lattner30c89792001-09-07 16:35:17 +0000828
Chris Lattner7e708292002-06-25 16:13:24 +0000829// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000830ArgTypeListI : TypeListI
831 | TypeListI ',' DOTDOTDOT {
832 ($$=$1)->push_back(Type::VoidTy);
833 }
834 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000835 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000836 }
837 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000838 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000839 };
Chris Lattner30c89792001-09-07 16:35:17 +0000840
Chris Lattnere98dda62001-07-14 06:10:16 +0000841// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000842// production is used ONLY to represent constants that show up AFTER a 'const',
843// 'constant' or 'global' token at global scope. Constants that can be inlined
844// into other expressions (such as integers and constexprs) are handled by the
845// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000846//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000847ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
848 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
849 if (ATy == 0)
850 ThrowException("Cannot make array constant with type: '" +
851 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000852 const Type *ETy = ATy->getElementType();
853 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000854
Chris Lattner30c89792001-09-07 16:35:17 +0000855 // Verify that we have the correct size...
856 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000857 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000858 utostr($3->size()) + " arguments, but has size of " +
859 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000860
Chris Lattner30c89792001-09-07 16:35:17 +0000861 // Verify all elements are correct type!
862 for (unsigned i = 0; i < $3->size(); i++) {
863 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000864 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000865 ETy->getDescription() +"' as required!\nIt is of type '"+
866 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000867 }
868
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000869 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000870 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000871 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000872 | Types '[' ']' {
873 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
874 if (ATy == 0)
875 ThrowException("Cannot make array constant with type: '" +
876 (*$1)->getDescription() + "'!");
877
878 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000879 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000880 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000881 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000882 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000883 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000884 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000885 | Types 'c' STRINGCONSTANT {
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
Chris Lattner30c89792001-09-07 16:35:17 +0000891 int NumElements = ATy->getNumElements();
892 const Type *ETy = ATy->getElementType();
893 char *EndStr = UnEscapeLexed($3, true);
894 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000895 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000896 itostr((int)(EndStr-$3)) +
897 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000898 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000899 if (ETy == Type::SByteTy) {
900 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000901 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000902 } else if (ETy == Type::UByteTy) {
903 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000904 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000905 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000906 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000907 ThrowException("Cannot build string arrays of non byte sized elements!");
908 }
Chris Lattner30c89792001-09-07 16:35:17 +0000909 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000910 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000911 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000912 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000913 | Types '{' ConstVector '}' {
914 const StructType *STy = dyn_cast<const StructType>($1->get());
915 if (STy == 0)
916 ThrowException("Cannot make struct constant with type: '" +
917 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000918 // FIXME: TODO: Check to see that the constants are compatible with the type
919 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000920 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000921 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000922 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000923 | Types NULL_TOK {
924 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
925 if (PTy == 0)
926 ThrowException("Cannot make null pointer constant with type: '" +
927 (*$1)->getDescription() + "'!");
928
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000929 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000930 delete $1;
931 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000932 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000933 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
934 if (Ty == 0)
935 ThrowException("Global const reference must be a pointer type!");
936
Chris Lattner3101c252002-08-15 17:58:33 +0000937 // ConstExprs can exist in the body of a function, thus creating
938 // ConstantPointerRefs whenever they refer to a variable. Because we are in
939 // the context of a function, getValNonImprovising will search the functions
940 // symbol table instead of the module symbol table for the global symbol,
941 // which throws things all off. To get around this, we just tell
942 // getValNonImprovising that we are at global scope here.
943 //
944 Function *SavedCurFn = CurMeth.CurrentFunction;
945 CurMeth.CurrentFunction = 0;
946
Chris Lattner2079fde2001-10-13 06:41:08 +0000947 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000948
Chris Lattner3101c252002-08-15 17:58:33 +0000949 CurMeth.CurrentFunction = SavedCurFn;
950
951
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
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000985 }
Chris Lattnerd78700d2002-08-16 21:14:40 +0000986 | Types ConstExpr {
987 if ($1->get() != $2->getType())
988 ThrowException("Mismatched types for constant expression!");
989 $$ = $2;
990 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000991 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000992
Chris Lattnere43f40b2002-10-09 00:25:32 +0000993ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +0000994 if (!ConstantSInt::isValueValidForType($1, $2))
995 ThrowException("Constant value doesn't fit in type!");
996 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +0000997 }
998 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +0000999 if (!ConstantUInt::isValueValidForType($1, $2))
1000 ThrowException("Constant value doesn't fit in type!");
1001 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001002 }
1003 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001004 $$ = ConstantBool::True;
1005 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001006 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001007 $$ = ConstantBool::False;
1008 }
1009 | FPType FPVAL { // Float & Double constants
1010 $$ = ConstantFP::get($1, $2);
1011 };
1012
Chris Lattner00950542001-06-06 20:29:01 +00001013
Chris Lattnerd78700d2002-08-16 21:14:40 +00001014ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001015 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001016 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001017 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001018 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1019 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001020 ThrowException("GetElementPtr requires a pointer operand!");
1021
1022 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001023 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001024 if (!IdxTy)
1025 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001026
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001027 vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001028 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1029 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001030 IdxVec.push_back(C);
1031 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001032 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001033
Chris Lattnerd78700d2002-08-16 21:14:40 +00001034 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001035
Chris Lattnerd78700d2002-08-16 21:14:40 +00001036 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001037 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001038 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001039 if ($3->getType() != $5->getType())
1040 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001041 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001042 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001043 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001044 if ($5->getType() != Type::UByteTy)
1045 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001046 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001047 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001048
1049
Chris Lattnere98dda62001-07-14 06:10:16 +00001050// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001051ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001052 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001053 }
1054 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001055 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001056 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001057 };
Chris Lattner00950542001-06-06 20:29:01 +00001058
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001059
Chris Lattner1781aca2001-09-18 04:00:54 +00001060// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001061GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001062
Chris Lattner00950542001-06-06 20:29:01 +00001063
Chris Lattner0e73ce62002-05-02 19:11:13 +00001064//===----------------------------------------------------------------------===//
1065// Rules to match Modules
1066//===----------------------------------------------------------------------===//
1067
1068// Module rule: Capture the result of parsing the whole file into a result
1069// variable...
1070//
1071Module : FunctionList {
1072 $$ = ParserResult = $1;
1073 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001074};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001075
Chris Lattner7e708292002-06-25 16:13:24 +00001076// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001077//
1078FunctionList : FunctionList Function {
1079 $$ = $1;
1080 assert($2->getParent() == 0 && "Function already in module!");
1081 $1->getFunctionList().push_back($2);
1082 CurMeth.FunctionDone();
1083 }
1084 | FunctionList FunctionProto {
1085 $$ = $1;
1086 }
1087 | FunctionList IMPLEMENTATION {
1088 $$ = $1;
1089 }
1090 | ConstPool {
1091 $$ = CurModule.CurrentModule;
1092 // Resolve circular types before we parse the body of the module
1093 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001094 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001095
Chris Lattnere98dda62001-07-14 06:10:16 +00001096// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001097ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001098 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001099 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001100 }
Chris Lattner30c89792001-09-07 16:35:17 +00001101 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001102 // Eagerly resolve types. This is not an optimization, this is a
1103 // requirement that is due to the fact that we could have this:
1104 //
1105 // %list = type { %list * }
1106 // %list = type { %list * } ; repeated type decl
1107 //
1108 // If types are not resolved eagerly, then the two types will not be
1109 // determined to be the same type!
1110 //
1111 ResolveTypeTo($2, $4->get());
1112
Chris Lattner1781aca2001-09-18 04:00:54 +00001113 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001114 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1115 // If this is not a redefinition of a type...
1116 if (!$2) {
1117 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001118 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001119 }
Chris Lattner30c89792001-09-07 16:35:17 +00001120 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001121
1122 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001123 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001124 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001125 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001126 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1127 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001128 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001129 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001130 if (Initializer == 0)
1131 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001132
Chris Lattnerdda71962001-11-26 18:54:16 +00001133 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001134 if (!setValueName(GV, $2)) { // If not redefining...
1135 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001136 int Slot = InsertValue(GV, CurModule.Values);
1137
1138 if (Slot != -1) {
1139 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1140 } else {
1141 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1142 (char*)GV->getName().c_str()));
1143 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001144 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001145 }
Chris Lattner08c0e6a2002-10-06 22:45:09 +00001146 | ConstPool OptAssign OptInternal EXTERNAL GlobalType Types {
Chris Lattnerdda71962001-11-26 18:54:16 +00001147 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001148 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001149 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001150 if (!setValueName(GV, $2)) { // If not redefining...
1151 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001152 int Slot = InsertValue(GV, CurModule.Values);
1153
1154 if (Slot != -1) {
1155 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1156 } else {
1157 assert(GV->hasName() && "Not named and not numbered!?");
1158 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1159 (char*)GV->getName().c_str()));
1160 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001161 }
Chris Lattner09c07532002-03-31 07:16:49 +00001162 delete $6;
Chris Lattnere98dda62001-07-14 06:10:16 +00001163 }
Chris Lattner00950542001-06-06 20:29:01 +00001164 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001165 };
Chris Lattner00950542001-06-06 20:29:01 +00001166
1167
1168//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001169// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001170//===----------------------------------------------------------------------===//
1171
Chris Lattner51727be2002-06-04 21:58:56 +00001172OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001173
1174ArgVal : Types OptVAR_ID {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001175 if (*$1 == Type::VoidTy)
1176 ThrowException("void typed arguments are invalid!");
1177 $$ = new pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001178};
Chris Lattner00950542001-06-06 20:29:01 +00001179
Chris Lattner69da5cf2002-10-13 20:57:00 +00001180ArgListH : ArgListH ',' ArgVal {
1181 $$ = $1;
1182 $1->push_back(*$3);
1183 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001184 }
1185 | ArgVal {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001186 $$ = new vector<pair<PATypeHolder*,char*> >();
1187 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001188 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001189 };
Chris Lattner00950542001-06-06 20:29:01 +00001190
1191ArgList : ArgListH {
1192 $$ = $1;
1193 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001194 | ArgListH ',' DOTDOTDOT {
1195 $$ = $1;
1196 $$->push_back(pair<PATypeHolder*, char*>(new PATypeHolder(Type::VoidTy),0));
1197 }
1198 | DOTDOTDOT {
1199 $$ = new vector<pair<PATypeHolder*,char*> >();
1200 $$->push_back(pair<PATypeHolder*, char*>(new PATypeHolder(Type::VoidTy),0));
1201 }
Chris Lattner00950542001-06-06 20:29:01 +00001202 | /* empty */ {
1203 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001204 };
Chris Lattner00950542001-06-06 20:29:01 +00001205
Chris Lattner8ebccb72002-05-22 22:33:00 +00001206FuncName : VAR_ID | STRINGCONSTANT;
1207
1208FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
Chris Lattnerdda71962001-11-26 18:54:16 +00001209 UnEscapeLexed($3);
Chris Lattner79df7c02002-03-26 18:01:55 +00001210 string FunctionName($3);
Chris Lattnerdda71962001-11-26 18:54:16 +00001211
Chris Lattner30c89792001-09-07 16:35:17 +00001212 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001213 if ($5)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001214 for (vector<pair<PATypeHolder*,char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001215 I != $5->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001216 ParamTypeList.push_back(I->first->get());
Chris Lattner00950542001-06-06 20:29:01 +00001217
Chris Lattner2079fde2001-10-13 06:41:08 +00001218 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1219 if (isVarArg) ParamTypeList.pop_back();
1220
Chris Lattner79df7c02002-03-26 18:01:55 +00001221 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001222 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001223 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001224
Chris Lattner79df7c02002-03-26 18:01:55 +00001225 Function *M = 0;
Chris Lattnere1815642001-07-15 06:35:53 +00001226 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner79df7c02002-03-26 18:01:55 +00001227 // Is the function already in symtab?
1228 if (Value *V = ST->lookup(PMT, FunctionName)) {
1229 M = cast<Function>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001230
Chris Lattnere1815642001-07-15 06:35:53 +00001231 // Yes it is. If this is the case, either we need to be a forward decl,
1232 // or it needs to be.
1233 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner7e708292002-06-25 16:13:24 +00001234 ThrowException("Redefinition of function '" + FunctionName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001235
Chris Lattner5659dd12002-07-15 00:10:33 +00001236 // Make sure that we keep track of the internal marker, even if there was
1237 // a previous "declare".
1238 if ($1)
1239 M->setInternalLinkage(true);
1240
Chris Lattner7e708292002-06-25 16:13:24 +00001241 // If we found a preexisting function prototype, remove it from the
1242 // module, so that we don't get spurious conflicts with global & local
1243 // variables.
Chris Lattner34538142002-03-08 19:11:42 +00001244 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001245 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001246 }
1247 }
1248
1249 if (M == 0) { // Not already defined?
Chris Lattner79df7c02002-03-26 18:01:55 +00001250 M = new Function(MT, $1, FunctionName);
Chris Lattnere1815642001-07-15 06:35:53 +00001251 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001252 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001253 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001254 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001255
Chris Lattner79df7c02002-03-26 18:01:55 +00001256 CurMeth.FunctionStart(M);
Chris Lattner00950542001-06-06 20:29:01 +00001257
Chris Lattner7e708292002-06-25 16:13:24 +00001258 // Add all of the arguments we parsed to the function...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001259 if ($5) { // Is null if empty...
1260 if (isVarArg) { // Nuke the last entry
1261 assert($5->back().first->get() == Type::VoidTy && $5->back().second == 0&&
1262 "Not a varargs marker!");
1263 delete $5->back().first;
1264 $5->pop_back(); // Delete the last entry
1265 }
1266 Function::aiterator ArgIt = M->abegin();
1267 for (vector<pair<PATypeHolder*, char*> >::iterator I = $5->begin();
1268 I != $5->end(); ++I, ++ArgIt) {
1269 delete I->first; // Delete the typeholder...
1270
1271 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001272 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001273
Chris Lattner69da5cf2002-10-13 20:57:00 +00001274 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001275 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001276
Chris Lattnerdda71962001-11-26 18:54:16 +00001277 delete $5; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001278 }
Chris Lattner51727be2002-06-04 21:58:56 +00001279};
Chris Lattner00950542001-06-06 20:29:01 +00001280
Chris Lattner9b02cc32002-05-03 18:23:48 +00001281BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1282
1283FunctionHeader : FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001284 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001285
Chris Lattner7e708292002-06-25 16:13:24 +00001286 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001287 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001288};
Chris Lattner00950542001-06-06 20:29:01 +00001289
Chris Lattner9b02cc32002-05-03 18:23:48 +00001290END : ENDTOK | '}'; // Allow end of '}' to end a function
1291
Chris Lattner79df7c02002-03-26 18:01:55 +00001292Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001293 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001294};
Chris Lattner00950542001-06-06 20:29:01 +00001295
Chris Lattner79df7c02002-03-26 18:01:55 +00001296FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1297 $$ = CurMeth.CurrentFunction;
1298 assert($$->getParent() == 0 && "Function already in module!");
1299 CurModule.CurrentModule->getFunctionList().push_back($$);
1300 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001301};
Chris Lattner00950542001-06-06 20:29:01 +00001302
1303//===----------------------------------------------------------------------===//
1304// Rules to match Basic Blocks
1305//===----------------------------------------------------------------------===//
1306
1307ConstValueRef : ESINT64VAL { // A reference to a direct constant
1308 $$ = ValID::create($1);
1309 }
1310 | EUINT64VAL {
1311 $$ = ValID::create($1);
1312 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001313 | FPVAL { // Perhaps it's an FP constant?
1314 $$ = ValID::create($1);
1315 }
Chris Lattner00950542001-06-06 20:29:01 +00001316 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001317 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001318 }
1319 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001320 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001321 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001322 | NULL_TOK {
1323 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001324 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001325 | ConstExpr {
1326 $$ = ValID::create($1);
1327 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001328
Chris Lattner2079fde2001-10-13 06:41:08 +00001329// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1330// another value.
1331//
1332SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001333 $$ = ValID::create($1);
1334 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001335 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001336 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001337 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001338
1339// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001340ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001341
Chris Lattner00950542001-06-06 20:29:01 +00001342
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001343// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1344// type immediately preceeds the value reference, and allows complex constant
1345// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001346ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001347 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001348 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001349
Chris Lattner00950542001-06-06 20:29:01 +00001350BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001351 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001352 }
Chris Lattner7e708292002-06-25 16:13:24 +00001353 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1354 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001355 };
Chris Lattner00950542001-06-06 20:29:01 +00001356
1357
1358// Basic blocks are terminated by branching instructions:
1359// br, br/cc, switch, ret
1360//
Chris Lattner2079fde2001-10-13 06:41:08 +00001361BasicBlock : InstructionList OptAssign BBTerminatorInst {
1362 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1363 InsertValue($3);
1364
1365 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001366 InsertValue($1);
1367 $$ = $1;
1368 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001369 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1370 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1371 InsertValue($4);
1372
1373 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001374 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001375
1376 InsertValue($2);
1377 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001378 };
Chris Lattner00950542001-06-06 20:29:01 +00001379
1380InstructionList : InstructionList Inst {
1381 $1->getInstList().push_back($2);
1382 $$ = $1;
1383 }
1384 | /* empty */ {
Chris Lattner0383cc42002-08-21 23:51:21 +00001385 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001386 };
Chris Lattner00950542001-06-06 20:29:01 +00001387
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001388BBTerminatorInst : RET ResolvedVal { // Return with a result...
1389 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001390 }
1391 | RET VOID { // Return with no result...
1392 $$ = new ReturnInst();
1393 }
1394 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001395 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001396 } // Conditional Branch...
1397 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001398 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1399 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001400 getVal(Type::BoolTy, $3));
1401 }
1402 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1403 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001404 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001405 $$ = S;
1406
Chris Lattner46748042002-04-09 19:41:42 +00001407 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1408 E = $8->end();
1409 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001410 S->dest_push_back(I->first, I->second);
1411 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001412 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1413 EXCEPT ResolvedVal {
1414 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001415 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001416
1417 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001418 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001419 // Pull out the types of all of the arguments...
1420 vector<const Type*> ParamTypes;
1421 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001422 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001423 ParamTypes.push_back((*I)->getType());
1424 }
1425
1426 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1427 if (isVarArg) ParamTypes.pop_back();
1428
Chris Lattner79df7c02002-03-26 18:01:55 +00001429 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner2079fde2001-10-13 06:41:08 +00001430 PMTy = PointerType::get(Ty);
1431 }
1432 delete $2;
1433
Chris Lattner7e708292002-06-25 16:13:24 +00001434 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001435
1436 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1437 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1438
1439 if (Normal == 0 || Except == 0)
1440 ThrowException("Invoke instruction without label destinations!");
1441
1442 // Create the call node...
1443 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001444 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001445 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001446 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001447 // correctly!
1448 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001449 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1450 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001451 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001452
1453 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1454 if ((*ArgI)->getType() != *I)
1455 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001456 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001457
1458 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1459 ThrowException("Invalid number of parameters detected!");
1460
Chris Lattner6cdb0112001-11-26 16:54:11 +00001461 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001462 }
1463 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001464 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001465
1466
Chris Lattner00950542001-06-06 20:29:01 +00001467
1468JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1469 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001470 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001471 if (V == 0)
1472 ThrowException("May only switch on a constant pool value!");
1473
Chris Lattner9636a912001-10-01 16:18:37 +00001474 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001475 }
1476 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001477 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001478 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001479
1480 if (V == 0)
1481 ThrowException("May only switch on a constant pool value!");
1482
Chris Lattner9636a912001-10-01 16:18:37 +00001483 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001484 };
Chris Lattner00950542001-06-06 20:29:01 +00001485
1486Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001487 // Is this definition named?? if so, assign the name...
1488 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001489 InsertValue($2);
1490 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001491};
Chris Lattner00950542001-06-06 20:29:01 +00001492
Chris Lattnerc24d2082001-06-11 15:04:20 +00001493PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1494 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001495 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001496 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001497 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001498 }
1499 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1500 $$ = $1;
1501 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001502 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001503 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001504
1505
Chris Lattner30c89792001-09-07 16:35:17 +00001506ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001507 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001508 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001509 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001510 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001511 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001512 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001513 };
Chris Lattner00950542001-06-06 20:29:01 +00001514
1515// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001516ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001517
Chris Lattner4a6482b2002-09-10 19:57:26 +00001518InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1519 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1520 ThrowException("Arithmetic operator requires integer or FP operands!");
1521 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1522 if ($$ == 0)
1523 ThrowException("binary operator returned null!");
1524 delete $2;
1525 }
1526 | LogicalOps Types ValueRef ',' ValueRef {
1527 if (!(*$2)->isIntegral())
1528 ThrowException("Logical operator requires integral operands!");
1529 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1530 if ($$ == 0)
1531 ThrowException("binary operator returned null!");
1532 delete $2;
1533 }
1534 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001535 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001536 if ($$ == 0)
1537 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001538 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001539 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001540 | NOT ResolvedVal {
1541 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1542 << " Replacing with 'xor'.\n";
1543
1544 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1545 if (Ones == 0)
1546 ThrowException("Expected integral type for not instruction!");
1547
1548 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001549 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001550 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001551 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001552 | ShiftOps ResolvedVal ',' ResolvedVal {
1553 if ($4->getType() != Type::UByteTy)
1554 ThrowException("Shift amount must be ubyte!");
1555 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001556 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001557 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001558 $$ = new CastInst($2, *$4);
1559 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001560 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001561 | PHI PHIList {
1562 const Type *Ty = $2->front().first->getType();
1563 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001564 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001565 if ($2->front().first->getType() != Ty)
1566 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001567 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001568 $2->pop_front();
1569 }
1570 delete $2; // Free the list...
1571 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001572 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001573 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001574 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001575
Chris Lattneref9c23f2001-10-03 14:53:21 +00001576 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001577 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001578 // Pull out the types of all of the arguments...
1579 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001580 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001581 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001582 ParamTypes.push_back((*I)->getType());
1583 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001584
1585 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1586 if (isVarArg) ParamTypes.pop_back();
1587
Chris Lattner79df7c02002-03-26 18:01:55 +00001588 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001589 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001590 }
Chris Lattner30c89792001-09-07 16:35:17 +00001591 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001592
Chris Lattner7e708292002-06-25 16:13:24 +00001593 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001594
Chris Lattner8b81bf52001-07-25 22:47:46 +00001595 // Create the call node...
1596 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001597 // Make sure no arguments is a good thing!
1598 if (Ty->getNumParams() != 0)
1599 ThrowException("No arguments passed to a function that "
1600 "expects arguments!");
1601
Chris Lattner386a3b72001-10-16 19:54:17 +00001602 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001603 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001604 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001605 // correctly!
1606 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001607 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1608 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001609 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001610
1611 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1612 if ((*ArgI)->getType() != *I)
1613 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001614 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001615
Chris Lattner8b81bf52001-07-25 22:47:46 +00001616 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001617 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001618
Chris Lattner6cdb0112001-11-26 16:54:11 +00001619 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001620 }
1621 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001622 }
1623 | MemoryInst {
1624 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001625 };
Chris Lattner00950542001-06-06 20:29:01 +00001626
Chris Lattner6cdb0112001-11-26 16:54:11 +00001627
1628// IndexList - List of indices for GEP based instructions...
1629IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001630 $$ = $2;
1631} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001632 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001633};
Chris Lattner027dcc52001-07-08 21:10:27 +00001634
Chris Lattner00950542001-06-06 20:29:01 +00001635MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001636 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001637 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001638 }
1639 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001640 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001641 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001642 }
1643 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001644 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001645 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001646 }
1647 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001648 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001649 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001650 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001651 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001652 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001653 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001654 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001655 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001656 }
1657
Chris Lattner6cdb0112001-11-26 16:54:11 +00001658 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001659 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001660 ThrowException("Can't load from nonpointer type: " +
1661 (*$2)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001662 if (GetElementPtrInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001663 ThrowException("Invalid indices for load instruction!");
1664
Chris Lattner0383cc42002-08-21 23:51:21 +00001665 Value *Src = getVal(*$2, $3);
1666 if (!$4->empty()) {
1667 std::cerr << "WARNING: Use of index load instruction:"
1668 << " replacing with getelementptr/load pair.\n";
1669 // Create a getelementptr hack instruction to do the right thing for
1670 // compatibility.
1671 //
1672 Instruction *I = new GetElementPtrInst(Src, *$4);
1673 CurBB->getInstList().push_back(I);
1674 Src = I;
1675 }
1676
1677 $$ = new LoadInst(Src);
Chris Lattner027dcc52001-07-08 21:10:27 +00001678 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001679 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001680 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001681 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001682 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001683 ThrowException("Can't store to a nonpointer type: " +
1684 (*$4)->getDescription());
Chris Lattner5dfe7672002-08-22 22:48:55 +00001685 const Type *ElTy = GetElementPtrInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001686 if (ElTy == 0)
1687 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001688 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001689 ThrowException("Can't store '" + $2->getType()->getDescription() +
1690 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001691
1692 Value *Ptr = getVal(*$4, $5);
1693 if (!$6->empty()) {
1694 std::cerr << "WARNING: Use of index store instruction:"
1695 << " replacing with getelementptr/store pair.\n";
1696 // Create a getelementptr hack instruction to do the right thing for
1697 // compatibility.
1698 //
1699 Instruction *I = new GetElementPtrInst(Ptr, *$6);
1700 CurBB->getInstList().push_back(I);
1701 Ptr = I;
1702 }
1703
1704 $$ = new StoreInst($2, Ptr);
Chris Lattner30c89792001-09-07 16:35:17 +00001705 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001706 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001707 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner0235fe22002-09-11 01:17:27 +00001708 for (unsigned i = 0, e = $4->size(); i != e; ++i) {
1709 if ((*$4)[i]->getType() == Type::UIntTy) {
1710 std::cerr << "WARNING: Use of uint type indexes to getelementptr "
1711 << "instruction: replacing with casts to long type.\n";
1712 Instruction *I = new CastInst((*$4)[i], Type::LongTy);
1713 CurBB->getInstList().push_back(I);
1714 (*$4)[i] = I;
1715 }
1716 }
1717
Chris Lattner51727be2002-06-04 21:58:56 +00001718 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001719 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001720 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001721 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001722 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1723 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001724 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001725
Chris Lattner00950542001-06-06 20:29:01 +00001726%%
Chris Lattner09083092001-07-08 04:57:15 +00001727int yyerror(const char *ErrorMsg) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001728 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1729 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1730 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1731 if (yychar == YYEMPTY)
1732 errMsg += "end-of-file.";
1733 else
1734 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1735 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001736 return 0;
1737}