blob: a55e735ca493f57b1d52a3ef83566c4c7affff6f [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000020#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000022#include "Support/STLExtras.h"
23#include "Support/DepthFirstIterator.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000025#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000026#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner386a3b72001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000030int yyparse();
31
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
33
Chris Lattner00950542001-06-06 20:29:01 +000034static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000035std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattner30c89792001-09-07 16:35:17 +000037// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
38// relating to upreferences in the input stream.
39//
40//#define DEBUG_UPREFS 1
41#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000042#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000043#else
44#define UR_OUT(X)
45#endif
46
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000047#define YYERROR_VERBOSE 1
48
Chris Lattner99e7ab72003-10-18 05:53:13 +000049// HACK ALERT: This variable is used to implement the automatic conversion of
50// variable argument instructions from their old to new forms. When this
51// compatiblity "Feature" is removed, this should be too.
52//
53static BasicBlock *CurBB;
54static bool ObsoleteVarArgs;
55
56
Chris Lattner7e708292002-06-25 16:13:24 +000057// This contains info used when building the body of a function. It is
58// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000059//
Chris Lattner9232b992003-04-22 18:42:41 +000060typedef std::vector<Value *> ValueList; // Numbered defs
61static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
62 std::vector<ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000063
64static struct PerModuleInfo {
65 Module *CurrentModule;
Chris Lattner9232b992003-04-22 18:42:41 +000066 std::vector<ValueList> Values; // Module level numbered definitions
67 std::vector<ValueList> LateResolveValues;
68 std::vector<PATypeHolder> Types;
69 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000070
Chris Lattner2079fde2001-10-13 06:41:08 +000071 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
72 // references to global values. Global values may be referenced before they
73 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000074 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000075 //
Chris Lattner9232b992003-04-22 18:42:41 +000076 typedef std::map<std::pair<const PointerType *,
77 ValID>, GlobalVariable*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000078 GlobalRefsType GlobalRefs;
79
Chris Lattner00950542001-06-06 20:29:01 +000080 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000081 // If we could not resolve some functions at function compilation time
82 // (calls to functions before they are defined), resolve them now... Types
83 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000084 //
Chris Lattner00950542001-06-06 20:29:01 +000085 ResolveDefinitions(LateResolveValues);
86
Chris Lattner2079fde2001-10-13 06:41:08 +000087 // Check to make sure that all global value forward references have been
88 // resolved!
89 //
90 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000091 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000092
93 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
94 I != E; ++I) {
95 UndefinedReferences += " " + I->first.first->getDescription() + " " +
96 I->first.second.getName() + "\n";
97 }
98 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000099 }
100
Chris Lattner7e708292002-06-25 16:13:24 +0000101 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000102 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000103 CurrentModule = 0;
104 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000105
106
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000107 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000108 // is used to remove things from the forward declaration map, resolving them
109 // to the correct thing as needed.
110 //
111 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
112 // Check to see if there is a forward reference to this global variable...
113 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000114 GlobalRefsType::iterator I =
115 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000116
117 if (I != GlobalRefs.end()) {
118 GlobalVariable *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000119 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner2079fde2001-10-13 06:41:08 +0000120
121 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000122 // allowed to be is ConstantPointerRef's.
Chris Lattnerfd059242003-10-15 16:48:29 +0000123 assert(OldGV->hasOneUse() && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000124 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
125 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000126
Chris Lattner9e932bd2002-10-14 03:28:42 +0000127 // Change the const pool reference to point to the real global variable
128 // now. This should drop a use from the OldGV.
129 CPR->mutateReferences(OldGV, GV);
130 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000131
132 // Remove OldGV from the module...
Chris Lattner2079fde2001-10-13 06:41:08 +0000133 CurrentModule->getGlobalList().remove(OldGV);
134 delete OldGV; // Delete the old placeholder
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000135
Chris Lattner2079fde2001-10-13 06:41:08 +0000136 // Remove the map entry for the global now that it has been created...
137 GlobalRefs.erase(I);
138 }
139 }
140
Chris Lattner00950542001-06-06 20:29:01 +0000141} CurModule;
142
Chris Lattner79df7c02002-03-26 18:01:55 +0000143static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000144 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000145
Chris Lattner9232b992003-04-22 18:42:41 +0000146 std::vector<ValueList> Values; // Keep track of numbered definitions
147 std::vector<ValueList> LateResolveValues;
148 std::vector<PATypeHolder> Types;
149 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000150 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000151
Chris Lattner79df7c02002-03-26 18:01:55 +0000152 inline PerFunctionInfo() {
153 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000154 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000155 }
156
Chris Lattner79df7c02002-03-26 18:01:55 +0000157 inline void FunctionStart(Function *M) {
158 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000159 }
160
Chris Lattner79df7c02002-03-26 18:01:55 +0000161 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000162 // If we could not resolve some blocks at parsing time (forward branches)
163 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000164 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000165
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000166 // Make sure to resolve any constant expr references that might exist within
167 // the function we just declared itself.
168 ValID FID;
169 if (CurrentFunction->hasName()) {
170 FID = ValID::create((char*)CurrentFunction->getName().c_str());
171 } else {
172 unsigned Slot = CurrentFunction->getType()->getUniqueID();
173 assert(CurModule.Values.size() > Slot && "Function not inserted?");
174 // Figure out which slot number if is...
175 for (unsigned i = 0; ; ++i) {
176 assert(i < CurModule.Values[Slot].size() && "Function not found!");
177 if (CurModule.Values[Slot][i] == CurrentFunction) {
178 FID = ValID::create((int)i);
179 break;
180 }
181 }
182 }
183 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
184
Chris Lattner7e708292002-06-25 16:13:24 +0000185 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000186 Types.clear();
Chris Lattner79df7c02002-03-26 18:01:55 +0000187 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000188 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000189 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000190} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000191
Chris Lattner394f2eb2003-10-16 20:12:13 +0000192static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000193
Chris Lattner00950542001-06-06 20:29:01 +0000194
195//===----------------------------------------------------------------------===//
196// Code to handle definitions of all the types
197//===----------------------------------------------------------------------===//
198
Chris Lattner9232b992003-04-22 18:42:41 +0000199static int InsertValue(Value *D,
Chris Lattner394f2eb2003-10-16 20:12:13 +0000200 std::vector<ValueList> &ValueTab = CurFun.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000201 if (D->hasName()) return -1; // Is this a numbered definition?
202
203 // Yes, insert the value into the value table...
204 unsigned type = D->getType()->getUniqueID();
205 if (ValueTab.size() <= type)
206 ValueTab.resize(type+1, ValueList());
207 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
208 ValueTab[type].push_back(D);
209 return ValueTab[type].size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000210}
211
Chris Lattner30c89792001-09-07 16:35:17 +0000212// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000213static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000214 Types.push_back(Ty);
215}
216
217static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000218 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000219 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000220 unsigned Num = (unsigned)D.Num;
221
222 // Module constants occupy the lowest numbered slots...
223 if (Num < CurModule.Types.size())
224 return CurModule.Types[Num];
225
226 Num -= CurModule.Types.size();
227
228 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000229 if (Num <= CurFun.Types.size())
230 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000231 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000232 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000233 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000234 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000235 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000236 Value *N = 0;
237 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000238 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000239 N = SymTab->lookup(Type::TypeTy, Name);
240 }
Chris Lattner30c89792001-09-07 16:35:17 +0000241
242 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000243 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000244 // hasn't been added to the module...
245 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000246 SymTab = &CurModule.CurrentModule->getSymbolTable();
247 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000248 if (N == 0) break;
249 }
250
251 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000252 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000253 }
254 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000255 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000256 }
257
258 // If we reached here, we referenced either a symbol that we don't know about
259 // or an id number that hasn't been read yet. We may be referencing something
260 // forward, so just create an entry to be resolved later and get to it...
261 //
262 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
263
Chris Lattner9232b992003-04-22 18:42:41 +0000264 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000265 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000266
Chris Lattner9232b992003-04-22 18:42:41 +0000267 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000268 if (I != LateResolver.end()) {
269 return I->second;
270 }
Chris Lattner30c89792001-09-07 16:35:17 +0000271
Chris Lattner82269592001-10-22 06:01:08 +0000272 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000273 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000274 return Typ;
275}
276
Chris Lattner9232b992003-04-22 18:42:41 +0000277static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000278 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000279 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000280 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000281 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000282}
283
Chris Lattner2079fde2001-10-13 06:41:08 +0000284// getValNonImprovising - Look up the value specified by the provided type and
285// the provided ValID. If the value exists and has already been defined, return
286// it. Otherwise return null.
287//
288static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000289 if (isa<FunctionType>(Ty))
290 ThrowException("Functions are not values and "
291 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000292
Chris Lattner30c89792001-09-07 16:35:17 +0000293 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000294 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000295 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000296 unsigned Num = (unsigned)D.Num;
297
298 // Module constants occupy the lowest numbered slots...
299 if (type < CurModule.Values.size()) {
300 if (Num < CurModule.Values[type].size())
301 return CurModule.Values[type][Num];
302
303 Num -= CurModule.Values[type].size();
304 }
305
306 // Make sure that our type is within bounds
Chris Lattner394f2eb2003-10-16 20:12:13 +0000307 if (CurFun.Values.size() <= type) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000308
309 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000310 if (CurFun.Values[type].size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000311
Chris Lattner394f2eb2003-10-16 20:12:13 +0000312 return CurFun.Values[type][Num];
Chris Lattner00950542001-06-06 20:29:01 +0000313 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000314
Chris Lattner1a1cb112001-09-30 22:46:54 +0000315 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000316 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000317 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000318
319 D.destroy(); // Free old strdup'd memory...
320 return N;
321 }
322
Chris Lattner2079fde2001-10-13 06:41:08 +0000323 // Check to make sure that "Ty" is an integral type, and that our
324 // value will fit into the specified type...
325 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000326 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
327 ThrowException("Signed integral constant '" +
328 itostr(D.ConstPool64) + "' is invalid for type '" +
329 Ty->getDescription() + "'!");
330 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000331
332 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000333 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
334 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000335 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
336 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000337 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000338 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000339 }
340 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000341 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000342 }
343
Chris Lattner2079fde2001-10-13 06:41:08 +0000344 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000345 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000346 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000347 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000348
349 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000350 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000351 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000352 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000353
Chris Lattnerd78700d2002-08-16 21:14:40 +0000354 case ValID::ConstantVal: // Fully resolved constant?
355 if (D.ConstantValue->getType() != Ty)
356 ThrowException("Constant expression type different from required type!");
357 return D.ConstantValue;
358
Chris Lattner30c89792001-09-07 16:35:17 +0000359 default:
360 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000361 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000362 } // End of switch
363
Chris Lattner2079fde2001-10-13 06:41:08 +0000364 assert(0 && "Unhandled case!");
365 return 0;
366}
367
368
369// getVal - This function is identical to getValNonImprovising, except that if a
370// value is not already defined, it "improvises" by creating a placeholder var
371// that looks and acts just like the requested variable. When the value is
372// defined later, all uses of the placeholder variable are replaced with the
373// real thing.
374//
375static Value *getVal(const Type *Ty, const ValID &D) {
376 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
377
378 // See if the value has already been defined...
379 Value *V = getValNonImprovising(Ty, D);
380 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000381
382 // If we reached here, we referenced either a symbol that we don't know about
383 // or an id number that hasn't been read yet. We may be referencing something
384 // forward, so just create an entry to be resolved later and get to it...
385 //
Chris Lattner00950542001-06-06 20:29:01 +0000386 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000387 switch (Ty->getPrimitiveID()) {
388 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000389 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000390 }
391
392 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000393 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000394 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000395 else
396 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000397 return d;
398}
399
400
401//===----------------------------------------------------------------------===//
402// Code to handle forward references in instructions
403//===----------------------------------------------------------------------===//
404//
405// This code handles the late binding needed with statements that reference
406// values not defined yet... for example, a forward branch, or the PHI node for
407// a loop body.
408//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000409// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000410// and back patchs after we are done.
411//
412
413// ResolveDefinitions - If we could not resolve some defs at parsing
414// time (forward branches, phi functions for loops, etc...) resolve the
415// defs now...
416//
Chris Lattner9232b992003-04-22 18:42:41 +0000417static void ResolveDefinitions(std::vector<ValueList> &LateResolvers,
418 std::vector<ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000419 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
420 for (unsigned ty = 0; ty < LateResolvers.size(); ty++) {
421 while (!LateResolvers[ty].empty()) {
422 Value *V = LateResolvers[ty].back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000423 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
424
Chris Lattner00950542001-06-06 20:29:01 +0000425 LateResolvers[ty].pop_back();
426 ValID &DID = getValIDFromPlaceHolder(V);
427
Chris Lattner2079fde2001-10-13 06:41:08 +0000428 Value *TheRealValue = getValNonImprovising(Type::getUniqueIDType(ty),DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000429 if (TheRealValue) {
430 V->replaceAllUsesWith(TheRealValue);
431 delete V;
432 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000433 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000434 // resolver table
435 InsertValue(V, *FutureLateResolvers);
436 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000437 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000438 ThrowException("Reference to an invalid definition: '" +DID.getName()+
439 "' of type '" + V->getType()->getDescription() + "'",
440 getLineNumFromPlaceHolder(V));
441 else
442 ThrowException("Reference to an invalid definition: #" +
443 itostr(DID.Num) + " of type '" +
444 V->getType()->getDescription() + "'",
445 getLineNumFromPlaceHolder(V));
446 }
Chris Lattner00950542001-06-06 20:29:01 +0000447 }
448 }
449
450 LateResolvers.clear();
451}
452
Chris Lattner4a42e902001-10-22 05:56:09 +0000453// ResolveTypeTo - A brand new type was just declared. This means that (if
454// name is not null) things referencing Name can be resolved. Otherwise, things
455// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000456//
Chris Lattner4a42e902001-10-22 05:56:09 +0000457static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000458 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000459 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000460
Chris Lattner4a42e902001-10-22 05:56:09 +0000461 ValID D;
462 if (Name) D = ValID::create(Name);
463 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000464
Chris Lattner9232b992003-04-22 18:42:41 +0000465 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000466 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000467
Chris Lattner9232b992003-04-22 18:42:41 +0000468 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000469 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000470 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000471 LateResolver.erase(I);
472 }
473}
474
475// ResolveTypes - At this point, all types should be resolved. Any that aren't
476// are errors.
477//
Chris Lattner9232b992003-04-22 18:42:41 +0000478static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000479 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000480 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000481
482 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000483 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000484 else
Chris Lattner82269592001-10-22 06:01:08 +0000485 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000486 }
487}
488
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000489
Chris Lattner1781aca2001-09-18 04:00:54 +0000490// setValueName - Set the specified value to the name given. The name may be
491// null potentially, in which case this is a noop. The string passed in is
492// assumed to be a malloc'd string buffer, and is freed by this function.
493//
Chris Lattnerb7474512001-10-03 15:39:04 +0000494// This function returns true if the value has already been defined, but is
495// allowed to be redefined in the specified context. If the name is a new name
496// for the typeplane, false is returned.
497//
498static bool setValueName(Value *V, char *NameStr) {
499 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000500
Chris Lattner9232b992003-04-22 18:42:41 +0000501 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000502 free(NameStr); // Free old string
503
Chris Lattner2079fde2001-10-13 06:41:08 +0000504 if (V->getType() == Type::VoidTy)
505 ThrowException("Can't assign name '" + Name +
506 "' to a null valued instruction!");
507
Chris Lattner6e6026b2002-11-20 18:36:02 +0000508 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000509 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000510 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000511
Chris Lattner6e6026b2002-11-20 18:36:02 +0000512 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000513 if (Existing) { // Inserting a name that is already defined???
514 // There is only one case where this is allowed: when we are refining an
515 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner949a3622003-07-23 15:30:06 +0000516 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000517 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000518 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000519 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000520 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000521 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000522 }
Chris Lattner30c89792001-09-07 16:35:17 +0000523
Chris Lattner9636a912001-10-01 16:18:37 +0000524 // Otherwise, we are a simple redefinition of a value, check to see if it
525 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000526 if (const Type *Ty = dyn_cast<Type>(Existing)) {
527 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000528 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattner949a3622003-07-23 15:30:06 +0000529 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000530 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
531 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000532 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000533 // We are allowed to redefine a global variable in two circumstances:
534 // 1. If at least one of the globals is uninitialized or
535 // 2. If both initializers have the same value.
536 //
Chris Lattner89219832001-10-03 19:35:04 +0000537 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000538 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
539 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000540
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000541 // Make sure the existing global version gets the initializer! Make
542 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000543 if (GV->hasInitializer() && !EGV->hasInitializer())
544 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000545 if (GV->isConstant())
546 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000547 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000548
Chris Lattner2079fde2001-10-13 06:41:08 +0000549 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000550 return true; // They are equivalent!
551 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000552 }
Chris Lattner9636a912001-10-01 16:18:37 +0000553 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000554 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000555 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000556 }
Chris Lattner00950542001-06-06 20:29:01 +0000557
Chris Lattner6e6026b2002-11-20 18:36:02 +0000558 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000559 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000560}
561
Chris Lattner8896eda2001-07-09 19:38:36 +0000562
Chris Lattner30c89792001-09-07 16:35:17 +0000563//===----------------------------------------------------------------------===//
564// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000565//
Chris Lattner8896eda2001-07-09 19:38:36 +0000566
Chris Lattner30c89792001-09-07 16:35:17 +0000567// TypeContains - Returns true if Ty contains E in it.
568//
569static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3ff43872001-09-28 22:56:31 +0000570 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
Chris Lattner30c89792001-09-07 16:35:17 +0000571}
Chris Lattner698b56e2001-07-20 19:15:08 +0000572
Chris Lattner30c89792001-09-07 16:35:17 +0000573
Chris Lattner9232b992003-04-22 18:42:41 +0000574static std::vector<std::pair<unsigned, OpaqueType *> > UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000575
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000576static PATypeHolder HandleUpRefs(const Type *ty) {
577 PATypeHolder Ty(ty);
Chris Lattner5084d032001-11-02 07:46:26 +0000578 UR_OUT("Type '" << ty->getDescription() <<
579 "' newly formed. Resolving upreferences.\n" <<
580 UpRefs.size() << " upreferences active!\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000581 for (unsigned i = 0; i < UpRefs.size(); ) {
Chris Lattner5084d032001-11-02 07:46:26 +0000582 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000583 << UpRefs[i].second->getDescription() << ") = "
Chris Lattner5084d032001-11-02 07:46:26 +0000584 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000585 if (TypeContains(Ty, UpRefs[i].second)) {
586 unsigned Level = --UpRefs[i].first; // Decrement level of upreference
Chris Lattner5084d032001-11-02 07:46:26 +0000587 UR_OUT(" Uplevel Ref Level = " << Level << endl);
Chris Lattner30c89792001-09-07 16:35:17 +0000588 if (Level == 0) { // Upreference should be resolved!
Chris Lattner5084d032001-11-02 07:46:26 +0000589 UR_OUT(" * Resolving upreference for "
590 << UpRefs[i].second->getDescription() << endl;
Chris Lattner9232b992003-04-22 18:42:41 +0000591 std::string OldName = UpRefs[i].second->getDescription());
Chris Lattner30c89792001-09-07 16:35:17 +0000592 UpRefs[i].second->refineAbstractTypeTo(Ty);
593 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
Chris Lattner5084d032001-11-02 07:46:26 +0000594 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
Chris Lattner30c89792001-09-07 16:35:17 +0000595 << (const void*)Ty << ", " << Ty->getDescription() << endl);
596 continue;
597 }
598 }
599
600 ++i; // Otherwise, no resolve, move on...
Chris Lattner8896eda2001-07-09 19:38:36 +0000601 }
Chris Lattner30c89792001-09-07 16:35:17 +0000602 // FIXME: TODO: this should return the updated type
Chris Lattner8896eda2001-07-09 19:38:36 +0000603 return Ty;
604}
605
Chris Lattner30c89792001-09-07 16:35:17 +0000606
Chris Lattner00950542001-06-06 20:29:01 +0000607//===----------------------------------------------------------------------===//
608// RunVMAsmParser - Define an interface to this parser
609//===----------------------------------------------------------------------===//
610//
Chris Lattner9232b992003-04-22 18:42:41 +0000611Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000612 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000613 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000614 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000615 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000616
Chris Lattner75f20532003-04-22 18:02:52 +0000617 // Allocate a new module to read
618 CurModule.CurrentModule = new Module(Filename);
Chris Lattner00950542001-06-06 20:29:01 +0000619 yyparse(); // Parse the file.
Chris Lattner99e7ab72003-10-18 05:53:13 +0000620
Chris Lattner00950542001-06-06 20:29:01 +0000621 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000622
623 // Check to see if they called va_start but not va_arg..
624 if (!ObsoleteVarArgs)
625 if (Function *F = Result->getNamedFunction("llvm.va_start"))
626 if (F->asize() == 1) {
627 std::cerr << "WARNING: this file uses obsolete features. "
628 << "Assemble and disassemble to update it.\n";
629 ObsoleteVarArgs = true;
630 }
631
632
633 if (ObsoleteVarArgs) {
634 // If the user is making use of obsolete varargs intrinsics, adjust them for
635 // the user.
636 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
637 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
638
639 const Type *RetTy = F->getFunctionType()->getParamType(0);
640 RetTy = cast<PointerType>(RetTy)->getElementType();
641 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
642
643 while (!F->use_empty()) {
644 CallInst *CI = cast<CallInst>(F->use_back());
645 Value *V = new CallInst(NF, "", CI);
646 new StoreInst(V, CI->getOperand(1), CI);
647 CI->getParent()->getInstList().erase(CI);
648 }
649 Result->getFunctionList().erase(F);
650 }
651
652 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
653 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
654 const Type *ArgTy = F->getFunctionType()->getParamType(0);
655 ArgTy = cast<PointerType>(ArgTy)->getElementType();
656 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
657 ArgTy, 0);
658
659 while (!F->use_empty()) {
660 CallInst *CI = cast<CallInst>(F->use_back());
661 Value *V = new LoadInst(CI->getOperand(1), "", CI);
662 new CallInst(NF, V, "", CI);
663 CI->getParent()->getInstList().erase(CI);
664 }
665 Result->getFunctionList().erase(F);
666 }
667
668 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
669 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
670 const Type *ArgTy = F->getFunctionType()->getParamType(0);
671 ArgTy = cast<PointerType>(ArgTy)->getElementType();
672 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
673 ArgTy, 0);
674
675 while (!F->use_empty()) {
676 CallInst *CI = cast<CallInst>(F->use_back());
677 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
678 new StoreInst(V, CI->getOperand(1), CI);
679 CI->getParent()->getInstList().erase(CI);
680 }
681 Result->getFunctionList().erase(F);
682 }
683 }
684
Chris Lattner00950542001-06-06 20:29:01 +0000685 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
686 ParserResult = 0;
687
688 return Result;
689}
690
Brian Gaeked0fde302003-11-11 22:41:34 +0000691} // End llvm namespace
692
693using namespace llvm;
694
Chris Lattner00950542001-06-06 20:29:01 +0000695%}
696
697%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000698 llvm::Module *ModuleVal;
699 llvm::Function *FunctionVal;
700 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
701 llvm::BasicBlock *BasicBlockVal;
702 llvm::TerminatorInst *TermInstVal;
703 llvm::Instruction *InstVal;
704 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000705
Brian Gaeked0fde302003-11-11 22:41:34 +0000706 const llvm::Type *PrimType;
707 llvm::PATypeHolder *TypeVal;
708 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000709
Brian Gaeked0fde302003-11-11 22:41:34 +0000710 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
711 std::vector<llvm::Value*> *ValueList;
712 std::list<llvm::PATypeHolder> *TypeList;
713 std::list<std::pair<llvm::Value*,
714 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
715 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
716 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000717
Brian Gaeked0fde302003-11-11 22:41:34 +0000718 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000719 int64_t SInt64Val;
720 uint64_t UInt64Val;
721 int SIntVal;
722 unsigned UIntVal;
723 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000724 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000725
Chris Lattner30c89792001-09-07 16:35:17 +0000726 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000727 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000728
Brian Gaeked0fde302003-11-11 22:41:34 +0000729 llvm::Instruction::BinaryOps BinaryOpVal;
730 llvm::Instruction::TermOps TermOpVal;
731 llvm::Instruction::MemoryOps MemOpVal;
732 llvm::Instruction::OtherOps OtherOpVal;
733 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000734}
735
Chris Lattner79df7c02002-03-26 18:01:55 +0000736%type <ModuleVal> Module FunctionList
737%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000738%type <BasicBlockVal> BasicBlock InstructionList
739%type <TermInstVal> BBTerminatorInst
740%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000741%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000742%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000743%type <ArgList> ArgList ArgListH
744%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000745%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000746%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000747%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000748%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000749%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000750%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000751%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000752%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000753%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000754
Chris Lattner2079fde2001-10-13 06:41:08 +0000755// ValueRef - Unresolved reference to a definition or BB
756%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000757%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000758// Tokens and types for handling constant integer values
759//
760// ESINT64VAL - A negative number within long long range
761%token <SInt64Val> ESINT64VAL
762
763// EUINT64VAL - A positive number within uns. long long range
764%token <UInt64Val> EUINT64VAL
765%type <SInt64Val> EINT64VAL
766
767%token <SIntVal> SINTVAL // Signed 32 bit ints...
768%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
769%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000770%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000771
772// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000773%type <TypeVal> Types TypesV UpRTypes UpRTypesV
774%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000775%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
776%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000777
Chris Lattner6c23f572003-08-22 05:42:10 +0000778%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
779%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000780
781
Chris Lattnerf4600482003-06-28 20:01:34 +0000782%token IMPLEMENTATION ZEROINITIALIZER TRUE FALSE BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000783%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnerf797cab2003-10-10 04:54:02 +0000784%token TO EXCEPT DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000785%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000786
787// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000788%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000789
Chris Lattner00950542001-06-06 20:29:01 +0000790// Binary Operators
791%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000792%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000793%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000794%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000795
796// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000797%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000798
Chris Lattner027dcc52001-07-08 21:10:27 +0000799// Other Operators
800%type <OtherOpVal> ShiftOps
Chris Lattner3b237fc2003-10-19 21:34:28 +0000801%token <OtherOpVal> PHI_TOK CALL CAST SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000802%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000803
Chris Lattner00950542001-06-06 20:29:01 +0000804%start Module
805%%
806
807// Handle constant integer size restriction and conversion...
808//
Chris Lattner51727be2002-06-04 21:58:56 +0000809INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000810INTVAL : UINTVAL {
811 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
812 ThrowException("Value too large for type!");
813 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000814};
Chris Lattner00950542001-06-06 20:29:01 +0000815
816
Chris Lattner51727be2002-06-04 21:58:56 +0000817EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000818EINT64VAL : EUINT64VAL {
819 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
820 ThrowException("Value too large for type!");
821 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000822};
Chris Lattner00950542001-06-06 20:29:01 +0000823
Chris Lattner00950542001-06-06 20:29:01 +0000824// Operations that are notably excluded from this list include:
825// RET, BR, & SWITCH because they end basic blocks and are treated specially.
826//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000827ArithmeticOps: ADD | SUB | MUL | DIV | REM;
828LogicalOps : AND | OR | XOR;
829SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
830BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
831
Chris Lattner51727be2002-06-04 21:58:56 +0000832ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000833
Chris Lattnere98dda62001-07-14 06:10:16 +0000834// These are some types that allow classification if we only want a particular
835// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000836SIntType : LONG | INT | SHORT | SBYTE;
837UIntType : ULONG | UINT | USHORT | UBYTE;
838IntType : SIntType | UIntType;
839FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000840
Chris Lattnere98dda62001-07-14 06:10:16 +0000841// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000842OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000843 $$ = $1;
844 }
845 | /*empty*/ {
846 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000847 };
Chris Lattner00950542001-06-06 20:29:01 +0000848
Chris Lattner4ad02e72003-04-16 20:28:45 +0000849OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
850 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000851 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000852 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
853 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000854
855//===----------------------------------------------------------------------===//
856// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000857// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000858// access to it, a user must explicitly use TypesV.
859//
860
861// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000862TypesV : Types | VOID { $$ = new PATypeHolder($1); };
863UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000864
865Types : UpRTypes {
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000866 if (UpRefs.size())
867 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
868 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000869 };
Chris Lattner30c89792001-09-07 16:35:17 +0000870
871
872// Derived types are added later...
873//
Chris Lattner51727be2002-06-04 21:58:56 +0000874PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
875PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000876UpRTypes : OPAQUE {
877 $$ = new PATypeHolder(OpaqueType::get());
878 }
879 | PrimType {
880 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000881 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000882UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000883 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000884};
Chris Lattner30c89792001-09-07 16:35:17 +0000885
Chris Lattner30c89792001-09-07 16:35:17 +0000886// Include derived types in the Types production.
887//
888UpRTypes : '\\' EUINT64VAL { // Type UpReference
889 if ($2 > (uint64_t)INT64_MAX) ThrowException("Value out of range!");
890 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner9232b992003-04-22 18:42:41 +0000891 UpRefs.push_back(std::make_pair((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000892 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000893 UR_OUT("New Upreference!\n");
894 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000895 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000896 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000897 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000898 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000899 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
900 if (isVarArg) Params.pop_back();
901
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000902 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000903 delete $3; // Delete the argument list
904 delete $1; // Delete the old type handle
905 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000906 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000907 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000908 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000909 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000910 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000911 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000912 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +0000913 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000914
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000915 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000916 delete $2;
917 }
918 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000919 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000920 }
921 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000922 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000923 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000924 };
Chris Lattner30c89792001-09-07 16:35:17 +0000925
Chris Lattner7e708292002-06-25 16:13:24 +0000926// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +0000927// declaration type lists
928//
929TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +0000930 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +0000931 $$->push_back(*$1); delete $1;
932 }
933 | TypeListI ',' UpRTypes {
934 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +0000935 };
Chris Lattner30c89792001-09-07 16:35:17 +0000936
Chris Lattner7e708292002-06-25 16:13:24 +0000937// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +0000938ArgTypeListI : TypeListI
939 | TypeListI ',' DOTDOTDOT {
940 ($$=$1)->push_back(Type::VoidTy);
941 }
942 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +0000943 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +0000944 }
945 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +0000946 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +0000947 };
Chris Lattner30c89792001-09-07 16:35:17 +0000948
Chris Lattnere98dda62001-07-14 06:10:16 +0000949// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +0000950// production is used ONLY to represent constants that show up AFTER a 'const',
951// 'constant' or 'global' token at global scope. Constants that can be inlined
952// into other expressions (such as integers and constexprs) are handled by the
953// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +0000954//
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000955ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +0000956 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000957 if (ATy == 0)
958 ThrowException("Cannot make array constant with type: '" +
959 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +0000960 const Type *ETy = ATy->getElementType();
961 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +0000962
Chris Lattner30c89792001-09-07 16:35:17 +0000963 // Verify that we have the correct size...
964 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +0000965 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +0000966 utostr($3->size()) + " arguments, but has size of " +
967 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +0000968
Chris Lattner30c89792001-09-07 16:35:17 +0000969 // Verify all elements are correct type!
970 for (unsigned i = 0; i < $3->size(); i++) {
971 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +0000972 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +0000973 ETy->getDescription() +"' as required!\nIt is of type '"+
974 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +0000975 }
976
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000977 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +0000978 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +0000979 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000980 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +0000981 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000982 if (ATy == 0)
983 ThrowException("Cannot make array constant with type: '" +
984 (*$1)->getDescription() + "'!");
985
986 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +0000987 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +0000988 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +0000989 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +0000990 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +0000991 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +0000992 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000993 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +0000994 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000995 if (ATy == 0)
996 ThrowException("Cannot make array constant with type: '" +
997 (*$1)->getDescription() + "'!");
998
Chris Lattner30c89792001-09-07 16:35:17 +0000999 int NumElements = ATy->getNumElements();
1000 const Type *ETy = ATy->getElementType();
1001 char *EndStr = UnEscapeLexed($3, true);
1002 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001003 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001004 itostr((int)(EndStr-$3)) +
1005 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001006 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001007 if (ETy == Type::SByteTy) {
1008 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001009 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001010 } else if (ETy == Type::UByteTy) {
1011 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001012 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001013 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001014 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001015 ThrowException("Cannot build string arrays of non byte sized elements!");
1016 }
Chris Lattner30c89792001-09-07 16:35:17 +00001017 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001018 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001019 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001020 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001021 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001022 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001023 if (STy == 0)
1024 ThrowException("Cannot make struct constant with type: '" +
1025 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001026
Chris Lattnerc6212f12003-05-21 16:06:56 +00001027 if ($3->size() != STy->getNumContainedTypes())
1028 ThrowException("Illegal number of initializers for structure type!");
1029
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001030 // Check to ensure that constants are compatible with the type initializer!
1031 for (unsigned i = 0, e = $3->size(); i != e; ++i)
1032 if ((*$3)[i]->getType() != STy->getElementTypes()[i])
1033 ThrowException("Expected type '" +
1034 STy->getElementTypes()[i]->getDescription() +
1035 "' for element #" + utostr(i) +
1036 " of structure initializer!");
1037
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001038 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001039 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001040 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001041 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001042 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001043 if (STy == 0)
1044 ThrowException("Cannot make struct constant with type: '" +
1045 (*$1)->getDescription() + "'!");
1046
1047 if (STy->getNumContainedTypes() != 0)
1048 ThrowException("Illegal number of initializers for structure type!");
1049
1050 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1051 delete $1;
1052 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001053 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001054 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001055 if (PTy == 0)
1056 ThrowException("Cannot make null pointer constant with type: '" +
1057 (*$1)->getDescription() + "'!");
1058
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001059 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001060 delete $1;
1061 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001062 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001063 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001064 if (Ty == 0)
1065 ThrowException("Global const reference must be a pointer type!");
1066
Chris Lattner3101c252002-08-15 17:58:33 +00001067 // ConstExprs can exist in the body of a function, thus creating
1068 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1069 // the context of a function, getValNonImprovising will search the functions
1070 // symbol table instead of the module symbol table for the global symbol,
1071 // which throws things all off. To get around this, we just tell
1072 // getValNonImprovising that we are at global scope here.
1073 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001074 Function *SavedCurFn = CurFun.CurrentFunction;
1075 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001076
Chris Lattner2079fde2001-10-13 06:41:08 +00001077 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001078
Chris Lattner394f2eb2003-10-16 20:12:13 +00001079 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001080
Chris Lattner2079fde2001-10-13 06:41:08 +00001081 // If this is an initializer for a constant pointer, which is referencing a
1082 // (currently) undefined variable, create a stub now that shall be replaced
1083 // in the future with the right type of variable.
1084 //
1085 if (V == 0) {
1086 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1087 const PointerType *PT = cast<PointerType>(Ty);
1088
1089 // First check to see if the forward references value is already created!
1090 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001091 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001092
1093 if (I != CurModule.GlobalRefs.end()) {
1094 V = I->second; // Placeholder already exists, use it...
1095 } else {
1096 // TODO: Include line number info by creating a subclass of
1097 // TODO: GlobalVariable here that includes the said information!
1098
1099 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001100 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001101 false,
1102 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001103 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001104 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001105
1106 // Must temporarily push this value into the module table...
1107 CurModule.CurrentModule->getGlobalList().push_back(GV);
1108 V = GV;
1109 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001110 }
1111
Chris Lattner2079fde2001-10-13 06:41:08 +00001112 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001113 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001114 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001115 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001116 | Types ConstExpr {
1117 if ($1->get() != $2->getType())
1118 ThrowException("Mismatched types for constant expression!");
1119 $$ = $2;
1120 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001121 }
1122 | Types ZEROINITIALIZER {
1123 $$ = Constant::getNullValue($1->get());
1124 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001125 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001126
Chris Lattnere43f40b2002-10-09 00:25:32 +00001127ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001128 if (!ConstantSInt::isValueValidForType($1, $2))
1129 ThrowException("Constant value doesn't fit in type!");
1130 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001131 }
1132 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001133 if (!ConstantUInt::isValueValidForType($1, $2))
1134 ThrowException("Constant value doesn't fit in type!");
1135 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001136 }
1137 | BOOL TRUE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001138 $$ = ConstantBool::True;
1139 }
Chris Lattnere43f40b2002-10-09 00:25:32 +00001140 | BOOL FALSE { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001141 $$ = ConstantBool::False;
1142 }
1143 | FPType FPVAL { // Float & Double constants
1144 $$ = ConstantFP::get($1, $2);
1145 };
1146
Chris Lattner00950542001-06-06 20:29:01 +00001147
Chris Lattnerd78700d2002-08-16 21:14:40 +00001148ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001149 if (!$5->get()->isFirstClassType())
1150 ThrowException("cast constant expression to a non-primitive type: '" +
1151 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001152 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001153 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001154 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001155 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1156 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001157 ThrowException("GetElementPtr requires a pointer operand!");
1158
1159 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001160 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001161 if (!IdxTy)
1162 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001163
Chris Lattner9232b992003-04-22 18:42:41 +00001164 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001165 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1166 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001167 IdxVec.push_back(C);
1168 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001169 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001170
Chris Lattnerd78700d2002-08-16 21:14:40 +00001171 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001172
Chris Lattnerd78700d2002-08-16 21:14:40 +00001173 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001174 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001175 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001176 if ($3->getType() != $5->getType())
1177 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001178 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001179 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001180 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001181 if ($5->getType() != Type::UByteTy)
1182 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001183 if (!$3->getType()->isInteger())
1184 ThrowException("Shift constant expression requires integer operand!");
Chris Lattner5e458e22003-05-21 17:48:56 +00001185 $$ = ConstantExpr::getShift($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001186 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001187
1188
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001189// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001190ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001191 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001192 }
1193 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001194 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001195 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001196 };
Chris Lattner00950542001-06-06 20:29:01 +00001197
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001198
Chris Lattner1781aca2001-09-18 04:00:54 +00001199// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001200GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001201
Chris Lattner00950542001-06-06 20:29:01 +00001202
Chris Lattner0e73ce62002-05-02 19:11:13 +00001203//===----------------------------------------------------------------------===//
1204// Rules to match Modules
1205//===----------------------------------------------------------------------===//
1206
1207// Module rule: Capture the result of parsing the whole file into a result
1208// variable...
1209//
1210Module : FunctionList {
1211 $$ = ParserResult = $1;
1212 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001213};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001214
Chris Lattner7e708292002-06-25 16:13:24 +00001215// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001216//
1217FunctionList : FunctionList Function {
1218 $$ = $1;
1219 assert($2->getParent() == 0 && "Function already in module!");
1220 $1->getFunctionList().push_back($2);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001221 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001222 }
1223 | FunctionList FunctionProto {
1224 $$ = $1;
1225 }
1226 | FunctionList IMPLEMENTATION {
1227 $$ = $1;
1228 }
1229 | ConstPool {
1230 $$ = CurModule.CurrentModule;
1231 // Resolve circular types before we parse the body of the module
1232 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001233 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001234
Chris Lattnere98dda62001-07-14 06:10:16 +00001235// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001236ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001237 if (!setValueName($4, $2))
1238 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001239 }
Chris Lattner30c89792001-09-07 16:35:17 +00001240 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001241 // Eagerly resolve types. This is not an optimization, this is a
1242 // requirement that is due to the fact that we could have this:
1243 //
1244 // %list = type { %list * }
1245 // %list = type { %list * } ; repeated type decl
1246 //
1247 // If types are not resolved eagerly, then the two types will not be
1248 // determined to be the same type!
1249 //
1250 ResolveTypeTo($2, $4->get());
1251
Chris Lattner1781aca2001-09-18 04:00:54 +00001252 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001253 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1254 // If this is not a redefinition of a type...
1255 if (!$2) {
1256 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001257 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001258 }
Chris Lattner30c89792001-09-07 16:35:17 +00001259 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001260
1261 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001262 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001263 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001264 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001265 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001266 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001267 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001268 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001269 if (Initializer == 0)
1270 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001271
Chris Lattnerdda71962001-11-26 18:54:16 +00001272 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001273 if (!setValueName(GV, $2)) { // If not redefining...
1274 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001275 int Slot = InsertValue(GV, CurModule.Values);
1276
1277 if (Slot != -1) {
1278 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1279 } else {
1280 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1281 (char*)GV->getName().c_str()));
1282 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001283 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001284 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001285 | ConstPool OptAssign EXTERNAL GlobalType Types {
1286 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001287 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001288 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001289 if (!setValueName(GV, $2)) { // If not redefining...
1290 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001291 int Slot = InsertValue(GV, CurModule.Values);
1292
1293 if (Slot != -1) {
1294 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1295 } else {
1296 assert(GV->hasName() && "Not named and not numbered!?");
1297 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1298 (char*)GV->getName().c_str()));
1299 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001300 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001301 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001302 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001303 | ConstPool TARGET TargetDefinition {
1304 }
Chris Lattner00950542001-06-06 20:29:01 +00001305 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001306 };
Chris Lattner00950542001-06-06 20:29:01 +00001307
1308
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001309
1310BigOrLittle : BIG { $$ = Module::BigEndian; };
1311BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1312
1313TargetDefinition : ENDIAN '=' BigOrLittle {
1314 CurModule.CurrentModule->setEndianness($3);
1315 }
1316 | POINTERSIZE '=' EUINT64VAL {
1317 if ($3 == 32)
1318 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1319 else if ($3 == 64)
1320 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1321 else
1322 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1323 };
1324
1325
Chris Lattner00950542001-06-06 20:29:01 +00001326//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001327// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001328//===----------------------------------------------------------------------===//
1329
Chris Lattner6c23f572003-08-22 05:42:10 +00001330Name : VAR_ID | STRINGCONSTANT;
1331OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001332
Chris Lattner6c23f572003-08-22 05:42:10 +00001333ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001334 if (*$1 == Type::VoidTy)
1335 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001336 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001337};
Chris Lattner00950542001-06-06 20:29:01 +00001338
Chris Lattner69da5cf2002-10-13 20:57:00 +00001339ArgListH : ArgListH ',' ArgVal {
1340 $$ = $1;
1341 $1->push_back(*$3);
1342 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001343 }
1344 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001345 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001346 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001347 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001348 };
Chris Lattner00950542001-06-06 20:29:01 +00001349
1350ArgList : ArgListH {
1351 $$ = $1;
1352 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001353 | ArgListH ',' DOTDOTDOT {
1354 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001355 $$->push_back(std::pair<PATypeHolder*,
1356 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001357 }
1358 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001359 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1360 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001361 }
Chris Lattner00950542001-06-06 20:29:01 +00001362 | /* empty */ {
1363 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001364 };
Chris Lattner00950542001-06-06 20:29:01 +00001365
Chris Lattner6c23f572003-08-22 05:42:10 +00001366FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001367 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001368 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001369
Chris Lattner9232b992003-04-22 18:42:41 +00001370 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001371 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001372 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001373 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001374 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001375 }
Chris Lattner00950542001-06-06 20:29:01 +00001376
Chris Lattner2079fde2001-10-13 06:41:08 +00001377 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1378 if (isVarArg) ParamTypeList.pop_back();
1379
Chris Lattner1f862af2003-04-16 18:13:57 +00001380 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001381 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001382 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001383
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001384 Function *Fn = 0;
1385 // Is the function already in symtab?
1386 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1387 // Yes it is. If this is the case, either we need to be a forward decl,
1388 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001389 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001390 ThrowException("Redefinition of function '" + FunctionName + "'!");
1391
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001392 // If we found a preexisting function prototype, remove it from the
1393 // module, so that we don't get spurious conflicts with global & local
1394 // variables.
1395 //
1396 CurModule.CurrentModule->getFunctionList().remove(Fn);
Chris Lattner34538142002-03-08 19:11:42 +00001397
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001398 // Make sure to strip off any argument names so we can't get conflicts...
1399 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1400 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001401
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001402 } else { // Not already defined?
Chris Lattner4ad02e72003-04-16 20:28:45 +00001403 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001404 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001405 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001406 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001407 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001408
Chris Lattner394f2eb2003-10-16 20:12:13 +00001409 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001410
Chris Lattner7e708292002-06-25 16:13:24 +00001411 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001412 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001413 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001414 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001415 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001416 delete $4->back().first;
1417 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001418 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001419 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001420 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001421 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001422 delete I->first; // Delete the typeholder...
1423
1424 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001425 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001426
Chris Lattner69da5cf2002-10-13 20:57:00 +00001427 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001428 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001429
Chris Lattner1f862af2003-04-16 18:13:57 +00001430 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001431 }
Chris Lattner51727be2002-06-04 21:58:56 +00001432};
Chris Lattner00950542001-06-06 20:29:01 +00001433
Chris Lattner9b02cc32002-05-03 18:23:48 +00001434BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1435
Chris Lattner4ad02e72003-04-16 20:28:45 +00001436FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001437 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001438
Chris Lattner4ad02e72003-04-16 20:28:45 +00001439 // Make sure that we keep track of the linkage type even if there was a
1440 // previous "declare".
1441 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001442
Chris Lattner7e708292002-06-25 16:13:24 +00001443 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001444 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001445};
Chris Lattner00950542001-06-06 20:29:01 +00001446
Chris Lattner9b02cc32002-05-03 18:23:48 +00001447END : ENDTOK | '}'; // Allow end of '}' to end a function
1448
Chris Lattner79df7c02002-03-26 18:01:55 +00001449Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001450 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001451};
Chris Lattner00950542001-06-06 20:29:01 +00001452
Chris Lattner394f2eb2003-10-16 20:12:13 +00001453FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1454 $$ = CurFun.CurrentFunction;
Chris Lattner79df7c02002-03-26 18:01:55 +00001455 assert($$->getParent() == 0 && "Function already in module!");
1456 CurModule.CurrentModule->getFunctionList().push_back($$);
Chris Lattner394f2eb2003-10-16 20:12:13 +00001457 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001458};
Chris Lattner00950542001-06-06 20:29:01 +00001459
1460//===----------------------------------------------------------------------===//
1461// Rules to match Basic Blocks
1462//===----------------------------------------------------------------------===//
1463
1464ConstValueRef : ESINT64VAL { // A reference to a direct constant
1465 $$ = ValID::create($1);
1466 }
1467 | EUINT64VAL {
1468 $$ = ValID::create($1);
1469 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001470 | FPVAL { // Perhaps it's an FP constant?
1471 $$ = ValID::create($1);
1472 }
Chris Lattner00950542001-06-06 20:29:01 +00001473 | TRUE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001474 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001475 }
1476 | FALSE {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001477 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001478 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001479 | NULL_TOK {
1480 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001481 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001482 | ConstExpr {
1483 $$ = ValID::create($1);
1484 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001485
Chris Lattner2079fde2001-10-13 06:41:08 +00001486// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1487// another value.
1488//
1489SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001490 $$ = ValID::create($1);
1491 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001492 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001493 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001494 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001495
1496// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001497ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001498
Chris Lattner00950542001-06-06 20:29:01 +00001499
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001500// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1501// type immediately preceeds the value reference, and allows complex constant
1502// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001503ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001504 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001505 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001506
Chris Lattner00950542001-06-06 20:29:01 +00001507BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001508 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001509 }
Chris Lattner7e708292002-06-25 16:13:24 +00001510 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1511 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001512 };
Chris Lattner00950542001-06-06 20:29:01 +00001513
1514
1515// Basic blocks are terminated by branching instructions:
1516// br, br/cc, switch, ret
1517//
Chris Lattner2079fde2001-10-13 06:41:08 +00001518BasicBlock : InstructionList OptAssign BBTerminatorInst {
1519 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1520 InsertValue($3);
1521
1522 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001523 InsertValue($1);
1524 $$ = $1;
1525 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001526 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1527 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1528 InsertValue($4);
1529
1530 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001531 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001532
1533 InsertValue($2);
1534 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001535 };
Chris Lattner00950542001-06-06 20:29:01 +00001536
1537InstructionList : InstructionList Inst {
1538 $1->getInstList().push_back($2);
1539 $$ = $1;
1540 }
1541 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001542 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001543 };
Chris Lattner00950542001-06-06 20:29:01 +00001544
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001545BBTerminatorInst : RET ResolvedVal { // Return with a result...
1546 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001547 }
1548 | RET VOID { // Return with no result...
1549 $$ = new ReturnInst();
1550 }
1551 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001552 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001553 } // Conditional Branch...
1554 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001555 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1556 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001557 getVal(Type::BoolTy, $3));
1558 }
1559 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1560 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001561 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001562 $$ = S;
1563
Chris Lattner9232b992003-04-22 18:42:41 +00001564 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001565 E = $8->end();
1566 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001567 S->addCase(I->first, I->second);
Chris Lattner00950542001-06-06 20:29:01 +00001568 }
Chris Lattner196850c2003-05-15 21:30:00 +00001569 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1570 SwitchInst *S = new SwitchInst(getVal($2, $3),
1571 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1572 $$ = S;
1573 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001574 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
1575 EXCEPT ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001576 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001577 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001578
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001579 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1580 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001581 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001582 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001583 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001584 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1585 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001586 ParamTypes.push_back((*I)->getType());
1587 }
1588
1589 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1590 if (isVarArg) ParamTypes.pop_back();
1591
Chris Lattner79df7c02002-03-26 18:01:55 +00001592 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001593 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001594 }
1595 delete $2;
1596
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001597 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001598
1599 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1600 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1601
1602 if (Normal == 0 || Except == 0)
1603 ThrowException("Invoke instruction without label destinations!");
1604
1605 // Create the call node...
1606 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001607 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001608 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001609 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001610 // correctly!
1611 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001612 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1613 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001614 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001615
1616 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1617 if ((*ArgI)->getType() != *I)
1618 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001619 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001620
1621 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1622 ThrowException("Invalid number of parameters detected!");
1623
Chris Lattner6cdb0112001-11-26 16:54:11 +00001624 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001625 }
1626 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001627 }
1628 | UNWIND {
1629 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001630 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001631
1632
Chris Lattner00950542001-06-06 20:29:01 +00001633
1634JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1635 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001636 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001637 if (V == 0)
1638 ThrowException("May only switch on a constant pool value!");
1639
Chris Lattner9232b992003-04-22 18:42:41 +00001640 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001641 }
1642 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001643 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001644 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001645
1646 if (V == 0)
1647 ThrowException("May only switch on a constant pool value!");
1648
Chris Lattner9232b992003-04-22 18:42:41 +00001649 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001650 };
Chris Lattner00950542001-06-06 20:29:01 +00001651
1652Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001653 // Is this definition named?? if so, assign the name...
1654 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001655 InsertValue($2);
1656 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001657};
Chris Lattner00950542001-06-06 20:29:01 +00001658
Chris Lattnerc24d2082001-06-11 15:04:20 +00001659PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001660 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1661 $$->push_back(std::make_pair(getVal(*$1, $3),
1662 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001663 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001664 }
1665 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1666 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001667 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1668 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001669 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001670
1671
Chris Lattner30c89792001-09-07 16:35:17 +00001672ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001673 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001674 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001675 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001676 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001677 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001678 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001679 };
Chris Lattner00950542001-06-06 20:29:01 +00001680
1681// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001682ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001683
Chris Lattner4a6482b2002-09-10 19:57:26 +00001684InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1685 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1686 ThrowException("Arithmetic operator requires integer or FP operands!");
1687 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1688 if ($$ == 0)
1689 ThrowException("binary operator returned null!");
1690 delete $2;
1691 }
1692 | LogicalOps Types ValueRef ',' ValueRef {
1693 if (!(*$2)->isIntegral())
1694 ThrowException("Logical operator requires integral operands!");
1695 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1696 if ($$ == 0)
1697 ThrowException("binary operator returned null!");
1698 delete $2;
1699 }
1700 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001701 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001702 if ($$ == 0)
1703 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001704 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001705 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001706 | NOT ResolvedVal {
1707 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1708 << " Replacing with 'xor'.\n";
1709
1710 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1711 if (Ones == 0)
1712 ThrowException("Expected integral type for not instruction!");
1713
1714 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001715 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001716 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001717 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001718 | ShiftOps ResolvedVal ',' ResolvedVal {
1719 if ($4->getType() != Type::UByteTy)
1720 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001721 if (!$2->getType()->isInteger())
1722 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001723 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001724 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001725 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001726 if (!$4->get()->isFirstClassType())
1727 ThrowException("cast instruction to a non-primitive type: '" +
1728 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001729 $$ = new CastInst($2, *$4);
1730 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001731 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001732 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001733 // FIXME: This is emulation code for an obsolete syntax. This should be
1734 // removed at some point.
1735 if (!ObsoleteVarArgs) {
1736 std::cerr << "WARNING: this file uses obsolete features. "
1737 << "Assemble and disassemble to update it.\n";
1738 ObsoleteVarArgs = true;
1739 }
1740
1741 // First, load the valist...
1742 Instruction *CurVAList = new LoadInst($2, "");
1743 CurBB->getInstList().push_back(CurVAList);
1744
1745 // Emit the vaarg instruction.
1746 $$ = new VAArgInst(CurVAList, *$4);
1747
1748 // Now we must advance the pointer and update it in memory.
1749 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1750 CurBB->getInstList().push_back(TheVANext);
1751
1752 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1753 delete $4;
1754 }
1755 | VAARG ResolvedVal ',' Types {
1756 $$ = new VAArgInst($2, *$4);
1757 delete $4;
1758 }
1759 | VANEXT ResolvedVal ',' Types {
1760 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001761 delete $4;
1762 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001763 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001764 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001765 if (!Ty->isFirstClassType())
1766 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001767 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001768 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001769 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001770 if ($2->front().first->getType() != Ty)
1771 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001772 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001773 $2->pop_front();
1774 }
1775 delete $2; // Free the list...
1776 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001777 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001778 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001779 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001780
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001781 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1782 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001783 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001784 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001785 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001786 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1787 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001788 ParamTypes.push_back((*I)->getType());
1789 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001790
1791 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1792 if (isVarArg) ParamTypes.pop_back();
1793
Chris Lattner79df7c02002-03-26 18:01:55 +00001794 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001795 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001796 }
Chris Lattner30c89792001-09-07 16:35:17 +00001797 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001798
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001799 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001800
Chris Lattner8b81bf52001-07-25 22:47:46 +00001801 // Create the call node...
1802 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001803 // Make sure no arguments is a good thing!
1804 if (Ty->getNumParams() != 0)
1805 ThrowException("No arguments passed to a function that "
1806 "expects arguments!");
1807
Chris Lattner9232b992003-04-22 18:42:41 +00001808 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001809 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001810 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001811 // correctly!
1812 //
Chris Lattner79df7c02002-03-26 18:01:55 +00001813 FunctionType::ParamTypes::const_iterator I = Ty->getParamTypes().begin();
1814 FunctionType::ParamTypes::const_iterator E = Ty->getParamTypes().end();
Chris Lattner9232b992003-04-22 18:42:41 +00001815 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001816
1817 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1818 if ((*ArgI)->getType() != *I)
1819 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001820 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001821
Chris Lattner8b81bf52001-07-25 22:47:46 +00001822 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001823 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001824
Chris Lattner6cdb0112001-11-26 16:54:11 +00001825 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001826 }
1827 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001828 }
1829 | MemoryInst {
1830 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001831 };
Chris Lattner00950542001-06-06 20:29:01 +00001832
Chris Lattner6cdb0112001-11-26 16:54:11 +00001833
1834// IndexList - List of indices for GEP based instructions...
1835IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001836 $$ = $2;
1837 } | /* empty */ {
1838 $$ = new std::vector<Value*>();
1839 };
1840
1841OptVolatile : VOLATILE {
1842 $$ = true;
1843 }
1844 | /* empty */ {
1845 $$ = false;
1846 };
1847
Chris Lattner027dcc52001-07-08 21:10:27 +00001848
Chris Lattner00950542001-06-06 20:29:01 +00001849MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001850 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001851 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001852 }
1853 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001854 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001855 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001856 }
1857 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001858 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001859 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001860 }
1861 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001862 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001863 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001864 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001865 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001866 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001867 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001868 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001869 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001870 }
1871
Chris Lattner15c9c032003-09-08 18:20:29 +00001872 | OptVolatile LOAD Types ValueRef {
1873 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001874 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001875 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001876 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001877 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001878 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001879 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1880 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001881 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001882 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001883 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001884 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00001885 if (ElTy != $3->getType())
1886 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00001887 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001888
Chris Lattner79ad13802003-09-08 20:29:46 +00001889 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001890 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001891 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001892 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001893 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001894 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner30c89792001-09-07 16:35:17 +00001895 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner72e00252001-12-14 16:28:42 +00001896 ThrowException("Can't get element ptr '" + (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001897 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
1898 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00001899 };
Chris Lattner027dcc52001-07-08 21:10:27 +00001900
Brian Gaeked0fde302003-11-11 22:41:34 +00001901
Chris Lattner00950542001-06-06 20:29:01 +00001902%%
Chris Lattner09083092001-07-08 04:57:15 +00001903int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00001904 std::string where
1905 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001906 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00001907 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001908 if (yychar == YYEMPTY)
1909 errMsg += "end-of-file.";
1910 else
Chris Lattner9232b992003-04-22 18:42:41 +00001911 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001912 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00001913 return 0;
1914}