blob: ee1f73affae5f6d81efc7f54b66e73cbdceaf7b9 [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 Lattner2079fde2001-10-13 06:41:08 +000072 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
73 // references to global values. Global values may be referenced before they
74 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000075 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000076 //
Chris Lattner9232b992003-04-22 18:42:41 +000077 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000078 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000079 GlobalRefsType GlobalRefs;
80
Chris Lattner00950542001-06-06 20:29:01 +000081 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000082 // If we could not resolve some functions at function compilation time
83 // (calls to functions before they are defined), resolve them now... Types
84 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000085 //
Chris Lattner00950542001-06-06 20:29:01 +000086 ResolveDefinitions(LateResolveValues);
87
Chris Lattner2079fde2001-10-13 06:41:08 +000088 // Check to make sure that all global value forward references have been
89 // resolved!
90 //
91 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000092 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000093
94 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
95 I != E; ++I) {
96 UndefinedReferences += " " + I->first.first->getDescription() + " " +
97 I->first.second.getName() + "\n";
98 }
99 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000100 }
101
Chris Lattner7e708292002-06-25 16:13:24 +0000102 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000103 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000104 CurrentModule = 0;
105 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000106
107
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000108 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000109 // is used to remove things from the forward declaration map, resolving them
110 // to the correct thing as needed.
111 //
112 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
113 // Check to see if there is a forward reference to this global variable...
114 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000115 GlobalRefsType::iterator I =
116 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000117
118 if (I != GlobalRefs.end()) {
Chris Lattnerbc207592004-03-08 06:09:57 +0000119 GlobalValue *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000120 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner38ab6bf2004-07-13 06:58:07 +0000121
122 // Replace all uses of the placeholder with the new GV
123 OldGV->replaceAllUsesWith(GV);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000124
125 // Remove OldGV from the module...
Chris Lattnerbc207592004-03-08 06:09:57 +0000126 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
127 CurrentModule->getGlobalList().erase(GVar);
128 else
129 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000130
Chris Lattner2079fde2001-10-13 06:41:08 +0000131 // Remove the map entry for the global now that it has been created...
132 GlobalRefs.erase(I);
133 }
134 }
135
Chris Lattner00950542001-06-06 20:29:01 +0000136} CurModule;
137
Chris Lattner79df7c02002-03-26 18:01:55 +0000138static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000139 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000140
Chris Lattner735f2702004-07-08 22:30:50 +0000141 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
142 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000143 std::vector<PATypeHolder> Types;
144 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner15b69722003-11-12 04:40:30 +0000145 SymbolTable LocalSymtab;
Chris Lattner7e708292002-06-25 16:13:24 +0000146 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000147
Chris Lattner79df7c02002-03-26 18:01:55 +0000148 inline PerFunctionInfo() {
149 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000150 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000151 }
152
Chris Lattner79df7c02002-03-26 18:01:55 +0000153 inline void FunctionStart(Function *M) {
154 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000155 }
156
Chris Lattner79df7c02002-03-26 18:01:55 +0000157 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000158 // If we could not resolve some blocks at parsing time (forward branches)
159 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000160 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000161
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000162 // Make sure to resolve any constant expr references that might exist within
163 // the function we just declared itself.
164 ValID FID;
165 if (CurrentFunction->hasName()) {
166 FID = ValID::create((char*)CurrentFunction->getName().c_str());
167 } else {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000168 // Figure out which slot number if is...
Chris Lattner735f2702004-07-08 22:30:50 +0000169 ValueList &List = CurModule.Values[CurrentFunction->getType()];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000170 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000171 assert(i < List.size() && "Function not found!");
172 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000173 FID = ValID::create((int)i);
174 break;
175 }
176 }
177 }
178 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
179
Chris Lattner7e708292002-06-25 16:13:24 +0000180 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000181 Types.clear(); // Clear out function local types
182 LocalSymtab.clear(); // Clear out function local symbol table
Chris Lattner79df7c02002-03-26 18:01:55 +0000183 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000184 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000185 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000186} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000187
Chris Lattner394f2eb2003-10-16 20:12:13 +0000188static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000189
Chris Lattner00950542001-06-06 20:29:01 +0000190
191//===----------------------------------------------------------------------===//
192// Code to handle definitions of all the types
193//===----------------------------------------------------------------------===//
194
Chris Lattner735f2702004-07-08 22:30:50 +0000195static int InsertValue(Value *V,
196 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
197 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000198
199 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000200 ValueList &List = ValueTab[V->getType()];
201 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000202 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000203}
204
Chris Lattner30c89792001-09-07 16:35:17 +0000205// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000206static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000207 Types.push_back(Ty);
208}
209
210static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000211 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000212 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000213 unsigned Num = (unsigned)D.Num;
214
215 // Module constants occupy the lowest numbered slots...
216 if (Num < CurModule.Types.size())
217 return CurModule.Types[Num];
218
219 Num -= CurModule.Types.size();
220
221 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000222 if (Num <= CurFun.Types.size())
223 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000224 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000225 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000226 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000227 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000228 SymbolTable *SymTab = 0;
Reid Spencer8ce1da72004-07-04 12:19:05 +0000229 Type *N = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000230 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000231 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000232 N = SymTab->lookupType(Name);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000233 }
Chris Lattner30c89792001-09-07 16:35:17 +0000234
235 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000236 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000237 // hasn't been added to the module...
238 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000239 SymTab = &CurModule.CurrentModule->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000240 N = SymTab->lookupType(Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000241 if (N == 0) break;
242 }
243
244 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000245 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000246 }
247 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000248 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000249 }
250
251 // If we reached here, we referenced either a symbol that we don't know about
252 // or an id number that hasn't been read yet. We may be referencing something
253 // forward, so just create an entry to be resolved later and get to it...
254 //
255 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
256
Chris Lattner9232b992003-04-22 18:42:41 +0000257 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000258 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000259
Chris Lattner9232b992003-04-22 18:42:41 +0000260 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000261 if (I != LateResolver.end()) {
262 return I->second;
263 }
Chris Lattner30c89792001-09-07 16:35:17 +0000264
Chris Lattner82269592001-10-22 06:01:08 +0000265 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000266 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000267 return Typ;
268}
269
Chris Lattner9232b992003-04-22 18:42:41 +0000270static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000271 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000272 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000273 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000274 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000275}
276
Chris Lattner2079fde2001-10-13 06:41:08 +0000277// getValNonImprovising - Look up the value specified by the provided type and
278// the provided ValID. If the value exists and has already been defined, return
279// it. Otherwise return null.
280//
281static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000282 if (isa<FunctionType>(Ty))
283 ThrowException("Functions are not values and "
284 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000285
Chris Lattner30c89792001-09-07 16:35:17 +0000286 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000287 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000288 unsigned Num = (unsigned)D.Num;
289
290 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000291 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000292 if (VI != CurModule.Values.end()) {
293 if (Num < VI->second.size())
294 return VI->second[Num];
295 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000296 }
297
298 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000299 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000300 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000301
302 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000303 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000304
Chris Lattner16af11d2004-02-09 21:03:38 +0000305 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000306 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000307
Chris Lattner1a1cb112001-09-30 22:46:54 +0000308 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000309 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000310 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000311
312 D.destroy(); // Free old strdup'd memory...
313 return N;
314 }
315
Chris Lattner2079fde2001-10-13 06:41:08 +0000316 // Check to make sure that "Ty" is an integral type, and that our
317 // value will fit into the specified type...
318 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000319 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
320 ThrowException("Signed integral constant '" +
321 itostr(D.ConstPool64) + "' is invalid for type '" +
322 Ty->getDescription() + "'!");
323 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000324
325 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000326 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
327 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000328 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
329 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000330 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000331 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000332 }
333 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000334 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000335 }
336
Chris Lattner2079fde2001-10-13 06:41:08 +0000337 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000338 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000339 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000340 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000341
342 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000343 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000344 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000345 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000346
Chris Lattnerd78700d2002-08-16 21:14:40 +0000347 case ValID::ConstantVal: // Fully resolved constant?
348 if (D.ConstantValue->getType() != Ty)
349 ThrowException("Constant expression type different from required type!");
350 return D.ConstantValue;
351
Chris Lattner30c89792001-09-07 16:35:17 +0000352 default:
353 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000354 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000355 } // End of switch
356
Chris Lattner2079fde2001-10-13 06:41:08 +0000357 assert(0 && "Unhandled case!");
358 return 0;
359}
360
361
362// getVal - This function is identical to getValNonImprovising, except that if a
363// value is not already defined, it "improvises" by creating a placeholder var
364// that looks and acts just like the requested variable. When the value is
365// defined later, all uses of the placeholder variable are replaced with the
366// real thing.
367//
368static Value *getVal(const Type *Ty, const ValID &D) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000369
370 // See if the value has already been defined...
371 Value *V = getValNonImprovising(Ty, D);
372 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000373
374 // If we reached here, we referenced either a symbol that we don't know about
375 // or an id number that hasn't been read yet. We may be referencing something
376 // forward, so just create an entry to be resolved later and get to it...
377 //
Chris Lattner00950542001-06-06 20:29:01 +0000378 Value *d = 0;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000379 switch (Ty->getTypeID()) {
Chris Lattner30c89792001-09-07 16:35:17 +0000380 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000381 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000382 }
383
384 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000385 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000386 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000387 else
388 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000389 return d;
390}
391
392
393//===----------------------------------------------------------------------===//
394// Code to handle forward references in instructions
395//===----------------------------------------------------------------------===//
396//
397// This code handles the late binding needed with statements that reference
398// values not defined yet... for example, a forward branch, or the PHI node for
399// a loop body.
400//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000401// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000402// and back patchs after we are done.
403//
404
405// ResolveDefinitions - If we could not resolve some defs at parsing
406// time (forward branches, phi functions for loops, etc...) resolve the
407// defs now...
408//
Chris Lattner735f2702004-07-08 22:30:50 +0000409static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
410 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000411 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000412 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000413 E = LateResolvers.end(); LRI != E; ++LRI) {
414 ValueList &List = LRI->second;
415 while (!List.empty()) {
416 Value *V = List.back();
417 List.pop_back();
Chris Lattner00950542001-06-06 20:29:01 +0000418 ValID &DID = getValIDFromPlaceHolder(V);
419
Chris Lattner735f2702004-07-08 22:30:50 +0000420 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000421 if (TheRealValue) {
422 V->replaceAllUsesWith(TheRealValue);
423 delete V;
424 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000425 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000426 // resolver table
427 InsertValue(V, *FutureLateResolvers);
428 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000429 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000430 ThrowException("Reference to an invalid definition: '" +DID.getName()+
431 "' of type '" + V->getType()->getDescription() + "'",
432 getLineNumFromPlaceHolder(V));
433 else
434 ThrowException("Reference to an invalid definition: #" +
435 itostr(DID.Num) + " of type '" +
436 V->getType()->getDescription() + "'",
437 getLineNumFromPlaceHolder(V));
438 }
Chris Lattner00950542001-06-06 20:29:01 +0000439 }
440 }
441
442 LateResolvers.clear();
443}
444
Chris Lattner4a42e902001-10-22 05:56:09 +0000445// ResolveTypeTo - A brand new type was just declared. This means that (if
446// name is not null) things referencing Name can be resolved. Otherwise, things
447// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000448//
Chris Lattner4a42e902001-10-22 05:56:09 +0000449static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000450 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000451 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000452
Chris Lattner4a42e902001-10-22 05:56:09 +0000453 ValID D;
454 if (Name) D = ValID::create(Name);
455 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000456
Chris Lattner9232b992003-04-22 18:42:41 +0000457 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000458 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000459
Chris Lattner9232b992003-04-22 18:42:41 +0000460 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000461 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000462 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000463 LateResolver.erase(I);
464 }
465}
466
467// ResolveTypes - At this point, all types should be resolved. Any that aren't
468// are errors.
469//
Chris Lattner9232b992003-04-22 18:42:41 +0000470static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000471 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000472 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000473
474 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000475 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000476 else
Chris Lattner82269592001-10-22 06:01:08 +0000477 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000478 }
479}
480
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000481
Chris Lattner8ab406d2004-07-13 07:59:27 +0000482static void setValueNameInternal(Value *V, const std::string &Name,
483 SymbolTable &ST) {
484 if (V->getType() == Type::VoidTy)
485 ThrowException("Can't assign name '" + Name + "' to value with void type!");
486
487 // Set the name
488 V->setName(Name, &ST);
489
490 // If we're in function scope
491 if (inFunctionScope()) {
492 // Look up the symbol in the Function's local symboltable
493 Value *Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
494
495 // If it already exists, bail out. We have to recheck this, because there
496 // may be entries inserted into the current basic block (which has not yet
497 // been inserted into the function) which collide.
498 if (Existing) {
499 ThrowException("Redefinition of value named '" + Name + "' in the '" +
500 V->getType()->getDescription() + "' type plane!");
501 } else {
502 // otherwise, since it doesn't exist, insert it.
503 CurFun.LocalSymtab.insert(V);
504 }
505 }
506}
507
Chris Lattner1781aca2001-09-18 04:00:54 +0000508// setValueName - Set the specified value to the name given. The name may be
509// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000510// assumed to be a malloc'd string buffer, and is free'd by this function.
511//
512static void setValueName(Value *V, char *NameStr) {
513 if (NameStr) {
514 std::string Name(NameStr); // Copy string
515 free(NameStr); // Free old string
516
517 assert(inFunctionScope() && "Must be in function scope!");
518 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
519 if (ST.lookup(V->getType(), Name))
520 ThrowException("Redefinition of value named '" + Name + "' in the '" +
521 V->getType()->getDescription() + "' type plane!");
522
523 setValueNameInternal(V, Name, ST);
524 }
525}
526
527// setValueNameMergingDuplicates - Set the specified value to the name given.
528// The name may be null potentially, in which case this is a noop. The string
529// passed in is assumed to be a malloc'd string buffer, and is free'd by this
530// function.
Chris Lattner1781aca2001-09-18 04:00:54 +0000531//
Chris Lattnerb7474512001-10-03 15:39:04 +0000532// This function returns true if the value has already been defined, but is
533// allowed to be redefined in the specified context. If the name is a new name
534// for the typeplane, false is returned.
535//
Chris Lattner8ab406d2004-07-13 07:59:27 +0000536static bool setValueNameMergingDuplicates(Value *V, char *NameStr) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000537 if (NameStr == 0) return false;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000538
Chris Lattner9232b992003-04-22 18:42:41 +0000539 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000540 free(NameStr); // Free old string
541
Chris Lattner8ab406d2004-07-13 07:59:27 +0000542 // FIXME: If we eliminated the function constant pool (which we should), this
543 // would just unconditionally look at the module symtab.
Chris Lattner6e6026b2002-11-20 18:36:02 +0000544 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000545 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000546 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000547
Reid Spencer75719bf2004-05-26 21:48:31 +0000548 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000549 if (Existing) { // Inserting a name that is already defined???
Chris Lattner8ab406d2004-07-13 07:59:27 +0000550 // We are a simple redefinition of a value, check to see if it is defined
551 // the same as the old one...
552 if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000553 // We are allowed to redefine a global variable in two circumstances:
554 // 1. If at least one of the globals is uninitialized or
555 // 2. If both initializers have the same value.
556 //
Chris Lattner89219832001-10-03 19:35:04 +0000557 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000558 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
559 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000560
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000561 // Make sure the existing global version gets the initializer! Make
562 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000563 if (GV->hasInitializer() && !EGV->hasInitializer())
564 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000565 if (GV->isConstant())
566 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000567 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000568
Chris Lattner2079fde2001-10-13 06:41:08 +0000569 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000570 return true; // They are equivalent!
571 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000572 }
Chris Lattner8ab406d2004-07-13 07:59:27 +0000573 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
574 if (C == V) return true; // Constants are equal to themselves
Chris Lattner9636a912001-10-01 16:18:37 +0000575 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000576
Chris Lattner2079fde2001-10-13 06:41:08 +0000577 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000578 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000579 }
Chris Lattner8ab406d2004-07-13 07:59:27 +0000580
581 setValueNameInternal(V, Name, ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000582 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000583}
584
Chris Lattner8ab406d2004-07-13 07:59:27 +0000585
586
Reid Spencer75719bf2004-05-26 21:48:31 +0000587// setTypeName - Set the specified type to the name given. The name may be
588// null potentially, in which case this is a noop. The string passed in is
589// assumed to be a malloc'd string buffer, and is freed by this function.
590//
591// This function returns true if the type has already been defined, but is
592// allowed to be redefined in the specified context. If the name is a new name
593// for the type plane, it is inserted and false is returned.
594static bool setTypeName(Type *T, char *NameStr) {
595 if (NameStr == 0) return false;
596
597 std::string Name(NameStr); // Copy string
598 free(NameStr); // Free old string
599
600 // We don't allow assigning names to void type
601 if (T == Type::VoidTy)
602 ThrowException("Can't assign name '" + Name + "' to the null type!");
603
604 SymbolTable &ST = inFunctionScope() ?
605 CurFun.CurrentFunction->getSymbolTable() :
606 CurModule.CurrentModule->getSymbolTable();
607
608 Type *Existing = ST.lookupType(Name);
609
610 if (Existing) { // Inserting a name that is already defined???
611 // There is only one case where this is allowed: when we are refining an
612 // opaque type. In this case, Existing will be an opaque type.
613 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
614 // We ARE replacing an opaque type!
615 ((OpaqueType*)OpTy)->refineAbstractTypeTo(T);
616 return true;
617 }
618
619 // Otherwise, this is an attempt to redefine a type. That's okay if
620 // the redefinition is identical to the original. This will be so if
621 // Existing and T point to the same Type object. In this one case we
622 // allow the equivalent redefinition.
623 if (Existing == T) return true; // Yes, it's equal.
624
625 // Any other kind of (non-equivalent) redefinition is an error.
626 ThrowException("Redefinition of type named '" + Name + "' in the '" +
627 T->getDescription() + "' type plane!");
628 }
629
630 // Okay, its a newly named type. Set its name.
Chris Lattner8ca2dc02004-07-09 16:43:55 +0000631 if (!Name.empty()) ST.insert(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000632
633 // If we're in function scope
634 if (inFunctionScope()) {
635 // Look up the symbol in the function's local symboltable
636 Existing = CurFun.LocalSymtab.lookupType(Name);
637
638 // If it already exists
639 if (Existing) {
640 // Bail
641 ThrowException("Redefinition of type named '" + Name + "' in the '" +
642 T->getDescription() + "' type plane in function scope!");
643
644 // otherwise, since it doesn't exist
645 } else {
646 // Insert it.
647 CurFun.LocalSymtab.insert(Name,T);
648 }
649 }
650 return false;
651}
Chris Lattner8896eda2001-07-09 19:38:36 +0000652
Chris Lattner30c89792001-09-07 16:35:17 +0000653//===----------------------------------------------------------------------===//
654// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000655//
Chris Lattner8896eda2001-07-09 19:38:36 +0000656
Chris Lattner3b073862004-02-09 03:19:29 +0000657// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000658//
659static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000660 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
661}
662
663namespace {
664 struct UpRefRecord {
665 // NestingLevel - The number of nesting levels that need to be popped before
666 // this type is resolved.
667 unsigned NestingLevel;
668
669 // LastContainedTy - This is the type at the current binding level for the
670 // type. Every time we reduce the nesting level, this gets updated.
671 const Type *LastContainedTy;
672
673 // UpRefTy - This is the actual opaque type that the upreference is
674 // represented with.
675 OpaqueType *UpRefTy;
676
677 UpRefRecord(unsigned NL, OpaqueType *URTy)
678 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
679 };
Chris Lattner30c89792001-09-07 16:35:17 +0000680}
Chris Lattner698b56e2001-07-20 19:15:08 +0000681
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000682// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000683static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000684
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000685/// HandleUpRefs - Every time we finish a new layer of types, this function is
686/// called. It loops through the UpRefs vector, which is a list of the
687/// currently active types. For each type, if the up reference is contained in
688/// the newly completed type, we decrement the level count. When the level
689/// count reaches zero, the upreferenced type is the type that is passed in:
690/// thus we can complete the cycle.
691///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000692static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000693 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000694 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000695 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000696 "' newly formed. Resolving upreferences.\n" <<
697 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000698
699 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
700 // to zero), we resolve them all together before we resolve them to Ty. At
701 // the end of the loop, if there is anything to resolve to Ty, it will be in
702 // this variable.
703 OpaqueType *TypeToResolve = 0;
704
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000705 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000706 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000707 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000708 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000709 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
710 // Decrement level of upreference
711 unsigned Level = --UpRefs[i].NestingLevel;
712 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000713 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000714 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000715 if (!TypeToResolve) {
716 TypeToResolve = UpRefs[i].UpRefTy;
717 } else {
718 UR_OUT(" * Resolving upreference for "
719 << UpRefs[i].second->getDescription() << "\n";
720 std::string OldName = UpRefs[i].UpRefTy->getDescription());
721 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
722 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
723 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
724 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000725 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
726 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000727 }
728 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000729 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000730
Chris Lattner026a8ce2004-02-09 18:53:54 +0000731 if (TypeToResolve) {
732 UR_OUT(" * Resolving upreference for "
733 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000734 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000735 TypeToResolve->refineAbstractTypeTo(Ty);
736 }
737
Chris Lattner8896eda2001-07-09 19:38:36 +0000738 return Ty;
739}
740
Chris Lattner30c89792001-09-07 16:35:17 +0000741
Chris Lattner00950542001-06-06 20:29:01 +0000742//===----------------------------------------------------------------------===//
743// RunVMAsmParser - Define an interface to this parser
744//===----------------------------------------------------------------------===//
745//
Chris Lattner9232b992003-04-22 18:42:41 +0000746Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000747 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000748 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000749 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000750 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000751
Chris Lattner75f20532003-04-22 18:02:52 +0000752 // Allocate a new module to read
753 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000754
755 try {
756 yyparse(); // Parse the file.
757 } catch (...) {
758 // Clear the symbol table so it doesn't complain when it
759 // gets destructed
760 CurFun.LocalSymtab.clear();
761 throw;
762 }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000763
Chris Lattner00950542001-06-06 20:29:01 +0000764 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000765
766 // Check to see if they called va_start but not va_arg..
767 if (!ObsoleteVarArgs)
768 if (Function *F = Result->getNamedFunction("llvm.va_start"))
769 if (F->asize() == 1) {
770 std::cerr << "WARNING: this file uses obsolete features. "
771 << "Assemble and disassemble to update it.\n";
772 ObsoleteVarArgs = true;
773 }
774
775
776 if (ObsoleteVarArgs) {
777 // If the user is making use of obsolete varargs intrinsics, adjust them for
778 // the user.
779 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
780 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
781
782 const Type *RetTy = F->getFunctionType()->getParamType(0);
783 RetTy = cast<PointerType>(RetTy)->getElementType();
784 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
785
786 while (!F->use_empty()) {
787 CallInst *CI = cast<CallInst>(F->use_back());
788 Value *V = new CallInst(NF, "", CI);
789 new StoreInst(V, CI->getOperand(1), CI);
790 CI->getParent()->getInstList().erase(CI);
791 }
792 Result->getFunctionList().erase(F);
793 }
794
795 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
796 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
797 const Type *ArgTy = F->getFunctionType()->getParamType(0);
798 ArgTy = cast<PointerType>(ArgTy)->getElementType();
799 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
800 ArgTy, 0);
801
802 while (!F->use_empty()) {
803 CallInst *CI = cast<CallInst>(F->use_back());
804 Value *V = new LoadInst(CI->getOperand(1), "", CI);
805 new CallInst(NF, V, "", CI);
806 CI->getParent()->getInstList().erase(CI);
807 }
808 Result->getFunctionList().erase(F);
809 }
810
811 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
812 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
813 const Type *ArgTy = F->getFunctionType()->getParamType(0);
814 ArgTy = cast<PointerType>(ArgTy)->getElementType();
815 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
816 ArgTy, 0);
817
818 while (!F->use_empty()) {
819 CallInst *CI = cast<CallInst>(F->use_back());
820 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
821 new StoreInst(V, CI->getOperand(1), CI);
822 CI->getParent()->getInstList().erase(CI);
823 }
824 Result->getFunctionList().erase(F);
825 }
826 }
827
Chris Lattner00950542001-06-06 20:29:01 +0000828 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
829 ParserResult = 0;
830
831 return Result;
832}
833
Brian Gaeked0fde302003-11-11 22:41:34 +0000834} // End llvm namespace
835
836using namespace llvm;
837
Chris Lattner00950542001-06-06 20:29:01 +0000838%}
839
840%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000841 llvm::Module *ModuleVal;
842 llvm::Function *FunctionVal;
843 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
844 llvm::BasicBlock *BasicBlockVal;
845 llvm::TerminatorInst *TermInstVal;
846 llvm::Instruction *InstVal;
847 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000848
Brian Gaeked0fde302003-11-11 22:41:34 +0000849 const llvm::Type *PrimType;
850 llvm::PATypeHolder *TypeVal;
851 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000852
Brian Gaeked0fde302003-11-11 22:41:34 +0000853 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
854 std::vector<llvm::Value*> *ValueList;
855 std::list<llvm::PATypeHolder> *TypeList;
856 std::list<std::pair<llvm::Value*,
857 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
858 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
859 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000860
Brian Gaeked0fde302003-11-11 22:41:34 +0000861 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000862 int64_t SInt64Val;
863 uint64_t UInt64Val;
864 int SIntVal;
865 unsigned UIntVal;
866 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000867 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000868
Chris Lattner30c89792001-09-07 16:35:17 +0000869 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000870 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000871
Brian Gaeked0fde302003-11-11 22:41:34 +0000872 llvm::Instruction::BinaryOps BinaryOpVal;
873 llvm::Instruction::TermOps TermOpVal;
874 llvm::Instruction::MemoryOps MemOpVal;
875 llvm::Instruction::OtherOps OtherOpVal;
876 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000877}
878
Chris Lattner79df7c02002-03-26 18:01:55 +0000879%type <ModuleVal> Module FunctionList
880%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000881%type <BasicBlockVal> BasicBlock InstructionList
882%type <TermInstVal> BBTerminatorInst
883%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000884%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000885%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000886%type <ArgList> ArgList ArgListH
887%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000888%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000889%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000890%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000891%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000892%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000893%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000894%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000895%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000896%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000897
Chris Lattner2079fde2001-10-13 06:41:08 +0000898// ValueRef - Unresolved reference to a definition or BB
899%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000900%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000901// Tokens and types for handling constant integer values
902//
903// ESINT64VAL - A negative number within long long range
904%token <SInt64Val> ESINT64VAL
905
906// EUINT64VAL - A positive number within uns. long long range
907%token <UInt64Val> EUINT64VAL
908%type <SInt64Val> EINT64VAL
909
910%token <SIntVal> SINTVAL // Signed 32 bit ints...
911%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
912%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000913%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000914
915// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000916%type <TypeVal> Types TypesV UpRTypes UpRTypesV
917%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000918%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
919%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000920
Chris Lattner6c23f572003-08-22 05:42:10 +0000921%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
922%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000923
924
Chris Lattner91ef4602004-03-31 03:49:47 +0000925%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000926%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000927%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000928%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000929
930// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000931%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000932
Chris Lattner00950542001-06-06 20:29:01 +0000933// Binary Operators
934%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000935%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000936%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000937%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000938
939// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000940%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000941
Chris Lattner027dcc52001-07-08 21:10:27 +0000942// Other Operators
943%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000944%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000945%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000946
Chris Lattner00950542001-06-06 20:29:01 +0000947%start Module
948%%
949
950// Handle constant integer size restriction and conversion...
951//
Chris Lattner51727be2002-06-04 21:58:56 +0000952INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000953INTVAL : UINTVAL {
954 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
955 ThrowException("Value too large for type!");
956 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000957};
Chris Lattner00950542001-06-06 20:29:01 +0000958
959
Chris Lattner51727be2002-06-04 21:58:56 +0000960EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000961EINT64VAL : EUINT64VAL {
962 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
963 ThrowException("Value too large for type!");
964 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000965};
Chris Lattner00950542001-06-06 20:29:01 +0000966
Chris Lattner00950542001-06-06 20:29:01 +0000967// Operations that are notably excluded from this list include:
968// RET, BR, & SWITCH because they end basic blocks and are treated specially.
969//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000970ArithmeticOps: ADD | SUB | MUL | DIV | REM;
971LogicalOps : AND | OR | XOR;
972SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
973BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
974
Chris Lattner51727be2002-06-04 21:58:56 +0000975ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000976
Chris Lattnere98dda62001-07-14 06:10:16 +0000977// These are some types that allow classification if we only want a particular
978// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000979SIntType : LONG | INT | SHORT | SBYTE;
980UIntType : ULONG | UINT | USHORT | UBYTE;
981IntType : SIntType | UIntType;
982FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000983
Chris Lattnere98dda62001-07-14 06:10:16 +0000984// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000985OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000986 $$ = $1;
987 }
988 | /*empty*/ {
989 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000990 };
Chris Lattner00950542001-06-06 20:29:01 +0000991
Chris Lattner4ad02e72003-04-16 20:28:45 +0000992OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
993 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000994 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000995 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
996 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000997
998//===----------------------------------------------------------------------===//
999// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001000// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001001// access to it, a user must explicitly use TypesV.
1002//
1003
1004// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001005TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1006UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001007
1008Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001009 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001010 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1011 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001012 };
Chris Lattner30c89792001-09-07 16:35:17 +00001013
1014
1015// Derived types are added later...
1016//
Chris Lattner51727be2002-06-04 21:58:56 +00001017PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1018PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001019UpRTypes : OPAQUE {
1020 $$ = new PATypeHolder(OpaqueType::get());
1021 }
1022 | PrimType {
1023 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001024 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001025UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001026 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001027};
Chris Lattner30c89792001-09-07 16:35:17 +00001028
Chris Lattner30c89792001-09-07 16:35:17 +00001029// Include derived types in the Types production.
1030//
1031UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001032 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001033 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001034 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001035 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001036 UR_OUT("New Upreference!\n");
1037 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001038 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001039 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001040 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001041 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001042 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1043 if (isVarArg) Params.pop_back();
1044
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001045 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001046 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001047 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001048 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001049 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001050 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001051 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001052 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001053 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001054 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001055 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001056 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001057
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001058 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001059 delete $2;
1060 }
1061 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001062 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001063 }
1064 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001065 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001066 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001067 };
Chris Lattner30c89792001-09-07 16:35:17 +00001068
Chris Lattner7e708292002-06-25 16:13:24 +00001069// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001070// declaration type lists
1071//
1072TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001073 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001074 $$->push_back(*$1); delete $1;
1075 }
1076 | TypeListI ',' UpRTypes {
1077 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001078 };
Chris Lattner30c89792001-09-07 16:35:17 +00001079
Chris Lattner7e708292002-06-25 16:13:24 +00001080// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001081ArgTypeListI : TypeListI
1082 | TypeListI ',' DOTDOTDOT {
1083 ($$=$1)->push_back(Type::VoidTy);
1084 }
1085 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001086 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001087 }
1088 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001089 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001090 };
Chris Lattner30c89792001-09-07 16:35:17 +00001091
Chris Lattnere98dda62001-07-14 06:10:16 +00001092// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001093// production is used ONLY to represent constants that show up AFTER a 'const',
1094// 'constant' or 'global' token at global scope. Constants that can be inlined
1095// into other expressions (such as integers and constexprs) are handled by the
1096// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001097//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001098ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001099 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001100 if (ATy == 0)
1101 ThrowException("Cannot make array constant with type: '" +
1102 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001103 const Type *ETy = ATy->getElementType();
1104 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001105
Chris Lattner30c89792001-09-07 16:35:17 +00001106 // Verify that we have the correct size...
1107 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001108 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001109 utostr($3->size()) + " arguments, but has size of " +
1110 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001111
Chris Lattner30c89792001-09-07 16:35:17 +00001112 // Verify all elements are correct type!
1113 for (unsigned i = 0; i < $3->size(); i++) {
1114 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001115 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001116 ETy->getDescription() +"' as required!\nIt is of type '"+
1117 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001118 }
1119
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001120 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001121 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001122 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001123 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001124 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001125 if (ATy == 0)
1126 ThrowException("Cannot make array constant with type: '" +
1127 (*$1)->getDescription() + "'!");
1128
1129 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001130 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001131 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001132 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001133 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001134 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001135 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001136 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001137 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001138 if (ATy == 0)
1139 ThrowException("Cannot make array constant with type: '" +
1140 (*$1)->getDescription() + "'!");
1141
Chris Lattner30c89792001-09-07 16:35:17 +00001142 int NumElements = ATy->getNumElements();
1143 const Type *ETy = ATy->getElementType();
1144 char *EndStr = UnEscapeLexed($3, true);
1145 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001146 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001147 itostr((int)(EndStr-$3)) +
1148 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001149 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001150 if (ETy == Type::SByteTy) {
1151 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001152 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001153 } else if (ETy == Type::UByteTy) {
1154 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001155 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001156 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001157 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001158 ThrowException("Cannot build string arrays of non byte sized elements!");
1159 }
Chris Lattner30c89792001-09-07 16:35:17 +00001160 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001161 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001162 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001163 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001164 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001165 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001166 if (STy == 0)
1167 ThrowException("Cannot make struct constant with type: '" +
1168 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001169
Chris Lattnerc6212f12003-05-21 16:06:56 +00001170 if ($3->size() != STy->getNumContainedTypes())
1171 ThrowException("Illegal number of initializers for structure type!");
1172
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001173 // Check to ensure that constants are compatible with the type initializer!
1174 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001175 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001176 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001177 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001178 "' for element #" + utostr(i) +
1179 " of structure initializer!");
1180
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001181 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001182 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001183 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001184 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001185 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001186 if (STy == 0)
1187 ThrowException("Cannot make struct constant with type: '" +
1188 (*$1)->getDescription() + "'!");
1189
1190 if (STy->getNumContainedTypes() != 0)
1191 ThrowException("Illegal number of initializers for structure type!");
1192
1193 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1194 delete $1;
1195 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001196 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001197 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001198 if (PTy == 0)
1199 ThrowException("Cannot make null pointer constant with type: '" +
1200 (*$1)->getDescription() + "'!");
1201
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001202 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001203 delete $1;
1204 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001205 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001206 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001207 if (Ty == 0)
1208 ThrowException("Global const reference must be a pointer type!");
1209
Chris Lattner3101c252002-08-15 17:58:33 +00001210 // ConstExprs can exist in the body of a function, thus creating
1211 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1212 // the context of a function, getValNonImprovising will search the functions
1213 // symbol table instead of the module symbol table for the global symbol,
1214 // which throws things all off. To get around this, we just tell
1215 // getValNonImprovising that we are at global scope here.
1216 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001217 Function *SavedCurFn = CurFun.CurrentFunction;
1218 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001219
Chris Lattner2079fde2001-10-13 06:41:08 +00001220 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001221
Chris Lattner394f2eb2003-10-16 20:12:13 +00001222 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001223
Chris Lattner2079fde2001-10-13 06:41:08 +00001224 // If this is an initializer for a constant pointer, which is referencing a
1225 // (currently) undefined variable, create a stub now that shall be replaced
1226 // in the future with the right type of variable.
1227 //
1228 if (V == 0) {
1229 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1230 const PointerType *PT = cast<PointerType>(Ty);
1231
1232 // First check to see if the forward references value is already created!
1233 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001234 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001235
1236 if (I != CurModule.GlobalRefs.end()) {
1237 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001238 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001239 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001240 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001241 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001242 false,
1243 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001244 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001245 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001246
1247 // Must temporarily push this value into the module table...
1248 CurModule.CurrentModule->getGlobalList().push_back(GV);
1249 V = GV;
1250 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001251 }
1252
Chris Lattner2079fde2001-10-13 06:41:08 +00001253 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001254 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001255 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001256 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001257 | Types ConstExpr {
1258 if ($1->get() != $2->getType())
1259 ThrowException("Mismatched types for constant expression!");
1260 $$ = $2;
1261 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001262 }
1263 | Types ZEROINITIALIZER {
1264 $$ = Constant::getNullValue($1->get());
1265 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001266 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001267
Chris Lattnere43f40b2002-10-09 00:25:32 +00001268ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001269 if (!ConstantSInt::isValueValidForType($1, $2))
1270 ThrowException("Constant value doesn't fit in type!");
1271 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001272 }
1273 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001274 if (!ConstantUInt::isValueValidForType($1, $2))
1275 ThrowException("Constant value doesn't fit in type!");
1276 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001277 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001278 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001279 $$ = ConstantBool::True;
1280 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001281 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001282 $$ = ConstantBool::False;
1283 }
1284 | FPType FPVAL { // Float & Double constants
1285 $$ = ConstantFP::get($1, $2);
1286 };
1287
Chris Lattner00950542001-06-06 20:29:01 +00001288
Chris Lattnerd78700d2002-08-16 21:14:40 +00001289ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001290 if (!$3->getType()->isFirstClassType())
1291 ThrowException("cast constant expression from a non-primitive type: '" +
1292 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001293 if (!$5->get()->isFirstClassType())
1294 ThrowException("cast constant expression to a non-primitive type: '" +
1295 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001296 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001297 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001298 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001299 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1300 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001301 ThrowException("GetElementPtr requires a pointer operand!");
1302
Chris Lattner830b6f92004-04-05 01:30:04 +00001303 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1304 // indices to uint struct indices for compatibility.
1305 generic_gep_type_iterator<std::vector<Value*>::iterator>
1306 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1307 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1308 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1309 if (isa<StructType>(*GTI)) // Only change struct indices
1310 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1311 if (CUI->getType() == Type::UByteTy)
1312 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1313
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001314 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001315 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001316 if (!IdxTy)
1317 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001318
Chris Lattner9232b992003-04-22 18:42:41 +00001319 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001320 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1321 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001322 IdxVec.push_back(C);
1323 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001324 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001325
Chris Lattnerd78700d2002-08-16 21:14:40 +00001326 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001327
Chris Lattnerd78700d2002-08-16 21:14:40 +00001328 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001329 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001330 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1331 if ($3->getType() != Type::BoolTy)
1332 ThrowException("Select condition must be of boolean type!");
1333 if ($5->getType() != $7->getType())
1334 ThrowException("Select operand types must match!");
1335 $$ = ConstantExpr::getSelect($3, $5, $7);
1336 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001337 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001338 if ($3->getType() != $5->getType())
1339 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001340 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001341 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001342 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001343 if ($5->getType() != Type::UByteTy)
1344 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001345 if (!$3->getType()->isInteger())
1346 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001347 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001348 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001349
1350
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001351// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001352ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001353 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001354 }
1355 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001356 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001357 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001358 };
Chris Lattner00950542001-06-06 20:29:01 +00001359
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001360
Chris Lattner1781aca2001-09-18 04:00:54 +00001361// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001362GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001363
Chris Lattner00950542001-06-06 20:29:01 +00001364
Chris Lattner0e73ce62002-05-02 19:11:13 +00001365//===----------------------------------------------------------------------===//
1366// Rules to match Modules
1367//===----------------------------------------------------------------------===//
1368
1369// Module rule: Capture the result of parsing the whole file into a result
1370// variable...
1371//
1372Module : FunctionList {
1373 $$ = ParserResult = $1;
1374 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001375};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001376
Chris Lattner7e708292002-06-25 16:13:24 +00001377// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001378//
1379FunctionList : FunctionList Function {
1380 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001381 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001382 }
1383 | FunctionList FunctionProto {
1384 $$ = $1;
1385 }
1386 | FunctionList IMPLEMENTATION {
1387 $$ = $1;
1388 }
1389 | ConstPool {
1390 $$ = CurModule.CurrentModule;
1391 // Resolve circular types before we parse the body of the module
1392 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001393 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001394
Chris Lattnere98dda62001-07-14 06:10:16 +00001395// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001396ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001397 // FIXME: THIS SHOULD REALLY BE ELIMINATED. It is totally unneeded.
1398 if (!setValueNameMergingDuplicates($4, $2))
Chris Lattneradf99702003-03-03 23:28:55 +00001399 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001400 }
Chris Lattner30c89792001-09-07 16:35:17 +00001401 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001402 // Eagerly resolve types. This is not an optimization, this is a
1403 // requirement that is due to the fact that we could have this:
1404 //
1405 // %list = type { %list * }
1406 // %list = type { %list * } ; repeated type decl
1407 //
1408 // If types are not resolved eagerly, then the two types will not be
1409 // determined to be the same type!
1410 //
1411 ResolveTypeTo($2, $4->get());
1412
Chris Lattner1781aca2001-09-18 04:00:54 +00001413 // TODO: FIXME when Type are not const
Reid Spencer75719bf2004-05-26 21:48:31 +00001414 if (!setTypeName(const_cast<Type*>($4->get()), $2)) {
Chris Lattnerb7474512001-10-03 15:39:04 +00001415 // If this is not a redefinition of a type...
1416 if (!$2) {
1417 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001418 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001419 }
Chris Lattner30c89792001-09-07 16:35:17 +00001420 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001421
1422 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001423 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001424 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001425 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001426 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001427 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001428 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001429 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001430 if (Initializer == 0)
1431 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001432
Chris Lattnerdda71962001-11-26 18:54:16 +00001433 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001434 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001435 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001436 int Slot = InsertValue(GV, CurModule.Values);
1437
1438 if (Slot != -1) {
1439 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1440 } else {
1441 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1442 (char*)GV->getName().c_str()));
1443 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001444 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001445 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001446 | ConstPool OptAssign EXTERNAL GlobalType Types {
1447 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001448 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001449 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001450 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001451 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001452 int Slot = InsertValue(GV, CurModule.Values);
1453
1454 if (Slot != -1) {
1455 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1456 } else {
1457 assert(GV->hasName() && "Not named and not numbered!?");
1458 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1459 (char*)GV->getName().c_str()));
1460 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001461 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001462 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001463 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001464 | ConstPool TARGET TargetDefinition {
1465 }
Chris Lattner00950542001-06-06 20:29:01 +00001466 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001467 };
Chris Lattner00950542001-06-06 20:29:01 +00001468
1469
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001470
1471BigOrLittle : BIG { $$ = Module::BigEndian; };
1472BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1473
1474TargetDefinition : ENDIAN '=' BigOrLittle {
1475 CurModule.CurrentModule->setEndianness($3);
1476 }
1477 | POINTERSIZE '=' EUINT64VAL {
1478 if ($3 == 32)
1479 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1480 else if ($3 == 64)
1481 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1482 else
1483 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1484 };
1485
1486
Chris Lattner00950542001-06-06 20:29:01 +00001487//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001488// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001489//===----------------------------------------------------------------------===//
1490
Chris Lattner6c23f572003-08-22 05:42:10 +00001491Name : VAR_ID | STRINGCONSTANT;
1492OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001493
Chris Lattner6c23f572003-08-22 05:42:10 +00001494ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001495 if (*$1 == Type::VoidTy)
1496 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001497 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001498};
Chris Lattner00950542001-06-06 20:29:01 +00001499
Chris Lattner69da5cf2002-10-13 20:57:00 +00001500ArgListH : ArgListH ',' ArgVal {
1501 $$ = $1;
1502 $1->push_back(*$3);
1503 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001504 }
1505 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001506 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001507 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001508 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001509 };
Chris Lattner00950542001-06-06 20:29:01 +00001510
1511ArgList : ArgListH {
1512 $$ = $1;
1513 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001514 | ArgListH ',' DOTDOTDOT {
1515 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001516 $$->push_back(std::pair<PATypeHolder*,
1517 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001518 }
1519 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001520 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1521 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001522 }
Chris Lattner00950542001-06-06 20:29:01 +00001523 | /* empty */ {
1524 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001525 };
Chris Lattner00950542001-06-06 20:29:01 +00001526
Chris Lattner6c23f572003-08-22 05:42:10 +00001527FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001528 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001529 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001530
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001531 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1532 ThrowException("LLVM functions cannot return aggregate types!");
1533
Chris Lattner9232b992003-04-22 18:42:41 +00001534 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001535 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001536 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001537 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001538 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001539 }
Chris Lattner00950542001-06-06 20:29:01 +00001540
Chris Lattner2079fde2001-10-13 06:41:08 +00001541 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1542 if (isVarArg) ParamTypeList.pop_back();
1543
Chris Lattner1f862af2003-04-16 18:13:57 +00001544 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001545 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001546 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001547
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001548 Function *Fn = 0;
1549 // Is the function already in symtab?
1550 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1551 // Yes it is. If this is the case, either we need to be a forward decl,
1552 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001553 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001554 ThrowException("Redefinition of function '" + FunctionName + "'!");
1555
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001556 // Make sure to strip off any argument names so we can't get conflicts...
1557 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1558 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001559
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001560 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001561 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1562 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001563 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001564 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001565 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001566 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001567
Chris Lattner394f2eb2003-10-16 20:12:13 +00001568 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001569
Chris Lattner7e708292002-06-25 16:13:24 +00001570 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001571 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001572 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001573 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001574 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001575 delete $4->back().first;
1576 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001577 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001578 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001579 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001580 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001581 delete I->first; // Delete the typeholder...
1582
Chris Lattner8ab406d2004-07-13 07:59:27 +00001583 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001584 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001585 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001586
Chris Lattner1f862af2003-04-16 18:13:57 +00001587 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001588 }
Chris Lattner51727be2002-06-04 21:58:56 +00001589};
Chris Lattner00950542001-06-06 20:29:01 +00001590
Chris Lattner9b02cc32002-05-03 18:23:48 +00001591BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1592
Chris Lattner4ad02e72003-04-16 20:28:45 +00001593FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001594 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001595
Chris Lattner4ad02e72003-04-16 20:28:45 +00001596 // Make sure that we keep track of the linkage type even if there was a
1597 // previous "declare".
1598 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001599
Chris Lattner7e708292002-06-25 16:13:24 +00001600 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001601 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001602};
Chris Lattner00950542001-06-06 20:29:01 +00001603
Chris Lattner9b02cc32002-05-03 18:23:48 +00001604END : ENDTOK | '}'; // Allow end of '}' to end a function
1605
Chris Lattner79df7c02002-03-26 18:01:55 +00001606Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001607 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001608};
Chris Lattner00950542001-06-06 20:29:01 +00001609
Chris Lattner394f2eb2003-10-16 20:12:13 +00001610FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1611 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001612 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001613};
Chris Lattner00950542001-06-06 20:29:01 +00001614
1615//===----------------------------------------------------------------------===//
1616// Rules to match Basic Blocks
1617//===----------------------------------------------------------------------===//
1618
1619ConstValueRef : ESINT64VAL { // A reference to a direct constant
1620 $$ = ValID::create($1);
1621 }
1622 | EUINT64VAL {
1623 $$ = ValID::create($1);
1624 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001625 | FPVAL { // Perhaps it's an FP constant?
1626 $$ = ValID::create($1);
1627 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001628 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001629 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001630 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001631 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001632 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001633 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001634 | NULL_TOK {
1635 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001636 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001637 | ConstExpr {
1638 $$ = ValID::create($1);
1639 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001640
Chris Lattner2079fde2001-10-13 06:41:08 +00001641// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1642// another value.
1643//
1644SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001645 $$ = ValID::create($1);
1646 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001647 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001648 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001649 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001650
1651// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001652ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001653
Chris Lattner00950542001-06-06 20:29:01 +00001654
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001655// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1656// type immediately preceeds the value reference, and allows complex constant
1657// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001658ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001659 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001660 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001661
Chris Lattner00950542001-06-06 20:29:01 +00001662BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001663 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001664 }
Chris Lattner7e708292002-06-25 16:13:24 +00001665 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001666 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001667 };
Chris Lattner00950542001-06-06 20:29:01 +00001668
1669
1670// Basic blocks are terminated by branching instructions:
1671// br, br/cc, switch, ret
1672//
Chris Lattner2079fde2001-10-13 06:41:08 +00001673BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001674 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001675 InsertValue($3);
1676
1677 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001678 InsertValue($1);
1679 $$ = $1;
1680 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001681 | LABELSTR InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001682 setValueName($4, $3);
Chris Lattner2079fde2001-10-13 06:41:08 +00001683 InsertValue($4);
1684
1685 $2->getInstList().push_back($4);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001686 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001687
1688 InsertValue($2);
1689 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001690 };
Chris Lattner00950542001-06-06 20:29:01 +00001691
1692InstructionList : InstructionList Inst {
1693 $1->getInstList().push_back($2);
1694 $$ = $1;
1695 }
1696 | /* empty */ {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001697 $$ = CurBB = new BasicBlock("", CurFun.CurrentFunction);
Chris Lattner51727be2002-06-04 21:58:56 +00001698 };
Chris Lattner00950542001-06-06 20:29:01 +00001699
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001700BBTerminatorInst : RET ResolvedVal { // Return with a result...
1701 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001702 }
1703 | RET VOID { // Return with no result...
1704 $$ = new ReturnInst();
1705 }
1706 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001707 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001708 } // Conditional Branch...
1709 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001710 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1711 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001712 getVal(Type::BoolTy, $3));
1713 }
1714 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1715 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001716 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001717 $$ = S;
1718
Chris Lattner9232b992003-04-22 18:42:41 +00001719 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001720 E = $8->end();
1721 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001722 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001723 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001724 }
Chris Lattner196850c2003-05-15 21:30:00 +00001725 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1726 SwitchInst *S = new SwitchInst(getVal($2, $3),
1727 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1728 $$ = S;
1729 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001730 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001731 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001732 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001733 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001734
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001735 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1736 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001737 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001738 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001739 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001740 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1741 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001742 ParamTypes.push_back((*I)->getType());
1743 }
1744
1745 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1746 if (isVarArg) ParamTypes.pop_back();
1747
Chris Lattner79df7c02002-03-26 18:01:55 +00001748 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001749 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001750 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001751
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001752 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001753
1754 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1755 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1756
1757 if (Normal == 0 || Except == 0)
1758 ThrowException("Invoke instruction without label destinations!");
1759
1760 // Create the call node...
1761 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001762 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001763 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001764 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001765 // correctly!
1766 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001767 FunctionType::param_iterator I = Ty->param_begin();
1768 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001769 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001770
1771 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1772 if ((*ArgI)->getType() != *I)
1773 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001774 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001775
1776 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1777 ThrowException("Invalid number of parameters detected!");
1778
Chris Lattner6cdb0112001-11-26 16:54:11 +00001779 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001780 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001781 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001782 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001783 }
1784 | UNWIND {
1785 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001786 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001787
1788
Chris Lattner00950542001-06-06 20:29:01 +00001789
1790JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1791 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001792 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001793 if (V == 0)
1794 ThrowException("May only switch on a constant pool value!");
1795
Chris Lattner9232b992003-04-22 18:42:41 +00001796 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001797 }
1798 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001799 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001800 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001801
1802 if (V == 0)
1803 ThrowException("May only switch on a constant pool value!");
1804
Chris Lattner9232b992003-04-22 18:42:41 +00001805 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001806 };
Chris Lattner00950542001-06-06 20:29:01 +00001807
1808Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001809 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001810 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001811 InsertValue($2);
1812 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001813};
Chris Lattner00950542001-06-06 20:29:01 +00001814
Chris Lattnerc24d2082001-06-11 15:04:20 +00001815PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001816 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1817 $$->push_back(std::make_pair(getVal(*$1, $3),
1818 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001819 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001820 }
1821 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1822 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001823 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1824 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001825 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001826
1827
Chris Lattner30c89792001-09-07 16:35:17 +00001828ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001829 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001830 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001831 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001832 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001833 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001834 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001835 };
Chris Lattner00950542001-06-06 20:29:01 +00001836
1837// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001838ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001839
Chris Lattner4a6482b2002-09-10 19:57:26 +00001840InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1841 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1842 ThrowException("Arithmetic operator requires integer or FP operands!");
1843 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1844 if ($$ == 0)
1845 ThrowException("binary operator returned null!");
1846 delete $2;
1847 }
1848 | LogicalOps Types ValueRef ',' ValueRef {
1849 if (!(*$2)->isIntegral())
1850 ThrowException("Logical operator requires integral operands!");
1851 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1852 if ($$ == 0)
1853 ThrowException("binary operator returned null!");
1854 delete $2;
1855 }
1856 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001857 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001858 if ($$ == 0)
1859 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001860 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001861 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001862 | NOT ResolvedVal {
1863 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1864 << " Replacing with 'xor'.\n";
1865
1866 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1867 if (Ones == 0)
1868 ThrowException("Expected integral type for not instruction!");
1869
1870 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001871 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001872 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001873 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001874 | ShiftOps ResolvedVal ',' ResolvedVal {
1875 if ($4->getType() != Type::UByteTy)
1876 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001877 if (!$2->getType()->isInteger())
1878 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001879 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001880 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001881 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001882 if (!$4->get()->isFirstClassType())
1883 ThrowException("cast instruction to a non-primitive type: '" +
1884 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001885 $$ = new CastInst($2, *$4);
1886 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001887 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001888 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1889 if ($2->getType() != Type::BoolTy)
1890 ThrowException("select condition must be boolean!");
1891 if ($4->getType() != $6->getType())
1892 ThrowException("select value types should match!");
1893 $$ = new SelectInst($2, $4, $6);
1894 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001895 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001896 // FIXME: This is emulation code for an obsolete syntax. This should be
1897 // removed at some point.
1898 if (!ObsoleteVarArgs) {
1899 std::cerr << "WARNING: this file uses obsolete features. "
1900 << "Assemble and disassemble to update it.\n";
1901 ObsoleteVarArgs = true;
1902 }
1903
1904 // First, load the valist...
1905 Instruction *CurVAList = new LoadInst($2, "");
1906 CurBB->getInstList().push_back(CurVAList);
1907
1908 // Emit the vaarg instruction.
1909 $$ = new VAArgInst(CurVAList, *$4);
1910
1911 // Now we must advance the pointer and update it in memory.
1912 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1913 CurBB->getInstList().push_back(TheVANext);
1914
1915 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1916 delete $4;
1917 }
1918 | VAARG ResolvedVal ',' Types {
1919 $$ = new VAArgInst($2, *$4);
1920 delete $4;
1921 }
1922 | VANEXT ResolvedVal ',' Types {
1923 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001924 delete $4;
1925 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001926 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001927 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001928 if (!Ty->isFirstClassType())
1929 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001930 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001931 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001932 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001933 if ($2->front().first->getType() != Ty)
1934 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001935 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001936 $2->pop_front();
1937 }
1938 delete $2; // Free the list...
1939 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001940 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001941 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001942 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001943
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001944 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1945 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001946 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001947 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001948 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001949 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1950 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001951 ParamTypes.push_back((*I)->getType());
1952 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001953
1954 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1955 if (isVarArg) ParamTypes.pop_back();
1956
Chris Lattner79df7c02002-03-26 18:01:55 +00001957 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001958 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001959 }
Chris Lattner00950542001-06-06 20:29:01 +00001960
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001961 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001962
Chris Lattner8b81bf52001-07-25 22:47:46 +00001963 // Create the call node...
1964 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001965 // Make sure no arguments is a good thing!
1966 if (Ty->getNumParams() != 0)
1967 ThrowException("No arguments passed to a function that "
1968 "expects arguments!");
1969
Chris Lattner9232b992003-04-22 18:42:41 +00001970 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001971 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001972 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001973 // correctly!
1974 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001975 FunctionType::param_iterator I = Ty->param_begin();
1976 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001977 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001978
1979 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1980 if ((*ArgI)->getType() != *I)
1981 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001982 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001983
Chris Lattner8b81bf52001-07-25 22:47:46 +00001984 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001985 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001986
Chris Lattner6cdb0112001-11-26 16:54:11 +00001987 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001988 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001989 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001990 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001991 }
1992 | MemoryInst {
1993 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001994 };
Chris Lattner00950542001-06-06 20:29:01 +00001995
Chris Lattner6cdb0112001-11-26 16:54:11 +00001996
1997// IndexList - List of indices for GEP based instructions...
1998IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001999 $$ = $2;
2000 } | /* empty */ {
2001 $$ = new std::vector<Value*>();
2002 };
2003
2004OptVolatile : VOLATILE {
2005 $$ = true;
2006 }
2007 | /* empty */ {
2008 $$ = false;
2009 };
2010
Chris Lattner027dcc52001-07-08 21:10:27 +00002011
Chris Lattner00950542001-06-06 20:29:01 +00002012MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002013 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002014 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002015 }
2016 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002017 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002018 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002019 }
2020 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002021 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002022 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002023 }
2024 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002025 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002026 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002027 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002028 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002029 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002030 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002031 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002032 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002033 }
2034
Chris Lattner15c9c032003-09-08 18:20:29 +00002035 | OptVolatile LOAD Types ValueRef {
2036 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002037 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002038 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002039 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002040 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002041 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002042 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2043 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002044 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002045 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002046 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002047 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002048 if (ElTy != $3->getType())
2049 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002050 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002051
Chris Lattner79ad13802003-09-08 20:29:46 +00002052 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002053 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002054 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002055 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002056 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002057 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002058
2059 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2060 // indices to uint struct indices for compatibility.
2061 generic_gep_type_iterator<std::vector<Value*>::iterator>
2062 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2063 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2064 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2065 if (isa<StructType>(*GTI)) // Only change struct indices
2066 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2067 if (CUI->getType() == Type::UByteTy)
2068 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2069
Chris Lattner30c89792001-09-07 16:35:17 +00002070 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002071 ThrowException("Invalid getelementptr indices for type '" +
2072 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002073 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2074 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002075 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002076
Brian Gaeked0fde302003-11-11 22:41:34 +00002077
Chris Lattner00950542001-06-06 20:29:01 +00002078%%
Chris Lattner09083092001-07-08 04:57:15 +00002079int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002080 std::string where
2081 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002082 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002083 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002084 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002085 errMsg += "end-of-file.";
2086 else
Chris Lattner9232b992003-04-22 18:42:41 +00002087 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002088 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002089 return 0;
2090}