blob: 725f2547573bef0c56c2bb1e6e5f563dad33050b [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 ~PerFunctionInfo() {}
Chris Lattner00950542001-06-06 20:29:01 +0000141
Chris Lattner79df7c02002-03-26 18:01:55 +0000142 inline void FunctionStart(Function *M) {
143 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000144 }
145
Chris Lattner79df7c02002-03-26 18:01:55 +0000146 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000147 // If we could not resolve some blocks at parsing time (forward branches)
148 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000149 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000150
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000151 // Make sure to resolve any constant expr references that might exist within
152 // the function we just declared itself.
153 ValID FID;
154 if (CurrentFunction->hasName()) {
155 FID = ValID::create((char*)CurrentFunction->getName().c_str());
156 } else {
157 unsigned Slot = CurrentFunction->getType()->getUniqueID();
158 assert(CurModule.Values.size() > Slot && "Function not inserted?");
159 // Figure out which slot number if is...
160 for (unsigned i = 0; ; ++i) {
161 assert(i < CurModule.Values[Slot].size() && "Function not found!");
162 if (CurModule.Values[Slot][i] == CurrentFunction) {
163 FID = ValID::create((int)i);
164 break;
165 }
166 }
167 }
168 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
169
Chris Lattner7e708292002-06-25 16:13:24 +0000170 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000171 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000172 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000173 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000174 }
Chris Lattner7e708292002-06-25 16:13:24 +0000175} CurMeth; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000176
Chris Lattner79df7c02002-03-26 18:01:55 +0000177static bool inFunctionScope() { return CurMeth.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000178
Chris Lattner00950542001-06-06 20:29:01 +0000179
180//===----------------------------------------------------------------------===//
181// Code to handle definitions of all the types
182//===----------------------------------------------------------------------===//
183
Chris Lattner9232b992003-04-22 18:42:41 +0000184static int InsertValue(Value *D,
185 std::vector<ValueList> &ValueTab = CurMeth.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000186 if (D->hasName()) return -1; // Is this a numbered definition?
187
188 // Yes, insert the value into the value table...
189 unsigned type = D->getType()->getUniqueID();
190 if (ValueTab.size() <= type)
191 ValueTab.resize(type+1, ValueList());
192 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
193 ValueTab[type].push_back(D);
194 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000195}
196
Chris Lattner30c89792001-09-07 16:35:17 +0000197// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000198static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000199 Types.push_back(Ty);
200}
201
202static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000203 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000204 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000205 unsigned Num = (unsigned)D.Num;
206
207 // Module constants occupy the lowest numbered slots...
208 if (Num < CurModule.Types.size())
209 return CurModule.Types[Num];
210
211 Num -= CurModule.Types.size();
212
213 // Check that the number is within bounds...
214 if (Num <= CurMeth.Types.size())
215 return CurMeth.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000216 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000217 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000218 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000219 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000220 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000221 Value *N = 0;
222 if (inFunctionScope()) {
223 SymTab = &CurMeth.CurrentFunction->getSymbolTable();
224 N = SymTab->lookup(Type::TypeTy, Name);
225 }
Chris Lattner30c89792001-09-07 16:35:17 +0000226
227 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000228 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000229 // hasn't been added to the module...
230 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000231 SymTab = &CurModule.CurrentModule->getSymbolTable();
232 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000233 if (N == 0) break;
234 }
235
236 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000237 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000238 }
239 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000240 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000241 }
242
243 // If we reached here, we referenced either a symbol that we don't know about
244 // or an id number that hasn't been read yet. We may be referencing something
245 // forward, so just create an entry to be resolved later and get to it...
246 //
247 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
248
Chris Lattner9232b992003-04-22 18:42:41 +0000249 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000250 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
251
Chris Lattner9232b992003-04-22 18:42:41 +0000252 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000253 if (I != LateResolver.end()) {
254 return I->second;
255 }
Chris Lattner30c89792001-09-07 16:35:17 +0000256
Chris Lattner82269592001-10-22 06:01:08 +0000257 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000258 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000259 return Typ;
260}
261
Chris Lattner9232b992003-04-22 18:42:41 +0000262static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000263 SymbolTable &SymTab =
Chris Lattner9705a152002-05-02 19:27:42 +0000264 inFunctionScope() ? CurMeth.CurrentFunction->getSymbolTable() :
265 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000266 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000267}
268
Chris Lattner2079fde2001-10-13 06:41:08 +0000269// getValNonImprovising - Look up the value specified by the provided type and
270// the provided ValID. If the value exists and has already been defined, return
271// it. Otherwise return null.
272//
273static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000274 if (isa<FunctionType>(Ty))
275 ThrowException("Functions are not values and "
276 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000277
Chris Lattner30c89792001-09-07 16:35:17 +0000278 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000279 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000280 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000281 unsigned Num = (unsigned)D.Num;
282
283 // Module constants occupy the lowest numbered slots...
284 if (type < CurModule.Values.size()) {
285 if (Num < CurModule.Values[type].size())
286 return CurModule.Values[type][Num];
287
288 Num -= CurModule.Values[type].size();
289 }
290
291 // Make sure that our type is within bounds
Chris Lattner2079fde2001-10-13 06:41:08 +0000292 if (CurMeth.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000293
294 // Check that the number is within bounds...
Chris Lattner2079fde2001-10-13 06:41:08 +0000295 if (CurMeth.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000296
297 return CurMeth.Values[type][Num];
298 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000299
Chris Lattner1a1cb112001-09-30 22:46:54 +0000300 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000301 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000302 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000303
304 D.destroy(); // Free old strdup'd memory...
305 return N;
306 }
307
Chris Lattner2079fde2001-10-13 06:41:08 +0000308 // Check to make sure that "Ty" is an integral type, and that our
309 // value will fit into the specified type...
310 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000311 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
312 ThrowException("Signed integral constant '" +
313 itostr(D.ConstPool64) + "' is invalid for type '" +
314 Ty->getDescription() + "'!");
315 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000316
317 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000318 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
319 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000320 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
321 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000322 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000323 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000324 }
325 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000326 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000327 }
328
Chris Lattner2079fde2001-10-13 06:41:08 +0000329 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000330 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000331 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000332 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000333
334 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000335 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000336 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000337 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000338
Chris Lattnerd78700d2002-08-16 21:14:40 +0000339 case ValID::ConstantVal: // Fully resolved constant?
340 if (D.ConstantValue->getType() != Ty)
341 ThrowException("Constant expression type different from required type!");
342 return D.ConstantValue;
343
Chris Lattner30c89792001-09-07 16:35:17 +0000344 default:
345 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000346 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000347 } // End of switch
348
Chris Lattner2079fde2001-10-13 06:41:08 +0000349 assert(0 && "Unhandled case!");
350 return 0;
351}
352
353
354// getVal - This function is identical to getValNonImprovising, except that if a
355// value is not already defined, it "improvises" by creating a placeholder var
356// that looks and acts just like the requested variable. When the value is
357// defined later, all uses of the placeholder variable are replaced with the
358// real thing.
359//
360static Value *getVal(const Type *Ty, const ValID &D) {
361 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
362
363 // See if the value has already been defined...
364 Value *V = getValNonImprovising(Ty, D);
365 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000366
367 // If we reached here, we referenced either a symbol that we don't know about
368 // or an id number that hasn't been read yet. We may be referencing something
369 // forward, so just create an entry to be resolved later and get to it...
370 //
Chris Lattner00950542001-06-06 20:29:01 +0000371 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000372 switch (Ty->getPrimitiveID()) {
373 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000374 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000375 }
376
377 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000378 if (inFunctionScope())
Chris Lattner386a3b72001-10-16 19:54:17 +0000379 InsertValue(d, CurMeth.LateResolveValues);
380 else
381 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000382 return d;
383}
384
385
386//===----------------------------------------------------------------------===//
387// Code to handle forward references in instructions
388//===----------------------------------------------------------------------===//
389//
390// This code handles the late binding needed with statements that reference
391// values not defined yet... for example, a forward branch, or the PHI node for
392// a loop body.
393//
394// This keeps a table (CurMeth.LateResolveValues) of all such forward references
395// and back patchs after we are done.
396//
397
398// ResolveDefinitions - If we could not resolve some defs at parsing
399// time (forward branches, phi functions for loops, etc...) resolve the
400// defs now...
401//
Chris Lattner9232b992003-04-22 18:42:41 +0000402static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
403 std::vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000404 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
405 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
406 while (!LateResolvers[ty].empty()) {
407 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000408 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
409
Chris Lattner00950542001-06-06 20:29:01 +0000410 LateResolvers[ty].pop_back();
411 ValID &DID = getValIDFromPlaceHolder(V);
412
Chris Lattner2079fde2001-10-13 06:41:08 +0000413 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000414 if (TheRealValue) {
415 V->replaceAllUsesWith(TheRealValue);
416 delete V;
417 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000418 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000419 // resolver table
420 InsertValue(V, *FutureLateResolvers);
421 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000422 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000423 ThrowException("Reference to an invalid definition: '" +DID.getName()+
424 "' of type '" + V->getType()->getDescription() + "'",
425 getLineNumFromPlaceHolder(V));
426 else
427 ThrowException("Reference to an invalid definition: #" +
428 itostr(DID.Num) + " of type '" +
429 V->getType()->getDescription() + "'",
430 getLineNumFromPlaceHolder(V));
431 }
Chris Lattner00950542001-06-06 20:29:01 +0000432 }
433 }
434
435 LateResolvers.clear();
436}
437
Chris Lattner4a42e902001-10-22 05:56:09 +0000438// ResolveTypeTo - A brand new type was just declared. This means that (if
439// name is not null) things referencing Name can be resolved. Otherwise, things
440// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000441//
Chris Lattner4a42e902001-10-22 05:56:09 +0000442static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000443 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000444 CurMeth.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000445
Chris Lattner4a42e902001-10-22 05:56:09 +0000446 ValID D;
447 if (Name) D = ValID::create(Name);
448 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000449
Chris Lattner9232b992003-04-22 18:42:41 +0000450 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner4a42e902001-10-22 05:56:09 +0000451 CurMeth.LateResolveTypes : CurModule.LateResolveTypes;
452
Chris Lattner9232b992003-04-22 18:42:41 +0000453 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000454 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000455 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000456 LateResolver.erase(I);
457 }
458}
459
460// ResolveTypes - At this point, all types should be resolved. Any that aren't
461// are errors.
462//
Chris Lattner9232b992003-04-22 18:42:41 +0000463static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000464 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000465 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000466
467 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000468 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000469 else
Chris Lattner82269592001-10-22 06:01:08 +0000470 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000471 }
472}
473
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000474
Chris Lattner1781aca2001-09-18 04:00:54 +0000475// setValueName - Set the specified value to the name given. The name may be
476// null potentially, in which case this is a noop. The string passed in is
477// assumed to be a malloc'd string buffer, and is freed by this function.
478//
Chris Lattnerb7474512001-10-03 15:39:04 +0000479// This function returns true if the value has already been defined, but is
480// allowed to be redefined in the specified context. If the name is a new name
481// for the typeplane, false is returned.
482//
483static bool setValueName(Value *V, char *NameStr) {
484 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000485
Chris Lattner9232b992003-04-22 18:42:41 +0000486 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000487 free(NameStr); // Free old string
488
Chris Lattner2079fde2001-10-13 06:41:08 +0000489 if (V->getType() == Type::VoidTy)
490 ThrowException("Can't assign name '" + Name +
491 "' to a null valued instruction!");
492
Chris Lattner6e6026b2002-11-20 18:36:02 +0000493 SymbolTable &ST = inFunctionScope() ?
494 CurMeth.CurrentFunction->getSymbolTable() :
495 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000496
Chris Lattner6e6026b2002-11-20 18:36:02 +0000497 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000498 if (Existing) { // Inserting a name that is already defined???
499 // There is only one case where this is allowed: when we are refining an
500 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner949a3622003-07-23 15:30:06 +0000501 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000502 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000503 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000504 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000505 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000506 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000507 }
Chris Lattner30c89792001-09-07 16:35:17 +0000508
Chris Lattner9636a912001-10-01 16:18:37 +0000509 // Otherwise, we are a simple redefinition of a value, check to see if it
510 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000511 if (const Type *Ty = dyn_cast<Type>(Existing)) {
512 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000513 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattner949a3622003-07-23 15:30:06 +0000514 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000515 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
516 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000517 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000518 // We are allowed to redefine a global variable in two circumstances:
519 // 1. If at least one of the globals is uninitialized or
520 // 2. If both initializers have the same value.
521 //
Chris Lattner89219832001-10-03 19:35:04 +0000522 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000523 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
524 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000525
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000526 // Make sure the existing global version gets the initializer! Make
527 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000528 if (GV->hasInitializer() && !EGV->hasInitializer())
529 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000530 if (GV->isConstant())
531 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000532 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000533
Chris Lattner2079fde2001-10-13 06:41:08 +0000534 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000535 return true; // They are equivalent!
536 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000537 }
Chris Lattner9636a912001-10-01 16:18:37 +0000538 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000539 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000540 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000541 }
Chris Lattner00950542001-06-06 20:29:01 +0000542
Chris Lattner6e6026b2002-11-20 18:36:02 +0000543 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000544 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000545}
546
Chris Lattner8896eda2001-07-09 19:38:36 +0000547
Chris Lattner30c89792001-09-07 16:35:17 +0000548//===----------------------------------------------------------------------===//
549// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000550//
Chris Lattner8896eda2001-07-09 19:38:36 +0000551
Chris Lattner30c89792001-09-07 16:35:17 +0000552// TypeContains - Returns true if Ty contains E in it.
553//
554static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000555 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000556}
Chris Lattner698b56e2001-07-20 19:15:08 +0000557
Chris Lattner30c89792001-09-07 16:35:17 +0000558
Chris Lattner9232b992003-04-22 18:42:41 +0000559static std::vector<std::pair<unsigned, OpaqueType *> > UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000560
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000561static PATypeHolder HandleUpRefs(const Type *ty) {
562 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000563 UR_OUT("Type '" << ty->getDescription() <<
564 "' newly formed. Resolving upreferences.\n" <<
565 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000566 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000567 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000568 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000569 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000570 if (TypeContains(Ty, UpRefs[i].second)) {
571 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000572 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000573 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000574 UR_OUT(" * Resolving upreference for "
575 << UpRefs[i].second->getDescription() << endl;
Chris Lattner9232b992003-04-22 18:42:41 +0000576 std::string OldName = UpRefs[i].second->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +0000577 UpRefs[i].second->refineAbstractTypeTo(Ty);
578 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000579 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000580 << (const void*)Ty << ", " << Ty->getDescription() << endl);
581 continue;
582 }
583 }
584
585 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000586 }
Chris Lattner30c89792001-09-07 16:35:17 +0000587 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000588 return Ty;
589}
590
Chris Lattner30c89792001-09-07 16:35:17 +0000591
Chris Lattner00950542001-06-06 20:29:01 +0000592//===----------------------------------------------------------------------===//
593// RunVMAsmParser - Define an interface to this parser
594//===----------------------------------------------------------------------===//
595//
Chris Lattner9232b992003-04-22 18:42:41 +0000596Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000597 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000598 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000599 llvmAsmlineno = 1; // Reset the current line number...
600
Chris Lattner75f20532003-04-22 18:02:52 +0000601 // Allocate a new module to read
602 CurModule.CurrentModule = new Module(Filename);
Chris Lattner00950542001-06-06 20:29:01 +0000603 yyparse(); // Parse the file.
604 Module *Result = ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +0000605 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
606 ParserResult = 0;
607
608 return Result;
609}
610
611%}
612
613%union {
Chris Lattner30c89792001-09-07 16:35:17 +0000614 Module *ModuleVal;
Chris Lattner79df7c02002-03-26 18:01:55 +0000615 Function *FunctionVal;
Chris Lattner69da5cf2002-10-13 20:57:00 +0000616 std::pair<PATypeHolder*, char*> *ArgVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000617 BasicBlock *BasicBlockVal;
618 TerminatorInst *TermInstVal;
619 Instruction *InstVal;
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000620 Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000621
Chris Lattner30c89792001-09-07 16:35:17 +0000622 const Type *PrimType;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000623 PATypeHolder *TypeVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000624 Value *ValueVal;
625
Chris Lattner69da5cf2002-10-13 20:57:00 +0000626 std::vector<std::pair<PATypeHolder*,char*> > *ArgList;
Chris Lattner697954c2002-01-20 22:54:45 +0000627 std::vector<Value*> *ValueList;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000628 std::list<PATypeHolder> *TypeList;
Chris Lattner697954c2002-01-20 22:54:45 +0000629 std::list<std::pair<Value*,
630 BasicBlock*> > *PHIList; // Represent the RHS of PHI node
Chris Lattner46748042002-04-09 19:41:42 +0000631 std::vector<std::pair<Constant*, BasicBlock*> > *JumpTable;
Chris Lattner697954c2002-01-20 22:54:45 +0000632 std::vector<Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000633
Chris Lattner4ad02e72003-04-16 20:28:45 +0000634 GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000635 int64_t SInt64Val;
636 uint64_t UInt64Val;
637 int SIntVal;
638 unsigned UIntVal;
639 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000640 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000641
Chris Lattner30c89792001-09-07 16:35:17 +0000642 char *StrVal; // This memory is strdup'd!
643 ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000644
Chris Lattner30c89792001-09-07 16:35:17 +0000645 Instruction::BinaryOps BinaryOpVal;
646 Instruction::TermOps TermOpVal;
647 Instruction::MemoryOps MemOpVal;
648 Instruction::OtherOps OtherOpVal;
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000649 Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000650}
651
Chris Lattner79df7c02002-03-26 18:01:55 +0000652%type <ModuleVal> Module FunctionList
653%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000654%type <BasicBlockVal> BasicBlock InstructionList
655%type <TermInstVal> BBTerminatorInst
656%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000657%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000658%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000659%type <ArgList> ArgList ArgListH
660%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000661%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000662%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000663%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000664%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000665%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000666%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000667%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000668%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000669%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000670
Chris Lattner2079fde2001-10-13 06:41:08 +0000671// ValueRef - Unresolved reference to a definition or BB
672%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000673%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000674// Tokens and types for handling constant integer values
675//
676// ESINT64VAL - A negative number within long long range
677%token <SInt64Val> ESINT64VAL
678
679// EUINT64VAL - A positive number within uns. long long range
680%token <UInt64Val> EUINT64VAL
681%type <SInt64Val> EINT64VAL
682
683%token <SIntVal> SINTVAL // Signed 32 bit ints...
684%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
685%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000686%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000687
688// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000689%type <TypeVal> Types TypesV UpRTypes UpRTypesV
690%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000691%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
692%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000693
Chris Lattner6c23f572003-08-22 05:42:10 +0000694%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
695%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000696
697
Chris Lattnerf4600482003-06-28 20:01:34 +0000698%token IMPLEMENTATION ZEROINITIALIZER TRUE FALSE BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000699%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnerf797cab2003-10-10 04:54:02 +0000700%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000701%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000702
703// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000704%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000705
Chris Lattner00950542001-06-06 20:29:01 +0000706// Binary Operators
707%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000708%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000709%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000710%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000711
712// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000713%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000714
Chris Lattner027dcc52001-07-08 21:10:27 +0000715// Other Operators
716%type <OtherOpVal> ShiftOps
Chris Lattner36143fc2003-09-08 18:54:55 +0000717%token <OtherOpVal> PHI CALL CAST SHL SHR VA_ARG
Chris Lattner027dcc52001-07-08 21:10:27 +0000718
Chris Lattner00950542001-06-06 20:29:01 +0000719%start Module
720%%
721
722// Handle constant integer size restriction and conversion...
723//
Chris Lattner51727be2002-06-04 21:58:56 +0000724INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000725INTVAL : UINTVAL {
726 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
727 ThrowException("Value too large for type!");
728 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000729};
Chris Lattner00950542001-06-06 20:29:01 +0000730
731
Chris Lattner51727be2002-06-04 21:58:56 +0000732EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000733EINT64VAL : EUINT64VAL {
734 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
735 ThrowException("Value too large for type!");
736 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000737};
Chris Lattner00950542001-06-06 20:29:01 +0000738
Chris Lattner00950542001-06-06 20:29:01 +0000739// Operations that are notably excluded from this list include:
740// RET, BR, & SWITCH because they end basic blocks and are treated specially.
741//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000742ArithmeticOps: ADD | SUB | MUL | DIV | REM;
743LogicalOps : AND | OR | XOR;
744SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
745BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
746
Chris Lattner51727be2002-06-04 21:58:56 +0000747ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000748
Chris Lattnere98dda62001-07-14 06:10:16 +0000749// These are some types that allow classification if we only want a particular
750// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000751SIntType : LONG | INT | SHORT | SBYTE;
752UIntType : ULONG | UINT | USHORT | UBYTE;
753IntType : SIntType | UIntType;
754FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000755
Chris Lattnere98dda62001-07-14 06:10:16 +0000756// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000757OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000758 $$ = $1;
759 }
760 | /*empty*/ {
761 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000762 };
Chris Lattner00950542001-06-06 20:29:01 +0000763
Chris Lattner4ad02e72003-04-16 20:28:45 +0000764OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
765 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000766 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000767 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
768 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000769
770//===----------------------------------------------------------------------===//
771// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000772// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000773// access to it, a user must explicitly use TypesV.
774//
775
776// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000777TypesV : Types | VOID { $$ = new PATypeHolder($1); };
778UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000779
780Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000781 if (UpRefs.size())
782 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
783 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000784 };
Chris Lattner30c89792001-09-07 16:35:17 +0000785
786
787// Derived types are added later...
788//
Chris Lattner51727be2002-06-04 21:58:56 +0000789PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
790PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000791UpRTypes : OPAQUE {
792 $$ = new PATypeHolder(OpaqueType::get());
793 }
794 | PrimType {
795 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000796 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000797UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000798 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000799};
Chris Lattner30c89792001-09-07 16:35:17 +0000800
Chris Lattner30c89792001-09-07 16:35:17 +0000801// Include derived types in the Types production.
802//
803UpRTypes : '\\' EUINT64VAL { // Type UpReference
804 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
805 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner9232b992003-04-22 18:42:41 +0000806 UpRefs.push_back(std::make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000807 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000808 UR_OUT("New Upreference!\n");
809 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000810 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000811 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000812 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000813 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000814 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
815 if (isVarArg) Params.pop_back();
816
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000817 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000818 delete $3; // Delete the argument list
819 delete $1; // Delete the old type handle
820 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000821 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000822 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000823 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000824 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000825 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000826 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000827 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +0000828 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000829
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000830 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000831 delete $2;
832 }
833 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000834 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000835 }
836 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000837 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000838 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000839 };
Chris Lattner30c89792001-09-07 16:35:17 +0000840
Chris Lattner7e708292002-06-25 16:13:24 +0000841// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000842// declaration type lists
843//
844TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +0000845 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000846 $$->push_back(*$1); delete $1;
847 }
848 | TypeListI ',' UpRTypes {
849 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000850 };
Chris Lattner30c89792001-09-07 16:35:17 +0000851
Chris Lattner7e708292002-06-25 16:13:24 +0000852// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000853ArgTypeListI : TypeListI
854 | TypeListI ',' DOTDOTDOT {
855 ($$=$1)->push_back(Type::VoidTy);
856 }
857 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +0000858 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000859 }
860 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +0000861 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000862 };
Chris Lattner30c89792001-09-07 16:35:17 +0000863
Chris Lattnere98dda62001-07-14 06:10:16 +0000864// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000865// production is used ONLY to represent constants that show up AFTER a 'const',
866// 'constant' or 'global' token at global scope. Constants that can be inlined
867// into other expressions (such as integers and constexprs) are handled by the
868// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000869//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000870ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +0000871 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000872 if (ATy == 0)
873 ThrowException("Cannot make array constant with type: '" +
874 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000875 const Type *ETy = ATy->getElementType();
876 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000877
Chris Lattner30c89792001-09-07 16:35:17 +0000878 // Verify that we have the correct size...
879 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000880 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000881 utostr($3->size()) + " arguments, but has size of " +
882 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000883
Chris Lattner30c89792001-09-07 16:35:17 +0000884 // Verify all elements are correct type!
885 for (unsigned i = 0; i < $3->size(); i++) {
886 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000887 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000888 ETy->getDescription() +"' as required!\nIt is of type '"+
889 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000890 }
891
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000892 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000893 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000894 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000895 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +0000896 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000897 if (ATy == 0)
898 ThrowException("Cannot make array constant with type: '" +
899 (*$1)->getDescription() + "'!");
900
901 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000902 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000903 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000904 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +0000905 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000906 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000907 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000908 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +0000909 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000910 if (ATy == 0)
911 ThrowException("Cannot make array constant with type: '" +
912 (*$1)->getDescription() + "'!");
913
Chris Lattner30c89792001-09-07 16:35:17 +0000914 int NumElements = ATy->getNumElements();
915 const Type *ETy = ATy->getElementType();
916 char *EndStr = UnEscapeLexed($3, true);
917 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +0000918 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +0000919 itostr((int)(EndStr-$3)) +
920 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +0000921 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +0000922 if (ETy == Type::SByteTy) {
923 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000924 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +0000925 } else if (ETy == Type::UByteTy) {
926 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +0000927 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +0000928 } else {
Chris Lattner30c89792001-09-07 16:35:17 +0000929 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +0000930 ThrowException("Cannot build string arrays of non byte sized elements!");
931 }
Chris Lattner30c89792001-09-07 16:35:17 +0000932 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000933 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +0000934 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +0000935 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000936 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +0000937 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000938 if (STy == 0)
939 ThrowException("Cannot make struct constant with type: '" +
940 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +0000941
Chris Lattnerc6212f12003-05-21 16:06:56 +0000942 if ($3->size() != STy->getNumContainedTypes())
943 ThrowException("Illegal number of initializers for structure type!");
944
Chris Lattneraf76d0e2003-04-15 16:09:31 +0000945 // Check to ensure that constants are compatible with the type initializer!
946 for (unsigned i = 0, e = $3->size(); i != e; ++i)
947 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
948 ThrowException("Expected type '" +
949 STy->getElementTypes()[i]->getDescription() +
950 "' for element #" + utostr(i) +
951 " of structure initializer!");
952
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000953 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000954 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000955 }
Chris Lattnerc6212f12003-05-21 16:06:56 +0000956 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +0000957 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +0000958 if (STy == 0)
959 ThrowException("Cannot make struct constant with type: '" +
960 (*$1)->getDescription() + "'!");
961
962 if (STy->getNumContainedTypes() != 0)
963 ThrowException("Illegal number of initializers for structure type!");
964
965 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
966 delete $1;
967 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000968 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +0000969 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000970 if (PTy == 0)
971 ThrowException("Cannot make null pointer constant with type: '" +
972 (*$1)->getDescription() + "'!");
973
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000974 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000975 delete $1;
976 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000977 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +0000978 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000979 if (Ty == 0)
980 ThrowException("Global const reference must be a pointer type!");
981
Chris Lattner3101c252002-08-15 17:58:33 +0000982 // ConstExprs can exist in the body of a function, thus creating
983 // ConstantPointerRefs whenever they refer to a variable. Because we are in
984 // the context of a function, getValNonImprovising will search the functions
985 // symbol table instead of the module symbol table for the global symbol,
986 // which throws things all off. To get around this, we just tell
987 // getValNonImprovising that we are at global scope here.
988 //
989 Function *SavedCurFn = CurMeth.CurrentFunction;
990 CurMeth.CurrentFunction = 0;
991
Chris Lattner2079fde2001-10-13 06:41:08 +0000992 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000993
Chris Lattner3101c252002-08-15 17:58:33 +0000994 CurMeth.CurrentFunction = SavedCurFn;
995
Chris Lattner2079fde2001-10-13 06:41:08 +0000996 // If this is an initializer for a constant pointer, which is referencing a
997 // (currently) undefined variable, create a stub now that shall be replaced
998 // in the future with the right type of variable.
999 //
1000 if (V == 0) {
1001 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1002 const PointerType *PT = cast<PointerType>(Ty);
1003
1004 // First check to see if the forward references value is already created!
1005 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001006 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001007
1008 if (I != CurModule.GlobalRefs.end()) {
1009 V = I->second; // Placeholder already exists, use it...
1010 } else {
1011 // TODO: Include line number info by creating a subclass of
1012 // TODO: GlobalVariable here that includes the said information!
1013
1014 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001015 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001016 false,
1017 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001018 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001019 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001020
1021 // Must temporarily push this value into the module table...
1022 CurModule.CurrentModule->getGlobalList().push_back(GV);
1023 V = GV;
1024 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001025 }
1026
Chris Lattner2079fde2001-10-13 06:41:08 +00001027 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001028 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001029 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001030 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001031 | Types ConstExpr {
1032 if ($1->get() != $2->getType())
1033 ThrowException("Mismatched types for constant expression!");
1034 $$ = $2;
1035 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001036 }
1037 | Types ZEROINITIALIZER {
1038 $$ = Constant::getNullValue($1->get());
1039 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001040 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001041
Chris Lattnere43f40b2002-10-09 00:25:32 +00001042ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001043 if (!ConstantSInt::isValueValidForType($1, $2))
1044 ThrowException("Constant value doesn't fit in type!");
1045 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001046 }
1047 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001048 if (!ConstantUInt::isValueValidForType($1, $2))
1049 ThrowException("Constant value doesn't fit in type!");
1050 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001051 }
1052 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001053 $$ = ConstantBool::True;
1054 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001055 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001056 $$ = ConstantBool::False;
1057 }
1058 | FPType FPVAL { // Float & Double constants
1059 $$ = ConstantFP::get($1, $2);
1060 };
1061
Chris Lattner00950542001-06-06 20:29:01 +00001062
Chris Lattnerd78700d2002-08-16 21:14:40 +00001063ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001064 if (!$5->get()->isFirstClassType())
1065 ThrowException("cast constant expression to a non-primitive type: '" +
1066 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001067 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001068 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001069 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001070 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1071 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001072 ThrowException("GetElementPtr requires a pointer operand!");
1073
1074 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001075 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001076 if (!IdxTy)
1077 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001078
Chris Lattner9232b992003-04-22 18:42:41 +00001079 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001080 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1081 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001082 IdxVec.push_back(C);
1083 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001084 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001085
Chris Lattnerd78700d2002-08-16 21:14:40 +00001086 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001087
Chris Lattnerd78700d2002-08-16 21:14:40 +00001088 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001089 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001090 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001091 if ($3->getType() != $5->getType())
1092 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001093 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001094 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001095 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001096 if ($5->getType() != Type::UByteTy)
1097 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner5e458e22003-05-21 17:48:56 +00001098 if (!$3->getType()->isIntegral())
1099 ThrowException("Shift constant expression requires integral operand!");
1100 $$ = ConstantExpr::getShift($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001101 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001102
1103
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001104// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001105ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001106 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001107 }
1108 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001109 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001110 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001111 };
Chris Lattner00950542001-06-06 20:29:01 +00001112
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001113
Chris Lattner1781aca2001-09-18 04:00:54 +00001114// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001115GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001116
Chris Lattner00950542001-06-06 20:29:01 +00001117
Chris Lattner0e73ce62002-05-02 19:11:13 +00001118//===----------------------------------------------------------------------===//
1119// Rules to match Modules
1120//===----------------------------------------------------------------------===//
1121
1122// Module rule: Capture the result of parsing the whole file into a result
1123// variable...
1124//
1125Module : FunctionList {
1126 $$ = ParserResult = $1;
1127 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001128};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001129
Chris Lattner7e708292002-06-25 16:13:24 +00001130// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001131//
1132FunctionList : FunctionList Function {
1133 $$ = $1;
1134 assert($2->getParent() == 0 && "Function already in module!");
1135 $1->getFunctionList().push_back($2);
1136 CurMeth.FunctionDone();
1137 }
1138 | FunctionList FunctionProto {
1139 $$ = $1;
1140 }
1141 | FunctionList IMPLEMENTATION {
1142 $$ = $1;
1143 }
1144 | ConstPool {
1145 $$ = CurModule.CurrentModule;
1146 // Resolve circular types before we parse the body of the module
1147 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001148 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001149
Chris Lattnere98dda62001-07-14 06:10:16 +00001150// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001151ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001152 if (!setValueName($4, $2))
1153 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001154 }
Chris Lattner30c89792001-09-07 16:35:17 +00001155 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001156 // Eagerly resolve types. This is not an optimization, this is a
1157 // requirement that is due to the fact that we could have this:
1158 //
1159 // %list = type { %list * }
1160 // %list = type { %list * } ; repeated type decl
1161 //
1162 // If types are not resolved eagerly, then the two types will not be
1163 // determined to be the same type!
1164 //
1165 ResolveTypeTo($2, $4->get());
1166
Chris Lattner1781aca2001-09-18 04:00:54 +00001167 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001168 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1169 // If this is not a redefinition of a type...
1170 if (!$2) {
1171 InsertType($4->get(),
Chris Lattner79df7c02002-03-26 18:01:55 +00001172 inFunctionScope() ? CurMeth.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001173 }
Chris Lattner30c89792001-09-07 16:35:17 +00001174 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001175
1176 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001177 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001178 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001179 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001180 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001181 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001182 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001183 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001184 if (Initializer == 0)
1185 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001186
Chris Lattnerdda71962001-11-26 18:54:16 +00001187 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001188 if (!setValueName(GV, $2)) { // If not redefining...
1189 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001190 int Slot = InsertValue(GV, CurModule.Values);
1191
1192 if (Slot != -1) {
1193 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1194 } else {
1195 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1196 (char*)GV->getName().c_str()));
1197 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001198 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001199 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001200 | ConstPool OptAssign EXTERNAL GlobalType Types {
1201 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001202 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001203 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001204 if (!setValueName(GV, $2)) { // If not redefining...
1205 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001206 int Slot = InsertValue(GV, CurModule.Values);
1207
1208 if (Slot != -1) {
1209 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1210 } else {
1211 assert(GV->hasName() && "Not named and not numbered!?");
1212 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1213 (char*)GV->getName().c_str()));
1214 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001215 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001216 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001217 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001218 | ConstPool TARGET TargetDefinition {
1219 }
Chris Lattner00950542001-06-06 20:29:01 +00001220 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001221 };
Chris Lattner00950542001-06-06 20:29:01 +00001222
1223
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001224
1225BigOrLittle : BIG { $$ = Module::BigEndian; };
1226BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1227
1228TargetDefinition : ENDIAN '=' BigOrLittle {
1229 CurModule.CurrentModule->setEndianness($3);
1230 }
1231 | POINTERSIZE '=' EUINT64VAL {
1232 if ($3 == 32)
1233 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1234 else if ($3 == 64)
1235 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1236 else
1237 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1238 };
1239
1240
Chris Lattner00950542001-06-06 20:29:01 +00001241//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001242// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001243//===----------------------------------------------------------------------===//
1244
Chris Lattner6c23f572003-08-22 05:42:10 +00001245Name : VAR_ID | STRINGCONSTANT;
1246OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001247
Chris Lattner6c23f572003-08-22 05:42:10 +00001248ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001249 if (*$1 == Type::VoidTy)
1250 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001251 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001252};
Chris Lattner00950542001-06-06 20:29:01 +00001253
Chris Lattner69da5cf2002-10-13 20:57:00 +00001254ArgListH : ArgListH ',' ArgVal {
1255 $$ = $1;
1256 $1->push_back(*$3);
1257 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001258 }
1259 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001260 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001261 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001262 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001263 };
Chris Lattner00950542001-06-06 20:29:01 +00001264
1265ArgList : ArgListH {
1266 $$ = $1;
1267 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001268 | ArgListH ',' DOTDOTDOT {
1269 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001270 $$->push_back(std::pair<PATypeHolder*,
1271 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001272 }
1273 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001274 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1275 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001276 }
Chris Lattner00950542001-06-06 20:29:01 +00001277 | /* empty */ {
1278 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001279 };
Chris Lattner00950542001-06-06 20:29:01 +00001280
Chris Lattner6c23f572003-08-22 05:42:10 +00001281FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001282 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001283 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001284
Chris Lattner9232b992003-04-22 18:42:41 +00001285 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001286 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001287 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001288 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001289 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001290 }
Chris Lattner00950542001-06-06 20:29:01 +00001291
Chris Lattner2079fde2001-10-13 06:41:08 +00001292 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1293 if (isVarArg) ParamTypeList.pop_back();
1294
Chris Lattner1f862af2003-04-16 18:13:57 +00001295 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001296 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001297 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001298
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001299 Function *Fn = 0;
1300 // Is the function already in symtab?
1301 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1302 // Yes it is. If this is the case, either we need to be a forward decl,
1303 // or it needs to be.
1304 if (!CurMeth.isDeclare && !Fn->isExternal())
1305 ThrowException("Redefinition of function '" + FunctionName + "'!");
1306
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001307 // If we found a preexisting function prototype, remove it from the
1308 // module, so that we don't get spurious conflicts with global & local
1309 // variables.
1310 //
1311 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001312
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001313 // Make sure to strip off any argument names so we can't get conflicts...
1314 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1315 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001316
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001317 } else { // Not already defined?
Chris Lattner4ad02e72003-04-16 20:28:45 +00001318 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001319 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001320 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001321 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001322 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001323
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001324 CurMeth.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001325
Chris Lattner7e708292002-06-25 16:13:24 +00001326 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001327 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001328 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001329 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001330 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001331 delete $4->back().first;
1332 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001333 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001334 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001335 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001336 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001337 delete I->first; // Delete the typeholder...
1338
1339 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001340 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001341
Chris Lattner69da5cf2002-10-13 20:57:00 +00001342 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001343 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001344
Chris Lattner1f862af2003-04-16 18:13:57 +00001345 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001346 }
Chris Lattner51727be2002-06-04 21:58:56 +00001347};
Chris Lattner00950542001-06-06 20:29:01 +00001348
Chris Lattner9b02cc32002-05-03 18:23:48 +00001349BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1350
Chris Lattner4ad02e72003-04-16 20:28:45 +00001351FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner79df7c02002-03-26 18:01:55 +00001352 $$ = CurMeth.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001353
Chris Lattner4ad02e72003-04-16 20:28:45 +00001354 // Make sure that we keep track of the linkage type even if there was a
1355 // previous "declare".
1356 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001357
Chris Lattner7e708292002-06-25 16:13:24 +00001358 // Resolve circular types before we parse the body of the function.
Chris Lattner30c89792001-09-07 16:35:17 +00001359 ResolveTypes(CurMeth.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001360};
Chris Lattner00950542001-06-06 20:29:01 +00001361
Chris Lattner9b02cc32002-05-03 18:23:48 +00001362END : ENDTOK | '}'; // Allow end of '}' to end a function
1363
Chris Lattner79df7c02002-03-26 18:01:55 +00001364Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001365 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001366};
Chris Lattner00950542001-06-06 20:29:01 +00001367
Chris Lattner79df7c02002-03-26 18:01:55 +00001368FunctionProto : DECLARE { CurMeth.isDeclare = true; } FunctionHeaderH {
1369 $$ = CurMeth.CurrentFunction;
1370 assert($$->getParent() == 0 && "Function already in module!");
1371 CurModule.CurrentModule->getFunctionList().push_back($$);
1372 CurMeth.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001373};
Chris Lattner00950542001-06-06 20:29:01 +00001374
1375//===----------------------------------------------------------------------===//
1376// Rules to match Basic Blocks
1377//===----------------------------------------------------------------------===//
1378
1379ConstValueRef : ESINT64VAL { // A reference to a direct constant
1380 $$ = ValID::create($1);
1381 }
1382 | EUINT64VAL {
1383 $$ = ValID::create($1);
1384 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001385 | FPVAL { // Perhaps it's an FP constant?
1386 $$ = ValID::create($1);
1387 }
Chris Lattner00950542001-06-06 20:29:01 +00001388 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001389 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001390 }
1391 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001392 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001393 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001394 | NULL_TOK {
1395 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001396 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001397 | ConstExpr {
1398 $$ = ValID::create($1);
1399 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001400
Chris Lattner2079fde2001-10-13 06:41:08 +00001401// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1402// another value.
1403//
1404SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001405 $$ = ValID::create($1);
1406 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001407 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001408 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001409 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001410
1411// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001412ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001413
Chris Lattner00950542001-06-06 20:29:01 +00001414
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001415// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1416// type immediately preceeds the value reference, and allows complex constant
1417// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001418ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001419 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001420 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001421
Chris Lattner00950542001-06-06 20:29:01 +00001422BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001423 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001424 }
Chris Lattner7e708292002-06-25 16:13:24 +00001425 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1426 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001427 };
Chris Lattner00950542001-06-06 20:29:01 +00001428
1429
1430// Basic blocks are terminated by branching instructions:
1431// br, br/cc, switch, ret
1432//
Chris Lattner2079fde2001-10-13 06:41:08 +00001433BasicBlock : InstructionList OptAssign BBTerminatorInst {
1434 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1435 InsertValue($3);
1436
1437 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001438 InsertValue($1);
1439 $$ = $1;
1440 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001441 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1442 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1443 InsertValue($4);
1444
1445 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001446 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001447
1448 InsertValue($2);
1449 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001450 };
Chris Lattner00950542001-06-06 20:29:01 +00001451
1452InstructionList : InstructionList Inst {
1453 $1->getInstList().push_back($2);
1454 $$ = $1;
1455 }
1456 | /* empty */ {
Chris Lattner8c8418d2003-09-01 16:31:28 +00001457 $$ = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001458 };
Chris Lattner00950542001-06-06 20:29:01 +00001459
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001460BBTerminatorInst : RET ResolvedVal { // Return with a result...
1461 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001462 }
1463 | RET VOID { // Return with no result...
1464 $$ = new ReturnInst();
1465 }
1466 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001467 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001468 } // Conditional Branch...
1469 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001470 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1471 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001472 getVal(Type::BoolTy, $3));
1473 }
1474 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1475 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001476 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001477 $$ = S;
1478
Chris Lattner9232b992003-04-22 18:42:41 +00001479 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001480 E = $8->end();
1481 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001482 S->addCase(I->first, I->second);
Chris Lattner00950542001-06-06 20:29:01 +00001483 }
Chris Lattner196850c2003-05-15 21:30:00 +00001484 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1485 SwitchInst *S = new SwitchInst(getVal($2, $3),
1486 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1487 $$ = S;
1488 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001489 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1490 EXCEPT ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001491 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001492 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001493
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001494 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1495 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001496 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001497 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001498 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001499 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1500 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001501 ParamTypes.push_back((*I)->getType());
1502 }
1503
1504 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1505 if (isVarArg) ParamTypes.pop_back();
1506
Chris Lattner79df7c02002-03-26 18:01:55 +00001507 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001508 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001509 }
1510 delete $2;
1511
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001512 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001513
1514 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1515 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1516
1517 if (Normal == 0 || Except == 0)
1518 ThrowException("Invoke instruction without label destinations!");
1519
1520 // Create the call node...
1521 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001522 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001523 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001524 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001525 // correctly!
1526 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001527 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1528 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001529 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001530
1531 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1532 if ((*ArgI)->getType() != *I)
1533 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001534 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001535
1536 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1537 ThrowException("Invalid number of parameters detected!");
1538
Chris Lattner6cdb0112001-11-26 16:54:11 +00001539 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001540 }
1541 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001542 }
1543 | UNWIND {
1544 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001545 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001546
1547
Chris Lattner00950542001-06-06 20:29:01 +00001548
1549JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1550 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001551 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001552 if (V == 0)
1553 ThrowException("May only switch on a constant pool value!");
1554
Chris Lattner9232b992003-04-22 18:42:41 +00001555 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001556 }
1557 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001558 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001559 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001560
1561 if (V == 0)
1562 ThrowException("May only switch on a constant pool value!");
1563
Chris Lattner9232b992003-04-22 18:42:41 +00001564 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001565 };
Chris Lattner00950542001-06-06 20:29:01 +00001566
1567Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001568 // Is this definition named?? if so, assign the name...
1569 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001570 InsertValue($2);
1571 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001572};
Chris Lattner00950542001-06-06 20:29:01 +00001573
Chris Lattnerc24d2082001-06-11 15:04:20 +00001574PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001575 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1576 $$->push_back(std::make_pair(getVal(*$1, $3),
1577 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001578 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001579 }
1580 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1581 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001582 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1583 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001584 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001585
1586
Chris Lattner30c89792001-09-07 16:35:17 +00001587ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001588 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001589 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001590 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001591 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001592 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001593 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001594 };
Chris Lattner00950542001-06-06 20:29:01 +00001595
1596// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001597ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001598
Chris Lattner4a6482b2002-09-10 19:57:26 +00001599InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1600 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1601 ThrowException("Arithmetic operator requires integer or FP operands!");
1602 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1603 if ($$ == 0)
1604 ThrowException("binary operator returned null!");
1605 delete $2;
1606 }
1607 | LogicalOps Types ValueRef ',' ValueRef {
1608 if (!(*$2)->isIntegral())
1609 ThrowException("Logical operator requires integral operands!");
1610 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1611 if ($$ == 0)
1612 ThrowException("binary operator returned null!");
1613 delete $2;
1614 }
1615 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001616 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001617 if ($$ == 0)
1618 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001619 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001620 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001621 | NOT ResolvedVal {
1622 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1623 << " Replacing with 'xor'.\n";
1624
1625 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1626 if (Ones == 0)
1627 ThrowException("Expected integral type for not instruction!");
1628
1629 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001630 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001631 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001632 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001633 | ShiftOps ResolvedVal ',' ResolvedVal {
1634 if ($4->getType() != Type::UByteTy)
1635 ThrowException("Shift amount must be ubyte!");
1636 $$ = 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}