blob: 029182c98849398c8912c6fc6eafed64b54a5e78 [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 Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/STLExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000024#include <algorithm>
25#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000026#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000027#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000028
Chris Lattner386a3b72001-10-16 19:54:17 +000029int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000030int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000031int yyparse();
32
Brian Gaeked0fde302003-11-11 22:41:34 +000033namespace llvm {
34
Chris Lattner00950542001-06-06 20:29:01 +000035static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000036std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000043#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000044#else
45#define UR_OUT(X)
46#endif
47
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000048#define YYERROR_VERBOSE 1
49
Chris Lattner99e7ab72003-10-18 05:53:13 +000050// HACK ALERT: This variable is used to implement the automatic conversion of
51// variable argument instructions from their old to new forms. When this
52// compatiblity "Feature" is removed, this should be too.
53//
54static BasicBlock *CurBB;
55static bool ObsoleteVarArgs;
56
57
Chris Lattner7e708292002-06-25 16:13:24 +000058// This contains info used when building the body of a function. It is
59// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000060//
Chris Lattner9232b992003-04-22 18:42:41 +000061typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner735f2702004-07-08 22:30:50 +000062static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
63 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000064
65static struct PerModuleInfo {
66 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000067 std::map<const Type *, ValueList> Values; // Module level numbered definitions
68 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000069 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000070 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000071
Chris Lattnerbc721ed2004-07-13 08:28:21 +000072 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
73 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000074 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000075 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
76
Chris Lattner2079fde2001-10-13 06:41:08 +000077 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
78 // references to global values. Global values may be referenced before they
79 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000080 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000081 //
Chris Lattner9232b992003-04-22 18:42:41 +000082 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000083 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000084 GlobalRefsType GlobalRefs;
85
Chris Lattner00950542001-06-06 20:29:01 +000086 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000087 // If we could not resolve some functions at function compilation time
88 // (calls to functions before they are defined), resolve them now... Types
89 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000090 //
Chris Lattner00950542001-06-06 20:29:01 +000091 ResolveDefinitions(LateResolveValues);
92
Chris Lattner2079fde2001-10-13 06:41:08 +000093 // Check to make sure that all global value forward references have been
94 // resolved!
95 //
96 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000097 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000098
99 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
100 I != E; ++I) {
101 UndefinedReferences += " " + I->first.first->getDescription() + " " +
102 I->first.second.getName() + "\n";
103 }
104 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000105 }
106
Chris Lattner7e708292002-06-25 16:13:24 +0000107 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000108 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000109 CurrentModule = 0;
110 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000111
112
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000113 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000114 // is used to remove things from the forward declaration map, resolving them
115 // to the correct thing as needed.
116 //
117 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
118 // Check to see if there is a forward reference to this global variable...
119 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000120 GlobalRefsType::iterator I =
121 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000122
123 if (I != GlobalRefs.end()) {
Chris Lattnerbc207592004-03-08 06:09:57 +0000124 GlobalValue *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000125 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner38ab6bf2004-07-13 06:58:07 +0000126
127 // Replace all uses of the placeholder with the new GV
128 OldGV->replaceAllUsesWith(GV);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000129
130 // Remove OldGV from the module...
Chris Lattnerbc207592004-03-08 06:09:57 +0000131 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
132 CurrentModule->getGlobalList().erase(GVar);
133 else
134 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
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 Lattner735f2702004-07-08 22:30:50 +0000146 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
147 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000148 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 {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000172 // Figure out which slot number if is...
Chris Lattner735f2702004-07-08 22:30:50 +0000173 ValueList &List = CurModule.Values[CurrentFunction->getType()];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000174 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000175 assert(i < List.size() && "Function not found!");
176 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000177 FID = ValID::create((int)i);
178 break;
179 }
180 }
181 }
182 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
183
Chris Lattner7e708292002-06-25 16:13:24 +0000184 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000185 Types.clear(); // Clear out function local types
Chris Lattner79df7c02002-03-26 18:01:55 +0000186 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000187 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000188 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000189} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000190
Chris Lattner394f2eb2003-10-16 20:12:13 +0000191static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000192
Chris Lattner00950542001-06-06 20:29:01 +0000193
194//===----------------------------------------------------------------------===//
195// Code to handle definitions of all the types
196//===----------------------------------------------------------------------===//
197
Chris Lattner735f2702004-07-08 22:30:50 +0000198static int InsertValue(Value *V,
199 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
200 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000201
202 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000203 ValueList &List = ValueTab[V->getType()];
204 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000205 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000206}
207
Chris Lattner30c89792001-09-07 16:35:17 +0000208static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000209 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000210 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000211 unsigned Num = (unsigned)D.Num;
212
213 // Module constants occupy the lowest numbered slots...
214 if (Num < CurModule.Types.size())
215 return CurModule.Types[Num];
216
217 Num -= CurModule.Types.size();
218
219 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000220 if (Num <= CurFun.Types.size())
221 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000222 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000223 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000224 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000225 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000226 SymbolTable *SymTab = 0;
Reid Spencer8ce1da72004-07-04 12:19:05 +0000227 Type *N = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000228 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000229 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000230 N = SymTab->lookupType(Name);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000231 }
Chris Lattner30c89792001-09-07 16:35:17 +0000232
233 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000234 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000235 // hasn't been added to the module...
236 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000237 SymTab = &CurModule.CurrentModule->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000238 N = SymTab->lookupType(Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000239 if (N == 0) break;
240 }
241
242 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000243 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000244 }
245 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000246 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000247 }
248
249 // If we reached here, we referenced either a symbol that we don't know about
250 // or an id number that hasn't been read yet. We may be referencing something
251 // forward, so just create an entry to be resolved later and get to it...
252 //
253 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
254
Chris Lattner9232b992003-04-22 18:42:41 +0000255 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000256 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000257
Chris Lattner9232b992003-04-22 18:42:41 +0000258 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000259 if (I != LateResolver.end()) {
260 return I->second;
261 }
Chris Lattner30c89792001-09-07 16:35:17 +0000262
Chris Lattner82269592001-10-22 06:01:08 +0000263 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000264 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000265 return Typ;
266}
267
Chris Lattner9232b992003-04-22 18:42:41 +0000268static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000269 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000270 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000271 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000272 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000273}
274
Chris Lattner2079fde2001-10-13 06:41:08 +0000275// getValNonImprovising - Look up the value specified by the provided type and
276// the provided ValID. If the value exists and has already been defined, return
277// it. Otherwise return null.
278//
279static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000280 if (isa<FunctionType>(Ty))
281 ThrowException("Functions are not values and "
282 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000283
Chris Lattner30c89792001-09-07 16:35:17 +0000284 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000285 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000286 unsigned Num = (unsigned)D.Num;
287
288 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000289 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000290 if (VI != CurModule.Values.end()) {
291 if (Num < VI->second.size())
292 return VI->second[Num];
293 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000294 }
295
296 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000297 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000298 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000299
300 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000301 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000302
Chris Lattner16af11d2004-02-09 21:03:38 +0000303 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000304 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000305
Chris Lattner1a1cb112001-09-30 22:46:54 +0000306 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000307 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000308 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000309
310 D.destroy(); // Free old strdup'd memory...
311 return N;
312 }
313
Chris Lattner2079fde2001-10-13 06:41:08 +0000314 // Check to make sure that "Ty" is an integral type, and that our
315 // value will fit into the specified type...
316 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000317 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
318 ThrowException("Signed integral constant '" +
319 itostr(D.ConstPool64) + "' is invalid for type '" +
320 Ty->getDescription() + "'!");
321 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000322
323 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000324 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
325 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000326 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
327 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000328 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000329 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000330 }
331 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000332 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000333 }
334
Chris Lattner2079fde2001-10-13 06:41:08 +0000335 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000336 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000337 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000338 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000339
340 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000341 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000342 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000343 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000344
Chris Lattnerd78700d2002-08-16 21:14:40 +0000345 case ValID::ConstantVal: // Fully resolved constant?
346 if (D.ConstantValue->getType() != Ty)
347 ThrowException("Constant expression type different from required type!");
348 return D.ConstantValue;
349
Chris Lattner30c89792001-09-07 16:35:17 +0000350 default:
351 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000352 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000353 } // End of switch
354
Chris Lattner2079fde2001-10-13 06:41:08 +0000355 assert(0 && "Unhandled case!");
356 return 0;
357}
358
Chris Lattner2079fde2001-10-13 06:41:08 +0000359// getVal - This function is identical to getValNonImprovising, except that if a
360// value is not already defined, it "improvises" by creating a placeholder var
361// that looks and acts just like the requested variable. When the value is
362// defined later, all uses of the placeholder variable are replaced with the
363// real thing.
364//
Chris Lattner64116f92004-07-14 01:33:11 +0000365static Value *getVal(const Type *Ty, const ValID &ID) {
366 if (Ty == Type::LabelTy)
367 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000368
Chris Lattner64116f92004-07-14 01:33:11 +0000369 // See if the value has already been defined.
370 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000371 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000372
373 // If we reached here, we referenced either a symbol that we don't know about
374 // or an id number that hasn't been read yet. We may be referencing something
375 // forward, so just create an entry to be resolved later and get to it...
376 //
Chris Lattner64116f92004-07-14 01:33:11 +0000377 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000378
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000379 // Remember where this forward reference came from. FIXME, shouldn't we try
380 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000381 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000382 llvmAsmlineno)));
383
Chris Lattner79df7c02002-03-26 18:01:55 +0000384 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000385 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000386 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000387 InsertValue(V, CurModule.LateResolveValues);
388 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000389}
390
Chris Lattner64116f92004-07-14 01:33:11 +0000391static BasicBlock *getBBVal(const ValID &ID) {
392 assert(inFunctionScope() && "Can't get basic block at global scope!");
393
394 // See if the value has already been defined.
395 Value *V = getValNonImprovising(Type::LabelTy, ID);
396 if (V) return cast<BasicBlock>(V);
397
398 BasicBlock *BB = new BasicBlock();
399 // Remember where this forward reference came from. FIXME, shouldn't we try
400 // to recycle these things??
401 CurModule.PlaceHolderInfo.insert(std::make_pair(BB, std::make_pair(ID,
402 llvmAsmlineno)));
403
404 InsertValue(BB, CurFun.LateResolveValues);
405 return BB;
406}
407
Chris Lattner00950542001-06-06 20:29:01 +0000408
409//===----------------------------------------------------------------------===//
410// Code to handle forward references in instructions
411//===----------------------------------------------------------------------===//
412//
413// This code handles the late binding needed with statements that reference
414// values not defined yet... for example, a forward branch, or the PHI node for
415// a loop body.
416//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000417// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000418// and back patchs after we are done.
419//
420
421// ResolveDefinitions - If we could not resolve some defs at parsing
422// time (forward branches, phi functions for loops, etc...) resolve the
423// defs now...
424//
Chris Lattner735f2702004-07-08 22:30:50 +0000425static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
426 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000427 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000428 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000429 E = LateResolvers.end(); LRI != E; ++LRI) {
430 ValueList &List = LRI->second;
431 while (!List.empty()) {
432 Value *V = List.back();
433 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000434
435 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
436 CurModule.PlaceHolderInfo.find(V);
437 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
438
439 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000440
Chris Lattner735f2702004-07-08 22:30:50 +0000441 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000442 if (TheRealValue) {
443 V->replaceAllUsesWith(TheRealValue);
444 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000445 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000446 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000447 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000448 // resolver table
449 InsertValue(V, *FutureLateResolvers);
450 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000451 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000452 ThrowException("Reference to an invalid definition: '" +DID.getName()+
453 "' of type '" + V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000454 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000455 else
456 ThrowException("Reference to an invalid definition: #" +
457 itostr(DID.Num) + " of type '" +
458 V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000459 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000460 }
Chris Lattner00950542001-06-06 20:29:01 +0000461 }
462 }
463
464 LateResolvers.clear();
465}
466
Chris Lattner4a42e902001-10-22 05:56:09 +0000467// ResolveTypeTo - A brand new type was just declared. This means that (if
468// name is not null) things referencing Name can be resolved. Otherwise, things
469// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000470//
Chris Lattner4a42e902001-10-22 05:56:09 +0000471static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000472 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000473 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000474
Chris Lattner4a42e902001-10-22 05:56:09 +0000475 ValID D;
476 if (Name) D = ValID::create(Name);
477 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000478
Chris Lattner9232b992003-04-22 18:42:41 +0000479 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000480 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000481
Chris Lattner9232b992003-04-22 18:42:41 +0000482 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000483 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000484 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000485 LateResolver.erase(I);
486 }
487}
488
489// ResolveTypes - At this point, all types should be resolved. Any that aren't
490// are errors.
491//
Chris Lattner9232b992003-04-22 18:42:41 +0000492static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000493 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000494 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000495
496 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000497 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000498 else
Chris Lattner82269592001-10-22 06:01:08 +0000499 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000500 }
501}
502
Chris Lattner1781aca2001-09-18 04:00:54 +0000503// setValueName - Set the specified value to the name given. The name may be
504// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000505// assumed to be a malloc'd string buffer, and is free'd by this function.
506//
507static void setValueName(Value *V, char *NameStr) {
508 if (NameStr) {
509 std::string Name(NameStr); // Copy string
510 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000511
512 if (V->getType() == Type::VoidTy)
513 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000514
515 assert(inFunctionScope() && "Must be in function scope!");
516 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
517 if (ST.lookup(V->getType(), Name))
518 ThrowException("Redefinition of value named '" + Name + "' in the '" +
519 V->getType()->getDescription() + "' type plane!");
520
Chris Lattnerc9aea522004-07-13 08:12:39 +0000521 // Set the name.
522 V->setName(Name, &ST);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000523 }
524}
525
526// setValueNameMergingDuplicates - Set the specified value to the name given.
527// The name may be null potentially, in which case this is a noop. The string
528// passed in is assumed to be a malloc'd string buffer, and is free'd by this
529// function.
Chris Lattner1781aca2001-09-18 04:00:54 +0000530//
Chris Lattnerb7474512001-10-03 15:39:04 +0000531// This function returns true if the value has already been defined, but is
532// allowed to be redefined in the specified context. If the name is a new name
533// for the typeplane, false is returned.
534//
Chris Lattner8ab406d2004-07-13 07:59:27 +0000535static bool setValueNameMergingDuplicates(Value *V, char *NameStr) {
Chris Lattnerc9aea522004-07-13 08:12:39 +0000536 assert(V->getType() != Type::VoidTy && "Global or constant of type void?");
537
Chris Lattnerb7474512001-10-03 15:39:04 +0000538 if (NameStr == 0) return false;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000539
Chris Lattner9232b992003-04-22 18:42:41 +0000540 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000541 free(NameStr); // Free old string
542
Chris Lattner8ab406d2004-07-13 07:59:27 +0000543 // FIXME: If we eliminated the function constant pool (which we should), this
544 // would just unconditionally look at the module symtab.
Chris Lattner6e6026b2002-11-20 18:36:02 +0000545 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000546 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000547 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000548
Reid Spencer75719bf2004-05-26 21:48:31 +0000549 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000550 if (Existing) { // Inserting a name that is already defined???
Chris Lattner8ab406d2004-07-13 07:59:27 +0000551 // We are a simple redefinition of a value, check to see if it is defined
552 // the same as the old one...
553 if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000554 // We are allowed to redefine a global variable in two circumstances:
555 // 1. If at least one of the globals is uninitialized or
556 // 2. If both initializers have the same value.
557 //
Chris Lattner89219832001-10-03 19:35:04 +0000558 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000559 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
560 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000561
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000562 // Make sure the existing global version gets the initializer! Make
563 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000564 if (GV->hasInitializer() && !EGV->hasInitializer())
565 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000566 if (GV->isConstant())
567 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000568 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000569
Chris Lattner2079fde2001-10-13 06:41:08 +0000570 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000571 return true; // They are equivalent!
572 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000573 }
Chris Lattner8ab406d2004-07-13 07:59:27 +0000574 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
575 if (C == V) return true; // Constants are equal to themselves
Chris Lattner9636a912001-10-01 16:18:37 +0000576 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000577
Chris Lattner2079fde2001-10-13 06:41:08 +0000578 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000579 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000580 }
Chris Lattnerc9aea522004-07-13 08:12:39 +0000581
582 // Set the name.
583 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000584 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000585}
586
Chris Lattner8ab406d2004-07-13 07:59:27 +0000587
588
Reid Spencer75719bf2004-05-26 21:48:31 +0000589// setTypeName - Set the specified type to the name given. The name may be
590// null potentially, in which case this is a noop. The string passed in is
591// assumed to be a malloc'd string buffer, and is freed by this function.
592//
593// This function returns true if the type has already been defined, but is
594// allowed to be redefined in the specified context. If the name is a new name
595// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000596static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000597 if (NameStr == 0) return false;
598
599 std::string Name(NameStr); // Copy string
600 free(NameStr); // Free old string
601
602 // We don't allow assigning names to void type
603 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000604 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000605
606 SymbolTable &ST = inFunctionScope() ?
607 CurFun.CurrentFunction->getSymbolTable() :
608 CurModule.CurrentModule->getSymbolTable();
609
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000610 // Inserting a name that is already defined???
611 if (Type *Existing = ST.lookupType(Name)) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000612 // There is only one case where this is allowed: when we are refining an
613 // opaque type. In this case, Existing will be an opaque type.
614 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
615 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000616 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000617 return true;
618 }
619
620 // Otherwise, this is an attempt to redefine a type. That's okay if
621 // the redefinition is identical to the original. This will be so if
622 // Existing and T point to the same Type object. In this one case we
623 // allow the equivalent redefinition.
624 if (Existing == T) return true; // Yes, it's equal.
625
626 // Any other kind of (non-equivalent) redefinition is an error.
627 ThrowException("Redefinition of type named '" + Name + "' in the '" +
628 T->getDescription() + "' type plane!");
629 }
630
631 // Okay, its a newly named type. Set its name.
Chris Lattner8ca2dc02004-07-09 16:43:55 +0000632 if (!Name.empty()) ST.insert(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000633
Reid Spencer75719bf2004-05-26 21:48:31 +0000634 return false;
635}
Chris Lattner8896eda2001-07-09 19:38:36 +0000636
Chris Lattner30c89792001-09-07 16:35:17 +0000637//===----------------------------------------------------------------------===//
638// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000639//
Chris Lattner8896eda2001-07-09 19:38:36 +0000640
Chris Lattner3b073862004-02-09 03:19:29 +0000641// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000642//
643static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000644 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
645}
646
647namespace {
648 struct UpRefRecord {
649 // NestingLevel - The number of nesting levels that need to be popped before
650 // this type is resolved.
651 unsigned NestingLevel;
652
653 // LastContainedTy - This is the type at the current binding level for the
654 // type. Every time we reduce the nesting level, this gets updated.
655 const Type *LastContainedTy;
656
657 // UpRefTy - This is the actual opaque type that the upreference is
658 // represented with.
659 OpaqueType *UpRefTy;
660
661 UpRefRecord(unsigned NL, OpaqueType *URTy)
662 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
663 };
Chris Lattner30c89792001-09-07 16:35:17 +0000664}
Chris Lattner698b56e2001-07-20 19:15:08 +0000665
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000666// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000667static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000668
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000669/// HandleUpRefs - Every time we finish a new layer of types, this function is
670/// called. It loops through the UpRefs vector, which is a list of the
671/// currently active types. For each type, if the up reference is contained in
672/// the newly completed type, we decrement the level count. When the level
673/// count reaches zero, the upreferenced type is the type that is passed in:
674/// thus we can complete the cycle.
675///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000676static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000677 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000678 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000679 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000680 "' newly formed. Resolving upreferences.\n" <<
681 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000682
683 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
684 // to zero), we resolve them all together before we resolve them to Ty. At
685 // the end of the loop, if there is anything to resolve to Ty, it will be in
686 // this variable.
687 OpaqueType *TypeToResolve = 0;
688
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000689 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000690 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000691 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000692 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000693 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
694 // Decrement level of upreference
695 unsigned Level = --UpRefs[i].NestingLevel;
696 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000697 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000698 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000699 if (!TypeToResolve) {
700 TypeToResolve = UpRefs[i].UpRefTy;
701 } else {
702 UR_OUT(" * Resolving upreference for "
703 << UpRefs[i].second->getDescription() << "\n";
704 std::string OldName = UpRefs[i].UpRefTy->getDescription());
705 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
706 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
707 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
708 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000709 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
710 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000711 }
712 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000713 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000714
Chris Lattner026a8ce2004-02-09 18:53:54 +0000715 if (TypeToResolve) {
716 UR_OUT(" * Resolving upreference for "
717 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000718 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000719 TypeToResolve->refineAbstractTypeTo(Ty);
720 }
721
Chris Lattner8896eda2001-07-09 19:38:36 +0000722 return Ty;
723}
724
Chris Lattner30c89792001-09-07 16:35:17 +0000725
Chris Lattner00950542001-06-06 20:29:01 +0000726//===----------------------------------------------------------------------===//
727// RunVMAsmParser - Define an interface to this parser
728//===----------------------------------------------------------------------===//
729//
Chris Lattner9232b992003-04-22 18:42:41 +0000730Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000731 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000732 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000733 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000734 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000735
Chris Lattner75f20532003-04-22 18:02:52 +0000736 // Allocate a new module to read
737 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000738
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000739 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000740
Chris Lattner00950542001-06-06 20:29:01 +0000741 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000742
743 // Check to see if they called va_start but not va_arg..
744 if (!ObsoleteVarArgs)
745 if (Function *F = Result->getNamedFunction("llvm.va_start"))
746 if (F->asize() == 1) {
747 std::cerr << "WARNING: this file uses obsolete features. "
748 << "Assemble and disassemble to update it.\n";
749 ObsoleteVarArgs = true;
750 }
751
Chris Lattner99e7ab72003-10-18 05:53:13 +0000752 if (ObsoleteVarArgs) {
753 // If the user is making use of obsolete varargs intrinsics, adjust them for
754 // the user.
755 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
756 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
757
758 const Type *RetTy = F->getFunctionType()->getParamType(0);
759 RetTy = cast<PointerType>(RetTy)->getElementType();
760 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
761
762 while (!F->use_empty()) {
763 CallInst *CI = cast<CallInst>(F->use_back());
764 Value *V = new CallInst(NF, "", CI);
765 new StoreInst(V, CI->getOperand(1), CI);
766 CI->getParent()->getInstList().erase(CI);
767 }
768 Result->getFunctionList().erase(F);
769 }
770
771 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
772 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
773 const Type *ArgTy = F->getFunctionType()->getParamType(0);
774 ArgTy = cast<PointerType>(ArgTy)->getElementType();
775 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
776 ArgTy, 0);
777
778 while (!F->use_empty()) {
779 CallInst *CI = cast<CallInst>(F->use_back());
780 Value *V = new LoadInst(CI->getOperand(1), "", CI);
781 new CallInst(NF, V, "", CI);
782 CI->getParent()->getInstList().erase(CI);
783 }
784 Result->getFunctionList().erase(F);
785 }
786
787 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
788 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
789 const Type *ArgTy = F->getFunctionType()->getParamType(0);
790 ArgTy = cast<PointerType>(ArgTy)->getElementType();
791 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
792 ArgTy, 0);
793
794 while (!F->use_empty()) {
795 CallInst *CI = cast<CallInst>(F->use_back());
796 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
797 new StoreInst(V, CI->getOperand(1), CI);
798 CI->getParent()->getInstList().erase(CI);
799 }
800 Result->getFunctionList().erase(F);
801 }
802 }
803
Chris Lattner00950542001-06-06 20:29:01 +0000804 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
805 ParserResult = 0;
806
807 return Result;
808}
809
Brian Gaeked0fde302003-11-11 22:41:34 +0000810} // End llvm namespace
811
812using namespace llvm;
813
Chris Lattner00950542001-06-06 20:29:01 +0000814%}
815
816%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000817 llvm::Module *ModuleVal;
818 llvm::Function *FunctionVal;
819 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
820 llvm::BasicBlock *BasicBlockVal;
821 llvm::TerminatorInst *TermInstVal;
822 llvm::Instruction *InstVal;
823 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000824
Brian Gaeked0fde302003-11-11 22:41:34 +0000825 const llvm::Type *PrimType;
826 llvm::PATypeHolder *TypeVal;
827 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000828
Brian Gaeked0fde302003-11-11 22:41:34 +0000829 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
830 std::vector<llvm::Value*> *ValueList;
831 std::list<llvm::PATypeHolder> *TypeList;
832 std::list<std::pair<llvm::Value*,
833 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
834 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
835 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000836
Brian Gaeked0fde302003-11-11 22:41:34 +0000837 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000838 int64_t SInt64Val;
839 uint64_t UInt64Val;
840 int SIntVal;
841 unsigned UIntVal;
842 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000843 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000844
Chris Lattner30c89792001-09-07 16:35:17 +0000845 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000846 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000847
Brian Gaeked0fde302003-11-11 22:41:34 +0000848 llvm::Instruction::BinaryOps BinaryOpVal;
849 llvm::Instruction::TermOps TermOpVal;
850 llvm::Instruction::MemoryOps MemOpVal;
851 llvm::Instruction::OtherOps OtherOpVal;
852 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000853}
854
Chris Lattner79df7c02002-03-26 18:01:55 +0000855%type <ModuleVal> Module FunctionList
856%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000857%type <BasicBlockVal> BasicBlock InstructionList
858%type <TermInstVal> BBTerminatorInst
859%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000860%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000861%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000862%type <ArgList> ArgList ArgListH
863%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000864%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000865%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000866%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000867%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000868%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000869%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000870%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000871%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000872%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000873
Chris Lattner2079fde2001-10-13 06:41:08 +0000874// ValueRef - Unresolved reference to a definition or BB
875%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000876%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000877// Tokens and types for handling constant integer values
878//
879// ESINT64VAL - A negative number within long long range
880%token <SInt64Val> ESINT64VAL
881
882// EUINT64VAL - A positive number within uns. long long range
883%token <UInt64Val> EUINT64VAL
884%type <SInt64Val> EINT64VAL
885
886%token <SIntVal> SINTVAL // Signed 32 bit ints...
887%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
888%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000889%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000890
891// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000892%type <TypeVal> Types TypesV UpRTypes UpRTypesV
893%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000894%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
895%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000896
Chris Lattner6c23f572003-08-22 05:42:10 +0000897%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
898%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000899
900
Chris Lattner91ef4602004-03-31 03:49:47 +0000901%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000902%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000903%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000904%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000905
906// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000907%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000908
Chris Lattner00950542001-06-06 20:29:01 +0000909// Binary Operators
910%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000911%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000912%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000913%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000914
915// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000916%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000917
Chris Lattner027dcc52001-07-08 21:10:27 +0000918// Other Operators
919%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000920%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000921%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000922
Chris Lattner00950542001-06-06 20:29:01 +0000923%start Module
924%%
925
926// Handle constant integer size restriction and conversion...
927//
Chris Lattner51727be2002-06-04 21:58:56 +0000928INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000929INTVAL : UINTVAL {
930 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
931 ThrowException("Value too large for type!");
932 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000933};
Chris Lattner00950542001-06-06 20:29:01 +0000934
935
Chris Lattner51727be2002-06-04 21:58:56 +0000936EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000937EINT64VAL : EUINT64VAL {
938 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
939 ThrowException("Value too large for type!");
940 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000941};
Chris Lattner00950542001-06-06 20:29:01 +0000942
Chris Lattner00950542001-06-06 20:29:01 +0000943// Operations that are notably excluded from this list include:
944// RET, BR, & SWITCH because they end basic blocks and are treated specially.
945//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000946ArithmeticOps: ADD | SUB | MUL | DIV | REM;
947LogicalOps : AND | OR | XOR;
948SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
949BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
950
Chris Lattner51727be2002-06-04 21:58:56 +0000951ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000952
Chris Lattnere98dda62001-07-14 06:10:16 +0000953// These are some types that allow classification if we only want a particular
954// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000955SIntType : LONG | INT | SHORT | SBYTE;
956UIntType : ULONG | UINT | USHORT | UBYTE;
957IntType : SIntType | UIntType;
958FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000959
Chris Lattnere98dda62001-07-14 06:10:16 +0000960// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000961OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000962 $$ = $1;
963 }
964 | /*empty*/ {
965 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000966 };
Chris Lattner00950542001-06-06 20:29:01 +0000967
Chris Lattner4ad02e72003-04-16 20:28:45 +0000968OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
969 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000970 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000971 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
972 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000973
974//===----------------------------------------------------------------------===//
975// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000976// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000977// access to it, a user must explicitly use TypesV.
978//
979
980// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000981TypesV : Types | VOID { $$ = new PATypeHolder($1); };
982UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000983
984Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000985 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000986 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
987 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000988 };
Chris Lattner30c89792001-09-07 16:35:17 +0000989
990
991// Derived types are added later...
992//
Chris Lattner51727be2002-06-04 21:58:56 +0000993PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
994PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000995UpRTypes : OPAQUE {
996 $$ = new PATypeHolder(OpaqueType::get());
997 }
998 | PrimType {
999 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001000 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001001UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001002 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001003};
Chris Lattner30c89792001-09-07 16:35:17 +00001004
Chris Lattner30c89792001-09-07 16:35:17 +00001005// Include derived types in the Types production.
1006//
1007UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001008 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001009 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001010 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001011 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001012 UR_OUT("New Upreference!\n");
1013 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001014 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001015 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001016 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001017 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001018 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1019 if (isVarArg) Params.pop_back();
1020
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001021 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001022 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001023 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001024 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001025 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001026 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001027 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001028 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001029 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001030 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001031 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001032 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001033
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001034 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001035 delete $2;
1036 }
1037 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001038 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001039 }
1040 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001041 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001042 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001043 };
Chris Lattner30c89792001-09-07 16:35:17 +00001044
Chris Lattner7e708292002-06-25 16:13:24 +00001045// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001046// declaration type lists
1047//
1048TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001049 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001050 $$->push_back(*$1); delete $1;
1051 }
1052 | TypeListI ',' UpRTypes {
1053 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001054 };
Chris Lattner30c89792001-09-07 16:35:17 +00001055
Chris Lattner7e708292002-06-25 16:13:24 +00001056// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001057ArgTypeListI : TypeListI
1058 | TypeListI ',' DOTDOTDOT {
1059 ($$=$1)->push_back(Type::VoidTy);
1060 }
1061 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001062 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001063 }
1064 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001065 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001066 };
Chris Lattner30c89792001-09-07 16:35:17 +00001067
Chris Lattnere98dda62001-07-14 06:10:16 +00001068// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001069// production is used ONLY to represent constants that show up AFTER a 'const',
1070// 'constant' or 'global' token at global scope. Constants that can be inlined
1071// into other expressions (such as integers and constexprs) are handled by the
1072// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001073//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001074ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001075 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001076 if (ATy == 0)
1077 ThrowException("Cannot make array constant with type: '" +
1078 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001079 const Type *ETy = ATy->getElementType();
1080 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001081
Chris Lattner30c89792001-09-07 16:35:17 +00001082 // Verify that we have the correct size...
1083 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001084 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001085 utostr($3->size()) + " arguments, but has size of " +
1086 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001087
Chris Lattner30c89792001-09-07 16:35:17 +00001088 // Verify all elements are correct type!
1089 for (unsigned i = 0; i < $3->size(); i++) {
1090 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001091 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001092 ETy->getDescription() +"' as required!\nIt is of type '"+
1093 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001094 }
1095
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001096 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001097 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001098 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001099 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001100 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001101 if (ATy == 0)
1102 ThrowException("Cannot make array constant with type: '" +
1103 (*$1)->getDescription() + "'!");
1104
1105 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001106 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001107 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001108 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001109 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001110 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001111 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001112 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001113 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001114 if (ATy == 0)
1115 ThrowException("Cannot make array constant with type: '" +
1116 (*$1)->getDescription() + "'!");
1117
Chris Lattner30c89792001-09-07 16:35:17 +00001118 int NumElements = ATy->getNumElements();
1119 const Type *ETy = ATy->getElementType();
1120 char *EndStr = UnEscapeLexed($3, true);
1121 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001122 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001123 itostr((int)(EndStr-$3)) +
1124 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001125 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001126 if (ETy == Type::SByteTy) {
1127 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001128 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001129 } else if (ETy == Type::UByteTy) {
1130 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001131 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001132 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001133 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001134 ThrowException("Cannot build string arrays of non byte sized elements!");
1135 }
Chris Lattner30c89792001-09-07 16:35:17 +00001136 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001137 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001138 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001139 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001140 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001141 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001142 if (STy == 0)
1143 ThrowException("Cannot make struct constant with type: '" +
1144 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001145
Chris Lattnerc6212f12003-05-21 16:06:56 +00001146 if ($3->size() != STy->getNumContainedTypes())
1147 ThrowException("Illegal number of initializers for structure type!");
1148
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001149 // Check to ensure that constants are compatible with the type initializer!
1150 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001151 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001152 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001153 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001154 "' for element #" + utostr(i) +
1155 " of structure initializer!");
1156
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001157 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001158 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001159 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001160 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001161 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001162 if (STy == 0)
1163 ThrowException("Cannot make struct constant with type: '" +
1164 (*$1)->getDescription() + "'!");
1165
1166 if (STy->getNumContainedTypes() != 0)
1167 ThrowException("Illegal number of initializers for structure type!");
1168
1169 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1170 delete $1;
1171 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001172 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001173 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001174 if (PTy == 0)
1175 ThrowException("Cannot make null pointer constant with type: '" +
1176 (*$1)->getDescription() + "'!");
1177
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001178 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001179 delete $1;
1180 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001181 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001182 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001183 if (Ty == 0)
1184 ThrowException("Global const reference must be a pointer type!");
1185
Chris Lattner3101c252002-08-15 17:58:33 +00001186 // ConstExprs can exist in the body of a function, thus creating
1187 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1188 // the context of a function, getValNonImprovising will search the functions
1189 // symbol table instead of the module symbol table for the global symbol,
1190 // which throws things all off. To get around this, we just tell
1191 // getValNonImprovising that we are at global scope here.
1192 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001193 Function *SavedCurFn = CurFun.CurrentFunction;
1194 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001195
Chris Lattner2079fde2001-10-13 06:41:08 +00001196 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001197
Chris Lattner394f2eb2003-10-16 20:12:13 +00001198 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001199
Chris Lattner2079fde2001-10-13 06:41:08 +00001200 // If this is an initializer for a constant pointer, which is referencing a
1201 // (currently) undefined variable, create a stub now that shall be replaced
1202 // in the future with the right type of variable.
1203 //
1204 if (V == 0) {
1205 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1206 const PointerType *PT = cast<PointerType>(Ty);
1207
1208 // First check to see if the forward references value is already created!
1209 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001210 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001211
1212 if (I != CurModule.GlobalRefs.end()) {
1213 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001214 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001215 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001216 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001217 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001218 false,
1219 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001220 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001221 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001222
1223 // Must temporarily push this value into the module table...
1224 CurModule.CurrentModule->getGlobalList().push_back(GV);
1225 V = GV;
1226 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001227 }
1228
Chris Lattner2079fde2001-10-13 06:41:08 +00001229 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001230 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001231 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001232 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001233 | Types ConstExpr {
1234 if ($1->get() != $2->getType())
1235 ThrowException("Mismatched types for constant expression!");
1236 $$ = $2;
1237 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001238 }
1239 | Types ZEROINITIALIZER {
1240 $$ = Constant::getNullValue($1->get());
1241 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001242 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001243
Chris Lattnere43f40b2002-10-09 00:25:32 +00001244ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001245 if (!ConstantSInt::isValueValidForType($1, $2))
1246 ThrowException("Constant value doesn't fit in type!");
1247 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001248 }
1249 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001250 if (!ConstantUInt::isValueValidForType($1, $2))
1251 ThrowException("Constant value doesn't fit in type!");
1252 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001253 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001254 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001255 $$ = ConstantBool::True;
1256 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001257 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001258 $$ = ConstantBool::False;
1259 }
1260 | FPType FPVAL { // Float & Double constants
1261 $$ = ConstantFP::get($1, $2);
1262 };
1263
Chris Lattner00950542001-06-06 20:29:01 +00001264
Chris Lattnerd78700d2002-08-16 21:14:40 +00001265ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001266 if (!$3->getType()->isFirstClassType())
1267 ThrowException("cast constant expression from a non-primitive type: '" +
1268 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001269 if (!$5->get()->isFirstClassType())
1270 ThrowException("cast constant expression to a non-primitive type: '" +
1271 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001272 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001273 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001274 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001275 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1276 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001277 ThrowException("GetElementPtr requires a pointer operand!");
1278
Chris Lattner830b6f92004-04-05 01:30:04 +00001279 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1280 // indices to uint struct indices for compatibility.
1281 generic_gep_type_iterator<std::vector<Value*>::iterator>
1282 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1283 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1284 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1285 if (isa<StructType>(*GTI)) // Only change struct indices
1286 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1287 if (CUI->getType() == Type::UByteTy)
1288 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1289
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001290 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001291 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001292 if (!IdxTy)
1293 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001294
Chris Lattner9232b992003-04-22 18:42:41 +00001295 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001296 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1297 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001298 IdxVec.push_back(C);
1299 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001300 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001301
Chris Lattnerd78700d2002-08-16 21:14:40 +00001302 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001303
Chris Lattnerd78700d2002-08-16 21:14:40 +00001304 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001305 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001306 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1307 if ($3->getType() != Type::BoolTy)
1308 ThrowException("Select condition must be of boolean type!");
1309 if ($5->getType() != $7->getType())
1310 ThrowException("Select operand types must match!");
1311 $$ = ConstantExpr::getSelect($3, $5, $7);
1312 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001313 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001314 if ($3->getType() != $5->getType())
1315 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001316 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001317 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001318 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001319 if ($5->getType() != Type::UByteTy)
1320 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001321 if (!$3->getType()->isInteger())
1322 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001323 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001324 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001325
1326
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001327// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001328ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001329 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001330 }
1331 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001332 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001333 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001334 };
Chris Lattner00950542001-06-06 20:29:01 +00001335
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001336
Chris Lattner1781aca2001-09-18 04:00:54 +00001337// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001338GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001339
Chris Lattner00950542001-06-06 20:29:01 +00001340
Chris Lattner0e73ce62002-05-02 19:11:13 +00001341//===----------------------------------------------------------------------===//
1342// Rules to match Modules
1343//===----------------------------------------------------------------------===//
1344
1345// Module rule: Capture the result of parsing the whole file into a result
1346// variable...
1347//
1348Module : FunctionList {
1349 $$ = ParserResult = $1;
1350 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001351};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001352
Chris Lattner7e708292002-06-25 16:13:24 +00001353// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001354//
1355FunctionList : FunctionList Function {
1356 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001357 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001358 }
1359 | FunctionList FunctionProto {
1360 $$ = $1;
1361 }
1362 | FunctionList IMPLEMENTATION {
1363 $$ = $1;
1364 }
1365 | ConstPool {
1366 $$ = CurModule.CurrentModule;
1367 // Resolve circular types before we parse the body of the module
1368 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001369 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001370
Chris Lattnere98dda62001-07-14 06:10:16 +00001371// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001372ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001373 // FIXME: THIS SHOULD REALLY BE ELIMINATED. It is totally unneeded.
1374 if (!setValueNameMergingDuplicates($4, $2))
Chris Lattneradf99702003-03-03 23:28:55 +00001375 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001376 }
Chris Lattner30c89792001-09-07 16:35:17 +00001377 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001378 // Eagerly resolve types. This is not an optimization, this is a
1379 // requirement that is due to the fact that we could have this:
1380 //
1381 // %list = type { %list * }
1382 // %list = type { %list * } ; repeated type decl
1383 //
1384 // If types are not resolved eagerly, then the two types will not be
1385 // determined to be the same type!
1386 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001387 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001388
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001389 if (!setTypeName(*$4, $2) && !$2) {
1390 // If this is a named type that is not a redefinition, add it to the slot
1391 // table.
Chris Lattner64116f92004-07-14 01:33:11 +00001392 if (inFunctionScope())
1393 CurFun.Types.push_back(*$4);
1394 else
1395 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001396 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001397
1398 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001399 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001400 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001401 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001402 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001403 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001404 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001405 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001406 if (Initializer == 0)
1407 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001408
Chris Lattnerdda71962001-11-26 18:54:16 +00001409 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001410 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001411 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001412 int Slot = InsertValue(GV, CurModule.Values);
1413
1414 if (Slot != -1) {
1415 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1416 } else {
1417 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1418 (char*)GV->getName().c_str()));
1419 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001420 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001421 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001422 | ConstPool OptAssign EXTERNAL GlobalType Types {
1423 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001424 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001425 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001426 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001427 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001428 int Slot = InsertValue(GV, CurModule.Values);
1429
1430 if (Slot != -1) {
1431 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1432 } else {
1433 assert(GV->hasName() && "Not named and not numbered!?");
1434 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1435 (char*)GV->getName().c_str()));
1436 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001437 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001438 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001439 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001440 | ConstPool TARGET TargetDefinition {
1441 }
Chris Lattner00950542001-06-06 20:29:01 +00001442 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001443 };
Chris Lattner00950542001-06-06 20:29:01 +00001444
1445
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001446
1447BigOrLittle : BIG { $$ = Module::BigEndian; };
1448BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1449
1450TargetDefinition : ENDIAN '=' BigOrLittle {
1451 CurModule.CurrentModule->setEndianness($3);
1452 }
1453 | POINTERSIZE '=' EUINT64VAL {
1454 if ($3 == 32)
1455 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1456 else if ($3 == 64)
1457 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1458 else
1459 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1460 };
1461
1462
Chris Lattner00950542001-06-06 20:29:01 +00001463//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001464// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001465//===----------------------------------------------------------------------===//
1466
Chris Lattner6c23f572003-08-22 05:42:10 +00001467Name : VAR_ID | STRINGCONSTANT;
1468OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001469
Chris Lattner6c23f572003-08-22 05:42:10 +00001470ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001471 if (*$1 == Type::VoidTy)
1472 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001473 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001474};
Chris Lattner00950542001-06-06 20:29:01 +00001475
Chris Lattner69da5cf2002-10-13 20:57:00 +00001476ArgListH : ArgListH ',' ArgVal {
1477 $$ = $1;
1478 $1->push_back(*$3);
1479 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001480 }
1481 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001482 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001483 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001484 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001485 };
Chris Lattner00950542001-06-06 20:29:01 +00001486
1487ArgList : ArgListH {
1488 $$ = $1;
1489 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001490 | ArgListH ',' DOTDOTDOT {
1491 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001492 $$->push_back(std::pair<PATypeHolder*,
1493 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001494 }
1495 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001496 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1497 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001498 }
Chris Lattner00950542001-06-06 20:29:01 +00001499 | /* empty */ {
1500 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001501 };
Chris Lattner00950542001-06-06 20:29:01 +00001502
Chris Lattner6c23f572003-08-22 05:42:10 +00001503FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001504 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001505 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001506
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001507 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1508 ThrowException("LLVM functions cannot return aggregate types!");
1509
Chris Lattner9232b992003-04-22 18:42:41 +00001510 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001511 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001512 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001513 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001514 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001515 }
Chris Lattner00950542001-06-06 20:29:01 +00001516
Chris Lattner2079fde2001-10-13 06:41:08 +00001517 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1518 if (isVarArg) ParamTypeList.pop_back();
1519
Chris Lattner1f862af2003-04-16 18:13:57 +00001520 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001521 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001522 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001523
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001524 Function *Fn = 0;
1525 // Is the function already in symtab?
1526 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1527 // Yes it is. If this is the case, either we need to be a forward decl,
1528 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001529 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001530 ThrowException("Redefinition of function '" + FunctionName + "'!");
1531
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001532 // Make sure to strip off any argument names so we can't get conflicts...
1533 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1534 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001535
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001536 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001537 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1538 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001539 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001540 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001541 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001542 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001543
Chris Lattner394f2eb2003-10-16 20:12:13 +00001544 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001545
Chris Lattner7e708292002-06-25 16:13:24 +00001546 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001547 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001548 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001549 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001550 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001551 delete $4->back().first;
1552 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001553 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001554 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001555 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001556 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001557 delete I->first; // Delete the typeholder...
1558
Chris Lattner8ab406d2004-07-13 07:59:27 +00001559 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001560 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001561 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001562
Chris Lattner1f862af2003-04-16 18:13:57 +00001563 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001564 }
Chris Lattner51727be2002-06-04 21:58:56 +00001565};
Chris Lattner00950542001-06-06 20:29:01 +00001566
Chris Lattner9b02cc32002-05-03 18:23:48 +00001567BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1568
Chris Lattner4ad02e72003-04-16 20:28:45 +00001569FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001570 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001571
Chris Lattner4ad02e72003-04-16 20:28:45 +00001572 // Make sure that we keep track of the linkage type even if there was a
1573 // previous "declare".
1574 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001575
Chris Lattner7e708292002-06-25 16:13:24 +00001576 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001577 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001578};
Chris Lattner00950542001-06-06 20:29:01 +00001579
Chris Lattner9b02cc32002-05-03 18:23:48 +00001580END : ENDTOK | '}'; // Allow end of '}' to end a function
1581
Chris Lattner79df7c02002-03-26 18:01:55 +00001582Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001583 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001584};
Chris Lattner00950542001-06-06 20:29:01 +00001585
Chris Lattner394f2eb2003-10-16 20:12:13 +00001586FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1587 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001588 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001589};
Chris Lattner00950542001-06-06 20:29:01 +00001590
1591//===----------------------------------------------------------------------===//
1592// Rules to match Basic Blocks
1593//===----------------------------------------------------------------------===//
1594
1595ConstValueRef : ESINT64VAL { // A reference to a direct constant
1596 $$ = ValID::create($1);
1597 }
1598 | EUINT64VAL {
1599 $$ = ValID::create($1);
1600 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001601 | FPVAL { // Perhaps it's an FP constant?
1602 $$ = ValID::create($1);
1603 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001604 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001605 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001606 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001607 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001608 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001609 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001610 | NULL_TOK {
1611 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001612 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001613 | ConstExpr {
1614 $$ = ValID::create($1);
1615 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001616
Chris Lattner2079fde2001-10-13 06:41:08 +00001617// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1618// another value.
1619//
1620SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001621 $$ = ValID::create($1);
1622 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001623 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001624 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001625 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001626
1627// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001628ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001629
Chris Lattner00950542001-06-06 20:29:01 +00001630
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001631// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1632// type immediately preceeds the value reference, and allows complex constant
1633// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001634ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001635 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001636 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001637
Chris Lattner00950542001-06-06 20:29:01 +00001638BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001639 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001640 }
Chris Lattner7e708292002-06-25 16:13:24 +00001641 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001642 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001643 };
Chris Lattner00950542001-06-06 20:29:01 +00001644
1645
1646// Basic blocks are terminated by branching instructions:
1647// br, br/cc, switch, ret
1648//
Chris Lattner2079fde2001-10-13 06:41:08 +00001649BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001650 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001651 InsertValue($3);
1652
1653 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001654 InsertValue($1);
1655 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001656 };
Chris Lattner00950542001-06-06 20:29:01 +00001657
1658InstructionList : InstructionList Inst {
1659 $1->getInstList().push_back($2);
1660 $$ = $1;
1661 }
1662 | /* empty */ {
Chris Lattner22ae2322004-07-13 08:39:15 +00001663 // FIXME: Should check to see if there is a forward ref'd basic block that
1664 // we can use and reuse it as appropriate. It doesn't make sense just to
1665 // make forward ref'd blocks then discard them.
Chris Lattner8ab406d2004-07-13 07:59:27 +00001666 $$ = CurBB = new BasicBlock("", CurFun.CurrentFunction);
Chris Lattner22ae2322004-07-13 08:39:15 +00001667 }
1668 | LABELSTR {
1669 // FIXME: Should check to see if there is a forward ref'd basic block that
1670 // we can use and reuse it as appropriate. It doesn't make sense just to
1671 // make forward ref'd blocks then discard them.
1672 $$ = CurBB = new BasicBlock("", CurFun.CurrentFunction);
1673 setValueName($$, $1);
1674 InsertValue($$);
Chris Lattner51727be2002-06-04 21:58:56 +00001675 };
Chris Lattner00950542001-06-06 20:29:01 +00001676
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001677BBTerminatorInst : RET ResolvedVal { // Return with a result...
1678 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001679 }
1680 | RET VOID { // Return with no result...
1681 $$ = new ReturnInst();
1682 }
1683 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001684 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001685 } // Conditional Branch...
1686 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001687 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001688 }
1689 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001690 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner00950542001-06-06 20:29:01 +00001691 $$ = S;
1692
Chris Lattner9232b992003-04-22 18:42:41 +00001693 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001694 E = $8->end();
1695 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001696 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001697 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001698 }
Chris Lattner196850c2003-05-15 21:30:00 +00001699 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001700 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner196850c2003-05-15 21:30:00 +00001701 $$ = S;
1702 }
Chris Lattner64116f92004-07-14 01:33:11 +00001703 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1704 UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001705 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001706 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001707
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001708 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1709 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001710 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001711 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001712 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001713 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1714 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001715 ParamTypes.push_back((*I)->getType());
1716 }
1717
1718 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1719 if (isVarArg) ParamTypes.pop_back();
1720
Chris Lattner79df7c02002-03-26 18:01:55 +00001721 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001722 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001723 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001724
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001725 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001726
Chris Lattner64116f92004-07-14 01:33:11 +00001727 BasicBlock *Normal = getBBVal($9);
1728 BasicBlock *Except = getBBVal($12);
Chris Lattner2079fde2001-10-13 06:41:08 +00001729
1730 // Create the call node...
1731 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001732 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001733 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001734 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001735 // correctly!
1736 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001737 FunctionType::param_iterator I = Ty->param_begin();
1738 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001739 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001740
1741 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1742 if ((*ArgI)->getType() != *I)
1743 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001744 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001745
1746 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1747 ThrowException("Invalid number of parameters detected!");
1748
Chris Lattner6cdb0112001-11-26 16:54:11 +00001749 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001750 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001751 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001752 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001753 }
1754 | UNWIND {
1755 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001756 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001757
1758
Chris Lattner00950542001-06-06 20:29:01 +00001759
1760JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1761 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001762 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001763 if (V == 0)
1764 ThrowException("May only switch on a constant pool value!");
1765
Chris Lattner64116f92004-07-14 01:33:11 +00001766 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001767 }
1768 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001769 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001770 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001771
1772 if (V == 0)
1773 ThrowException("May only switch on a constant pool value!");
1774
Chris Lattner64116f92004-07-14 01:33:11 +00001775 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001776 };
Chris Lattner00950542001-06-06 20:29:01 +00001777
1778Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001779 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001780 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001781 InsertValue($2);
1782 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001783};
Chris Lattner00950542001-06-06 20:29:01 +00001784
Chris Lattnerc24d2082001-06-11 15:04:20 +00001785PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001786 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001787 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001788 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001789 }
1790 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1791 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001792 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001793 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001794 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001795
1796
Chris Lattner30c89792001-09-07 16:35:17 +00001797ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001798 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001799 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001800 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001801 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001802 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001803 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001804 };
Chris Lattner00950542001-06-06 20:29:01 +00001805
1806// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001807ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001808
Chris Lattner4a6482b2002-09-10 19:57:26 +00001809InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1810 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1811 ThrowException("Arithmetic operator requires integer or FP operands!");
1812 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1813 if ($$ == 0)
1814 ThrowException("binary operator returned null!");
1815 delete $2;
1816 }
1817 | LogicalOps Types ValueRef ',' ValueRef {
1818 if (!(*$2)->isIntegral())
1819 ThrowException("Logical operator requires integral operands!");
1820 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1821 if ($$ == 0)
1822 ThrowException("binary operator returned null!");
1823 delete $2;
1824 }
1825 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001826 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001827 if ($$ == 0)
1828 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001829 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001830 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001831 | NOT ResolvedVal {
1832 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1833 << " Replacing with 'xor'.\n";
1834
1835 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1836 if (Ones == 0)
1837 ThrowException("Expected integral type for not instruction!");
1838
1839 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001840 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001841 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001842 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001843 | ShiftOps ResolvedVal ',' ResolvedVal {
1844 if ($4->getType() != Type::UByteTy)
1845 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001846 if (!$2->getType()->isInteger())
1847 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001848 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001849 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001850 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001851 if (!$4->get()->isFirstClassType())
1852 ThrowException("cast instruction to a non-primitive type: '" +
1853 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001854 $$ = new CastInst($2, *$4);
1855 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001856 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001857 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1858 if ($2->getType() != Type::BoolTy)
1859 ThrowException("select condition must be boolean!");
1860 if ($4->getType() != $6->getType())
1861 ThrowException("select value types should match!");
1862 $$ = new SelectInst($2, $4, $6);
1863 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001864 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001865 // FIXME: This is emulation code for an obsolete syntax. This should be
1866 // removed at some point.
1867 if (!ObsoleteVarArgs) {
1868 std::cerr << "WARNING: this file uses obsolete features. "
1869 << "Assemble and disassemble to update it.\n";
1870 ObsoleteVarArgs = true;
1871 }
1872
1873 // First, load the valist...
1874 Instruction *CurVAList = new LoadInst($2, "");
1875 CurBB->getInstList().push_back(CurVAList);
1876
1877 // Emit the vaarg instruction.
1878 $$ = new VAArgInst(CurVAList, *$4);
1879
1880 // Now we must advance the pointer and update it in memory.
1881 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1882 CurBB->getInstList().push_back(TheVANext);
1883
1884 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1885 delete $4;
1886 }
1887 | VAARG ResolvedVal ',' Types {
1888 $$ = new VAArgInst($2, *$4);
1889 delete $4;
1890 }
1891 | VANEXT ResolvedVal ',' Types {
1892 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001893 delete $4;
1894 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001895 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001896 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001897 if (!Ty->isFirstClassType())
1898 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001899 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001900 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001901 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001902 if ($2->front().first->getType() != Ty)
1903 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001904 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001905 $2->pop_front();
1906 }
1907 delete $2; // Free the list...
1908 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001909 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001910 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001911 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001912
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001913 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1914 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001915 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001916 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001917 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001918 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1919 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001920 ParamTypes.push_back((*I)->getType());
1921 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001922
1923 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1924 if (isVarArg) ParamTypes.pop_back();
1925
Chris Lattner79df7c02002-03-26 18:01:55 +00001926 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001927 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001928 }
Chris Lattner00950542001-06-06 20:29:01 +00001929
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001930 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001931
Chris Lattner8b81bf52001-07-25 22:47:46 +00001932 // Create the call node...
1933 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001934 // Make sure no arguments is a good thing!
1935 if (Ty->getNumParams() != 0)
1936 ThrowException("No arguments passed to a function that "
1937 "expects arguments!");
1938
Chris Lattner9232b992003-04-22 18:42:41 +00001939 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001940 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001941 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001942 // correctly!
1943 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001944 FunctionType::param_iterator I = Ty->param_begin();
1945 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001946 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001947
1948 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1949 if ((*ArgI)->getType() != *I)
1950 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001951 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001952
Chris Lattner8b81bf52001-07-25 22:47:46 +00001953 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001954 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001955
Chris Lattner6cdb0112001-11-26 16:54:11 +00001956 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001957 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001958 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001959 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001960 }
1961 | MemoryInst {
1962 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001963 };
Chris Lattner00950542001-06-06 20:29:01 +00001964
Chris Lattner6cdb0112001-11-26 16:54:11 +00001965
1966// IndexList - List of indices for GEP based instructions...
1967IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001968 $$ = $2;
1969 } | /* empty */ {
1970 $$ = new std::vector<Value*>();
1971 };
1972
1973OptVolatile : VOLATILE {
1974 $$ = true;
1975 }
1976 | /* empty */ {
1977 $$ = false;
1978 };
1979
Chris Lattner027dcc52001-07-08 21:10:27 +00001980
Chris Lattner00950542001-06-06 20:29:01 +00001981MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001982 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001983 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001984 }
1985 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001986 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001987 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001988 }
1989 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001990 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001991 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001992 }
1993 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001994 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001995 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001996 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001997 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001998 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001999 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002000 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002001 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002002 }
2003
Chris Lattner15c9c032003-09-08 18:20:29 +00002004 | OptVolatile LOAD Types ValueRef {
2005 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002006 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002007 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002008 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002009 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002010 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002011 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2012 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002013 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002014 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002015 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002016 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002017 if (ElTy != $3->getType())
2018 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002019 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002020
Chris Lattner79ad13802003-09-08 20:29:46 +00002021 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002022 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002023 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002024 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002025 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002026 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002027
2028 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2029 // indices to uint struct indices for compatibility.
2030 generic_gep_type_iterator<std::vector<Value*>::iterator>
2031 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2032 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2033 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2034 if (isa<StructType>(*GTI)) // Only change struct indices
2035 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2036 if (CUI->getType() == Type::UByteTy)
2037 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2038
Chris Lattner30c89792001-09-07 16:35:17 +00002039 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002040 ThrowException("Invalid getelementptr indices for type '" +
2041 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002042 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2043 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002044 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002045
Brian Gaeked0fde302003-11-11 22:41:34 +00002046
Chris Lattner00950542001-06-06 20:29:01 +00002047%%
Chris Lattner09083092001-07-08 04:57:15 +00002048int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002049 std::string where
2050 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002051 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002052 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002053 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002054 errMsg += "end-of-file.";
2055 else
Chris Lattner9232b992003-04-22 18:42:41 +00002056 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002057 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002058 return 0;
2059}