blob: 33ae800b594284247be7067540821e2239f4c780 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files ---------*- C++ -*--=//
2//
3// This file implements the bison parser for LLVM assembly languages files.
4//
5//===------------------------------------------------------------------------=//
6
Chris Lattner00950542001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattner70cc3392001-09-10 07:58:01 +000011#include "llvm/GlobalVariable.h"
Chris Lattner00950542001-06-06 20:29:01 +000012#include "llvm/iTerminators.h"
13#include "llvm/iMemory.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
Chris Lattner46748042002-04-09 19:41:42 +000015#include "llvm/Argument.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000016#include "Support/STLExtras.h"
17#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include <list>
19#include <utility> // Get definition of pair class
Chris Lattner30c89792001-09-07 16:35:17 +000020#include <algorithm>
Chris Lattner697954c2002-01-20 22:54:45 +000021#include <iostream>
22using std::list;
23using std::vector;
24using std::pair;
25using std::map;
26using std::pair;
27using std::make_pair;
28using std::cerr;
29using std::string;
Chris Lattner00950542001-06-06 20:29:01 +000030
Chris Lattner386a3b72001-10-16 19:54:17 +000031int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000032int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000033int yyparse();
34
35static Module *ParserResult;
Chris Lattnera2850432001-07-22 18:36:00 +000036string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
43#define UR_OUT(X) cerr << X
44#else
45#define UR_OUT(X)
46#endif
47
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000048#define YYERROR_VERBOSE 1
49
Chris Lattner7e708292002-06-25 16:13:24 +000050// This contains info used when building the body of a function. It is
51// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000052//
53typedef vector<Value *> ValueList; // Numbered defs
Chris Lattner386a3b72001-10-16 19:54:17 +000054static void ResolveDefinitions(vector<ValueList> &LateResolvers,
55 vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000056
57static struct PerModuleInfo {
58 Module *CurrentModule;
Chris Lattner30c89792001-09-07 16:35:17 +000059 vector<ValueList> Values; // Module level numbered definitions
60 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +000061 vector<PATypeHolder> Types;
62 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000063
Chris Lattner2079fde2001-10-13 06:41:08 +000064 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
65 // references to global values. Global values may be referenced before they
66 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000067 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000068 //
69 typedef map<pair<const PointerType *, ValID>, GlobalVariable*> GlobalRefsType;
70 GlobalRefsType GlobalRefs;
71
Chris Lattner00950542001-06-06 20:29:01 +000072 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000073 // If we could not resolve some functions at function compilation time
74 // (calls to functions before they are defined), resolve them now... Types
75 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000076 //
Chris Lattner00950542001-06-06 20:29:01 +000077 ResolveDefinitions(LateResolveValues);
78
Chris Lattner2079fde2001-10-13 06:41:08 +000079 // Check to make sure that all global value forward references have been
80 // resolved!
81 //
82 if (!GlobalRefs.empty()) {
Chris Lattner749ce032002-03-11 22:12:39 +000083 string UndefinedReferences = "Unresolved global references exist:\n";
84
85 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
86 I != E; ++I) {
87 UndefinedReferences += " " + I->first.first->getDescription() + " " +
88 I->first.second.getName() + "\n";
89 }
90 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000091 }
92
Chris Lattner7e708292002-06-25 16:13:24 +000093 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000094 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000095 CurrentModule = 0;
96 }
Chris Lattner2079fde2001-10-13 06:41:08 +000097
98
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000099 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000100 // is used to remove things from the forward declaration map, resolving them
101 // to the correct thing as needed.
102 //
103 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
104 // Check to see if there is a forward reference to this global variable...
105 // if there is, eliminate it and patch the reference to use the new def'n.
106 GlobalRefsType::iterator I = GlobalRefs.find(make_pair(GV->getType(), D));
107
108 if (I != GlobalRefs.end()) {
109 GlobalVariable *OldGV = I->second; // Get the placeholder...
110 I->first.second.destroy(); // Free string memory if neccesary
111
112 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000113 // allowed to be is ConstantPointerRef's.
Chris Lattner2079fde2001-10-13 06:41:08 +0000114 assert(OldGV->use_size() == 1 && "Only one reference should exist!");
115 while (!OldGV->use_empty()) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000116 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
117 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
118 assert(CPR->getValue() == OldGV && "Something isn't happy");
119
120 // Change the const pool reference to point to the real global variable
121 // now. This should drop a use from the OldGV.
122 CPR->mutateReferences(OldGV, GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000123 }
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000124
125 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000126 CurrentModule->getGlobalList().remove(OldGV);
127 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000128
Chris Lattner2079fde2001-10-13 06:41:08 +0000129 // Remove the map entry for the global now that it has been created...
130 GlobalRefs.erase(I);
131 }
132 }
133
Chris Lattner00950542001-06-06 20:29:01 +0000134} CurModule;
135
Chris Lattner79df7c02002-03-26 18:01:55 +0000136static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000137 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000138
Chris Lattnere1815642001-07-15 06:35:53 +0000139 vector<ValueList> Values; // Keep track of numbered definitions
Chris Lattner00950542001-06-06 20:29:01 +0000140 vector<ValueList> LateResolveValues;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000141 vector<PATypeHolder> Types;
142 map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000143 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000144
Chris Lattner79df7c02002-03-26 18:01:55 +0000145 inline PerFunctionInfo() {
146 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000147 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000148 }
149
Chris Lattner79df7c02002-03-26 18:01:55 +0000150 inline ~PerFunctionInfo() {}
Chris Lattner00950542001-06-06 20:29:01 +0000151
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 inline void FunctionStart(Function *M) {
153 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000154 }
155
Chris Lattner79df7c02002-03-26 18:01:55 +0000156 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000157 // If we could not resolve some blocks at parsing time (forward branches)
158 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000159 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000160
Chris Lattner7e708292002-06-25 16:13:24 +0000161 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000162 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000163 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000164 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000165 }
Chris Lattner7e708292002-06-25 16:13:24 +0000166} CurMeth; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000167
Chris Lattner79df7c02002-03-26 18:01:55 +0000168static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000169
Chris Lattner00950542001-06-06 20:29:01 +0000170
171//===----------------------------------------------------------------------===//
172// Code to handle definitions of all the types
173//===----------------------------------------------------------------------===//
174
Chris Lattner2079fde2001-10-13 06:41:08 +0000175static int InsertValue(Value *D, vector<ValueList> &ValueTab = CurMeth.Values) {
176 if (D->hasName()) return -1; // Is this a numbered definition?
177
178 // Yes, insert the value into the value table...
179 unsigned type = D->getType()->getUniqueID();
180 if (ValueTab.size() <= type)
181 ValueTab.resize(type+1, ValueList());
182 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
183 ValueTab[type].push_back(D);
184 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000185}
186
Chris Lattner30c89792001-09-07 16:35:17 +0000187// TODO: FIXME when Type are not const
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000188static void InsertType(const Type *Ty, vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000189 Types.push_back(Ty);
190}
191
192static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000193 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000194 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000195 unsigned Num = (unsigned)D.Num;
196
197 // Module constants occupy the lowest numbered slots...
198 if (Num < CurModule.Types.size())
199 return CurModule.Types[Num];
200
201 Num -= CurModule.Types.size();
202
203 // Check that the number is within bounds...
204 if (Num <= CurMeth.Types.size())
205 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000206 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000207 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000208 case ValID::NameVal: { // Is it a named definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000209 string Name(D.Name);
210 SymbolTable *SymTab = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000211 if (inFunctionScope()) SymTab = CurMeth.CurrentFunction->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000212 Value *N = SymTab ? SymTab->lookup(Type::TypeTy, Name) : 0;
213
214 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000215 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000216 // hasn't been added to the module...
217 //
218 SymTab = CurModule.CurrentModule->getSymbolTable();
219 if (SymTab)
220 N = SymTab->lookup(Type::TypeTy, Name);
221 if (N == 0) break;
222 }
223
224 D.destroy(); // Free old strdup'd memory...
Chris Lattnercfe26c92001-10-01 18:26:53 +0000225 return cast<const Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000226 }
227 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000228 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000229 }
230
231 // If we reached here, we referenced either a symbol that we don't know about
232 // or an id number that hasn't been read yet. We may be referencing something
233 // forward, so just create an entry to be resolved later and get to it...
234 //
235 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
236
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000237 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000238 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
239
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000240 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000241 if (I != LateResolver.end()) {
242 return I->second;
243 }
Chris Lattner30c89792001-09-07 16:35:17 +0000244
Chris Lattner82269592001-10-22 06:01:08 +0000245 Type *Typ = OpaqueType::get();
Chris Lattner4a42e902001-10-22 05:56:09 +0000246 LateResolver.insert(make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000247 return Typ;
248}
249
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000250static Value *lookupInSymbolTable(const Type *Ty, const string &Name) {
251 SymbolTable *SymTab =
Chris Lattner9705a152002-05-02 19:27:42 +0000252 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
253 CurModule.CurrentModule->getSymbolTable();
Chris Lattner924025e2002-04-29 18:25:33 +0000254 return SymTab ? SymTab->lookup(Ty, Name) : 0;
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000255}
256
Chris Lattner2079fde2001-10-13 06:41:08 +0000257// getValNonImprovising - Look up the value specified by the provided type and
258// the provided ValID. If the value exists and has already been defined, return
259// it. Otherwise return null.
260//
261static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000262 if (isa<FunctionType>(Ty))
263 ThrowException("Functions are not values and "
264 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000265
Chris Lattner30c89792001-09-07 16:35:17 +0000266 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000267 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000268 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000269 unsigned Num = (unsigned)D.Num;
270
271 // Module constants occupy the lowest numbered slots...
272 if (type < CurModule.Values.size()) {
273 if (Num < CurModule.Values[type].size())
274 return CurModule.Values[type][Num];
275
276 Num -= CurModule.Values[type].size();
277 }
278
279 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000280 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000281
282 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000283 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000284
285 return CurMeth.Values[type][Num];
286 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000287
Chris Lattner1a1cb112001-09-30 22:46:54 +0000288 case ValID::NameVal: { // Is it a named definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000289 Value *N = lookupInSymbolTable(Ty, string(D.Name));
290 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000291
292 D.destroy(); // Free old strdup'd memory...
293 return N;
294 }
295
Chris Lattner2079fde2001-10-13 06:41:08 +0000296 // Check to make sure that "Ty" is an integral type, and that our
297 // value will fit into the specified type...
298 case ValID::ConstSIntVal: // Is it a constant pool reference??
299 if (Ty == Type::BoolTy) { // Special handling for boolean data
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000300 return ConstantBool::get(D.ConstPool64 != 0);
Chris Lattner2079fde2001-10-13 06:41:08 +0000301 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000302 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
Chris Lattnerf8dff732002-07-18 05:18:37 +0000303 ThrowException("Signed integral constant '" +
Chris Lattner2079fde2001-10-13 06:41:08 +0000304 itostr(D.ConstPool64) + "' is invalid for type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000305 Ty->getDescription() + "'!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000306 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner00950542001-06-06 20:29:01 +0000307 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000308
309 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000310 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
311 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000312 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
313 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000314 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000315 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000316 }
317 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000318 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000319 }
320
Chris Lattner2079fde2001-10-13 06:41:08 +0000321 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000322 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000323 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000324 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000325
326 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000327 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000328 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000329 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000330
Chris Lattner30c89792001-09-07 16:35:17 +0000331 default:
332 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000333 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000334 } // End of switch
335
Chris Lattner2079fde2001-10-13 06:41:08 +0000336 assert(0 && "Unhandled case!");
337 return 0;
338}
339
340
341// getVal - This function is identical to getValNonImprovising, except that if a
342// value is not already defined, it "improvises" by creating a placeholder var
343// that looks and acts just like the requested variable. When the value is
344// defined later, all uses of the placeholder variable are replaced with the
345// real thing.
346//
347static Value *getVal(const Type *Ty, const ValID &D) {
348 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
349
350 // See if the value has already been defined...
351 Value *V = getValNonImprovising(Ty, D);
352 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000353
354 // If we reached here, we referenced either a symbol that we don't know about
355 // or an id number that hasn't been read yet. We may be referencing something
356 // forward, so just create an entry to be resolved later and get to it...
357 //
Chris Lattner00950542001-06-06 20:29:01 +0000358 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000359 switch (Ty->getPrimitiveID()) {
360 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000361 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000362 }
363
364 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000365 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000366 InsertValue(d, CurMeth.LateResolveValues);
367 else
368 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000369 return d;
370}
371
372
373//===----------------------------------------------------------------------===//
374// Code to handle forward references in instructions
375//===----------------------------------------------------------------------===//
376//
377// This code handles the late binding needed with statements that reference
378// values not defined yet... for example, a forward branch, or the PHI node for
379// a loop body.
380//
381// This keeps a table (CurMeth.LateResolveValues) of all such forward references
382// and back patchs after we are done.
383//
384
385// ResolveDefinitions - If we could not resolve some defs at parsing
386// time (forward branches, phi functions for loops, etc...) resolve the
387// defs now...
388//
Chris Lattner386a3b72001-10-16 19:54:17 +0000389static void ResolveDefinitions(vector<ValueList> &LateResolvers,
Chris Lattnerbcafcce2002-07-25 06:17:42 +0000390 vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000391 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
392 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
393 while (!LateResolvers[ty].empty()) {
394 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000395 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
396
Chris Lattner00950542001-06-06 20:29:01 +0000397 LateResolvers[ty].pop_back();
398 ValID &DID = getValIDFromPlaceHolder(V);
399
Chris Lattner2079fde2001-10-13 06:41:08 +0000400 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000401 if (TheRealValue) {
402 V->replaceAllUsesWith(TheRealValue);
403 delete V;
404 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000405 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000406 // resolver table
407 InsertValue(V, *FutureLateResolvers);
408 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000409 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000410 ThrowException("Reference to an invalid definition: '" +DID.getName()+
411 "' of type '" + V->getType()->getDescription() + "'",
412 getLineNumFromPlaceHolder(V));
413 else
414 ThrowException("Reference to an invalid definition: #" +
415 itostr(DID.Num) + " of type '" +
416 V->getType()->getDescription() + "'",
417 getLineNumFromPlaceHolder(V));
418 }
Chris Lattner00950542001-06-06 20:29:01 +0000419 }
420 }
421
422 LateResolvers.clear();
423}
424
Chris Lattner4a42e902001-10-22 05:56:09 +0000425// ResolveTypeTo - A brand new type was just declared. This means that (if
426// name is not null) things referencing Name can be resolved. Otherwise, things
427// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000428//
Chris Lattner4a42e902001-10-22 05:56:09 +0000429static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000430 vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000431 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000432
Chris Lattner4a42e902001-10-22 05:56:09 +0000433 ValID D;
434 if (Name) D = ValID::create(Name);
435 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000436
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000437 map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000438 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
439
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000440 map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000441 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000442 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000443 LateResolver.erase(I);
444 }
445}
446
447// ResolveTypes - At this point, all types should be resolved. Any that aren't
448// are errors.
449//
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000450static void ResolveTypes(map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000451 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000452 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000453
454 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000455 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000456 else
Chris Lattner82269592001-10-22 06:01:08 +0000457 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000458 }
459}
460
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000461
Chris Lattner1781aca2001-09-18 04:00:54 +0000462// setValueName - Set the specified value to the name given. The name may be
463// null potentially, in which case this is a noop. The string passed in is
464// assumed to be a malloc'd string buffer, and is freed by this function.
465//
Chris Lattnerb7474512001-10-03 15:39:04 +0000466// This function returns true if the value has already been defined, but is
467// allowed to be redefined in the specified context. If the name is a new name
468// for the typeplane, false is returned.
469//
470static bool setValueName(Value *V, char *NameStr) {
471 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000472
Chris Lattner1781aca2001-09-18 04:00:54 +0000473 string Name(NameStr); // Copy string
474 free(NameStr); // Free old string
475
Chris Lattner2079fde2001-10-13 06:41:08 +0000476 if (V->getType() == Type::VoidTy)
477 ThrowException("Can't assign name '" + Name +
478 "' to a null valued instruction!");
479
Chris Lattner79df7c02002-03-26 18:01:55 +0000480 SymbolTable *ST = inFunctionScope() ?
481 CurMeth.CurrentFunction->getSymbolTableSure() :
Chris Lattner30c89792001-09-07 16:35:17 +0000482 CurModule.CurrentModule->getSymbolTableSure();
483
484 Value *Existing = ST->lookup(V->getType(), Name);
485 if (Existing) { // Inserting a name that is already defined???
486 // There is only one case where this is allowed: when we are refining an
487 // opaque type. In this case, Existing will be an opaque type.
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000488 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000489 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000490 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000491 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000492 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000493 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000494 }
Chris Lattner30c89792001-09-07 16:35:17 +0000495
Chris Lattner9636a912001-10-01 16:18:37 +0000496 // Otherwise, we are a simple redefinition of a value, check to see if it
497 // is defined the same as the old one...
498 if (const Type *Ty = dyn_cast<const Type>(Existing)) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000499 if (Ty == cast<const Type>(V)) return true; // Yes, it's equal.
500 // cerr << "Type: " << Ty->getDescription() << " != "
501 // << cast<const Type>(V)->getDescription() << "!\n";
502 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000503 // We are allowed to redefine a global variable in two circumstances:
504 // 1. If at least one of the globals is uninitialized or
505 // 2. If both initializers have the same value.
506 //
507 // This can only be done if the const'ness of the vars is the same.
508 //
Chris Lattner89219832001-10-03 19:35:04 +0000509 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
510 if (EGV->isConstant() == GV->isConstant() &&
511 (!EGV->hasInitializer() || !GV->hasInitializer() ||
512 EGV->getInitializer() == GV->getInitializer())) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000513
Chris Lattner89219832001-10-03 19:35:04 +0000514 // Make sure the existing global version gets the initializer!
515 if (GV->hasInitializer() && !EGV->hasInitializer())
516 EGV->setInitializer(GV->getInitializer());
517
Chris Lattner2079fde2001-10-13 06:41:08 +0000518 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000519 return true; // They are equivalent!
520 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000521 }
Chris Lattner9636a912001-10-01 16:18:37 +0000522 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000523 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000524 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000525 }
Chris Lattner00950542001-06-06 20:29:01 +0000526
Chris Lattner30c89792001-09-07 16:35:17 +0000527 V->setName(Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000528 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000529}
530
Chris Lattner8896eda2001-07-09 19:38:36 +0000531
Chris Lattner30c89792001-09-07 16:35:17 +0000532//===----------------------------------------------------------------------===//
533// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000534//
Chris Lattner8896eda2001-07-09 19:38:36 +0000535
Chris Lattner30c89792001-09-07 16:35:17 +0000536// TypeContains - Returns true if Ty contains E in it.
537//
538static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000539 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000540}
Chris Lattner698b56e2001-07-20 19:15:08 +0000541
Chris Lattner30c89792001-09-07 16:35:17 +0000542
543static vector<pair<unsigned, OpaqueType *> > UpRefs;
544
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000545static PATypeHolder HandleUpRefs(const Type *ty) {
546 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000547 UR_OUT("Type '" << ty->getDescription() <<
548 "' newly formed. Resolving upreferences.\n" <<
549 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000550 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000551 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000552 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000553 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000554 if (TypeContains(Ty, UpRefs[i].second)) {
555 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000556 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000557 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000558 UR_OUT(" * Resolving upreference for "
559 << UpRefs[i].second->getDescription() << endl;
Chris Lattner30c89792001-09-07 16:35:17 +0000560 string OldName = UpRefs[i].second->getDescription());
561 UpRefs[i].second->refineAbstractTypeTo(Ty);
562 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000563 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000564 << (const void*)Ty << ", " << Ty->getDescription() << endl);
565 continue;
566 }
567 }
568
569 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000570 }
Chris Lattner30c89792001-09-07 16:35:17 +0000571 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000572 return Ty;
573}
574
Chris Lattner30c89792001-09-07 16:35:17 +0000575
Chris Lattner00950542001-06-06 20:29:01 +0000576//===----------------------------------------------------------------------===//
577// RunVMAsmParser - Define an interface to this parser
578//===----------------------------------------------------------------------===//
579//
Chris Lattnera2850432001-07-22 18:36:00 +0000580Module *RunVMAsmParser(const string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000581 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000582 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000583 llvmAsmlineno = 1; // Reset the current line number...
584
585 CurModule.CurrentModule = new Module(); // Allocate a new module to read
586 yyparse(); // Parse the file.
587 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000588 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
589 ParserResult = 0;
590
591 return Result;
592}
593
594%}
595
596%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000597 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000598 Function *FunctionVal;
Chris Lattner46748042002-04-09 19:41:42 +0000599 std::pair<Argument*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000600 BasicBlock *BasicBlockVal;
601 TerminatorInst *TermInstVal;
602 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000603 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000604
Chris Lattner30c89792001-09-07 16:35:17 +0000605 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000606 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000607 Value *ValueVal;
608
Chris Lattner46748042002-04-09 19:41:42 +0000609 std::list<std::pair<Argument*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000610 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000611 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000612 std::list<std::pair<Value*,
613 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000614 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000615 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000616
Chris Lattner30c89792001-09-07 16:35:17 +0000617 int64_t SInt64Val;
618 uint64_t UInt64Val;
619 int SIntVal;
620 unsigned UIntVal;
621 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000622 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000623
Chris Lattner30c89792001-09-07 16:35:17 +0000624 char *StrVal; // This memory is strdup'd!
625 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000626
Chris Lattner30c89792001-09-07 16:35:17 +0000627 Instruction::UnaryOps UnaryOpVal;
628 Instruction::BinaryOps BinaryOpVal;
629 Instruction::TermOps TermOpVal;
630 Instruction::MemoryOps MemOpVal;
631 Instruction::OtherOps OtherOpVal;
Chris Lattner00950542001-06-06 20:29:01 +0000632}
633
Chris Lattner79df7c02002-03-26 18:01:55 +0000634%type <ModuleVal> Module FunctionList
635%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000636%type <BasicBlockVal> BasicBlock InstructionList
637%type <TermInstVal> BBTerminatorInst
638%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000639%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000640%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000641%type <ArgList> ArgList ArgListH
642%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000643%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000644%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000645%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000646%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000647%type <JumpTable> JumpTable
Chris Lattnerdda71962001-11-26 18:54:16 +0000648%type <BoolVal> GlobalType OptInternal // GLOBAL or CONSTANT? Intern?
Chris Lattner00950542001-06-06 20:29:01 +0000649
Chris Lattner2079fde2001-10-13 06:41:08 +0000650// ValueRef - Unresolved reference to a definition or BB
651%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000652%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000653// Tokens and types for handling constant integer values
654//
655// ESINT64VAL - A negative number within long long range
656%token <SInt64Val> ESINT64VAL
657
658// EUINT64VAL - A positive number within uns. long long range
659%token <UInt64Val> EUINT64VAL
660%type <SInt64Val> EINT64VAL
661
662%token <SIntVal> SINTVAL // Signed 32 bit ints...
663%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
664%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000665%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000666
667// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000668%type <TypeVal> Types TypesV UpRTypes UpRTypesV
669%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000670%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
671%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000672
673%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
Chris Lattner8ebccb72002-05-22 22:33:00 +0000674%type <StrVal> OptVAR_ID OptAssign FuncName
Chris Lattner00950542001-06-06 20:29:01 +0000675
676
Chris Lattner9b02cc32002-05-03 18:23:48 +0000677%token IMPLEMENTATION TRUE FALSE BEGINTOK ENDTOK DECLARE GLOBAL CONSTANT UNINIT
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000678%token TO EXCEPT DOTDOTDOT STRING NULL_TOK CONST INTERNAL OPAQUE
Chris Lattner00950542001-06-06 20:29:01 +0000679
680// Basic Block Terminating Operators
681%token <TermOpVal> RET BR SWITCH
682
683// Unary Operators
684%type <UnaryOpVal> UnaryOps // all the unary operators
Chris Lattner71496b32001-07-08 19:03:27 +0000685%token <UnaryOpVal> NOT
Chris Lattner00950542001-06-06 20:29:01 +0000686
687// Binary Operators
688%type <BinaryOpVal> BinaryOps // all the binary operators
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 Lattner51727be2002-06-04 21:58:56 +0000723UnaryOps : NOT;
724BinaryOps : ADD | SUB | MUL | DIV | REM | AND | OR | XOR;
725BinaryOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
726ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000727
Chris Lattnere98dda62001-07-14 06:10:16 +0000728// These are some types that allow classification if we only want a particular
729// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000730SIntType : LONG | INT | SHORT | SBYTE;
731UIntType : ULONG | UINT | USHORT | UBYTE;
732IntType : SIntType | UIntType;
733FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000734
Chris Lattnere98dda62001-07-14 06:10:16 +0000735// OptAssign - Value producing statements have an optional assignment component
Chris Lattner00950542001-06-06 20:29:01 +0000736OptAssign : VAR_ID '=' {
737 $$ = $1;
738 }
739 | /*empty*/ {
740 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000741 };
Chris Lattner00950542001-06-06 20:29:01 +0000742
Chris Lattner51727be2002-06-04 21:58:56 +0000743OptInternal : INTERNAL { $$ = true; } | /*empty*/ { $$ = false; };
Chris Lattner30c89792001-09-07 16:35:17 +0000744
745//===----------------------------------------------------------------------===//
746// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000747// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000748// access to it, a user must explicitly use TypesV.
749//
750
751// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000752TypesV : Types | VOID { $$ = new PATypeHolder($1); };
753UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000754
755Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000756 if (UpRefs.size())
757 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
758 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000759 };
Chris Lattner30c89792001-09-07 16:35:17 +0000760
761
762// Derived types are added later...
763//
Chris Lattner51727be2002-06-04 21:58:56 +0000764PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
765PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000766UpRTypes : OPAQUE {
767 $$ = new PATypeHolder(OpaqueType::get());
768 }
769 | PrimType {
770 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000771 };
Chris Lattner30c89792001-09-07 16:35:17 +0000772UpRTypes : ValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000773 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000774};
Chris Lattner30c89792001-09-07 16:35:17 +0000775
Chris Lattner30c89792001-09-07 16:35:17 +0000776// Include derived types in the Types production.
777//
778UpRTypes : '\\' EUINT64VAL { // Type UpReference
779 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
780 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
781 UpRefs.push_back(make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000782 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000783 UR_OUT("New Upreference!\n");
784 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000785 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner30c89792001-09-07 16:35:17 +0000786 vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000787 mapto($3->begin(), $3->end(), std::back_inserter(Params),
788 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000789 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
790 if (isVarArg) Params.pop_back();
791
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000792 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000793 delete $3; // Delete the argument list
794 delete $1; // Delete the old type handle
795 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000796 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000797 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000798 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000799 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000800 | '{' TypeListI '}' { // Structure type?
801 vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000802 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
803 std::mem_fun_ref(&PATypeHandle<Type>::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000804
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000805 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000806 delete $2;
807 }
808 | '{' '}' { // Empty structure type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000809 $$ = new PATypeHolder(StructType::get(vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000810 }
811 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000812 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000813 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000814 };
Chris Lattner30c89792001-09-07 16:35:17 +0000815
Chris Lattner7e708292002-06-25 16:13:24 +0000816// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000817// declaration type lists
818//
819TypeListI : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000820 $$ = new list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000821 $$->push_back(*$1); delete $1;
822 }
823 | TypeListI ',' UpRTypes {
824 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000825 };
Chris Lattner30c89792001-09-07 16:35:17 +0000826
Chris Lattner7e708292002-06-25 16:13:24 +0000827// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000828ArgTypeListI : TypeListI
829 | TypeListI ',' DOTDOTDOT {
830 ($$=$1)->push_back(Type::VoidTy);
831 }
832 | DOTDOTDOT {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000833 ($$ = new list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000834 }
835 | /*empty*/ {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000836 $$ = new list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000837 };
Chris Lattner30c89792001-09-07 16:35:17 +0000838
Chris Lattnere98dda62001-07-14 06:10:16 +0000839// ConstVal - The various declarations that go into the constant pool. This
840// includes all forward declarations of types, constants, and functions.
841//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000842ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
843 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
844 if (ATy == 0)
845 ThrowException("Cannot make array constant with type: '" +
846 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000847 const Type *ETy = ATy->getElementType();
848 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000849
Chris Lattner30c89792001-09-07 16:35:17 +0000850 // Verify that we have the correct size...
851 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000852 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000853 utostr($3->size()) + " arguments, but has size of " +
854 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000855
Chris Lattner30c89792001-09-07 16:35:17 +0000856 // Verify all elements are correct type!
857 for (unsigned i = 0; i < $3->size(); i++) {
858 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000859 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000860 ETy->getDescription() +"' as required!\nIt is of type '"+
861 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000862 }
863
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000864 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000865 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000866 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000867 | Types '[' ']' {
868 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
869 if (ATy == 0)
870 ThrowException("Cannot make array constant with type: '" +
871 (*$1)->getDescription() + "'!");
872
873 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000874 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000875 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000876 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000877 $$ = ConstantArray::get(ATy, vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000878 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000879 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000880 | Types 'c' STRINGCONSTANT {
881 const ArrayType *ATy = dyn_cast<const ArrayType>($1->get());
882 if (ATy == 0)
883 ThrowException("Cannot make array constant with type: '" +
884 (*$1)->getDescription() + "'!");
885
Chris Lattner30c89792001-09-07 16:35:17 +0000886 int NumElements = ATy->getNumElements();
887 const Type *ETy = ATy->getElementType();
888 char *EndStr = UnEscapeLexed($3, true);
889 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000890 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000891 itostr((int)(EndStr-$3)) +
892 " when array has size " + itostr(NumElements) + "!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000893 vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000894 if (ETy == Type::SByteTy) {
895 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000896 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000897 } else if (ETy == Type::UByteTy) {
898 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000899 Vals.push_back(ConstantUInt::get(ETy, *C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000900 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000901 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000902 ThrowException("Cannot build string arrays of non byte sized elements!");
903 }
Chris Lattner30c89792001-09-07 16:35:17 +0000904 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000905 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000906 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000907 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000908 | Types '{' ConstVector '}' {
909 const StructType *STy = dyn_cast<const StructType>($1->get());
910 if (STy == 0)
911 ThrowException("Cannot make struct constant with type: '" +
912 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000913 // FIXME: TODO: Check to see that the constants are compatible with the type
914 // initializer!
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000915 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000916 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000917 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000918 | Types NULL_TOK {
919 const PointerType *PTy = dyn_cast<const PointerType>($1->get());
920 if (PTy == 0)
921 ThrowException("Cannot make null pointer constant with type: '" +
922 (*$1)->getDescription() + "'!");
923
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000924 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000925 delete $1;
926 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000927 | Types SymbolicValueRef {
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000928 const PointerType *Ty = dyn_cast<const PointerType>($1->get());
929 if (Ty == 0)
930 ThrowException("Global const reference must be a pointer type!");
931
Chris Lattner2079fde2001-10-13 06:41:08 +0000932 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000933
Chris Lattner2079fde2001-10-13 06:41:08 +0000934 // If this is an initializer for a constant pointer, which is referencing a
935 // (currently) undefined variable, create a stub now that shall be replaced
936 // in the future with the right type of variable.
937 //
938 if (V == 0) {
939 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
940 const PointerType *PT = cast<PointerType>(Ty);
941
942 // First check to see if the forward references value is already created!
943 PerModuleInfo::GlobalRefsType::iterator I =
944 CurModule.GlobalRefs.find(make_pair(PT, $2));
945
946 if (I != CurModule.GlobalRefs.end()) {
947 V = I->second; // Placeholder already exists, use it...
948 } else {
949 // TODO: Include line number info by creating a subclass of
950 // TODO: GlobalVariable here that includes the said information!
951
952 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +0000953 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
954 false, true);
Chris Lattner2079fde2001-10-13 06:41:08 +0000955 // Keep track of the fact that we have a forward ref to recycle it
956 CurModule.GlobalRefs.insert(make_pair(make_pair(PT, $2), GV));
957
958 // Must temporarily push this value into the module table...
959 CurModule.CurrentModule->getGlobalList().push_back(GV);
960 V = GV;
961 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000962 }
963
Chris Lattner2079fde2001-10-13 06:41:08 +0000964 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000965 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +0000966 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000967 }
968 | ConstExpr {
969 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000970 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000971
Chris Lattner00950542001-06-06 20:29:01 +0000972
Chris Lattnerf8dff732002-07-18 05:18:37 +0000973// FIXME: ConstExpr::get never return null! Do checking here in the parser.
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000974ConstExpr: Types CAST ConstVal {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000975 $$ = ConstantExpr::get($2, $3, $1->get());
976 if ($$ == 0) ThrowException("constant expression builder returned null!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000977 }
978 | Types GETELEMENTPTR '(' ConstVal IndexList ')' {
Chris Lattnercc4b6ec2002-07-18 00:14:27 +0000979 vector<Constant*> IdxVec;
980 for (unsigned i = 0, e = $5->size(); i != e; ++i)
981 if (Constant *C = dyn_cast<Constant>((*$5)[i]))
982 IdxVec.push_back(C);
983 else
984 ThrowException("Arguments to getelementptr must be constants!");
985
986 delete $5;
987
Chris Lattnerf8dff732002-07-18 05:18:37 +0000988 $$ = ConstantExpr::get($2, $4, IdxVec, $1->get());
989 if ($$ == 0) ThrowException("constant expression builder returned null!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000990 }
991 | Types UnaryOps ConstVal {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000992 $$ = ConstantExpr::get($2, $3, $1->get());
993 if ($$ == 0) ThrowException("constant expression builder returned null!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000994 }
995 | Types BinaryOps ConstVal ',' ConstVal {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000996 $$ = ConstantExpr::get($2, $3, $5, $1->get());
997 if ($$ == 0) ThrowException("constant expression builder returned null!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000998 }
999 | Types ShiftOps ConstVal ',' ConstVal {
Chris Lattnerf8dff732002-07-18 05:18:37 +00001000 $$ = ConstantExpr::get($2, $3, $5, $1->get());
1001 if ($$ == 0) ThrowException("constant expression builder returned null!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001002 }
1003 ;
1004
1005
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001006ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001007 if (!ConstantSInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001008 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001009 $$ = ConstantSInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001010 }
1011 | UIntType EUINT64VAL { // integral constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001012 if (!ConstantUInt::isValueValidForType($1, $2))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001013 ThrowException("Constant value doesn't fit in type!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001014 $$ = ConstantUInt::get($1, $2);
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001015 }
1016 | BOOL TRUE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001017 $$ = ConstantBool::True;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001018 }
1019 | BOOL FALSE { // Boolean constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001020 $$ = ConstantBool::False;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001021 }
1022 | FPType FPVAL { // Float & Double constants
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001023 $$ = ConstantFP::get($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001024 };
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001025
Chris Lattnere98dda62001-07-14 06:10:16 +00001026// ConstVector - A list of comma seperated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001027ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001028 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001029 }
1030 | ConstVal {
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001031 $$ = new vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001032 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001033 };
Chris Lattner00950542001-06-06 20:29:01 +00001034
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001035
Chris Lattner1781aca2001-09-18 04:00:54 +00001036// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001037GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001038
Chris Lattner00950542001-06-06 20:29:01 +00001039
Chris Lattner0e73ce62002-05-02 19:11:13 +00001040//===----------------------------------------------------------------------===//
1041// Rules to match Modules
1042//===----------------------------------------------------------------------===//
1043
1044// Module rule: Capture the result of parsing the whole file into a result
1045// variable...
1046//
1047Module : FunctionList {
1048 $$ = ParserResult = $1;
1049 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001050};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001051
Chris Lattner7e708292002-06-25 16:13:24 +00001052// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001053//
1054FunctionList : FunctionList Function {
1055 $$ = $1;
1056 assert($2->getParent() == 0 && "Function already in module!");
1057 $1->getFunctionList().push_back($2);
1058 CurMeth.FunctionDone();
1059 }
1060 | FunctionList FunctionProto {
1061 $$ = $1;
1062 }
1063 | FunctionList IMPLEMENTATION {
1064 $$ = $1;
1065 }
1066 | ConstPool {
1067 $$ = CurModule.CurrentModule;
1068 // Resolve circular types before we parse the body of the module
1069 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001070 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001071
Chris Lattnere98dda62001-07-14 06:10:16 +00001072// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001073ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001074 if (setValueName($4, $2)) { assert(0 && "No redefinitions allowed!"); }
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001075 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001076 }
Chris Lattner30c89792001-09-07 16:35:17 +00001077 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001078 // Eagerly resolve types. This is not an optimization, this is a
1079 // requirement that is due to the fact that we could have this:
1080 //
1081 // %list = type { %list * }
1082 // %list = type { %list * } ; repeated type decl
1083 //
1084 // If types are not resolved eagerly, then the two types will not be
1085 // determined to be the same type!
1086 //
1087 ResolveTypeTo($2, $4->get());
1088
Chris Lattner1781aca2001-09-18 04:00:54 +00001089 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001090 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1091 // If this is not a redefinition of a type...
1092 if (!$2) {
1093 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001094 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001095 }
Chris Lattner30c89792001-09-07 16:35:17 +00001096 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001097
1098 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001099 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001100 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001101 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001102 | ConstPool OptAssign OptInternal GlobalType ConstVal {
1103 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001104 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001105 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001106 if (Initializer == 0)
1107 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001108
Chris Lattnerdda71962001-11-26 18:54:16 +00001109 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001110 if (!setValueName(GV, $2)) { // If not redefining...
1111 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001112 int Slot = InsertValue(GV, CurModule.Values);
1113
1114 if (Slot != -1) {
1115 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1116 } else {
1117 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1118 (char*)GV->getName().c_str()));
1119 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001120 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001121 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001122 | ConstPool OptAssign OptInternal UNINIT GlobalType Types {
1123 const Type *Ty = *$6;
Chris Lattner1781aca2001-09-18 04:00:54 +00001124 // Global declarations appear in Constant Pool
Chris Lattnerdda71962001-11-26 18:54:16 +00001125 GlobalVariable *GV = new GlobalVariable(Ty, $5, $3);
Chris Lattnerb7474512001-10-03 15:39:04 +00001126 if (!setValueName(GV, $2)) { // If not redefining...
1127 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001128 int Slot = InsertValue(GV, CurModule.Values);
1129
1130 if (Slot != -1) {
1131 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1132 } else {
1133 assert(GV->hasName() && "Not named and not numbered!?");
1134 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1135 (char*)GV->getName().c_str()));
1136 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001137 }
Chris Lattner09c07532002-03-31 07:16:49 +00001138 delete $6;
Chris Lattnere98dda62001-07-14 06:10:16 +00001139 }
Chris Lattner00950542001-06-06 20:29:01 +00001140 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001141 };
Chris Lattner00950542001-06-06 20:29:01 +00001142
1143
1144//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001145// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001146//===----------------------------------------------------------------------===//
1147
Chris Lattner51727be2002-06-04 21:58:56 +00001148OptVAR_ID : VAR_ID | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001149
1150ArgVal : Types OptVAR_ID {
Chris Lattner46748042002-04-09 19:41:42 +00001151 $$ = new pair<Argument*, char*>(new Argument(*$1), $2);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001152 delete $1; // Delete the type handle..
Chris Lattner51727be2002-06-04 21:58:56 +00001153};
Chris Lattner00950542001-06-06 20:29:01 +00001154
1155ArgListH : ArgVal ',' ArgListH {
1156 $$ = $3;
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001157 $3->push_front(*$1);
1158 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001159 }
1160 | ArgVal {
Chris Lattner46748042002-04-09 19:41:42 +00001161 $$ = new list<pair<Argument*,char*> >();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001162 $$->push_front(*$1);
1163 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001164 }
Chris Lattner8b81bf52001-07-25 22:47:46 +00001165 | DOTDOTDOT {
Chris Lattner46748042002-04-09 19:41:42 +00001166 $$ = new list<pair<Argument*, char*> >();
1167 $$->push_front(pair<Argument*,char*>(new Argument(Type::VoidTy), 0));
Chris Lattner51727be2002-06-04 21:58:56 +00001168 };
Chris Lattner00950542001-06-06 20:29:01 +00001169
1170ArgList : ArgListH {
1171 $$ = $1;
1172 }
1173 | /* empty */ {
1174 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001175 };
Chris Lattner00950542001-06-06 20:29:01 +00001176
Chris Lattner8ebccb72002-05-22 22:33:00 +00001177FuncName : VAR_ID | STRINGCONSTANT;
1178
1179FunctionHeaderH : OptInternal TypesV FuncName '(' ArgList ')' {
Chris Lattnerdda71962001-11-26 18:54:16 +00001180 UnEscapeLexed($3);
Chris Lattner79df7c02002-03-26 18:01:55 +00001181 string FunctionName($3);
Chris Lattnerdda71962001-11-26 18:54:16 +00001182
Chris Lattner30c89792001-09-07 16:35:17 +00001183 vector<const Type*> ParamTypeList;
Chris Lattnerdda71962001-11-26 18:54:16 +00001184 if ($5)
Chris Lattner46748042002-04-09 19:41:42 +00001185 for (list<pair<Argument*,char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001186 I != $5->end(); ++I)
1187 ParamTypeList.push_back(I->first->getType());
Chris Lattner00950542001-06-06 20:29:01 +00001188
Chris Lattner2079fde2001-10-13 06:41:08 +00001189 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1190 if (isVarArg) ParamTypeList.pop_back();
1191
Chris Lattner79df7c02002-03-26 18:01:55 +00001192 const FunctionType *MT = FunctionType::get(*$2, ParamTypeList, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001193 const PointerType *PMT = PointerType::get(MT);
Chris Lattnerdda71962001-11-26 18:54:16 +00001194 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001195
Chris Lattner79df7c02002-03-26 18:01:55 +00001196 Function *M = 0;
Chris Lattnere1815642001-07-15 06:35:53 +00001197 if (SymbolTable *ST = CurModule.CurrentModule->getSymbolTable()) {
Chris Lattner79df7c02002-03-26 18:01:55 +00001198 // Is the function already in symtab?
1199 if (Value *V = ST->lookup(PMT, FunctionName)) {
1200 M = cast<Function>(V);
Chris Lattner00950542001-06-06 20:29:01 +00001201
Chris Lattnere1815642001-07-15 06:35:53 +00001202 // Yes it is. If this is the case, either we need to be a forward decl,
1203 // or it needs to be.
1204 if (!CurMeth.isDeclare && !M->isExternal())
Chris Lattner7e708292002-06-25 16:13:24 +00001205 ThrowException("Redefinition of function '" + FunctionName + "'!");
Chris Lattner34538142002-03-08 19:11:42 +00001206
Chris Lattner5659dd12002-07-15 00:10:33 +00001207 // Make sure that we keep track of the internal marker, even if there was
1208 // a previous "declare".
1209 if ($1)
1210 M->setInternalLinkage(true);
1211
Chris Lattner7e708292002-06-25 16:13:24 +00001212 // If we found a preexisting function prototype, remove it from the
1213 // module, so that we don't get spurious conflicts with global & local
1214 // variables.
Chris Lattner34538142002-03-08 19:11:42 +00001215 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001216 CurModule.CurrentModule->getFunctionList().remove(M);
Chris Lattnere1815642001-07-15 06:35:53 +00001217 }
1218 }
1219
1220 if (M == 0) { // Not already defined?
Chris Lattner79df7c02002-03-26 18:01:55 +00001221 M = new Function(MT, $1, FunctionName);
Chris Lattnere1815642001-07-15 06:35:53 +00001222 InsertValue(M, CurModule.Values);
Chris Lattnerdda71962001-11-26 18:54:16 +00001223 CurModule.DeclareNewGlobalValue(M, ValID::create($3));
Chris Lattnere1815642001-07-15 06:35:53 +00001224 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001225 free($3); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001226
Chris Lattner79df7c02002-03-26 18:01:55 +00001227 CurMeth.FunctionStart(M);
Chris Lattner00950542001-06-06 20:29:01 +00001228
Chris Lattner7e708292002-06-25 16:13:24 +00001229 // Add all of the arguments we parsed to the function...
Chris Lattnerdda71962001-11-26 18:54:16 +00001230 if ($5 && !CurMeth.isDeclare) { // Is null if empty...
Chris Lattner46748042002-04-09 19:41:42 +00001231 for (list<pair<Argument*, char*> >::iterator I = $5->begin();
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001232 I != $5->end(); ++I) {
1233 if (setValueName(I->first, I->second)) { // Insert into symtab...
1234 assert(0 && "No arg redef allowed!");
1235 }
1236
1237 InsertValue(I->first);
Chris Lattner7e708292002-06-25 16:13:24 +00001238 M->getArgumentList().push_back(I->first);
Chris Lattner00950542001-06-06 20:29:01 +00001239 }
Chris Lattnerdda71962001-11-26 18:54:16 +00001240 delete $5; // We're now done with the argument list
Chris Lattner9176fe42002-03-08 18:57:56 +00001241 } else if ($5) {
1242 // If we are a declaration, we should free the memory for the argument list!
Chris Lattner46748042002-04-09 19:41:42 +00001243 for (list<pair<Argument*, char*> >::iterator I = $5->begin(), E = $5->end();
1244 I != E; ++I) {
Chris Lattner9176fe42002-03-08 18:57:56 +00001245 if (I->second) free(I->second); // Free the memory for the name...
Chris Lattner09c07532002-03-31 07:16:49 +00001246 delete I->first; // Free the unused function argument
1247 }
Chris Lattner9176fe42002-03-08 18:57:56 +00001248 delete $5; // Free the memory for the list itself
Chris Lattner00950542001-06-06 20:29:01 +00001249 }
Chris Lattner51727be2002-06-04 21:58:56 +00001250};
Chris Lattner00950542001-06-06 20:29:01 +00001251
Chris Lattner9b02cc32002-05-03 18:23:48 +00001252BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1253
1254FunctionHeader : FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001255 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001256
Chris Lattner7e708292002-06-25 16:13:24 +00001257 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001258 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001259};
Chris Lattner00950542001-06-06 20:29:01 +00001260
Chris Lattner9b02cc32002-05-03 18:23:48 +00001261END : ENDTOK | '}'; // Allow end of '}' to end a function
1262
Chris Lattner79df7c02002-03-26 18:01:55 +00001263Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001264 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001265};
Chris Lattner00950542001-06-06 20:29:01 +00001266
Chris Lattner79df7c02002-03-26 18:01:55 +00001267FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1268 $$ = CurMeth.CurrentFunction;
1269 assert($$->getParent() == 0 && "Function already in module!");
1270 CurModule.CurrentModule->getFunctionList().push_back($$);
1271 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001272};
Chris Lattner00950542001-06-06 20:29:01 +00001273
1274//===----------------------------------------------------------------------===//
1275// Rules to match Basic Blocks
1276//===----------------------------------------------------------------------===//
1277
1278ConstValueRef : ESINT64VAL { // A reference to a direct constant
1279 $$ = ValID::create($1);
1280 }
1281 | EUINT64VAL {
1282 $$ = ValID::create($1);
1283 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001284 | FPVAL { // Perhaps it's an FP constant?
1285 $$ = ValID::create($1);
1286 }
Chris Lattner00950542001-06-06 20:29:01 +00001287 | TRUE {
1288 $$ = ValID::create((int64_t)1);
1289 }
1290 | FALSE {
1291 $$ = ValID::create((int64_t)0);
1292 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001293 | NULL_TOK {
1294 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001295 }
1296 ;
Chris Lattner1a1cb112001-09-30 22:46:54 +00001297
Chris Lattner2079fde2001-10-13 06:41:08 +00001298// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1299// another value.
1300//
1301SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001302 $$ = ValID::create($1);
1303 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001304 | VAR_ID { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001305 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001306 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001307
1308// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001309ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001310
Chris Lattner00950542001-06-06 20:29:01 +00001311
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001312// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1313// type immediately preceeds the value reference, and allows complex constant
1314// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001315ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001316 $$ = getVal(*$1, $2); delete $1;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001317 }
1318 | ConstExpr {
1319 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001320 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001321
Chris Lattner00950542001-06-06 20:29:01 +00001322BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001323 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001324 }
Chris Lattner7e708292002-06-25 16:13:24 +00001325 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1326 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001327 };
Chris Lattner00950542001-06-06 20:29:01 +00001328
1329
1330// Basic blocks are terminated by branching instructions:
1331// br, br/cc, switch, ret
1332//
Chris Lattner2079fde2001-10-13 06:41:08 +00001333BasicBlock : InstructionList OptAssign BBTerminatorInst {
1334 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1335 InsertValue($3);
1336
1337 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001338 InsertValue($1);
1339 $$ = $1;
1340 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001341 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1342 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1343 InsertValue($4);
1344
1345 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001346 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001347
1348 InsertValue($2);
1349 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001350 };
Chris Lattner00950542001-06-06 20:29:01 +00001351
1352InstructionList : InstructionList Inst {
1353 $1->getInstList().push_back($2);
1354 $$ = $1;
1355 }
1356 | /* empty */ {
1357 $$ = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001358 };
Chris Lattner00950542001-06-06 20:29:01 +00001359
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001360BBTerminatorInst : RET ResolvedVal { // Return with a result...
1361 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001362 }
1363 | RET VOID { // Return with no result...
1364 $$ = new ReturnInst();
1365 }
1366 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001367 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001368 } // Conditional Branch...
1369 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001370 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1371 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001372 getVal(Type::BoolTy, $3));
1373 }
1374 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1375 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001376 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001377 $$ = S;
1378
Chris Lattner46748042002-04-09 19:41:42 +00001379 vector<pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
1380 E = $8->end();
1381 for (; I != E; ++I)
Chris Lattner00950542001-06-06 20:29:01 +00001382 S->dest_push_back(I->first, I->second);
1383 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001384 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1385 EXCEPT ResolvedVal {
1386 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001387 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001388
1389 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001390 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001391 // Pull out the types of all of the arguments...
1392 vector<const Type*> ParamTypes;
1393 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001394 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001395 ParamTypes.push_back((*I)->getType());
1396 }
1397
1398 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1399 if (isVarArg) ParamTypes.pop_back();
1400
Chris Lattner79df7c02002-03-26 18:01:55 +00001401 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattner2079fde2001-10-13 06:41:08 +00001402 PMTy = PointerType::get(Ty);
1403 }
1404 delete $2;
1405
Chris Lattner7e708292002-06-25 16:13:24 +00001406 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001407
1408 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1409 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1410
1411 if (Normal == 0 || Except == 0)
1412 ThrowException("Invoke instruction without label destinations!");
1413
1414 // Create the call node...
1415 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001416 $$ = new InvokeInst(V, Normal, Except, vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001417 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001418 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001419 // correctly!
1420 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001421 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1422 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001423 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001424
1425 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1426 if ((*ArgI)->getType() != *I)
1427 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001428 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001429
1430 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1431 ThrowException("Invalid number of parameters detected!");
1432
Chris Lattner6cdb0112001-11-26 16:54:11 +00001433 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001434 }
1435 delete $5;
Chris Lattner51727be2002-06-04 21:58:56 +00001436 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001437
1438
Chris Lattner00950542001-06-06 20:29:01 +00001439
1440JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1441 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001442 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001443 if (V == 0)
1444 ThrowException("May only switch on a constant pool value!");
1445
Chris Lattner9636a912001-10-01 16:18:37 +00001446 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001447 }
1448 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner46748042002-04-09 19:41:42 +00001449 $$ = new vector<pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001450 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001451
1452 if (V == 0)
1453 ThrowException("May only switch on a constant pool value!");
1454
Chris Lattner9636a912001-10-01 16:18:37 +00001455 $$->push_back(make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001456 };
Chris Lattner00950542001-06-06 20:29:01 +00001457
1458Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001459 // Is this definition named?? if so, assign the name...
1460 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001461 InsertValue($2);
1462 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001463};
Chris Lattner00950542001-06-06 20:29:01 +00001464
Chris Lattnerc24d2082001-06-11 15:04:20 +00001465PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1466 $$ = new list<pair<Value*, BasicBlock*> >();
Chris Lattner30c89792001-09-07 16:35:17 +00001467 $$->push_back(make_pair(getVal(*$1, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001468 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001469 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001470 }
1471 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1472 $$ = $1;
1473 $1->push_back(make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner9636a912001-10-01 16:18:37 +00001474 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001475 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001476
1477
Chris Lattner30c89792001-09-07 16:35:17 +00001478ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner6cdb0112001-11-26 16:54:11 +00001479 $$ = new vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001480 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001481 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001482 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001483 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001484 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001485 };
Chris Lattner00950542001-06-06 20:29:01 +00001486
1487// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001488ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001489
1490InstVal : BinaryOps Types ValueRef ',' ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001491 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001492 if ($$ == 0)
1493 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001494 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001495 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001496 | UnaryOps ResolvedVal {
1497 $$ = UnaryOperator::create($1, $2);
Chris Lattner00950542001-06-06 20:29:01 +00001498 if ($$ == 0)
1499 ThrowException("unary operator returned null!");
Chris Lattner09083092001-07-08 04:57:15 +00001500 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001501 | ShiftOps ResolvedVal ',' ResolvedVal {
1502 if ($4->getType() != Type::UByteTy)
1503 ThrowException("Shift amount must be ubyte!");
1504 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001505 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001506 | CAST ResolvedVal TO Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001507 $$ = new CastInst($2, *$4);
1508 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001509 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001510 | PHI PHIList {
1511 const Type *Ty = $2->front().first->getType();
1512 $$ = new PHINode(Ty);
Chris Lattner00950542001-06-06 20:29:01 +00001513 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001514 if ($2->front().first->getType() != Ty)
1515 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001516 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001517 $2->pop_front();
1518 }
1519 delete $2; // Free the list...
1520 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001521 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattneref9c23f2001-10-03 14:53:21 +00001522 const PointerType *PMTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001523 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001524
Chris Lattneref9c23f2001-10-03 14:53:21 +00001525 if (!(PMTy = dyn_cast<PointerType>($2->get())) ||
Chris Lattner79df7c02002-03-26 18:01:55 +00001526 !(Ty = dyn_cast<FunctionType>(PMTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001527 // Pull out the types of all of the arguments...
1528 vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001529 if ($5) {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001530 for (vector<Value*>::iterator I = $5->begin(), E = $5->end(); I!=E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001531 ParamTypes.push_back((*I)->getType());
1532 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001533
1534 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1535 if (isVarArg) ParamTypes.pop_back();
1536
Chris Lattner79df7c02002-03-26 18:01:55 +00001537 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattneref9c23f2001-10-03 14:53:21 +00001538 PMTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001539 }
Chris Lattner30c89792001-09-07 16:35:17 +00001540 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001541
Chris Lattner7e708292002-06-25 16:13:24 +00001542 Value *V = getVal(PMTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001543
Chris Lattner8b81bf52001-07-25 22:47:46 +00001544 // Create the call node...
1545 if (!$5) { // Has no arguments?
Chris Lattner386a3b72001-10-16 19:54:17 +00001546 $$ = new CallInst(V, vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001547 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001548 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001549 // correctly!
1550 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001551 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1552 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner6cdb0112001-11-26 16:54:11 +00001553 vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001554
1555 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1556 if ((*ArgI)->getType() != *I)
1557 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001558 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001559
Chris Lattner8b81bf52001-07-25 22:47:46 +00001560 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001561 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001562
Chris Lattner6cdb0112001-11-26 16:54:11 +00001563 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001564 }
1565 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001566 }
1567 | MemoryInst {
1568 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001569 };
Chris Lattner00950542001-06-06 20:29:01 +00001570
Chris Lattner6cdb0112001-11-26 16:54:11 +00001571
1572// IndexList - List of indices for GEP based instructions...
1573IndexList : ',' ValueRefList {
Chris Lattner027dcc52001-07-08 21:10:27 +00001574 $$ = $2;
1575} | /* empty */ {
Chris Lattner6cdb0112001-11-26 16:54:11 +00001576 $$ = new vector<Value*>();
Chris Lattner51727be2002-06-04 21:58:56 +00001577};
Chris Lattner027dcc52001-07-08 21:10:27 +00001578
Chris Lattner00950542001-06-06 20:29:01 +00001579MemoryInst : MALLOC Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001580 $$ = new MallocInst(PointerType::get(*$2));
1581 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001582 }
1583 | MALLOC Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001584 const Type *Ty = PointerType::get(*$2);
Chris Lattner8896eda2001-07-09 19:38:36 +00001585 $$ = new MallocInst(Ty, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001586 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001587 }
1588 | ALLOCA Types {
Chris Lattner30c89792001-09-07 16:35:17 +00001589 $$ = new AllocaInst(PointerType::get(*$2));
1590 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001591 }
1592 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001593 const Type *Ty = PointerType::get(*$2);
Chris Lattner00950542001-06-06 20:29:01 +00001594 Value *ArrSize = getVal($4, $5);
Chris Lattnerf0d0e9c2001-07-07 08:36:30 +00001595 $$ = new AllocaInst(Ty, ArrSize);
Chris Lattner30c89792001-09-07 16:35:17 +00001596 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001597 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001598 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001599 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001600 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001601 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001602 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001603 }
1604
Chris Lattner6cdb0112001-11-26 16:54:11 +00001605 | LOAD Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001606 if (!isa<PointerType>($2->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001607 ThrowException("Can't load from nonpointer type: " +
1608 (*$2)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001609 if (LoadInst::getIndexedType(*$2, *$4) == 0)
Chris Lattner027dcc52001-07-08 21:10:27 +00001610 ThrowException("Invalid indices for load instruction!");
1611
Chris Lattner30c89792001-09-07 16:35:17 +00001612 $$ = new LoadInst(getVal(*$2, $3), *$4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001613 delete $4; // Free the vector...
Chris Lattner30c89792001-09-07 16:35:17 +00001614 delete $2;
Chris Lattner027dcc52001-07-08 21:10:27 +00001615 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001616 | STORE ResolvedVal ',' Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001617 if (!isa<PointerType>($4->get()))
Chris Lattner72e00252001-12-14 16:28:42 +00001618 ThrowException("Can't store to a nonpointer type: " +
1619 (*$4)->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +00001620 const Type *ElTy = StoreInst::getIndexedType(*$4, *$6);
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001621 if (ElTy == 0)
1622 ThrowException("Can't store into that field list!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001623 if (ElTy != $2->getType())
Chris Lattner72e00252001-12-14 16:28:42 +00001624 ThrowException("Can't store '" + $2->getType()->getDescription() +
1625 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001626 $$ = new StoreInst($2, getVal(*$4, $5), *$6);
1627 delete $4; delete $6;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001628 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001629 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001630 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001631 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001632 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001633 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001634 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1635 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001636 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001637
Chris Lattner00950542001-06-06 20:29:01 +00001638%%
Chris Lattner09083092001-07-08 04:57:15 +00001639int yyerror(const char *ErrorMsg) {
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001640 string where = string((CurFilename == "-")? string("<stdin>") : CurFilename)
1641 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
1642 string errMsg = string(ErrorMsg) + string("\n") + where + " while reading ";
1643 if (yychar == YYEMPTY)
1644 errMsg += "end-of-file.";
1645 else
1646 errMsg += "token: '" + string(llvmAsmtext, llvmAsmleng) + "'";
1647 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001648 return 0;
1649}