blob: 65ba280fd53e6703c7b8bd0cfcdff9ddfb870bc2 [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
74 /// that we can resolve them alter and print error messages as appropriate.
75 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 +0000208// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000209static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000210 Types.push_back(Ty);
211}
212
213static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000214 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000215 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000216 unsigned Num = (unsigned)D.Num;
217
218 // Module constants occupy the lowest numbered slots...
219 if (Num < CurModule.Types.size())
220 return CurModule.Types[Num];
221
222 Num -= CurModule.Types.size();
223
224 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000225 if (Num <= CurFun.Types.size())
226 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000227 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000228 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000229 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000230 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000231 SymbolTable *SymTab = 0;
Reid Spencer8ce1da72004-07-04 12:19:05 +0000232 Type *N = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000233 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000234 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000235 N = SymTab->lookupType(Name);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000236 }
Chris Lattner30c89792001-09-07 16:35:17 +0000237
238 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000239 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000240 // hasn't been added to the module...
241 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000242 SymTab = &CurModule.CurrentModule->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000243 N = SymTab->lookupType(Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000244 if (N == 0) break;
245 }
246
247 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000248 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000249 }
250 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000251 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000252 }
253
254 // If we reached here, we referenced either a symbol that we don't know about
255 // or an id number that hasn't been read yet. We may be referencing something
256 // forward, so just create an entry to be resolved later and get to it...
257 //
258 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
259
Chris Lattner9232b992003-04-22 18:42:41 +0000260 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000261 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000262
Chris Lattner9232b992003-04-22 18:42:41 +0000263 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000264 if (I != LateResolver.end()) {
265 return I->second;
266 }
Chris Lattner30c89792001-09-07 16:35:17 +0000267
Chris Lattner82269592001-10-22 06:01:08 +0000268 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000269 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000270 return Typ;
271}
272
Chris Lattner9232b992003-04-22 18:42:41 +0000273static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000274 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000275 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000276 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000277 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000278}
279
Chris Lattner2079fde2001-10-13 06:41:08 +0000280// getValNonImprovising - Look up the value specified by the provided type and
281// the provided ValID. If the value exists and has already been defined, return
282// it. Otherwise return null.
283//
284static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000285 if (isa<FunctionType>(Ty))
286 ThrowException("Functions are not values and "
287 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000288
Chris Lattner30c89792001-09-07 16:35:17 +0000289 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000290 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000291 unsigned Num = (unsigned)D.Num;
292
293 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000294 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000295 if (VI != CurModule.Values.end()) {
296 if (Num < VI->second.size())
297 return VI->second[Num];
298 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000299 }
300
301 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000302 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000303 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000304
305 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000306 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000307
Chris Lattner16af11d2004-02-09 21:03:38 +0000308 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000309 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000310
Chris Lattner1a1cb112001-09-30 22:46:54 +0000311 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000312 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000313 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000314
315 D.destroy(); // Free old strdup'd memory...
316 return N;
317 }
318
Chris Lattner2079fde2001-10-13 06:41:08 +0000319 // Check to make sure that "Ty" is an integral type, and that our
320 // value will fit into the specified type...
321 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000322 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
323 ThrowException("Signed integral constant '" +
324 itostr(D.ConstPool64) + "' is invalid for type '" +
325 Ty->getDescription() + "'!");
326 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000327
328 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000329 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
330 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000331 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
332 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000333 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000334 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000335 }
336 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000337 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000338 }
339
Chris Lattner2079fde2001-10-13 06:41:08 +0000340 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000341 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000342 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000343 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000344
345 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000346 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000347 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000348 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000349
Chris Lattnerd78700d2002-08-16 21:14:40 +0000350 case ValID::ConstantVal: // Fully resolved constant?
351 if (D.ConstantValue->getType() != Ty)
352 ThrowException("Constant expression type different from required type!");
353 return D.ConstantValue;
354
Chris Lattner30c89792001-09-07 16:35:17 +0000355 default:
356 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000357 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000358 } // End of switch
359
Chris Lattner2079fde2001-10-13 06:41:08 +0000360 assert(0 && "Unhandled case!");
361 return 0;
362}
363
Chris Lattner2079fde2001-10-13 06:41:08 +0000364// getVal - This function is identical to getValNonImprovising, except that if a
365// value is not already defined, it "improvises" by creating a placeholder var
366// that looks and acts just like the requested variable. When the value is
367// defined later, all uses of the placeholder variable are replaced with the
368// real thing.
369//
370static Value *getVal(const Type *Ty, const ValID &D) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000371
372 // See if the value has already been defined...
373 Value *V = getValNonImprovising(Ty, D);
374 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000375
376 // If we reached here, we referenced either a symbol that we don't know about
377 // or an id number that hasn't been read yet. We may be referencing something
378 // forward, so just create an entry to be resolved later and get to it...
379 //
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000380 if (Ty == Type::LabelTy)
381 V = new BasicBlock();
382 else
383 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000384
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000385 // Remember where this forward reference came from. FIXME, shouldn't we try
386 // to recycle these things??
387 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(D,
388 llvmAsmlineno)));
389
Chris Lattner79df7c02002-03-26 18:01:55 +0000390 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000391 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000392 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000393 InsertValue(V, CurModule.LateResolveValues);
394 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000395}
396
397
398//===----------------------------------------------------------------------===//
399// Code to handle forward references in instructions
400//===----------------------------------------------------------------------===//
401//
402// This code handles the late binding needed with statements that reference
403// values not defined yet... for example, a forward branch, or the PHI node for
404// a loop body.
405//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000406// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000407// and back patchs after we are done.
408//
409
410// ResolveDefinitions - If we could not resolve some defs at parsing
411// time (forward branches, phi functions for loops, etc...) resolve the
412// defs now...
413//
Chris Lattner735f2702004-07-08 22:30:50 +0000414static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
415 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000416 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000417 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000418 E = LateResolvers.end(); LRI != E; ++LRI) {
419 ValueList &List = LRI->second;
420 while (!List.empty()) {
421 Value *V = List.back();
422 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000423
424 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
425 CurModule.PlaceHolderInfo.find(V);
426 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
427
428 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000429
Chris Lattner735f2702004-07-08 22:30:50 +0000430 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000431 if (TheRealValue) {
432 V->replaceAllUsesWith(TheRealValue);
433 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000434 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000435 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000436 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000437 // resolver table
438 InsertValue(V, *FutureLateResolvers);
439 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000440 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000441 ThrowException("Reference to an invalid definition: '" +DID.getName()+
442 "' of type '" + V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000443 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000444 else
445 ThrowException("Reference to an invalid definition: #" +
446 itostr(DID.Num) + " of type '" +
447 V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000448 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000449 }
Chris Lattner00950542001-06-06 20:29:01 +0000450 }
451 }
452
453 LateResolvers.clear();
454}
455
Chris Lattner4a42e902001-10-22 05:56:09 +0000456// ResolveTypeTo - A brand new type was just declared. This means that (if
457// name is not null) things referencing Name can be resolved. Otherwise, things
458// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000459//
Chris Lattner4a42e902001-10-22 05:56:09 +0000460static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000461 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000462 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000463
Chris Lattner4a42e902001-10-22 05:56:09 +0000464 ValID D;
465 if (Name) D = ValID::create(Name);
466 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000467
Chris Lattner9232b992003-04-22 18:42:41 +0000468 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000469 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000470
Chris Lattner9232b992003-04-22 18:42:41 +0000471 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000472 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000473 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000474 LateResolver.erase(I);
475 }
476}
477
478// ResolveTypes - At this point, all types should be resolved. Any that aren't
479// are errors.
480//
Chris Lattner9232b992003-04-22 18:42:41 +0000481static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000482 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000483 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000484
485 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000486 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000487 else
Chris Lattner82269592001-10-22 06:01:08 +0000488 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000489 }
490}
491
Chris Lattner1781aca2001-09-18 04:00:54 +0000492// setValueName - Set the specified value to the name given. The name may be
493// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000494// assumed to be a malloc'd string buffer, and is free'd by this function.
495//
496static void setValueName(Value *V, char *NameStr) {
497 if (NameStr) {
498 std::string Name(NameStr); // Copy string
499 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000500
501 if (V->getType() == Type::VoidTy)
502 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000503
504 assert(inFunctionScope() && "Must be in function scope!");
505 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
506 if (ST.lookup(V->getType(), Name))
507 ThrowException("Redefinition of value named '" + Name + "' in the '" +
508 V->getType()->getDescription() + "' type plane!");
509
Chris Lattnerc9aea522004-07-13 08:12:39 +0000510 // Set the name.
511 V->setName(Name, &ST);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000512 }
513}
514
515// setValueNameMergingDuplicates - Set the specified value to the name given.
516// The name may be null potentially, in which case this is a noop. The string
517// passed in is assumed to be a malloc'd string buffer, and is free'd by this
518// function.
Chris Lattner1781aca2001-09-18 04:00:54 +0000519//
Chris Lattnerb7474512001-10-03 15:39:04 +0000520// This function returns true if the value has already been defined, but is
521// allowed to be redefined in the specified context. If the name is a new name
522// for the typeplane, false is returned.
523//
Chris Lattner8ab406d2004-07-13 07:59:27 +0000524static bool setValueNameMergingDuplicates(Value *V, char *NameStr) {
Chris Lattnerc9aea522004-07-13 08:12:39 +0000525 assert(V->getType() != Type::VoidTy && "Global or constant of type void?");
526
Chris Lattnerb7474512001-10-03 15:39:04 +0000527 if (NameStr == 0) return false;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000528
Chris Lattner9232b992003-04-22 18:42:41 +0000529 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000530 free(NameStr); // Free old string
531
Chris Lattner8ab406d2004-07-13 07:59:27 +0000532 // FIXME: If we eliminated the function constant pool (which we should), this
533 // would just unconditionally look at the module symtab.
Chris Lattner6e6026b2002-11-20 18:36:02 +0000534 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000535 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000536 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000537
Reid Spencer75719bf2004-05-26 21:48:31 +0000538 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000539 if (Existing) { // Inserting a name that is already defined???
Chris Lattner8ab406d2004-07-13 07:59:27 +0000540 // We are a simple redefinition of a value, check to see if it is defined
541 // the same as the old one...
542 if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000543 // We are allowed to redefine a global variable in two circumstances:
544 // 1. If at least one of the globals is uninitialized or
545 // 2. If both initializers have the same value.
546 //
Chris Lattner89219832001-10-03 19:35:04 +0000547 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000548 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
549 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000550
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000551 // Make sure the existing global version gets the initializer! Make
552 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000553 if (GV->hasInitializer() && !EGV->hasInitializer())
554 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000555 if (GV->isConstant())
556 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000557 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000558
Chris Lattner2079fde2001-10-13 06:41:08 +0000559 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000560 return true; // They are equivalent!
561 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000562 }
Chris Lattner8ab406d2004-07-13 07:59:27 +0000563 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
564 if (C == V) return true; // Constants are equal to themselves
Chris Lattner9636a912001-10-01 16:18:37 +0000565 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000566
Chris Lattner2079fde2001-10-13 06:41:08 +0000567 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000568 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000569 }
Chris Lattnerc9aea522004-07-13 08:12:39 +0000570
571 // Set the name.
572 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000573 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000574}
575
Chris Lattner8ab406d2004-07-13 07:59:27 +0000576
577
Reid Spencer75719bf2004-05-26 21:48:31 +0000578// setTypeName - Set the specified type to the name given. The name may be
579// null potentially, in which case this is a noop. The string passed in is
580// assumed to be a malloc'd string buffer, and is freed by this function.
581//
582// This function returns true if the type has already been defined, but is
583// allowed to be redefined in the specified context. If the name is a new name
584// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000585static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000586 if (NameStr == 0) return false;
587
588 std::string Name(NameStr); // Copy string
589 free(NameStr); // Free old string
590
591 // We don't allow assigning names to void type
592 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000593 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000594
595 SymbolTable &ST = inFunctionScope() ?
596 CurFun.CurrentFunction->getSymbolTable() :
597 CurModule.CurrentModule->getSymbolTable();
598
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000599 // Inserting a name that is already defined???
600 if (Type *Existing = ST.lookupType(Name)) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000601 // There is only one case where this is allowed: when we are refining an
602 // opaque type. In this case, Existing will be an opaque type.
603 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
604 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000605 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000606 return true;
607 }
608
609 // Otherwise, this is an attempt to redefine a type. That's okay if
610 // the redefinition is identical to the original. This will be so if
611 // Existing and T point to the same Type object. In this one case we
612 // allow the equivalent redefinition.
613 if (Existing == T) return true; // Yes, it's equal.
614
615 // Any other kind of (non-equivalent) redefinition is an error.
616 ThrowException("Redefinition of type named '" + Name + "' in the '" +
617 T->getDescription() + "' type plane!");
618 }
619
620 // Okay, its a newly named type. Set its name.
Chris Lattner8ca2dc02004-07-09 16:43:55 +0000621 if (!Name.empty()) ST.insert(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000622
Reid Spencer75719bf2004-05-26 21:48:31 +0000623 return false;
624}
Chris Lattner8896eda2001-07-09 19:38:36 +0000625
Chris Lattner30c89792001-09-07 16:35:17 +0000626//===----------------------------------------------------------------------===//
627// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000628//
Chris Lattner8896eda2001-07-09 19:38:36 +0000629
Chris Lattner3b073862004-02-09 03:19:29 +0000630// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000631//
632static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000633 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
634}
635
636namespace {
637 struct UpRefRecord {
638 // NestingLevel - The number of nesting levels that need to be popped before
639 // this type is resolved.
640 unsigned NestingLevel;
641
642 // LastContainedTy - This is the type at the current binding level for the
643 // type. Every time we reduce the nesting level, this gets updated.
644 const Type *LastContainedTy;
645
646 // UpRefTy - This is the actual opaque type that the upreference is
647 // represented with.
648 OpaqueType *UpRefTy;
649
650 UpRefRecord(unsigned NL, OpaqueType *URTy)
651 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
652 };
Chris Lattner30c89792001-09-07 16:35:17 +0000653}
Chris Lattner698b56e2001-07-20 19:15:08 +0000654
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000655// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000656static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000657
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000658/// HandleUpRefs - Every time we finish a new layer of types, this function is
659/// called. It loops through the UpRefs vector, which is a list of the
660/// currently active types. For each type, if the up reference is contained in
661/// the newly completed type, we decrement the level count. When the level
662/// count reaches zero, the upreferenced type is the type that is passed in:
663/// thus we can complete the cycle.
664///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000665static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000666 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000667 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000668 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000669 "' newly formed. Resolving upreferences.\n" <<
670 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000671
672 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
673 // to zero), we resolve them all together before we resolve them to Ty. At
674 // the end of the loop, if there is anything to resolve to Ty, it will be in
675 // this variable.
676 OpaqueType *TypeToResolve = 0;
677
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000678 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000679 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000680 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000681 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000682 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
683 // Decrement level of upreference
684 unsigned Level = --UpRefs[i].NestingLevel;
685 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000686 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000687 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000688 if (!TypeToResolve) {
689 TypeToResolve = UpRefs[i].UpRefTy;
690 } else {
691 UR_OUT(" * Resolving upreference for "
692 << UpRefs[i].second->getDescription() << "\n";
693 std::string OldName = UpRefs[i].UpRefTy->getDescription());
694 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
695 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
696 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
697 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000698 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
699 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000700 }
701 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000702 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000703
Chris Lattner026a8ce2004-02-09 18:53:54 +0000704 if (TypeToResolve) {
705 UR_OUT(" * Resolving upreference for "
706 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000707 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000708 TypeToResolve->refineAbstractTypeTo(Ty);
709 }
710
Chris Lattner8896eda2001-07-09 19:38:36 +0000711 return Ty;
712}
713
Chris Lattner30c89792001-09-07 16:35:17 +0000714
Chris Lattner00950542001-06-06 20:29:01 +0000715//===----------------------------------------------------------------------===//
716// RunVMAsmParser - Define an interface to this parser
717//===----------------------------------------------------------------------===//
718//
Chris Lattner9232b992003-04-22 18:42:41 +0000719Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000720 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000721 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000722 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000723 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000724
Chris Lattner75f20532003-04-22 18:02:52 +0000725 // Allocate a new module to read
726 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000727
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000728 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000729
Chris Lattner00950542001-06-06 20:29:01 +0000730 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000731
732 // Check to see if they called va_start but not va_arg..
733 if (!ObsoleteVarArgs)
734 if (Function *F = Result->getNamedFunction("llvm.va_start"))
735 if (F->asize() == 1) {
736 std::cerr << "WARNING: this file uses obsolete features. "
737 << "Assemble and disassemble to update it.\n";
738 ObsoleteVarArgs = true;
739 }
740
Chris Lattner99e7ab72003-10-18 05:53:13 +0000741 if (ObsoleteVarArgs) {
742 // If the user is making use of obsolete varargs intrinsics, adjust them for
743 // the user.
744 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
745 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
746
747 const Type *RetTy = F->getFunctionType()->getParamType(0);
748 RetTy = cast<PointerType>(RetTy)->getElementType();
749 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
750
751 while (!F->use_empty()) {
752 CallInst *CI = cast<CallInst>(F->use_back());
753 Value *V = new CallInst(NF, "", CI);
754 new StoreInst(V, CI->getOperand(1), CI);
755 CI->getParent()->getInstList().erase(CI);
756 }
757 Result->getFunctionList().erase(F);
758 }
759
760 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
761 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
762 const Type *ArgTy = F->getFunctionType()->getParamType(0);
763 ArgTy = cast<PointerType>(ArgTy)->getElementType();
764 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
765 ArgTy, 0);
766
767 while (!F->use_empty()) {
768 CallInst *CI = cast<CallInst>(F->use_back());
769 Value *V = new LoadInst(CI->getOperand(1), "", CI);
770 new CallInst(NF, V, "", CI);
771 CI->getParent()->getInstList().erase(CI);
772 }
773 Result->getFunctionList().erase(F);
774 }
775
776 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
777 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
778 const Type *ArgTy = F->getFunctionType()->getParamType(0);
779 ArgTy = cast<PointerType>(ArgTy)->getElementType();
780 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
781 ArgTy, 0);
782
783 while (!F->use_empty()) {
784 CallInst *CI = cast<CallInst>(F->use_back());
785 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
786 new StoreInst(V, CI->getOperand(1), CI);
787 CI->getParent()->getInstList().erase(CI);
788 }
789 Result->getFunctionList().erase(F);
790 }
791 }
792
Chris Lattner00950542001-06-06 20:29:01 +0000793 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
794 ParserResult = 0;
795
796 return Result;
797}
798
Brian Gaeked0fde302003-11-11 22:41:34 +0000799} // End llvm namespace
800
801using namespace llvm;
802
Chris Lattner00950542001-06-06 20:29:01 +0000803%}
804
805%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000806 llvm::Module *ModuleVal;
807 llvm::Function *FunctionVal;
808 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
809 llvm::BasicBlock *BasicBlockVal;
810 llvm::TerminatorInst *TermInstVal;
811 llvm::Instruction *InstVal;
812 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000813
Brian Gaeked0fde302003-11-11 22:41:34 +0000814 const llvm::Type *PrimType;
815 llvm::PATypeHolder *TypeVal;
816 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000817
Brian Gaeked0fde302003-11-11 22:41:34 +0000818 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
819 std::vector<llvm::Value*> *ValueList;
820 std::list<llvm::PATypeHolder> *TypeList;
821 std::list<std::pair<llvm::Value*,
822 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
823 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
824 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000825
Brian Gaeked0fde302003-11-11 22:41:34 +0000826 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000827 int64_t SInt64Val;
828 uint64_t UInt64Val;
829 int SIntVal;
830 unsigned UIntVal;
831 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000832 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000833
Chris Lattner30c89792001-09-07 16:35:17 +0000834 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000835 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000836
Brian Gaeked0fde302003-11-11 22:41:34 +0000837 llvm::Instruction::BinaryOps BinaryOpVal;
838 llvm::Instruction::TermOps TermOpVal;
839 llvm::Instruction::MemoryOps MemOpVal;
840 llvm::Instruction::OtherOps OtherOpVal;
841 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000842}
843
Chris Lattner79df7c02002-03-26 18:01:55 +0000844%type <ModuleVal> Module FunctionList
845%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000846%type <BasicBlockVal> BasicBlock InstructionList
847%type <TermInstVal> BBTerminatorInst
848%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000849%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000850%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000851%type <ArgList> ArgList ArgListH
852%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000853%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000854%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000855%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000856%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000857%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000858%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000859%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000860%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000861%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000862
Chris Lattner2079fde2001-10-13 06:41:08 +0000863// ValueRef - Unresolved reference to a definition or BB
864%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000865%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000866// Tokens and types for handling constant integer values
867//
868// ESINT64VAL - A negative number within long long range
869%token <SInt64Val> ESINT64VAL
870
871// EUINT64VAL - A positive number within uns. long long range
872%token <UInt64Val> EUINT64VAL
873%type <SInt64Val> EINT64VAL
874
875%token <SIntVal> SINTVAL // Signed 32 bit ints...
876%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
877%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000878%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000879
880// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000881%type <TypeVal> Types TypesV UpRTypes UpRTypesV
882%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000883%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
884%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000885
Chris Lattner6c23f572003-08-22 05:42:10 +0000886%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
887%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000888
889
Chris Lattner91ef4602004-03-31 03:49:47 +0000890%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000891%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000892%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000893%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000894
895// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000896%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000897
Chris Lattner00950542001-06-06 20:29:01 +0000898// Binary Operators
899%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000900%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000901%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000902%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000903
904// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000905%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000906
Chris Lattner027dcc52001-07-08 21:10:27 +0000907// Other Operators
908%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000909%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000910%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000911
Chris Lattner00950542001-06-06 20:29:01 +0000912%start Module
913%%
914
915// Handle constant integer size restriction and conversion...
916//
Chris Lattner51727be2002-06-04 21:58:56 +0000917INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000918INTVAL : UINTVAL {
919 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
920 ThrowException("Value too large for type!");
921 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000922};
Chris Lattner00950542001-06-06 20:29:01 +0000923
924
Chris Lattner51727be2002-06-04 21:58:56 +0000925EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000926EINT64VAL : EUINT64VAL {
927 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
928 ThrowException("Value too large for type!");
929 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000930};
Chris Lattner00950542001-06-06 20:29:01 +0000931
Chris Lattner00950542001-06-06 20:29:01 +0000932// Operations that are notably excluded from this list include:
933// RET, BR, & SWITCH because they end basic blocks and are treated specially.
934//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000935ArithmeticOps: ADD | SUB | MUL | DIV | REM;
936LogicalOps : AND | OR | XOR;
937SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
938BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
939
Chris Lattner51727be2002-06-04 21:58:56 +0000940ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000941
Chris Lattnere98dda62001-07-14 06:10:16 +0000942// These are some types that allow classification if we only want a particular
943// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000944SIntType : LONG | INT | SHORT | SBYTE;
945UIntType : ULONG | UINT | USHORT | UBYTE;
946IntType : SIntType | UIntType;
947FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000948
Chris Lattnere98dda62001-07-14 06:10:16 +0000949// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000950OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000951 $$ = $1;
952 }
953 | /*empty*/ {
954 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000955 };
Chris Lattner00950542001-06-06 20:29:01 +0000956
Chris Lattner4ad02e72003-04-16 20:28:45 +0000957OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
958 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000959 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000960 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
961 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000962
963//===----------------------------------------------------------------------===//
964// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000965// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000966// access to it, a user must explicitly use TypesV.
967//
968
969// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000970TypesV : Types | VOID { $$ = new PATypeHolder($1); };
971UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000972
973Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000974 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000975 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
976 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000977 };
Chris Lattner30c89792001-09-07 16:35:17 +0000978
979
980// Derived types are added later...
981//
Chris Lattner51727be2002-06-04 21:58:56 +0000982PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
983PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000984UpRTypes : OPAQUE {
985 $$ = new PATypeHolder(OpaqueType::get());
986 }
987 | PrimType {
988 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000989 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000990UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000991 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000992};
Chris Lattner30c89792001-09-07 16:35:17 +0000993
Chris Lattner30c89792001-09-07 16:35:17 +0000994// Include derived types in the Types production.
995//
996UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000997 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000998 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +0000999 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001000 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001001 UR_OUT("New Upreference!\n");
1002 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001003 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001004 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001005 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001006 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001007 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1008 if (isVarArg) Params.pop_back();
1009
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001010 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001011 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001012 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001013 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001014 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001015 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001016 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001017 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001018 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001019 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001020 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001021 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001022
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001023 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001024 delete $2;
1025 }
1026 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001027 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001028 }
1029 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001030 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001031 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001032 };
Chris Lattner30c89792001-09-07 16:35:17 +00001033
Chris Lattner7e708292002-06-25 16:13:24 +00001034// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001035// declaration type lists
1036//
1037TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001038 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001039 $$->push_back(*$1); delete $1;
1040 }
1041 | TypeListI ',' UpRTypes {
1042 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001043 };
Chris Lattner30c89792001-09-07 16:35:17 +00001044
Chris Lattner7e708292002-06-25 16:13:24 +00001045// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001046ArgTypeListI : TypeListI
1047 | TypeListI ',' DOTDOTDOT {
1048 ($$=$1)->push_back(Type::VoidTy);
1049 }
1050 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001051 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001052 }
1053 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001054 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001055 };
Chris Lattner30c89792001-09-07 16:35:17 +00001056
Chris Lattnere98dda62001-07-14 06:10:16 +00001057// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001058// production is used ONLY to represent constants that show up AFTER a 'const',
1059// 'constant' or 'global' token at global scope. Constants that can be inlined
1060// into other expressions (such as integers and constexprs) are handled by the
1061// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001062//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001063ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001064 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001065 if (ATy == 0)
1066 ThrowException("Cannot make array constant with type: '" +
1067 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001068 const Type *ETy = ATy->getElementType();
1069 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001070
Chris Lattner30c89792001-09-07 16:35:17 +00001071 // Verify that we have the correct size...
1072 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001073 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001074 utostr($3->size()) + " arguments, but has size of " +
1075 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001076
Chris Lattner30c89792001-09-07 16:35:17 +00001077 // Verify all elements are correct type!
1078 for (unsigned i = 0; i < $3->size(); i++) {
1079 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001080 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001081 ETy->getDescription() +"' as required!\nIt is of type '"+
1082 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001083 }
1084
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001085 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001086 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001087 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001088 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001089 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001090 if (ATy == 0)
1091 ThrowException("Cannot make array constant with type: '" +
1092 (*$1)->getDescription() + "'!");
1093
1094 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001095 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001096 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001097 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001098 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001099 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001100 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001101 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001102 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001103 if (ATy == 0)
1104 ThrowException("Cannot make array constant with type: '" +
1105 (*$1)->getDescription() + "'!");
1106
Chris Lattner30c89792001-09-07 16:35:17 +00001107 int NumElements = ATy->getNumElements();
1108 const Type *ETy = ATy->getElementType();
1109 char *EndStr = UnEscapeLexed($3, true);
1110 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001111 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001112 itostr((int)(EndStr-$3)) +
1113 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001114 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001115 if (ETy == Type::SByteTy) {
1116 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001117 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001118 } else if (ETy == Type::UByteTy) {
1119 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001120 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001121 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001122 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001123 ThrowException("Cannot build string arrays of non byte sized elements!");
1124 }
Chris Lattner30c89792001-09-07 16:35:17 +00001125 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001126 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001127 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001128 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001129 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001130 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001131 if (STy == 0)
1132 ThrowException("Cannot make struct constant with type: '" +
1133 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001134
Chris Lattnerc6212f12003-05-21 16:06:56 +00001135 if ($3->size() != STy->getNumContainedTypes())
1136 ThrowException("Illegal number of initializers for structure type!");
1137
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001138 // Check to ensure that constants are compatible with the type initializer!
1139 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001140 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001141 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001142 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001143 "' for element #" + utostr(i) +
1144 " of structure initializer!");
1145
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001146 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001147 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001148 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001149 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001150 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001151 if (STy == 0)
1152 ThrowException("Cannot make struct constant with type: '" +
1153 (*$1)->getDescription() + "'!");
1154
1155 if (STy->getNumContainedTypes() != 0)
1156 ThrowException("Illegal number of initializers for structure type!");
1157
1158 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1159 delete $1;
1160 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001161 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001162 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001163 if (PTy == 0)
1164 ThrowException("Cannot make null pointer constant with type: '" +
1165 (*$1)->getDescription() + "'!");
1166
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001167 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001168 delete $1;
1169 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001170 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001171 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001172 if (Ty == 0)
1173 ThrowException("Global const reference must be a pointer type!");
1174
Chris Lattner3101c252002-08-15 17:58:33 +00001175 // ConstExprs can exist in the body of a function, thus creating
1176 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1177 // the context of a function, getValNonImprovising will search the functions
1178 // symbol table instead of the module symbol table for the global symbol,
1179 // which throws things all off. To get around this, we just tell
1180 // getValNonImprovising that we are at global scope here.
1181 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001182 Function *SavedCurFn = CurFun.CurrentFunction;
1183 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001184
Chris Lattner2079fde2001-10-13 06:41:08 +00001185 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001186
Chris Lattner394f2eb2003-10-16 20:12:13 +00001187 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001188
Chris Lattner2079fde2001-10-13 06:41:08 +00001189 // If this is an initializer for a constant pointer, which is referencing a
1190 // (currently) undefined variable, create a stub now that shall be replaced
1191 // in the future with the right type of variable.
1192 //
1193 if (V == 0) {
1194 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1195 const PointerType *PT = cast<PointerType>(Ty);
1196
1197 // First check to see if the forward references value is already created!
1198 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001199 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001200
1201 if (I != CurModule.GlobalRefs.end()) {
1202 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001203 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001204 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001205 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001206 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001207 false,
1208 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001209 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001210 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001211
1212 // Must temporarily push this value into the module table...
1213 CurModule.CurrentModule->getGlobalList().push_back(GV);
1214 V = GV;
1215 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001216 }
1217
Chris Lattner2079fde2001-10-13 06:41:08 +00001218 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001219 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001220 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001221 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001222 | Types ConstExpr {
1223 if ($1->get() != $2->getType())
1224 ThrowException("Mismatched types for constant expression!");
1225 $$ = $2;
1226 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001227 }
1228 | Types ZEROINITIALIZER {
1229 $$ = Constant::getNullValue($1->get());
1230 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001231 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001232
Chris Lattnere43f40b2002-10-09 00:25:32 +00001233ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001234 if (!ConstantSInt::isValueValidForType($1, $2))
1235 ThrowException("Constant value doesn't fit in type!");
1236 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001237 }
1238 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001239 if (!ConstantUInt::isValueValidForType($1, $2))
1240 ThrowException("Constant value doesn't fit in type!");
1241 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001242 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001243 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001244 $$ = ConstantBool::True;
1245 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001246 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001247 $$ = ConstantBool::False;
1248 }
1249 | FPType FPVAL { // Float & Double constants
1250 $$ = ConstantFP::get($1, $2);
1251 };
1252
Chris Lattner00950542001-06-06 20:29:01 +00001253
Chris Lattnerd78700d2002-08-16 21:14:40 +00001254ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001255 if (!$3->getType()->isFirstClassType())
1256 ThrowException("cast constant expression from a non-primitive type: '" +
1257 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001258 if (!$5->get()->isFirstClassType())
1259 ThrowException("cast constant expression to a non-primitive type: '" +
1260 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001261 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001262 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001263 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001264 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1265 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001266 ThrowException("GetElementPtr requires a pointer operand!");
1267
Chris Lattner830b6f92004-04-05 01:30:04 +00001268 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1269 // indices to uint struct indices for compatibility.
1270 generic_gep_type_iterator<std::vector<Value*>::iterator>
1271 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1272 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1273 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1274 if (isa<StructType>(*GTI)) // Only change struct indices
1275 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1276 if (CUI->getType() == Type::UByteTy)
1277 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1278
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001279 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001280 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001281 if (!IdxTy)
1282 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001283
Chris Lattner9232b992003-04-22 18:42:41 +00001284 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001285 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1286 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001287 IdxVec.push_back(C);
1288 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001289 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001290
Chris Lattnerd78700d2002-08-16 21:14:40 +00001291 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001292
Chris Lattnerd78700d2002-08-16 21:14:40 +00001293 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001294 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001295 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1296 if ($3->getType() != Type::BoolTy)
1297 ThrowException("Select condition must be of boolean type!");
1298 if ($5->getType() != $7->getType())
1299 ThrowException("Select operand types must match!");
1300 $$ = ConstantExpr::getSelect($3, $5, $7);
1301 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001302 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001303 if ($3->getType() != $5->getType())
1304 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001305 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001306 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001307 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001308 if ($5->getType() != Type::UByteTy)
1309 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001310 if (!$3->getType()->isInteger())
1311 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001312 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001313 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001314
1315
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001316// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001317ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001318 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001319 }
1320 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001321 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001322 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001323 };
Chris Lattner00950542001-06-06 20:29:01 +00001324
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001325
Chris Lattner1781aca2001-09-18 04:00:54 +00001326// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001327GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001328
Chris Lattner00950542001-06-06 20:29:01 +00001329
Chris Lattner0e73ce62002-05-02 19:11:13 +00001330//===----------------------------------------------------------------------===//
1331// Rules to match Modules
1332//===----------------------------------------------------------------------===//
1333
1334// Module rule: Capture the result of parsing the whole file into a result
1335// variable...
1336//
1337Module : FunctionList {
1338 $$ = ParserResult = $1;
1339 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001340};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001341
Chris Lattner7e708292002-06-25 16:13:24 +00001342// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001343//
1344FunctionList : FunctionList Function {
1345 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001346 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001347 }
1348 | FunctionList FunctionProto {
1349 $$ = $1;
1350 }
1351 | FunctionList IMPLEMENTATION {
1352 $$ = $1;
1353 }
1354 | ConstPool {
1355 $$ = CurModule.CurrentModule;
1356 // Resolve circular types before we parse the body of the module
1357 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001358 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001359
Chris Lattnere98dda62001-07-14 06:10:16 +00001360// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001361ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001362 // FIXME: THIS SHOULD REALLY BE ELIMINATED. It is totally unneeded.
1363 if (!setValueNameMergingDuplicates($4, $2))
Chris Lattneradf99702003-03-03 23:28:55 +00001364 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001365 }
Chris Lattner30c89792001-09-07 16:35:17 +00001366 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001367 // Eagerly resolve types. This is not an optimization, this is a
1368 // requirement that is due to the fact that we could have this:
1369 //
1370 // %list = type { %list * }
1371 // %list = type { %list * } ; repeated type decl
1372 //
1373 // If types are not resolved eagerly, then the two types will not be
1374 // determined to be the same type!
1375 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001376 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001377
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001378 if (!setTypeName(*$4, $2) && !$2) {
1379 // If this is a named type that is not a redefinition, add it to the slot
1380 // table.
1381 InsertType(*$4, inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattner30c89792001-09-07 16:35:17 +00001382 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001383
1384 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001385 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001386 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001387 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001388 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001389 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001390 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001391 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001392 if (Initializer == 0)
1393 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001394
Chris Lattnerdda71962001-11-26 18:54:16 +00001395 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001396 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001397 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001398 int Slot = InsertValue(GV, CurModule.Values);
1399
1400 if (Slot != -1) {
1401 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1402 } else {
1403 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1404 (char*)GV->getName().c_str()));
1405 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001406 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001407 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001408 | ConstPool OptAssign EXTERNAL GlobalType Types {
1409 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001410 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001411 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001412 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001413 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001414 int Slot = InsertValue(GV, CurModule.Values);
1415
1416 if (Slot != -1) {
1417 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1418 } else {
1419 assert(GV->hasName() && "Not named and not numbered!?");
1420 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1421 (char*)GV->getName().c_str()));
1422 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001423 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001424 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001425 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001426 | ConstPool TARGET TargetDefinition {
1427 }
Chris Lattner00950542001-06-06 20:29:01 +00001428 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001429 };
Chris Lattner00950542001-06-06 20:29:01 +00001430
1431
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001432
1433BigOrLittle : BIG { $$ = Module::BigEndian; };
1434BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1435
1436TargetDefinition : ENDIAN '=' BigOrLittle {
1437 CurModule.CurrentModule->setEndianness($3);
1438 }
1439 | POINTERSIZE '=' EUINT64VAL {
1440 if ($3 == 32)
1441 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1442 else if ($3 == 64)
1443 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1444 else
1445 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1446 };
1447
1448
Chris Lattner00950542001-06-06 20:29:01 +00001449//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001450// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001451//===----------------------------------------------------------------------===//
1452
Chris Lattner6c23f572003-08-22 05:42:10 +00001453Name : VAR_ID | STRINGCONSTANT;
1454OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001455
Chris Lattner6c23f572003-08-22 05:42:10 +00001456ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001457 if (*$1 == Type::VoidTy)
1458 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001459 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001460};
Chris Lattner00950542001-06-06 20:29:01 +00001461
Chris Lattner69da5cf2002-10-13 20:57:00 +00001462ArgListH : ArgListH ',' ArgVal {
1463 $$ = $1;
1464 $1->push_back(*$3);
1465 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001466 }
1467 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001468 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001469 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001470 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001471 };
Chris Lattner00950542001-06-06 20:29:01 +00001472
1473ArgList : ArgListH {
1474 $$ = $1;
1475 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001476 | ArgListH ',' DOTDOTDOT {
1477 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001478 $$->push_back(std::pair<PATypeHolder*,
1479 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001480 }
1481 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001482 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1483 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001484 }
Chris Lattner00950542001-06-06 20:29:01 +00001485 | /* empty */ {
1486 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001487 };
Chris Lattner00950542001-06-06 20:29:01 +00001488
Chris Lattner6c23f572003-08-22 05:42:10 +00001489FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001490 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001491 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001492
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001493 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1494 ThrowException("LLVM functions cannot return aggregate types!");
1495
Chris Lattner9232b992003-04-22 18:42:41 +00001496 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001497 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001498 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001499 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001500 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001501 }
Chris Lattner00950542001-06-06 20:29:01 +00001502
Chris Lattner2079fde2001-10-13 06:41:08 +00001503 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1504 if (isVarArg) ParamTypeList.pop_back();
1505
Chris Lattner1f862af2003-04-16 18:13:57 +00001506 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001507 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001508 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001509
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001510 Function *Fn = 0;
1511 // Is the function already in symtab?
1512 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1513 // Yes it is. If this is the case, either we need to be a forward decl,
1514 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001515 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001516 ThrowException("Redefinition of function '" + FunctionName + "'!");
1517
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001518 // Make sure to strip off any argument names so we can't get conflicts...
1519 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1520 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001521
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001522 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001523 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1524 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001525 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001526 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001527 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001528 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001529
Chris Lattner394f2eb2003-10-16 20:12:13 +00001530 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001531
Chris Lattner7e708292002-06-25 16:13:24 +00001532 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001533 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001534 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001535 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001536 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001537 delete $4->back().first;
1538 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001539 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001540 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001541 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001542 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001543 delete I->first; // Delete the typeholder...
1544
Chris Lattner8ab406d2004-07-13 07:59:27 +00001545 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001546 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001547 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001548
Chris Lattner1f862af2003-04-16 18:13:57 +00001549 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001550 }
Chris Lattner51727be2002-06-04 21:58:56 +00001551};
Chris Lattner00950542001-06-06 20:29:01 +00001552
Chris Lattner9b02cc32002-05-03 18:23:48 +00001553BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1554
Chris Lattner4ad02e72003-04-16 20:28:45 +00001555FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001556 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001557
Chris Lattner4ad02e72003-04-16 20:28:45 +00001558 // Make sure that we keep track of the linkage type even if there was a
1559 // previous "declare".
1560 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001561
Chris Lattner7e708292002-06-25 16:13:24 +00001562 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001563 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001564};
Chris Lattner00950542001-06-06 20:29:01 +00001565
Chris Lattner9b02cc32002-05-03 18:23:48 +00001566END : ENDTOK | '}'; // Allow end of '}' to end a function
1567
Chris Lattner79df7c02002-03-26 18:01:55 +00001568Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001569 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001570};
Chris Lattner00950542001-06-06 20:29:01 +00001571
Chris Lattner394f2eb2003-10-16 20:12:13 +00001572FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1573 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001574 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001575};
Chris Lattner00950542001-06-06 20:29:01 +00001576
1577//===----------------------------------------------------------------------===//
1578// Rules to match Basic Blocks
1579//===----------------------------------------------------------------------===//
1580
1581ConstValueRef : ESINT64VAL { // A reference to a direct constant
1582 $$ = ValID::create($1);
1583 }
1584 | EUINT64VAL {
1585 $$ = ValID::create($1);
1586 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001587 | FPVAL { // Perhaps it's an FP constant?
1588 $$ = ValID::create($1);
1589 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001590 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001591 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001592 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001593 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001594 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001595 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001596 | NULL_TOK {
1597 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001598 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001599 | ConstExpr {
1600 $$ = ValID::create($1);
1601 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001602
Chris Lattner2079fde2001-10-13 06:41:08 +00001603// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1604// another value.
1605//
1606SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001607 $$ = ValID::create($1);
1608 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001609 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001610 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001611 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001612
1613// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001614ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001615
Chris Lattner00950542001-06-06 20:29:01 +00001616
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001617// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1618// type immediately preceeds the value reference, and allows complex constant
1619// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001620ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001621 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001622 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001623
Chris Lattner00950542001-06-06 20:29:01 +00001624BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001625 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001626 }
Chris Lattner7e708292002-06-25 16:13:24 +00001627 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001628 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001629 };
Chris Lattner00950542001-06-06 20:29:01 +00001630
1631
1632// Basic blocks are terminated by branching instructions:
1633// br, br/cc, switch, ret
1634//
Chris Lattner2079fde2001-10-13 06:41:08 +00001635BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001636 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001637 InsertValue($3);
1638
1639 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001640 InsertValue($1);
1641 $$ = $1;
1642 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001643 | LABELSTR InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001644 setValueName($4, $3);
Chris Lattner2079fde2001-10-13 06:41:08 +00001645 InsertValue($4);
1646
1647 $2->getInstList().push_back($4);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001648 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001649
1650 InsertValue($2);
1651 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001652 };
Chris Lattner00950542001-06-06 20:29:01 +00001653
1654InstructionList : InstructionList Inst {
1655 $1->getInstList().push_back($2);
1656 $$ = $1;
1657 }
1658 | /* empty */ {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001659 $$ = CurBB = new BasicBlock("", CurFun.CurrentFunction);
Chris Lattner51727be2002-06-04 21:58:56 +00001660 };
Chris Lattner00950542001-06-06 20:29:01 +00001661
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001662BBTerminatorInst : RET ResolvedVal { // Return with a result...
1663 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001664 }
1665 | RET VOID { // Return with no result...
1666 $$ = new ReturnInst();
1667 }
1668 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001669 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001670 } // Conditional Branch...
1671 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001672 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1673 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001674 getVal(Type::BoolTy, $3));
1675 }
1676 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1677 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001678 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001679 $$ = S;
1680
Chris Lattner9232b992003-04-22 18:42:41 +00001681 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001682 E = $8->end();
1683 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001684 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001685 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001686 }
Chris Lattner196850c2003-05-15 21:30:00 +00001687 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1688 SwitchInst *S = new SwitchInst(getVal($2, $3),
1689 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1690 $$ = S;
1691 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001692 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001693 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001694 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001695 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001696
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001697 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1698 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001699 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001700 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001701 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001702 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1703 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001704 ParamTypes.push_back((*I)->getType());
1705 }
1706
1707 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1708 if (isVarArg) ParamTypes.pop_back();
1709
Chris Lattner79df7c02002-03-26 18:01:55 +00001710 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001711 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001712 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001713
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001714 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001715
1716 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1717 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1718
1719 if (Normal == 0 || Except == 0)
1720 ThrowException("Invoke instruction without label destinations!");
1721
1722 // Create the call node...
1723 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001724 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001725 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001726 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001727 // correctly!
1728 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001729 FunctionType::param_iterator I = Ty->param_begin();
1730 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001731 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001732
1733 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1734 if ((*ArgI)->getType() != *I)
1735 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001736 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001737
1738 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1739 ThrowException("Invalid number of parameters detected!");
1740
Chris Lattner6cdb0112001-11-26 16:54:11 +00001741 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001742 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001743 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001744 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001745 }
1746 | UNWIND {
1747 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001748 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001749
1750
Chris Lattner00950542001-06-06 20:29:01 +00001751
1752JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1753 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001754 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001755 if (V == 0)
1756 ThrowException("May only switch on a constant pool value!");
1757
Chris Lattner9232b992003-04-22 18:42:41 +00001758 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001759 }
1760 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001761 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001762 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001763
1764 if (V == 0)
1765 ThrowException("May only switch on a constant pool value!");
1766
Chris Lattner9232b992003-04-22 18:42:41 +00001767 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001768 };
Chris Lattner00950542001-06-06 20:29:01 +00001769
1770Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001771 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001772 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001773 InsertValue($2);
1774 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001775};
Chris Lattner00950542001-06-06 20:29:01 +00001776
Chris Lattnerc24d2082001-06-11 15:04:20 +00001777PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001778 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1779 $$->push_back(std::make_pair(getVal(*$1, $3),
1780 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001781 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001782 }
1783 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1784 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001785 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1786 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001787 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001788
1789
Chris Lattner30c89792001-09-07 16:35:17 +00001790ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001791 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001792 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001793 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001794 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001795 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001796 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001797 };
Chris Lattner00950542001-06-06 20:29:01 +00001798
1799// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001800ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001801
Chris Lattner4a6482b2002-09-10 19:57:26 +00001802InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1803 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1804 ThrowException("Arithmetic operator requires integer or FP operands!");
1805 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1806 if ($$ == 0)
1807 ThrowException("binary operator returned null!");
1808 delete $2;
1809 }
1810 | LogicalOps Types ValueRef ',' ValueRef {
1811 if (!(*$2)->isIntegral())
1812 ThrowException("Logical operator requires integral operands!");
1813 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1814 if ($$ == 0)
1815 ThrowException("binary operator returned null!");
1816 delete $2;
1817 }
1818 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001819 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001820 if ($$ == 0)
1821 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001822 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001823 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001824 | NOT ResolvedVal {
1825 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1826 << " Replacing with 'xor'.\n";
1827
1828 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1829 if (Ones == 0)
1830 ThrowException("Expected integral type for not instruction!");
1831
1832 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001833 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001834 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001835 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001836 | ShiftOps ResolvedVal ',' ResolvedVal {
1837 if ($4->getType() != Type::UByteTy)
1838 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001839 if (!$2->getType()->isInteger())
1840 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001841 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001842 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001843 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001844 if (!$4->get()->isFirstClassType())
1845 ThrowException("cast instruction to a non-primitive type: '" +
1846 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001847 $$ = new CastInst($2, *$4);
1848 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001849 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001850 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1851 if ($2->getType() != Type::BoolTy)
1852 ThrowException("select condition must be boolean!");
1853 if ($4->getType() != $6->getType())
1854 ThrowException("select value types should match!");
1855 $$ = new SelectInst($2, $4, $6);
1856 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001857 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001858 // FIXME: This is emulation code for an obsolete syntax. This should be
1859 // removed at some point.
1860 if (!ObsoleteVarArgs) {
1861 std::cerr << "WARNING: this file uses obsolete features. "
1862 << "Assemble and disassemble to update it.\n";
1863 ObsoleteVarArgs = true;
1864 }
1865
1866 // First, load the valist...
1867 Instruction *CurVAList = new LoadInst($2, "");
1868 CurBB->getInstList().push_back(CurVAList);
1869
1870 // Emit the vaarg instruction.
1871 $$ = new VAArgInst(CurVAList, *$4);
1872
1873 // Now we must advance the pointer and update it in memory.
1874 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1875 CurBB->getInstList().push_back(TheVANext);
1876
1877 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1878 delete $4;
1879 }
1880 | VAARG ResolvedVal ',' Types {
1881 $$ = new VAArgInst($2, *$4);
1882 delete $4;
1883 }
1884 | VANEXT ResolvedVal ',' Types {
1885 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001886 delete $4;
1887 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001888 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001889 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001890 if (!Ty->isFirstClassType())
1891 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001892 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001893 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001894 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001895 if ($2->front().first->getType() != Ty)
1896 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001897 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001898 $2->pop_front();
1899 }
1900 delete $2; // Free the list...
1901 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001902 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001903 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001904 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001905
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001906 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1907 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001908 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001909 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001910 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001911 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1912 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001913 ParamTypes.push_back((*I)->getType());
1914 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001915
1916 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1917 if (isVarArg) ParamTypes.pop_back();
1918
Chris Lattner79df7c02002-03-26 18:01:55 +00001919 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001920 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001921 }
Chris Lattner00950542001-06-06 20:29:01 +00001922
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001923 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001924
Chris Lattner8b81bf52001-07-25 22:47:46 +00001925 // Create the call node...
1926 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001927 // Make sure no arguments is a good thing!
1928 if (Ty->getNumParams() != 0)
1929 ThrowException("No arguments passed to a function that "
1930 "expects arguments!");
1931
Chris Lattner9232b992003-04-22 18:42:41 +00001932 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001933 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001934 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001935 // correctly!
1936 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001937 FunctionType::param_iterator I = Ty->param_begin();
1938 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001939 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001940
1941 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1942 if ((*ArgI)->getType() != *I)
1943 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001944 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001945
Chris Lattner8b81bf52001-07-25 22:47:46 +00001946 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001947 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001948
Chris Lattner6cdb0112001-11-26 16:54:11 +00001949 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001950 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001951 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001952 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001953 }
1954 | MemoryInst {
1955 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001956 };
Chris Lattner00950542001-06-06 20:29:01 +00001957
Chris Lattner6cdb0112001-11-26 16:54:11 +00001958
1959// IndexList - List of indices for GEP based instructions...
1960IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001961 $$ = $2;
1962 } | /* empty */ {
1963 $$ = new std::vector<Value*>();
1964 };
1965
1966OptVolatile : VOLATILE {
1967 $$ = true;
1968 }
1969 | /* empty */ {
1970 $$ = false;
1971 };
1972
Chris Lattner027dcc52001-07-08 21:10:27 +00001973
Chris Lattner00950542001-06-06 20:29:01 +00001974MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001975 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001976 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001977 }
1978 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001979 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001980 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001981 }
1982 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001983 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001984 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001985 }
1986 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001987 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001988 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001989 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001990 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001991 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001992 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001993 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001994 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001995 }
1996
Chris Lattner15c9c032003-09-08 18:20:29 +00001997 | OptVolatile LOAD Types ValueRef {
1998 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001999 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002000 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002001 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002002 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002003 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002004 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2005 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002006 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002007 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002008 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002009 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002010 if (ElTy != $3->getType())
2011 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002012 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002013
Chris Lattner79ad13802003-09-08 20:29:46 +00002014 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002015 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002016 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002017 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002018 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002019 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002020
2021 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2022 // indices to uint struct indices for compatibility.
2023 generic_gep_type_iterator<std::vector<Value*>::iterator>
2024 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2025 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2026 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2027 if (isa<StructType>(*GTI)) // Only change struct indices
2028 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2029 if (CUI->getType() == Type::UByteTy)
2030 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2031
Chris Lattner30c89792001-09-07 16:35:17 +00002032 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002033 ThrowException("Invalid getelementptr indices for type '" +
2034 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002035 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2036 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002037 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002038
Brian Gaeked0fde302003-11-11 22:41:34 +00002039
Chris Lattner00950542001-06-06 20:29:01 +00002040%%
Chris Lattner09083092001-07-08 04:57:15 +00002041int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002042 std::string where
2043 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002044 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002045 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002046 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002047 errMsg += "end-of-file.";
2048 else
Chris Lattner9232b992003-04-22 18:42:41 +00002049 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002050 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002051 return 0;
2052}