blob: b14d6a7ecc08d2ea9f61f2ba238a2a70a69a92e3 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This file implements the bison parser for LLVM assembly languages files.
4//
Chris Lattnercf3056d2003-10-13 03:32:08 +00005//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00006
Chris Lattner00950542001-06-06 20:29:01 +00007%{
8#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +00009#include "llvm/SymbolTable.h"
10#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000011#include "llvm/iTerminators.h"
12#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000013#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000014#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000015#include "Support/STLExtras.h"
16#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000017#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000018#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000019#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000020
Chris Lattner386a3b72001-10-16 19:54:17 +000021int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000022int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000023int yyparse();
24
25static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000026std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner30c89792001-09-07 16:35:17 +000028// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
29// relating to upreferences in the input stream.
30//
31//#define DEBUG_UPREFS 1
32#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000033#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000034#else
35#define UR_OUT(X)
36#endif
37
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000038#define YYERROR_VERBOSE 1
39
Chris Lattner7e708292002-06-25 16:13:24 +000040// This contains info used when building the body of a function. It is
41// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000042//
Chris Lattner9232b992003-04-22 18:42:41 +000043typedef std::vector<Value *> ValueList; // Numbered defs
44static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
45 std::vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000046
47static struct PerModuleInfo {
48 Module *CurrentModule;
Chris Lattner9232b992003-04-22 18:42:41 +000049 std::vector<ValueList> Values; // Module level numbered definitions
50 std::vector<ValueList> LateResolveValues;
51 std::vector<PATypeHolder> Types;
52 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000053
Chris Lattner2079fde2001-10-13 06:41:08 +000054 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
55 // references to global values. Global values may be referenced before they
56 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000057 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000058 //
Chris Lattner9232b992003-04-22 18:42:41 +000059 typedef std::map<std::pair<const PointerType *,
60 ValID>, GlobalVariable*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000061 GlobalRefsType GlobalRefs;
62
Chris Lattner00950542001-06-06 20:29:01 +000063 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000064 // If we could not resolve some functions at function compilation time
65 // (calls to functions before they are defined), resolve them now... Types
66 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000067 //
Chris Lattner00950542001-06-06 20:29:01 +000068 ResolveDefinitions(LateResolveValues);
69
Chris Lattner2079fde2001-10-13 06:41:08 +000070 // Check to make sure that all global value forward references have been
71 // resolved!
72 //
73 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000074 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000075
76 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
77 I != E; ++I) {
78 UndefinedReferences += " " + I->first.first->getDescription() + " " +
79 I->first.second.getName() + "\n";
80 }
81 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000082 }
83
Chris Lattner7e708292002-06-25 16:13:24 +000084 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +000085 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +000086 CurrentModule = 0;
87 }
Chris Lattner2079fde2001-10-13 06:41:08 +000088
89
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000090 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +000091 // is used to remove things from the forward declaration map, resolving them
92 // to the correct thing as needed.
93 //
94 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
95 // Check to see if there is a forward reference to this global variable...
96 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +000097 GlobalRefsType::iterator I =
98 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +000099
100 if (I != GlobalRefs.end()) {
101 GlobalVariable *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000102 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner2079fde2001-10-13 06:41:08 +0000103
104 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000105 // allowed to be is ConstantPointerRef's.
Chris Lattnerfd059242003-10-15 16:48:29 +0000106 assert(OldGV->hasOneUse() && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000107 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
108 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000109
Chris Lattner9e932bd2002-10-14 03:28:42 +0000110 // Change the const pool reference to point to the real global variable
111 // now. This should drop a use from the OldGV.
112 CPR->mutateReferences(OldGV, GV);
113 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000114
115 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000116 CurrentModule->getGlobalList().remove(OldGV);
117 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000118
Chris Lattner2079fde2001-10-13 06:41:08 +0000119 // Remove the map entry for the global now that it has been created...
120 GlobalRefs.erase(I);
121 }
122 }
123
Chris Lattner00950542001-06-06 20:29:01 +0000124} CurModule;
125
Chris Lattner79df7c02002-03-26 18:01:55 +0000126static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000127 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000128
Chris Lattner9232b992003-04-22 18:42:41 +0000129 std::vector<ValueList> Values; // Keep track of numbered definitions
130 std::vector<ValueList> LateResolveValues;
131 std::vector<PATypeHolder> Types;
132 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000133 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000134
Chris Lattner79df7c02002-03-26 18:01:55 +0000135 inline PerFunctionInfo() {
136 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000137 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000138 }
139
Chris Lattner79df7c02002-03-26 18:01:55 +0000140 inline void FunctionStart(Function *M) {
141 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000142 }
143
Chris Lattner79df7c02002-03-26 18:01:55 +0000144 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000145 // If we could not resolve some blocks at parsing time (forward branches)
146 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000147 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000148
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000149 // Make sure to resolve any constant expr references that might exist within
150 // the function we just declared itself.
151 ValID FID;
152 if (CurrentFunction->hasName()) {
153 FID = ValID::create((char*)CurrentFunction->getName().c_str());
154 } else {
155 unsigned Slot = CurrentFunction->getType()->getUniqueID();
156 assert(CurModule.Values.size() > Slot && "Function not inserted?");
157 // Figure out which slot number if is...
158 for (unsigned i = 0; ; ++i) {
159 assert(i < CurModule.Values[Slot].size() && "Function not found!");
160 if (CurModule.Values[Slot][i] == CurrentFunction) {
161 FID = ValID::create((int)i);
162 break;
163 }
164 }
165 }
166 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
167
Chris Lattner7e708292002-06-25 16:13:24 +0000168 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000169 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000170 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000171 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000172 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000173} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000174
Chris Lattner394f2eb2003-10-16 20:12:13 +0000175static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000176
Chris Lattner00950542001-06-06 20:29:01 +0000177
178//===----------------------------------------------------------------------===//
179// Code to handle definitions of all the types
180//===----------------------------------------------------------------------===//
181
Chris Lattner9232b992003-04-22 18:42:41 +0000182static int InsertValue(Value *D,
Chris Lattner394f2eb2003-10-16 20:12:13 +0000183 std::vector<ValueList> &ValueTab = CurFun.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000184 if (D->hasName()) return -1; // Is this a numbered definition?
185
186 // Yes, insert the value into the value table...
187 unsigned type = D->getType()->getUniqueID();
188 if (ValueTab.size() <= type)
189 ValueTab.resize(type+1, ValueList());
190 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
191 ValueTab[type].push_back(D);
192 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000193}
194
Chris Lattner30c89792001-09-07 16:35:17 +0000195// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000196static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000197 Types.push_back(Ty);
198}
199
200static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000201 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000202 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000203 unsigned Num = (unsigned)D.Num;
204
205 // Module constants occupy the lowest numbered slots...
206 if (Num < CurModule.Types.size())
207 return CurModule.Types[Num];
208
209 Num -= CurModule.Types.size();
210
211 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000212 if (Num <= CurFun.Types.size())
213 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000214 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000215 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000216 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000217 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000218 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000219 Value *N = 0;
220 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000221 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000222 N = SymTab->lookup(Type::TypeTy, Name);
223 }
Chris Lattner30c89792001-09-07 16:35:17 +0000224
225 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000226 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000227 // hasn't been added to the module...
228 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000229 SymTab = &CurModule.CurrentModule->getSymbolTable();
230 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000231 if (N == 0) break;
232 }
233
234 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000235 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000236 }
237 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000238 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000239 }
240
241 // If we reached here, we referenced either a symbol that we don't know about
242 // or an id number that hasn't been read yet. We may be referencing something
243 // forward, so just create an entry to be resolved later and get to it...
244 //
245 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
246
Chris Lattner9232b992003-04-22 18:42:41 +0000247 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000248 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000249
Chris Lattner9232b992003-04-22 18:42:41 +0000250 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000251 if (I != LateResolver.end()) {
252 return I->second;
253 }
Chris Lattner30c89792001-09-07 16:35:17 +0000254
Chris Lattner82269592001-10-22 06:01:08 +0000255 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000256 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000257 return Typ;
258}
259
Chris Lattner9232b992003-04-22 18:42:41 +0000260static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000261 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000262 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000263 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000264 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000265}
266
Chris Lattner2079fde2001-10-13 06:41:08 +0000267// getValNonImprovising - Look up the value specified by the provided type and
268// the provided ValID. If the value exists and has already been defined, return
269// it. Otherwise return null.
270//
271static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000272 if (isa<FunctionType>(Ty))
273 ThrowException("Functions are not values and "
274 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000275
Chris Lattner30c89792001-09-07 16:35:17 +0000276 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000277 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000278 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000279 unsigned Num = (unsigned)D.Num;
280
281 // Module constants occupy the lowest numbered slots...
282 if (type < CurModule.Values.size()) {
283 if (Num < CurModule.Values[type].size())
284 return CurModule.Values[type][Num];
285
286 Num -= CurModule.Values[type].size();
287 }
288
289 // Make sure that our type is within bounds
Chris Lattner394f2eb2003-10-16 20:12:13 +0000290 if (CurFun.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000291
292 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000293 if (CurFun.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000294
Chris Lattner394f2eb2003-10-16 20:12:13 +0000295 return CurFun.Values[type][Num];
Chris Lattner00950542001-06-06 20:29:01 +0000296 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000297
Chris Lattner1a1cb112001-09-30 22:46:54 +0000298 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000299 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000300 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000301
302 D.destroy(); // Free old strdup'd memory...
303 return N;
304 }
305
Chris Lattner2079fde2001-10-13 06:41:08 +0000306 // Check to make sure that "Ty" is an integral type, and that our
307 // value will fit into the specified type...
308 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000309 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
310 ThrowException("Signed integral constant '" +
311 itostr(D.ConstPool64) + "' is invalid for type '" +
312 Ty->getDescription() + "'!");
313 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000314
315 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000316 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
317 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000318 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
319 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000320 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000321 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000322 }
323 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000324 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000325 }
326
Chris Lattner2079fde2001-10-13 06:41:08 +0000327 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000328 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000329 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000330 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000331
332 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000333 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000334 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000335 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000336
Chris Lattnerd78700d2002-08-16 21:14:40 +0000337 case ValID::ConstantVal: // Fully resolved constant?
338 if (D.ConstantValue->getType() != Ty)
339 ThrowException("Constant expression type different from required type!");
340 return D.ConstantValue;
341
Chris Lattner30c89792001-09-07 16:35:17 +0000342 default:
343 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000344 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000345 } // End of switch
346
Chris Lattner2079fde2001-10-13 06:41:08 +0000347 assert(0 && "Unhandled case!");
348 return 0;
349}
350
351
352// getVal - This function is identical to getValNonImprovising, except that if a
353// value is not already defined, it "improvises" by creating a placeholder var
354// that looks and acts just like the requested variable. When the value is
355// defined later, all uses of the placeholder variable are replaced with the
356// real thing.
357//
358static Value *getVal(const Type *Ty, const ValID &D) {
359 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
360
361 // See if the value has already been defined...
362 Value *V = getValNonImprovising(Ty, D);
363 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000364
365 // If we reached here, we referenced either a symbol that we don't know about
366 // or an id number that hasn't been read yet. We may be referencing something
367 // forward, so just create an entry to be resolved later and get to it...
368 //
Chris Lattner00950542001-06-06 20:29:01 +0000369 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000370 switch (Ty->getPrimitiveID()) {
371 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000372 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000373 }
374
375 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000376 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000377 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000378 else
379 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000380 return d;
381}
382
383
384//===----------------------------------------------------------------------===//
385// Code to handle forward references in instructions
386//===----------------------------------------------------------------------===//
387//
388// This code handles the late binding needed with statements that reference
389// values not defined yet... for example, a forward branch, or the PHI node for
390// a loop body.
391//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000392// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000393// and back patchs after we are done.
394//
395
396// ResolveDefinitions - If we could not resolve some defs at parsing
397// time (forward branches, phi functions for loops, etc...) resolve the
398// defs now...
399//
Chris Lattner9232b992003-04-22 18:42:41 +0000400static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
401 std::vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000402 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
403 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
404 while (!LateResolvers[ty].empty()) {
405 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000406 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
407
Chris Lattner00950542001-06-06 20:29:01 +0000408 LateResolvers[ty].pop_back();
409 ValID &DID = getValIDFromPlaceHolder(V);
410
Chris Lattner2079fde2001-10-13 06:41:08 +0000411 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000412 if (TheRealValue) {
413 V->replaceAllUsesWith(TheRealValue);
414 delete V;
415 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000416 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000417 // resolver table
418 InsertValue(V, *FutureLateResolvers);
419 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000420 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000421 ThrowException("Reference to an invalid definition: '" +DID.getName()+
422 "' of type '" + V->getType()->getDescription() + "'",
423 getLineNumFromPlaceHolder(V));
424 else
425 ThrowException("Reference to an invalid definition: #" +
426 itostr(DID.Num) + " of type '" +
427 V->getType()->getDescription() + "'",
428 getLineNumFromPlaceHolder(V));
429 }
Chris Lattner00950542001-06-06 20:29:01 +0000430 }
431 }
432
433 LateResolvers.clear();
434}
435
Chris Lattner4a42e902001-10-22 05:56:09 +0000436// ResolveTypeTo - A brand new type was just declared. This means that (if
437// name is not null) things referencing Name can be resolved. Otherwise, things
438// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000439//
Chris Lattner4a42e902001-10-22 05:56:09 +0000440static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000441 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000442 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000443
Chris Lattner4a42e902001-10-22 05:56:09 +0000444 ValID D;
445 if (Name) D = ValID::create(Name);
446 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000447
Chris Lattner9232b992003-04-22 18:42:41 +0000448 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000449 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000450
Chris Lattner9232b992003-04-22 18:42:41 +0000451 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000452 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000453 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000454 LateResolver.erase(I);
455 }
456}
457
458// ResolveTypes - At this point, all types should be resolved. Any that aren't
459// are errors.
460//
Chris Lattner9232b992003-04-22 18:42:41 +0000461static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000462 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000463 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000464
465 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000466 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000467 else
Chris Lattner82269592001-10-22 06:01:08 +0000468 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000469 }
470}
471
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000472
Chris Lattner1781aca2001-09-18 04:00:54 +0000473// setValueName - Set the specified value to the name given. The name may be
474// null potentially, in which case this is a noop. The string passed in is
475// assumed to be a malloc'd string buffer, and is freed by this function.
476//
Chris Lattnerb7474512001-10-03 15:39:04 +0000477// This function returns true if the value has already been defined, but is
478// allowed to be redefined in the specified context. If the name is a new name
479// for the typeplane, false is returned.
480//
481static bool setValueName(Value *V, char *NameStr) {
482 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000483
Chris Lattner9232b992003-04-22 18:42:41 +0000484 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000485 free(NameStr); // Free old string
486
Chris Lattner2079fde2001-10-13 06:41:08 +0000487 if (V->getType() == Type::VoidTy)
488 ThrowException("Can't assign name '" + Name +
489 "' to a null valued instruction!");
490
Chris Lattner6e6026b2002-11-20 18:36:02 +0000491 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000492 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000493 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000494
Chris Lattner6e6026b2002-11-20 18:36:02 +0000495 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000496 if (Existing) { // Inserting a name that is already defined???
497 // There is only one case where this is allowed: when we are refining an
498 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner949a3622003-07-23 15:30:06 +0000499 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000500 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000501 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000502 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000503 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000504 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000505 }
Chris Lattner30c89792001-09-07 16:35:17 +0000506
Chris Lattner9636a912001-10-01 16:18:37 +0000507 // Otherwise, we are a simple redefinition of a value, check to see if it
508 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000509 if (const Type *Ty = dyn_cast<Type>(Existing)) {
510 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000511 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattner949a3622003-07-23 15:30:06 +0000512 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000513 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
514 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000515 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000516 // We are allowed to redefine a global variable in two circumstances:
517 // 1. If at least one of the globals is uninitialized or
518 // 2. If both initializers have the same value.
519 //
Chris Lattner89219832001-10-03 19:35:04 +0000520 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000521 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
522 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000523
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000524 // Make sure the existing global version gets the initializer! Make
525 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000526 if (GV->hasInitializer() && !EGV->hasInitializer())
527 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000528 if (GV->isConstant())
529 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000530 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000531
Chris Lattner2079fde2001-10-13 06:41:08 +0000532 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000533 return true; // They are equivalent!
534 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000535 }
Chris Lattner9636a912001-10-01 16:18:37 +0000536 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000537 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000538 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000539 }
Chris Lattner00950542001-06-06 20:29:01 +0000540
Chris Lattner6e6026b2002-11-20 18:36:02 +0000541 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000542 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000543}
544
Chris Lattner8896eda2001-07-09 19:38:36 +0000545
Chris Lattner30c89792001-09-07 16:35:17 +0000546//===----------------------------------------------------------------------===//
547// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000548//
Chris Lattner8896eda2001-07-09 19:38:36 +0000549
Chris Lattner30c89792001-09-07 16:35:17 +0000550// TypeContains - Returns true if Ty contains E in it.
551//
552static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000553 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000554}
Chris Lattner698b56e2001-07-20 19:15:08 +0000555
Chris Lattner30c89792001-09-07 16:35:17 +0000556
Chris Lattner9232b992003-04-22 18:42:41 +0000557static std::vector<std::pair<unsigned, OpaqueType *> > UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000558
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000559static PATypeHolder HandleUpRefs(const Type *ty) {
560 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000561 UR_OUT("Type '" << ty->getDescription() <<
562 "' newly formed. Resolving upreferences.\n" <<
563 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000564 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000565 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000566 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000567 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000568 if (TypeContains(Ty, UpRefs[i].second)) {
569 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000570 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000571 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000572 UR_OUT(" * Resolving upreference for "
573 << UpRefs[i].second->getDescription() << endl;
Chris Lattner9232b992003-04-22 18:42:41 +0000574 std::string OldName = UpRefs[i].second->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +0000575 UpRefs[i].second->refineAbstractTypeTo(Ty);
576 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000577 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000578 << (const void*)Ty << ", " << Ty->getDescription() << endl);
579 continue;
580 }
581 }
582
583 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000584 }
Chris Lattner30c89792001-09-07 16:35:17 +0000585 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000586 return Ty;
587}
588
Chris Lattner30c89792001-09-07 16:35:17 +0000589
Chris Lattner00950542001-06-06 20:29:01 +0000590//===----------------------------------------------------------------------===//
591// RunVMAsmParser - Define an interface to this parser
592//===----------------------------------------------------------------------===//
593//
Chris Lattner9232b992003-04-22 18:42:41 +0000594Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000595 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000596 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000597 llvmAsmlineno = 1; // Reset the current line number...
598
Chris Lattner75f20532003-04-22 18:02:52 +0000599 // Allocate a new module to read
600 CurModule.CurrentModule = new Module(Filename);
Chris Lattner00950542001-06-06 20:29:01 +0000601 yyparse(); // Parse the file.
602 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000603 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
604 ParserResult = 0;
605
606 return Result;
607}
608
609%}
610
611%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000612 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000613 Function *FunctionVal;
Chris Lattner69da5cf2002-10-13 20:57:00 +0000614 std::pair<PATypeHolder*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000615 BasicBlock *BasicBlockVal;
616 TerminatorInst *TermInstVal;
617 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000618 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000619
Chris Lattner30c89792001-09-07 16:35:17 +0000620 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000621 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000622 Value *ValueVal;
623
Chris Lattner69da5cf2002-10-13 20:57:00 +0000624 std::vector<std::pair<PATypeHolder*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000625 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000626 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000627 std::list<std::pair<Value*,
628 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000629 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000630 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000631
Chris Lattner4ad02e72003-04-16 20:28:45 +0000632 GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000633 int64_t SInt64Val;
634 uint64_t UInt64Val;
635 int SIntVal;
636 unsigned UIntVal;
637 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000638 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000639
Chris Lattner30c89792001-09-07 16:35:17 +0000640 char *StrVal; // This memory is strdup'd!
641 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000642
Chris Lattner30c89792001-09-07 16:35:17 +0000643 Instruction::BinaryOps BinaryOpVal;
644 Instruction::TermOps TermOpVal;
645 Instruction::MemoryOps MemOpVal;
646 Instruction::OtherOps OtherOpVal;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000647 Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000648}
649
Chris Lattner79df7c02002-03-26 18:01:55 +0000650%type <ModuleVal> Module FunctionList
651%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000652%type <BasicBlockVal> BasicBlock InstructionList
653%type <TermInstVal> BBTerminatorInst
654%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000655%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000656%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000657%type <ArgList> ArgList ArgListH
658%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000659%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000660%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000661%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000662%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000663%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000664%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000665%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000666%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000667%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000668
Chris Lattner2079fde2001-10-13 06:41:08 +0000669// ValueRef - Unresolved reference to a definition or BB
670%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000671%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000672// Tokens and types for handling constant integer values
673//
674// ESINT64VAL - A negative number within long long range
675%token <SInt64Val> ESINT64VAL
676
677// EUINT64VAL - A positive number within uns. long long range
678%token <UInt64Val> EUINT64VAL
679%type <SInt64Val> EINT64VAL
680
681%token <SIntVal> SINTVAL // Signed 32 bit ints...
682%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
683%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000684%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000685
686// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000687%type <TypeVal> Types TypesV UpRTypes UpRTypesV
688%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000689%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
690%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000691
Chris Lattner6c23f572003-08-22 05:42:10 +0000692%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
693%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000694
695
Chris Lattnerf4600482003-06-28 20:01:34 +0000696%token IMPLEMENTATION ZEROINITIALIZER TRUE FALSE BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000697%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnerf797cab2003-10-10 04:54:02 +0000698%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000699%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000700
701// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000702%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000703
Chris Lattner00950542001-06-06 20:29:01 +0000704// Binary Operators
705%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000706%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000707%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000708%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000709
710// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000711%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000712
Chris Lattner027dcc52001-07-08 21:10:27 +0000713// Other Operators
714%type <OtherOpVal> ShiftOps
Chris Lattner36143fc2003-09-08 18:54:55 +0000715%token <OtherOpVal> PHI CALL CAST SHL SHR VA_ARG
Chris Lattner027dcc52001-07-08 21:10:27 +0000716
Chris Lattner00950542001-06-06 20:29:01 +0000717%start Module
718%%
719
720// Handle constant integer size restriction and conversion...
721//
Chris Lattner51727be2002-06-04 21:58:56 +0000722INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000723INTVAL : UINTVAL {
724 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
725 ThrowException("Value too large for type!");
726 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000727};
Chris Lattner00950542001-06-06 20:29:01 +0000728
729
Chris Lattner51727be2002-06-04 21:58:56 +0000730EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000731EINT64VAL : EUINT64VAL {
732 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
733 ThrowException("Value too large for type!");
734 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000735};
Chris Lattner00950542001-06-06 20:29:01 +0000736
Chris Lattner00950542001-06-06 20:29:01 +0000737// Operations that are notably excluded from this list include:
738// RET, BR, & SWITCH because they end basic blocks and are treated specially.
739//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000740ArithmeticOps: ADD | SUB | MUL | DIV | REM;
741LogicalOps : AND | OR | XOR;
742SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
743BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
744
Chris Lattner51727be2002-06-04 21:58:56 +0000745ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000746
Chris Lattnere98dda62001-07-14 06:10:16 +0000747// These are some types that allow classification if we only want a particular
748// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000749SIntType : LONG | INT | SHORT | SBYTE;
750UIntType : ULONG | UINT | USHORT | UBYTE;
751IntType : SIntType | UIntType;
752FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000753
Chris Lattnere98dda62001-07-14 06:10:16 +0000754// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000755OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000756 $$ = $1;
757 }
758 | /*empty*/ {
759 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000760 };
Chris Lattner00950542001-06-06 20:29:01 +0000761
Chris Lattner4ad02e72003-04-16 20:28:45 +0000762OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
763 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000764 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000765 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
766 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000767
768//===----------------------------------------------------------------------===//
769// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000770// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000771// access to it, a user must explicitly use TypesV.
772//
773
774// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000775TypesV : Types | VOID { $$ = new PATypeHolder($1); };
776UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000777
778Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000779 if (UpRefs.size())
780 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
781 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000782 };
Chris Lattner30c89792001-09-07 16:35:17 +0000783
784
785// Derived types are added later...
786//
Chris Lattner51727be2002-06-04 21:58:56 +0000787PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
788PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000789UpRTypes : OPAQUE {
790 $$ = new PATypeHolder(OpaqueType::get());
791 }
792 | PrimType {
793 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000794 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000795UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000796 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000797};
Chris Lattner30c89792001-09-07 16:35:17 +0000798
Chris Lattner30c89792001-09-07 16:35:17 +0000799// Include derived types in the Types production.
800//
801UpRTypes : '\\' EUINT64VAL { // Type UpReference
802 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
803 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner9232b992003-04-22 18:42:41 +0000804 UpRefs.push_back(std::make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000805 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000806 UR_OUT("New Upreference!\n");
807 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000808 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000809 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000810 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000811 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000812 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
813 if (isVarArg) Params.pop_back();
814
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000815 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000816 delete $3; // Delete the argument list
817 delete $1; // Delete the old type handle
818 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000819 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000820 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000821 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000822 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000823 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000824 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000825 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +0000826 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000827
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000828 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000829 delete $2;
830 }
831 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000832 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000833 }
834 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000835 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000836 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000837 };
Chris Lattner30c89792001-09-07 16:35:17 +0000838
Chris Lattner7e708292002-06-25 16:13:24 +0000839// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000840// declaration type lists
841//
842TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +0000843 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000844 $$->push_back(*$1); delete $1;
845 }
846 | TypeListI ',' UpRTypes {
847 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000848 };
Chris Lattner30c89792001-09-07 16:35:17 +0000849
Chris Lattner7e708292002-06-25 16:13:24 +0000850// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000851ArgTypeListI : TypeListI
852 | TypeListI ',' DOTDOTDOT {
853 ($$=$1)->push_back(Type::VoidTy);
854 }
855 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +0000856 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000857 }
858 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +0000859 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000860 };
Chris Lattner30c89792001-09-07 16:35:17 +0000861
Chris Lattnere98dda62001-07-14 06:10:16 +0000862// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000863// production is used ONLY to represent constants that show up AFTER a 'const',
864// 'constant' or 'global' token at global scope. Constants that can be inlined
865// into other expressions (such as integers and constexprs) are handled by the
866// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000867//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000868ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +0000869 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000870 if (ATy == 0)
871 ThrowException("Cannot make array constant with type: '" +
872 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000873 const Type *ETy = ATy->getElementType();
874 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000875
Chris Lattner30c89792001-09-07 16:35:17 +0000876 // Verify that we have the correct size...
877 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000878 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000879 utostr($3->size()) + " arguments, but has size of " +
880 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000881
Chris Lattner30c89792001-09-07 16:35:17 +0000882 // Verify all elements are correct type!
883 for (unsigned i = 0; i < $3->size(); i++) {
884 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000885 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000886 ETy->getDescription() +"' as required!\nIt is of type '"+
887 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000888 }
889
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000890 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000891 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000892 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000893 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +0000894 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000895 if (ATy == 0)
896 ThrowException("Cannot make array constant with type: '" +
897 (*$1)->getDescription() + "'!");
898
899 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000900 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000901 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000902 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +0000903 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000904 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000905 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000906 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +0000907 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000908 if (ATy == 0)
909 ThrowException("Cannot make array constant with type: '" +
910 (*$1)->getDescription() + "'!");
911
Chris Lattner30c89792001-09-07 16:35:17 +0000912 int NumElements = ATy->getNumElements();
913 const Type *ETy = ATy->getElementType();
914 char *EndStr = UnEscapeLexed($3, true);
915 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000916 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000917 itostr((int)(EndStr-$3)) +
918 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +0000919 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000920 if (ETy == Type::SByteTy) {
921 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000922 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000923 } else if (ETy == Type::UByteTy) {
924 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +0000925 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000926 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000927 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000928 ThrowException("Cannot build string arrays of non byte sized elements!");
929 }
Chris Lattner30c89792001-09-07 16:35:17 +0000930 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000931 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000932 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000933 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000934 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +0000935 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000936 if (STy == 0)
937 ThrowException("Cannot make struct constant with type: '" +
938 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +0000939
Chris Lattnerc6212f12003-05-21 16:06:56 +0000940 if ($3->size() != STy->getNumContainedTypes())
941 ThrowException("Illegal number of initializers for structure type!");
942
Chris Lattneraf76d0e2003-04-15 16:09:31 +0000943 // Check to ensure that constants are compatible with the type initializer!
944 for (unsigned i = 0, e = $3->size(); i != e; ++i)
945 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
946 ThrowException("Expected type '" +
947 STy->getElementTypes()[i]->getDescription() +
948 "' for element #" + utostr(i) +
949 " of structure initializer!");
950
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000951 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000952 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000953 }
Chris Lattnerc6212f12003-05-21 16:06:56 +0000954 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +0000955 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +0000956 if (STy == 0)
957 ThrowException("Cannot make struct constant with type: '" +
958 (*$1)->getDescription() + "'!");
959
960 if (STy->getNumContainedTypes() != 0)
961 ThrowException("Illegal number of initializers for structure type!");
962
963 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
964 delete $1;
965 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000966 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +0000967 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000968 if (PTy == 0)
969 ThrowException("Cannot make null pointer constant with type: '" +
970 (*$1)->getDescription() + "'!");
971
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000972 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000973 delete $1;
974 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000975 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +0000976 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000977 if (Ty == 0)
978 ThrowException("Global const reference must be a pointer type!");
979
Chris Lattner3101c252002-08-15 17:58:33 +0000980 // ConstExprs can exist in the body of a function, thus creating
981 // ConstantPointerRefs whenever they refer to a variable. Because we are in
982 // the context of a function, getValNonImprovising will search the functions
983 // symbol table instead of the module symbol table for the global symbol,
984 // which throws things all off. To get around this, we just tell
985 // getValNonImprovising that we are at global scope here.
986 //
Chris Lattner394f2eb2003-10-16 20:12:13 +0000987 Function *SavedCurFn = CurFun.CurrentFunction;
988 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +0000989
Chris Lattner2079fde2001-10-13 06:41:08 +0000990 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000991
Chris Lattner394f2eb2003-10-16 20:12:13 +0000992 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +0000993
Chris Lattner2079fde2001-10-13 06:41:08 +0000994 // If this is an initializer for a constant pointer, which is referencing a
995 // (currently) undefined variable, create a stub now that shall be replaced
996 // in the future with the right type of variable.
997 //
998 if (V == 0) {
999 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1000 const PointerType *PT = cast<PointerType>(Ty);
1001
1002 // First check to see if the forward references value is already created!
1003 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001004 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001005
1006 if (I != CurModule.GlobalRefs.end()) {
1007 V = I->second; // Placeholder already exists, use it...
1008 } else {
1009 // TODO: Include line number info by creating a subclass of
1010 // TODO: GlobalVariable here that includes the said information!
1011
1012 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001013 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001014 false,
1015 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001016 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001017 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001018
1019 // Must temporarily push this value into the module table...
1020 CurModule.CurrentModule->getGlobalList().push_back(GV);
1021 V = GV;
1022 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001023 }
1024
Chris Lattner2079fde2001-10-13 06:41:08 +00001025 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001026 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001027 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001028 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001029 | Types ConstExpr {
1030 if ($1->get() != $2->getType())
1031 ThrowException("Mismatched types for constant expression!");
1032 $$ = $2;
1033 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001034 }
1035 | Types ZEROINITIALIZER {
1036 $$ = Constant::getNullValue($1->get());
1037 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001038 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001039
Chris Lattnere43f40b2002-10-09 00:25:32 +00001040ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001041 if (!ConstantSInt::isValueValidForType($1, $2))
1042 ThrowException("Constant value doesn't fit in type!");
1043 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001044 }
1045 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001046 if (!ConstantUInt::isValueValidForType($1, $2))
1047 ThrowException("Constant value doesn't fit in type!");
1048 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001049 }
1050 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001051 $$ = ConstantBool::True;
1052 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001053 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001054 $$ = ConstantBool::False;
1055 }
1056 | FPType FPVAL { // Float & Double constants
1057 $$ = ConstantFP::get($1, $2);
1058 };
1059
Chris Lattner00950542001-06-06 20:29:01 +00001060
Chris Lattnerd78700d2002-08-16 21:14:40 +00001061ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001062 if (!$5->get()->isFirstClassType())
1063 ThrowException("cast constant expression to a non-primitive type: '" +
1064 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001065 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001066 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001067 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001068 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1069 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001070 ThrowException("GetElementPtr requires a pointer operand!");
1071
1072 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001073 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001074 if (!IdxTy)
1075 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001076
Chris Lattner9232b992003-04-22 18:42:41 +00001077 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001078 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1079 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001080 IdxVec.push_back(C);
1081 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001082 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001083
Chris Lattnerd78700d2002-08-16 21:14:40 +00001084 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001085
Chris Lattnerd78700d2002-08-16 21:14:40 +00001086 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001087 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001088 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001089 if ($3->getType() != $5->getType())
1090 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001091 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001092 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001093 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001094 if ($5->getType() != Type::UByteTy)
1095 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001096 if (!$3->getType()->isInteger())
1097 ThrowException("Shift constant expression requires integer operand!");
Chris Lattner5e458e22003-05-21 17:48:56 +00001098 $$ = ConstantExpr::getShift($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001099 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001100
1101
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001102// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001103ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001104 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001105 }
1106 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001107 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001108 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001109 };
Chris Lattner00950542001-06-06 20:29:01 +00001110
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001111
Chris Lattner1781aca2001-09-18 04:00:54 +00001112// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001113GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001114
Chris Lattner00950542001-06-06 20:29:01 +00001115
Chris Lattner0e73ce62002-05-02 19:11:13 +00001116//===----------------------------------------------------------------------===//
1117// Rules to match Modules
1118//===----------------------------------------------------------------------===//
1119
1120// Module rule: Capture the result of parsing the whole file into a result
1121// variable...
1122//
1123Module : FunctionList {
1124 $$ = ParserResult = $1;
1125 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001126};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001127
Chris Lattner7e708292002-06-25 16:13:24 +00001128// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001129//
1130FunctionList : FunctionList Function {
1131 $$ = $1;
1132 assert($2->getParent() == 0 && "Function already in module!");
1133 $1->getFunctionList().push_back($2);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001134 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001135 }
1136 | FunctionList FunctionProto {
1137 $$ = $1;
1138 }
1139 | FunctionList IMPLEMENTATION {
1140 $$ = $1;
1141 }
1142 | ConstPool {
1143 $$ = CurModule.CurrentModule;
1144 // Resolve circular types before we parse the body of the module
1145 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001146 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001147
Chris Lattnere98dda62001-07-14 06:10:16 +00001148// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001149ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001150 if (!setValueName($4, $2))
1151 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001152 }
Chris Lattner30c89792001-09-07 16:35:17 +00001153 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001154 // Eagerly resolve types. This is not an optimization, this is a
1155 // requirement that is due to the fact that we could have this:
1156 //
1157 // %list = type { %list * }
1158 // %list = type { %list * } ; repeated type decl
1159 //
1160 // If types are not resolved eagerly, then the two types will not be
1161 // determined to be the same type!
1162 //
1163 ResolveTypeTo($2, $4->get());
1164
Chris Lattner1781aca2001-09-18 04:00:54 +00001165 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001166 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1167 // If this is not a redefinition of a type...
1168 if (!$2) {
1169 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001170 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001171 }
Chris Lattner30c89792001-09-07 16:35:17 +00001172 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001173
1174 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001175 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001176 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001177 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001178 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001179 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001180 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001181 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001182 if (Initializer == 0)
1183 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001184
Chris Lattnerdda71962001-11-26 18:54:16 +00001185 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001186 if (!setValueName(GV, $2)) { // If not redefining...
1187 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001188 int Slot = InsertValue(GV, CurModule.Values);
1189
1190 if (Slot != -1) {
1191 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1192 } else {
1193 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1194 (char*)GV->getName().c_str()));
1195 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001196 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001197 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001198 | ConstPool OptAssign EXTERNAL GlobalType Types {
1199 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001200 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001201 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001202 if (!setValueName(GV, $2)) { // If not redefining...
1203 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001204 int Slot = InsertValue(GV, CurModule.Values);
1205
1206 if (Slot != -1) {
1207 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1208 } else {
1209 assert(GV->hasName() && "Not named and not numbered!?");
1210 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1211 (char*)GV->getName().c_str()));
1212 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001213 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001214 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001215 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001216 | ConstPool TARGET TargetDefinition {
1217 }
Chris Lattner00950542001-06-06 20:29:01 +00001218 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001219 };
Chris Lattner00950542001-06-06 20:29:01 +00001220
1221
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001222
1223BigOrLittle : BIG { $$ = Module::BigEndian; };
1224BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1225
1226TargetDefinition : ENDIAN '=' BigOrLittle {
1227 CurModule.CurrentModule->setEndianness($3);
1228 }
1229 | POINTERSIZE '=' EUINT64VAL {
1230 if ($3 == 32)
1231 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1232 else if ($3 == 64)
1233 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1234 else
1235 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1236 };
1237
1238
Chris Lattner00950542001-06-06 20:29:01 +00001239//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001240// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001241//===----------------------------------------------------------------------===//
1242
Chris Lattner6c23f572003-08-22 05:42:10 +00001243Name : VAR_ID | STRINGCONSTANT;
1244OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001245
Chris Lattner6c23f572003-08-22 05:42:10 +00001246ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001247 if (*$1 == Type::VoidTy)
1248 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001249 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001250};
Chris Lattner00950542001-06-06 20:29:01 +00001251
Chris Lattner69da5cf2002-10-13 20:57:00 +00001252ArgListH : ArgListH ',' ArgVal {
1253 $$ = $1;
1254 $1->push_back(*$3);
1255 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001256 }
1257 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001258 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001259 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001260 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001261 };
Chris Lattner00950542001-06-06 20:29:01 +00001262
1263ArgList : ArgListH {
1264 $$ = $1;
1265 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001266 | ArgListH ',' DOTDOTDOT {
1267 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001268 $$->push_back(std::pair<PATypeHolder*,
1269 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001270 }
1271 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001272 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1273 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001274 }
Chris Lattner00950542001-06-06 20:29:01 +00001275 | /* empty */ {
1276 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001277 };
Chris Lattner00950542001-06-06 20:29:01 +00001278
Chris Lattner6c23f572003-08-22 05:42:10 +00001279FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001280 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001281 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001282
Chris Lattner9232b992003-04-22 18:42:41 +00001283 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001284 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001285 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001286 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001287 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001288 }
Chris Lattner00950542001-06-06 20:29:01 +00001289
Chris Lattner2079fde2001-10-13 06:41:08 +00001290 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1291 if (isVarArg) ParamTypeList.pop_back();
1292
Chris Lattner1f862af2003-04-16 18:13:57 +00001293 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001294 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001295 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001296
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001297 Function *Fn = 0;
1298 // Is the function already in symtab?
1299 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1300 // Yes it is. If this is the case, either we need to be a forward decl,
1301 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001302 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001303 ThrowException("Redefinition of function '" + FunctionName + "'!");
1304
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001305 // If we found a preexisting function prototype, remove it from the
1306 // module, so that we don't get spurious conflicts with global & local
1307 // variables.
1308 //
1309 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001310
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001311 // Make sure to strip off any argument names so we can't get conflicts...
1312 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1313 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001314
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001315 } else { // Not already defined?
Chris Lattner4ad02e72003-04-16 20:28:45 +00001316 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001317 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001318 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001319 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001320 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001321
Chris Lattner394f2eb2003-10-16 20:12:13 +00001322 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001323
Chris Lattner7e708292002-06-25 16:13:24 +00001324 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001325 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001326 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001327 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001328 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001329 delete $4->back().first;
1330 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001331 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001332 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001333 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001334 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001335 delete I->first; // Delete the typeholder...
1336
1337 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001338 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001339
Chris Lattner69da5cf2002-10-13 20:57:00 +00001340 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001341 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001342
Chris Lattner1f862af2003-04-16 18:13:57 +00001343 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001344 }
Chris Lattner51727be2002-06-04 21:58:56 +00001345};
Chris Lattner00950542001-06-06 20:29:01 +00001346
Chris Lattner9b02cc32002-05-03 18:23:48 +00001347BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1348
Chris Lattner4ad02e72003-04-16 20:28:45 +00001349FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001350 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001351
Chris Lattner4ad02e72003-04-16 20:28:45 +00001352 // Make sure that we keep track of the linkage type even if there was a
1353 // previous "declare".
1354 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001355
Chris Lattner7e708292002-06-25 16:13:24 +00001356 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001357 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001358};
Chris Lattner00950542001-06-06 20:29:01 +00001359
Chris Lattner9b02cc32002-05-03 18:23:48 +00001360END : ENDTOK | '}'; // Allow end of '}' to end a function
1361
Chris Lattner79df7c02002-03-26 18:01:55 +00001362Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001363 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001364};
Chris Lattner00950542001-06-06 20:29:01 +00001365
Chris Lattner394f2eb2003-10-16 20:12:13 +00001366FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1367 $$ = CurFun.CurrentFunction;
Chris Lattner79df7c02002-03-26 18:01:55 +00001368 assert($$->getParent() == 0 && "Function already in module!");
1369 CurModule.CurrentModule->getFunctionList().push_back($$);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001370 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001371};
Chris Lattner00950542001-06-06 20:29:01 +00001372
1373//===----------------------------------------------------------------------===//
1374// Rules to match Basic Blocks
1375//===----------------------------------------------------------------------===//
1376
1377ConstValueRef : ESINT64VAL { // A reference to a direct constant
1378 $$ = ValID::create($1);
1379 }
1380 | EUINT64VAL {
1381 $$ = ValID::create($1);
1382 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001383 | FPVAL { // Perhaps it's an FP constant?
1384 $$ = ValID::create($1);
1385 }
Chris Lattner00950542001-06-06 20:29:01 +00001386 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001387 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001388 }
1389 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001390 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001391 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001392 | NULL_TOK {
1393 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001394 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001395 | ConstExpr {
1396 $$ = ValID::create($1);
1397 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001398
Chris Lattner2079fde2001-10-13 06:41:08 +00001399// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1400// another value.
1401//
1402SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001403 $$ = ValID::create($1);
1404 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001405 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001406 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001407 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001408
1409// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001410ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001411
Chris Lattner00950542001-06-06 20:29:01 +00001412
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001413// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1414// type immediately preceeds the value reference, and allows complex constant
1415// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001416ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001417 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001418 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001419
Chris Lattner00950542001-06-06 20:29:01 +00001420BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001421 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001422 }
Chris Lattner7e708292002-06-25 16:13:24 +00001423 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1424 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001425 };
Chris Lattner00950542001-06-06 20:29:01 +00001426
1427
1428// Basic blocks are terminated by branching instructions:
1429// br, br/cc, switch, ret
1430//
Chris Lattner2079fde2001-10-13 06:41:08 +00001431BasicBlock : InstructionList OptAssign BBTerminatorInst {
1432 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1433 InsertValue($3);
1434
1435 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001436 InsertValue($1);
1437 $$ = $1;
1438 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001439 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1440 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1441 InsertValue($4);
1442
1443 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001444 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001445
1446 InsertValue($2);
1447 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001448 };
Chris Lattner00950542001-06-06 20:29:01 +00001449
1450InstructionList : InstructionList Inst {
1451 $1->getInstList().push_back($2);
1452 $$ = $1;
1453 }
1454 | /* empty */ {
Chris Lattner8c8418d2003-09-01 16:31:28 +00001455 $$ = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001456 };
Chris Lattner00950542001-06-06 20:29:01 +00001457
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001458BBTerminatorInst : RET ResolvedVal { // Return with a result...
1459 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001460 }
1461 | RET VOID { // Return with no result...
1462 $$ = new ReturnInst();
1463 }
1464 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001465 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001466 } // Conditional Branch...
1467 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001468 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1469 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001470 getVal(Type::BoolTy, $3));
1471 }
1472 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1473 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001474 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001475 $$ = S;
1476
Chris Lattner9232b992003-04-22 18:42:41 +00001477 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001478 E = $8->end();
1479 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001480 S->addCase(I->first, I->second);
Chris Lattner00950542001-06-06 20:29:01 +00001481 }
Chris Lattner196850c2003-05-15 21:30:00 +00001482 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1483 SwitchInst *S = new SwitchInst(getVal($2, $3),
1484 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1485 $$ = S;
1486 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001487 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1488 EXCEPT ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001489 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001490 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001491
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001492 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1493 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001494 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001495 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001496 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001497 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1498 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001499 ParamTypes.push_back((*I)->getType());
1500 }
1501
1502 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1503 if (isVarArg) ParamTypes.pop_back();
1504
Chris Lattner79df7c02002-03-26 18:01:55 +00001505 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001506 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001507 }
1508 delete $2;
1509
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001510 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001511
1512 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1513 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1514
1515 if (Normal == 0 || Except == 0)
1516 ThrowException("Invoke instruction without label destinations!");
1517
1518 // Create the call node...
1519 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001520 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001521 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001522 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001523 // correctly!
1524 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001525 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1526 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001527 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001528
1529 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1530 if ((*ArgI)->getType() != *I)
1531 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001532 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001533
1534 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1535 ThrowException("Invalid number of parameters detected!");
1536
Chris Lattner6cdb0112001-11-26 16:54:11 +00001537 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001538 }
1539 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001540 }
1541 | UNWIND {
1542 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001543 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001544
1545
Chris Lattner00950542001-06-06 20:29:01 +00001546
1547JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1548 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001549 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001550 if (V == 0)
1551 ThrowException("May only switch on a constant pool value!");
1552
Chris Lattner9232b992003-04-22 18:42:41 +00001553 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001554 }
1555 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001556 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001557 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001558
1559 if (V == 0)
1560 ThrowException("May only switch on a constant pool value!");
1561
Chris Lattner9232b992003-04-22 18:42:41 +00001562 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001563 };
Chris Lattner00950542001-06-06 20:29:01 +00001564
1565Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001566 // Is this definition named?? if so, assign the name...
1567 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001568 InsertValue($2);
1569 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001570};
Chris Lattner00950542001-06-06 20:29:01 +00001571
Chris Lattnerc24d2082001-06-11 15:04:20 +00001572PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001573 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1574 $$->push_back(std::make_pair(getVal(*$1, $3),
1575 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001576 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001577 }
1578 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1579 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001580 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1581 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001582 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001583
1584
Chris Lattner30c89792001-09-07 16:35:17 +00001585ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001586 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001587 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001588 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001589 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001590 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001591 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001592 };
Chris Lattner00950542001-06-06 20:29:01 +00001593
1594// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001595ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001596
Chris Lattner4a6482b2002-09-10 19:57:26 +00001597InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1598 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1599 ThrowException("Arithmetic operator requires integer or FP operands!");
1600 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1601 if ($$ == 0)
1602 ThrowException("binary operator returned null!");
1603 delete $2;
1604 }
1605 | LogicalOps Types ValueRef ',' ValueRef {
1606 if (!(*$2)->isIntegral())
1607 ThrowException("Logical operator requires integral operands!");
1608 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1609 if ($$ == 0)
1610 ThrowException("binary operator returned null!");
1611 delete $2;
1612 }
1613 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001614 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001615 if ($$ == 0)
1616 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001617 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001618 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001619 | NOT ResolvedVal {
1620 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1621 << " Replacing with 'xor'.\n";
1622
1623 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1624 if (Ones == 0)
1625 ThrowException("Expected integral type for not instruction!");
1626
1627 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001628 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001629 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001630 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001631 | ShiftOps ResolvedVal ',' ResolvedVal {
1632 if ($4->getType() != Type::UByteTy)
1633 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001634 if (!$2->getType()->isInteger())
1635 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001636 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001637 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001638 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001639 if (!$4->get()->isFirstClassType())
1640 ThrowException("cast instruction to a non-primitive type: '" +
1641 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001642 $$ = new CastInst($2, *$4);
1643 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001644 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001645 | VA_ARG ResolvedVal ',' Types {
1646 $$ = new VarArgInst($2, *$4);
1647 delete $4;
1648 }
Chris Lattnerc24d2082001-06-11 15:04:20 +00001649 | PHI PHIList {
1650 const Type *Ty = $2->front().first->getType();
1651 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001652 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001653 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001654 if ($2->front().first->getType() != Ty)
1655 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001656 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001657 $2->pop_front();
1658 }
1659 delete $2; // Free the list...
1660 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001661 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001662 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001663 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001664
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001665 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1666 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001667 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001668 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001669 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001670 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1671 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001672 ParamTypes.push_back((*I)->getType());
1673 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001674
1675 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1676 if (isVarArg) ParamTypes.pop_back();
1677
Chris Lattner79df7c02002-03-26 18:01:55 +00001678 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001679 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001680 }
Chris Lattner30c89792001-09-07 16:35:17 +00001681 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001682
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001683 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001684
Chris Lattner8b81bf52001-07-25 22:47:46 +00001685 // Create the call node...
1686 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001687 // Make sure no arguments is a good thing!
1688 if (Ty->getNumParams() != 0)
1689 ThrowException("No arguments passed to a function that "
1690 "expects arguments!");
1691
Chris Lattner9232b992003-04-22 18:42:41 +00001692 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001693 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001694 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001695 // correctly!
1696 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001697 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1698 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001699 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001700
1701 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1702 if ((*ArgI)->getType() != *I)
1703 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001704 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001705
Chris Lattner8b81bf52001-07-25 22:47:46 +00001706 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001707 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001708
Chris Lattner6cdb0112001-11-26 16:54:11 +00001709 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001710 }
1711 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001712 }
1713 | MemoryInst {
1714 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001715 };
Chris Lattner00950542001-06-06 20:29:01 +00001716
Chris Lattner6cdb0112001-11-26 16:54:11 +00001717
1718// IndexList - List of indices for GEP based instructions...
1719IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001720 $$ = $2;
1721 } | /* empty */ {
1722 $$ = new std::vector<Value*>();
1723 };
1724
1725OptVolatile : VOLATILE {
1726 $$ = true;
1727 }
1728 | /* empty */ {
1729 $$ = false;
1730 };
1731
Chris Lattner027dcc52001-07-08 21:10:27 +00001732
Chris Lattner00950542001-06-06 20:29:01 +00001733MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001734 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001735 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001736 }
1737 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001738 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001739 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001740 }
1741 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001742 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001743 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001744 }
1745 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001746 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001747 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001748 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001749 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001750 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001751 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001752 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001753 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001754 }
1755
Chris Lattner15c9c032003-09-08 18:20:29 +00001756 | OptVolatile LOAD Types ValueRef {
1757 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001758 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001759 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001760 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001761 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001762 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001763 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1764 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001765 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001766 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001767 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001768 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00001769 if (ElTy != $3->getType())
1770 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00001771 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001772
Chris Lattner79ad13802003-09-08 20:29:46 +00001773 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001774 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001775 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001776 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001777 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001778 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001779 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001780 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001781 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1782 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001783 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001784
Chris Lattner00950542001-06-06 20:29:01 +00001785%%
Chris Lattner09083092001-07-08 04:57:15 +00001786int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00001787 std::string where
1788 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001789 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00001790 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001791 if (yychar == YYEMPTY)
1792 errMsg += "end-of-file.";
1793 else
Chris Lattner9232b992003-04-22 18:42:41 +00001794 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001795 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001796 return 0;
1797}