blob: 1f77b3bf2b2d9b0d13e7e3e61f9f07933d46a4a3 [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 {
Chris Lattner86427632004-07-14 06:39:48 +000034 std::string CurFilename;
35}
36using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner00950542001-06-06 20:29:01 +000038static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000039
Chris Lattner30c89792001-09-07 16:35:17 +000040// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
41// relating to upreferences in the input stream.
42//
43//#define DEBUG_UPREFS 1
44#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000045#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000046#else
47#define UR_OUT(X)
48#endif
49
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000050#define YYERROR_VERBOSE 1
51
Chris Lattner99e7ab72003-10-18 05:53:13 +000052// HACK ALERT: This variable is used to implement the automatic conversion of
53// variable argument instructions from their old to new forms. When this
54// compatiblity "Feature" is removed, this should be too.
55//
56static BasicBlock *CurBB;
57static bool ObsoleteVarArgs;
58
59
Chris Lattner7e708292002-06-25 16:13:24 +000060// This contains info used when building the body of a function. It is
61// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000062//
Chris Lattner9232b992003-04-22 18:42:41 +000063typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner735f2702004-07-08 22:30:50 +000064static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
65 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000066
67static struct PerModuleInfo {
68 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000069 std::map<const Type *, ValueList> Values; // Module level numbered definitions
70 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000071 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000072 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000073
Chris Lattnerbc721ed2004-07-13 08:28:21 +000074 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
75 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000076 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000077 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
78
Chris Lattner2079fde2001-10-13 06:41:08 +000079 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
80 // references to global values. Global values may be referenced before they
81 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000082 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000083 //
Chris Lattner9232b992003-04-22 18:42:41 +000084 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000085 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000086 GlobalRefsType GlobalRefs;
87
Chris Lattner00950542001-06-06 20:29:01 +000088 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000089 // If we could not resolve some functions at function compilation time
90 // (calls to functions before they are defined), resolve them now... Types
91 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000092 //
Chris Lattner00950542001-06-06 20:29:01 +000093 ResolveDefinitions(LateResolveValues);
94
Chris Lattner2079fde2001-10-13 06:41:08 +000095 // Check to make sure that all global value forward references have been
96 // resolved!
97 //
98 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000099 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +0000100
101 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
102 I != E; ++I) {
103 UndefinedReferences += " " + I->first.first->getDescription() + " " +
104 I->first.second.getName() + "\n";
105 }
106 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000107 }
108
Chris Lattner7e708292002-06-25 16:13:24 +0000109 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000110 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000111 CurrentModule = 0;
112 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000113
114
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000115 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000116 // is used to remove things from the forward declaration map, resolving them
117 // to the correct thing as needed.
118 //
119 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
120 // Check to see if there is a forward reference to this global variable...
121 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000122 GlobalRefsType::iterator I =
123 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000124
125 if (I != GlobalRefs.end()) {
Chris Lattnerbc207592004-03-08 06:09:57 +0000126 GlobalValue *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000127 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner38ab6bf2004-07-13 06:58:07 +0000128
129 // Replace all uses of the placeholder with the new GV
130 OldGV->replaceAllUsesWith(GV);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000131
132 // Remove OldGV from the module...
Chris Lattnerbc207592004-03-08 06:09:57 +0000133 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
134 CurrentModule->getGlobalList().erase(GVar);
135 else
136 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000137
Chris Lattner2079fde2001-10-13 06:41:08 +0000138 // Remove the map entry for the global now that it has been created...
139 GlobalRefs.erase(I);
140 }
141 }
142
Chris Lattner00950542001-06-06 20:29:01 +0000143} CurModule;
144
Chris Lattner79df7c02002-03-26 18:01:55 +0000145static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000146 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000147
Chris Lattner735f2702004-07-08 22:30:50 +0000148 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
149 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000150 std::vector<PATypeHolder> Types;
151 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000152 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000153
Chris Lattner2e2ed292004-07-14 06:28:35 +0000154 /// BBForwardRefs - When we see forward references to basic blocks, keep
155 /// track of them here.
156 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
157 std::vector<BasicBlock*> NumberedBlocks;
158 unsigned NextBBNum;
159
Chris Lattner79df7c02002-03-26 18:01:55 +0000160 inline PerFunctionInfo() {
161 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000162 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000163 }
164
Chris Lattner79df7c02002-03-26 18:01:55 +0000165 inline void FunctionStart(Function *M) {
166 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000167 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000168 }
169
Chris Lattner79df7c02002-03-26 18:01:55 +0000170 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000171 NumberedBlocks.clear();
172
173 // Any forward referenced blocks left?
174 if (!BBForwardRefs.empty())
175 ThrowException("Undefined reference to label " +
176 BBForwardRefs.begin()->second.first.getName());
177
178 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000179 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000180
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000181 // Make sure to resolve any constant expr references that might exist within
182 // the function we just declared itself.
183 ValID FID;
184 if (CurrentFunction->hasName()) {
185 FID = ValID::create((char*)CurrentFunction->getName().c_str());
186 } else {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000187 // Figure out which slot number if is...
Chris Lattner735f2702004-07-08 22:30:50 +0000188 ValueList &List = CurModule.Values[CurrentFunction->getType()];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000189 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000190 assert(i < List.size() && "Function not found!");
191 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000192 FID = ValID::create((int)i);
193 break;
194 }
195 }
196 }
197 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
198
Chris Lattner7e708292002-06-25 16:13:24 +0000199 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000200 Types.clear(); // Clear out function local types
Chris Lattner79df7c02002-03-26 18:01:55 +0000201 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000202 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000203 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000204} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000205
Chris Lattner394f2eb2003-10-16 20:12:13 +0000206static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000207
Chris Lattner00950542001-06-06 20:29:01 +0000208
209//===----------------------------------------------------------------------===//
210// Code to handle definitions of all the types
211//===----------------------------------------------------------------------===//
212
Chris Lattner735f2702004-07-08 22:30:50 +0000213static int InsertValue(Value *V,
214 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
215 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000216
217 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000218 ValueList &List = ValueTab[V->getType()];
219 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000220 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000221}
222
Chris Lattner30c89792001-09-07 16:35:17 +0000223static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000224 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000225 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000226 unsigned Num = (unsigned)D.Num;
227
228 // Module constants occupy the lowest numbered slots...
229 if (Num < CurModule.Types.size())
230 return CurModule.Types[Num];
231
232 Num -= CurModule.Types.size();
233
234 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000235 if (Num <= CurFun.Types.size())
236 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000237 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000238 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000239 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000240 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000241 SymbolTable *SymTab = 0;
Reid Spencer8ce1da72004-07-04 12:19:05 +0000242 Type *N = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000243 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000244 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000245 N = SymTab->lookupType(Name);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000246 }
Chris Lattner30c89792001-09-07 16:35:17 +0000247
248 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000249 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000250 // hasn't been added to the module...
251 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000252 SymTab = &CurModule.CurrentModule->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000253 N = SymTab->lookupType(Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000254 if (N == 0) break;
255 }
256
257 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000258 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000259 }
260 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000261 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000262 }
263
264 // If we reached here, we referenced either a symbol that we don't know about
265 // or an id number that hasn't been read yet. We may be referencing something
266 // forward, so just create an entry to be resolved later and get to it...
267 //
268 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
269
Chris Lattner9232b992003-04-22 18:42:41 +0000270 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000271 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000272
Chris Lattner9232b992003-04-22 18:42:41 +0000273 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000274 if (I != LateResolver.end()) {
275 return I->second;
276 }
Chris Lattner30c89792001-09-07 16:35:17 +0000277
Chris Lattner82269592001-10-22 06:01:08 +0000278 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000279 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000280 return Typ;
281}
282
Chris Lattner9232b992003-04-22 18:42:41 +0000283static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000284 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000285 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000286 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000287 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000288}
289
Chris Lattner2079fde2001-10-13 06:41:08 +0000290// getValNonImprovising - Look up the value specified by the provided type and
291// the provided ValID. If the value exists and has already been defined, return
292// it. Otherwise return null.
293//
294static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000295 if (isa<FunctionType>(Ty))
296 ThrowException("Functions are not values and "
297 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000298
Chris Lattner30c89792001-09-07 16:35:17 +0000299 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000300 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000301 unsigned Num = (unsigned)D.Num;
302
303 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000304 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000305 if (VI != CurModule.Values.end()) {
306 if (Num < VI->second.size())
307 return VI->second[Num];
308 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000309 }
310
311 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000312 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000313 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000314
315 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000316 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000317
Chris Lattner16af11d2004-02-09 21:03:38 +0000318 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000319 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000320
Chris Lattner1a1cb112001-09-30 22:46:54 +0000321 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000322 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000323 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000324
325 D.destroy(); // Free old strdup'd memory...
326 return N;
327 }
328
Chris Lattner2079fde2001-10-13 06:41:08 +0000329 // Check to make sure that "Ty" is an integral type, and that our
330 // value will fit into the specified type...
331 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000332 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
333 ThrowException("Signed integral constant '" +
334 itostr(D.ConstPool64) + "' is invalid for type '" +
335 Ty->getDescription() + "'!");
336 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000337
338 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000339 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
340 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000341 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
342 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000343 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000344 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000345 }
346 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000347 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000348 }
349
Chris Lattner2079fde2001-10-13 06:41:08 +0000350 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000351 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000352 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000353 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000354
355 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000356 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000357 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000358 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000359
Chris Lattnerd78700d2002-08-16 21:14:40 +0000360 case ValID::ConstantVal: // Fully resolved constant?
361 if (D.ConstantValue->getType() != Ty)
362 ThrowException("Constant expression type different from required type!");
363 return D.ConstantValue;
364
Chris Lattner30c89792001-09-07 16:35:17 +0000365 default:
366 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000367 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000368 } // End of switch
369
Chris Lattner2079fde2001-10-13 06:41:08 +0000370 assert(0 && "Unhandled case!");
371 return 0;
372}
373
Chris Lattner2079fde2001-10-13 06:41:08 +0000374// getVal - This function is identical to getValNonImprovising, except that if a
375// value is not already defined, it "improvises" by creating a placeholder var
376// that looks and acts just like the requested variable. When the value is
377// defined later, all uses of the placeholder variable are replaced with the
378// real thing.
379//
Chris Lattner64116f92004-07-14 01:33:11 +0000380static Value *getVal(const Type *Ty, const ValID &ID) {
381 if (Ty == Type::LabelTy)
382 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000383
Chris Lattner64116f92004-07-14 01:33:11 +0000384 // See if the value has already been defined.
385 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000386 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000387
388 // If we reached here, we referenced either a symbol that we don't know about
389 // or an id number that hasn't been read yet. We may be referencing something
390 // forward, so just create an entry to be resolved later and get to it...
391 //
Chris Lattner64116f92004-07-14 01:33:11 +0000392 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000393
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000394 // Remember where this forward reference came from. FIXME, shouldn't we try
395 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000396 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000397 llvmAsmlineno)));
398
Chris Lattner79df7c02002-03-26 18:01:55 +0000399 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000400 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000401 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000402 InsertValue(V, CurModule.LateResolveValues);
403 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000404}
405
Chris Lattner2e2ed292004-07-14 06:28:35 +0000406/// getBBVal - This is used for two purposes:
407/// * If isDefinition is true, a new basic block with the specified ID is being
408/// defined.
409/// * If isDefinition is true, this is a reference to a basic block, which may
410/// or may not be a forward reference.
411///
412static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000413 assert(inFunctionScope() && "Can't get basic block at global scope!");
414
Chris Lattner2e2ed292004-07-14 06:28:35 +0000415 std::string Name;
416 BasicBlock *BB = 0;
417 switch (ID.Type) {
418 default: ThrowException("Illegal label reference " + ID.getName());
419 case ValID::NumberVal: // Is it a numbered definition?
420 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
421 CurFun.NumberedBlocks.resize(ID.Num+1);
422 BB = CurFun.NumberedBlocks[ID.Num];
423 break;
424 case ValID::NameVal: // Is it a named definition?
425 Name = ID.Name;
426 if (Value *N = lookupInSymbolTable(Type::LabelTy, Name))
427 BB = cast<BasicBlock>(N);
428 break;
429 }
Chris Lattner64116f92004-07-14 01:33:11 +0000430
Chris Lattner2e2ed292004-07-14 06:28:35 +0000431 // See if the block has already been defined.
432 if (BB) {
433 // If this is the definition of the block, make sure the existing value was
434 // just a forward reference. If it was a forward reference, there will be
435 // an entry for it in the PlaceHolderInfo map.
436 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
437 // The existing value was a definition, not a forward reference.
438 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000439
Chris Lattner2e2ed292004-07-14 06:28:35 +0000440 ID.destroy(); // Free strdup'd memory.
441 return BB;
442 }
443
444 // Otherwise this block has not been seen before.
445 BB = new BasicBlock("", CurFun.CurrentFunction);
446 if (ID.Type == ValID::NameVal) {
447 BB->setName(ID.Name);
448 } else {
449 CurFun.NumberedBlocks[ID.Num] = BB;
450 }
451
452 // If this is not a definition, keep track of it so we can use it as a forward
453 // reference.
454 if (!isDefinition) {
455 // Remember where this forward reference came from.
456 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
457 } else {
458 // The forward declaration could have been inserted anywhere in the
459 // function: insert it into the correct place now.
460 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
461 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
462 }
463
Chris Lattner64116f92004-07-14 01:33:11 +0000464 return BB;
465}
466
Chris Lattner00950542001-06-06 20:29:01 +0000467
468//===----------------------------------------------------------------------===//
469// Code to handle forward references in instructions
470//===----------------------------------------------------------------------===//
471//
472// This code handles the late binding needed with statements that reference
473// values not defined yet... for example, a forward branch, or the PHI node for
474// a loop body.
475//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000476// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000477// and back patchs after we are done.
478//
479
480// ResolveDefinitions - If we could not resolve some defs at parsing
481// time (forward branches, phi functions for loops, etc...) resolve the
482// defs now...
483//
Chris Lattner735f2702004-07-08 22:30:50 +0000484static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
485 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000486 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000487 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000488 E = LateResolvers.end(); LRI != E; ++LRI) {
489 ValueList &List = LRI->second;
490 while (!List.empty()) {
491 Value *V = List.back();
492 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000493
494 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
495 CurModule.PlaceHolderInfo.find(V);
496 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
497
498 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000499
Chris Lattner735f2702004-07-08 22:30:50 +0000500 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000501 if (TheRealValue) {
502 V->replaceAllUsesWith(TheRealValue);
503 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000504 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000505 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000506 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000507 // resolver table
508 InsertValue(V, *FutureLateResolvers);
509 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000510 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000511 ThrowException("Reference to an invalid definition: '" +DID.getName()+
512 "' of type '" + V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000513 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000514 else
515 ThrowException("Reference to an invalid definition: #" +
516 itostr(DID.Num) + " of type '" +
517 V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000518 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000519 }
Chris Lattner00950542001-06-06 20:29:01 +0000520 }
521 }
522
523 LateResolvers.clear();
524}
525
Chris Lattner4a42e902001-10-22 05:56:09 +0000526// ResolveTypeTo - A brand new type was just declared. This means that (if
527// name is not null) things referencing Name can be resolved. Otherwise, things
528// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000529//
Chris Lattner4a42e902001-10-22 05:56:09 +0000530static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000531 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000532 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000533
Chris Lattner4a42e902001-10-22 05:56:09 +0000534 ValID D;
535 if (Name) D = ValID::create(Name);
536 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000537
Chris Lattner9232b992003-04-22 18:42:41 +0000538 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000539 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000540
Chris Lattner9232b992003-04-22 18:42:41 +0000541 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000542 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000543 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000544 LateResolver.erase(I);
545 }
546}
547
548// ResolveTypes - At this point, all types should be resolved. Any that aren't
549// are errors.
550//
Chris Lattner9232b992003-04-22 18:42:41 +0000551static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000552 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000553 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000554
555 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000556 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000557 else
Chris Lattner82269592001-10-22 06:01:08 +0000558 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000559 }
560}
561
Chris Lattner1781aca2001-09-18 04:00:54 +0000562// setValueName - Set the specified value to the name given. The name may be
563// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000564// assumed to be a malloc'd string buffer, and is free'd by this function.
565//
566static void setValueName(Value *V, char *NameStr) {
567 if (NameStr) {
568 std::string Name(NameStr); // Copy string
569 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000570
571 if (V->getType() == Type::VoidTy)
572 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000573
574 assert(inFunctionScope() && "Must be in function scope!");
575 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
576 if (ST.lookup(V->getType(), Name))
577 ThrowException("Redefinition of value named '" + Name + "' in the '" +
578 V->getType()->getDescription() + "' type plane!");
579
Chris Lattnerc9aea522004-07-13 08:12:39 +0000580 // Set the name.
581 V->setName(Name, &ST);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000582 }
583}
584
585// setValueNameMergingDuplicates - Set the specified value to the name given.
586// The name may be null potentially, in which case this is a noop. The string
587// passed in is assumed to be a malloc'd string buffer, and is free'd by this
588// function.
Chris Lattner1781aca2001-09-18 04:00:54 +0000589//
Chris Lattnerb7474512001-10-03 15:39:04 +0000590// This function returns true if the value has already been defined, but is
591// allowed to be redefined in the specified context. If the name is a new name
592// for the typeplane, false is returned.
593//
Chris Lattner66028f22004-07-14 07:12:48 +0000594static bool setValueNameMergingDuplicates(GlobalValue *V, char *NameStr) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000595 if (NameStr == 0) return false;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000596
Chris Lattner9232b992003-04-22 18:42:41 +0000597 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000598 free(NameStr); // Free old string
599
Chris Lattner66028f22004-07-14 07:12:48 +0000600 SymbolTable &ST = CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000601
Reid Spencer75719bf2004-05-26 21:48:31 +0000602 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000603 if (Existing) { // Inserting a name that is already defined???
Chris Lattner66028f22004-07-14 07:12:48 +0000604
Chris Lattner8ab406d2004-07-13 07:59:27 +0000605 // We are a simple redefinition of a value, check to see if it is defined
606 // the same as the old one...
607 if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000608 // We are allowed to redefine a global variable in two circumstances:
609 // 1. If at least one of the globals is uninitialized or
610 // 2. If both initializers have the same value.
611 //
Chris Lattner89219832001-10-03 19:35:04 +0000612 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000613 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
614 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000615
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000616 // Make sure the existing global version gets the initializer! Make
617 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000618 if (GV->hasInitializer() && !EGV->hasInitializer())
619 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000620 if (GV->isConstant())
621 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000622 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000623
Chris Lattner2079fde2001-10-13 06:41:08 +0000624 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000625 return true; // They are equivalent!
626 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000627 }
Chris Lattner9636a912001-10-01 16:18:37 +0000628 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000629
Chris Lattner2079fde2001-10-13 06:41:08 +0000630 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000631 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000632 }
Chris Lattnerc9aea522004-07-13 08:12:39 +0000633
634 // Set the name.
635 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000636 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000637}
638
Chris Lattner8ab406d2004-07-13 07:59:27 +0000639
640
Reid Spencer75719bf2004-05-26 21:48:31 +0000641// setTypeName - Set the specified type to the name given. The name may be
642// null potentially, in which case this is a noop. The string passed in is
643// assumed to be a malloc'd string buffer, and is freed by this function.
644//
645// This function returns true if the type has already been defined, but is
646// allowed to be redefined in the specified context. If the name is a new name
647// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000648static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000649 if (NameStr == 0) return false;
650
651 std::string Name(NameStr); // Copy string
652 free(NameStr); // Free old string
653
654 // We don't allow assigning names to void type
655 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000656 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000657
658 SymbolTable &ST = inFunctionScope() ?
659 CurFun.CurrentFunction->getSymbolTable() :
660 CurModule.CurrentModule->getSymbolTable();
661
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000662 // Inserting a name that is already defined???
663 if (Type *Existing = ST.lookupType(Name)) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000664 // There is only one case where this is allowed: when we are refining an
665 // opaque type. In this case, Existing will be an opaque type.
666 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
667 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000668 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000669 return true;
670 }
671
672 // Otherwise, this is an attempt to redefine a type. That's okay if
673 // the redefinition is identical to the original. This will be so if
674 // Existing and T point to the same Type object. In this one case we
675 // allow the equivalent redefinition.
676 if (Existing == T) return true; // Yes, it's equal.
677
678 // Any other kind of (non-equivalent) redefinition is an error.
679 ThrowException("Redefinition of type named '" + Name + "' in the '" +
680 T->getDescription() + "' type plane!");
681 }
682
683 // Okay, its a newly named type. Set its name.
Chris Lattner8ca2dc02004-07-09 16:43:55 +0000684 if (!Name.empty()) ST.insert(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000685
Reid Spencer75719bf2004-05-26 21:48:31 +0000686 return false;
687}
Chris Lattner8896eda2001-07-09 19:38:36 +0000688
Chris Lattner30c89792001-09-07 16:35:17 +0000689//===----------------------------------------------------------------------===//
690// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000691//
Chris Lattner8896eda2001-07-09 19:38:36 +0000692
Chris Lattner3b073862004-02-09 03:19:29 +0000693// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000694//
695static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000696 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
697}
698
699namespace {
700 struct UpRefRecord {
701 // NestingLevel - The number of nesting levels that need to be popped before
702 // this type is resolved.
703 unsigned NestingLevel;
704
705 // LastContainedTy - This is the type at the current binding level for the
706 // type. Every time we reduce the nesting level, this gets updated.
707 const Type *LastContainedTy;
708
709 // UpRefTy - This is the actual opaque type that the upreference is
710 // represented with.
711 OpaqueType *UpRefTy;
712
713 UpRefRecord(unsigned NL, OpaqueType *URTy)
714 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
715 };
Chris Lattner30c89792001-09-07 16:35:17 +0000716}
Chris Lattner698b56e2001-07-20 19:15:08 +0000717
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000718// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000719static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000720
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000721/// HandleUpRefs - Every time we finish a new layer of types, this function is
722/// called. It loops through the UpRefs vector, which is a list of the
723/// currently active types. For each type, if the up reference is contained in
724/// the newly completed type, we decrement the level count. When the level
725/// count reaches zero, the upreferenced type is the type that is passed in:
726/// thus we can complete the cycle.
727///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000728static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000729 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000730 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000731 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000732 "' newly formed. Resolving upreferences.\n" <<
733 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000734
735 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
736 // to zero), we resolve them all together before we resolve them to Ty. At
737 // the end of the loop, if there is anything to resolve to Ty, it will be in
738 // this variable.
739 OpaqueType *TypeToResolve = 0;
740
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000741 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000742 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000743 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000744 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000745 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
746 // Decrement level of upreference
747 unsigned Level = --UpRefs[i].NestingLevel;
748 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000749 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000750 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000751 if (!TypeToResolve) {
752 TypeToResolve = UpRefs[i].UpRefTy;
753 } else {
754 UR_OUT(" * Resolving upreference for "
755 << UpRefs[i].second->getDescription() << "\n";
756 std::string OldName = UpRefs[i].UpRefTy->getDescription());
757 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
758 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
759 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
760 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000761 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
762 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000763 }
764 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000765 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000766
Chris Lattner026a8ce2004-02-09 18:53:54 +0000767 if (TypeToResolve) {
768 UR_OUT(" * Resolving upreference for "
769 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000770 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000771 TypeToResolve->refineAbstractTypeTo(Ty);
772 }
773
Chris Lattner8896eda2001-07-09 19:38:36 +0000774 return Ty;
775}
776
Chris Lattner30c89792001-09-07 16:35:17 +0000777
Chris Lattner00950542001-06-06 20:29:01 +0000778//===----------------------------------------------------------------------===//
779// RunVMAsmParser - Define an interface to this parser
780//===----------------------------------------------------------------------===//
781//
Chris Lattner86427632004-07-14 06:39:48 +0000782Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000783 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000784 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000785 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000786 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000787
Chris Lattner75f20532003-04-22 18:02:52 +0000788 // Allocate a new module to read
789 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000790
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000791 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000792
Chris Lattner00950542001-06-06 20:29:01 +0000793 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000794
795 // Check to see if they called va_start but not va_arg..
796 if (!ObsoleteVarArgs)
797 if (Function *F = Result->getNamedFunction("llvm.va_start"))
798 if (F->asize() == 1) {
799 std::cerr << "WARNING: this file uses obsolete features. "
800 << "Assemble and disassemble to update it.\n";
801 ObsoleteVarArgs = true;
802 }
803
Chris Lattner99e7ab72003-10-18 05:53:13 +0000804 if (ObsoleteVarArgs) {
805 // If the user is making use of obsolete varargs intrinsics, adjust them for
806 // the user.
807 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
808 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
809
810 const Type *RetTy = F->getFunctionType()->getParamType(0);
811 RetTy = cast<PointerType>(RetTy)->getElementType();
812 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
813
814 while (!F->use_empty()) {
815 CallInst *CI = cast<CallInst>(F->use_back());
816 Value *V = new CallInst(NF, "", CI);
817 new StoreInst(V, CI->getOperand(1), CI);
818 CI->getParent()->getInstList().erase(CI);
819 }
820 Result->getFunctionList().erase(F);
821 }
822
823 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
824 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
825 const Type *ArgTy = F->getFunctionType()->getParamType(0);
826 ArgTy = cast<PointerType>(ArgTy)->getElementType();
827 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
828 ArgTy, 0);
829
830 while (!F->use_empty()) {
831 CallInst *CI = cast<CallInst>(F->use_back());
832 Value *V = new LoadInst(CI->getOperand(1), "", CI);
833 new CallInst(NF, V, "", CI);
834 CI->getParent()->getInstList().erase(CI);
835 }
836 Result->getFunctionList().erase(F);
837 }
838
839 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
840 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
841 const Type *ArgTy = F->getFunctionType()->getParamType(0);
842 ArgTy = cast<PointerType>(ArgTy)->getElementType();
843 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
844 ArgTy, 0);
845
846 while (!F->use_empty()) {
847 CallInst *CI = cast<CallInst>(F->use_back());
848 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
849 new StoreInst(V, CI->getOperand(1), CI);
850 CI->getParent()->getInstList().erase(CI);
851 }
852 Result->getFunctionList().erase(F);
853 }
854 }
855
Chris Lattner00950542001-06-06 20:29:01 +0000856 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
857 ParserResult = 0;
858
859 return Result;
860}
861
862%}
863
864%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000865 llvm::Module *ModuleVal;
866 llvm::Function *FunctionVal;
867 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
868 llvm::BasicBlock *BasicBlockVal;
869 llvm::TerminatorInst *TermInstVal;
870 llvm::Instruction *InstVal;
871 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000872
Brian Gaeked0fde302003-11-11 22:41:34 +0000873 const llvm::Type *PrimType;
874 llvm::PATypeHolder *TypeVal;
875 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000876
Brian Gaeked0fde302003-11-11 22:41:34 +0000877 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
878 std::vector<llvm::Value*> *ValueList;
879 std::list<llvm::PATypeHolder> *TypeList;
880 std::list<std::pair<llvm::Value*,
881 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
882 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
883 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000884
Brian Gaeked0fde302003-11-11 22:41:34 +0000885 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000886 int64_t SInt64Val;
887 uint64_t UInt64Val;
888 int SIntVal;
889 unsigned UIntVal;
890 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000891 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000892
Chris Lattner30c89792001-09-07 16:35:17 +0000893 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000894 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000895
Brian Gaeked0fde302003-11-11 22:41:34 +0000896 llvm::Instruction::BinaryOps BinaryOpVal;
897 llvm::Instruction::TermOps TermOpVal;
898 llvm::Instruction::MemoryOps MemOpVal;
899 llvm::Instruction::OtherOps OtherOpVal;
900 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000901}
902
Chris Lattner79df7c02002-03-26 18:01:55 +0000903%type <ModuleVal> Module FunctionList
904%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000905%type <BasicBlockVal> BasicBlock InstructionList
906%type <TermInstVal> BBTerminatorInst
907%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000908%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000909%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000910%type <ArgList> ArgList ArgListH
911%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000912%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000913%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000914%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000915%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000916%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000917%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000918%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000919%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000920%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000921
Chris Lattner2079fde2001-10-13 06:41:08 +0000922// ValueRef - Unresolved reference to a definition or BB
923%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000924%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000925// Tokens and types for handling constant integer values
926//
927// ESINT64VAL - A negative number within long long range
928%token <SInt64Val> ESINT64VAL
929
930// EUINT64VAL - A positive number within uns. long long range
931%token <UInt64Val> EUINT64VAL
932%type <SInt64Val> EINT64VAL
933
934%token <SIntVal> SINTVAL // Signed 32 bit ints...
935%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
936%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000937%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000938
939// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000940%type <TypeVal> Types TypesV UpRTypes UpRTypesV
941%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000942%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
943%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000944
Chris Lattner6c23f572003-08-22 05:42:10 +0000945%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
946%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000947
948
Chris Lattner91ef4602004-03-31 03:49:47 +0000949%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000950%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000951%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000952%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000953
954// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000955%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000956
Chris Lattner00950542001-06-06 20:29:01 +0000957// Binary Operators
958%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000959%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000960%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000961%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000962
963// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000964%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000965
Chris Lattner027dcc52001-07-08 21:10:27 +0000966// Other Operators
967%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000968%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000969%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000970
Chris Lattner00950542001-06-06 20:29:01 +0000971%start Module
972%%
973
974// Handle constant integer size restriction and conversion...
975//
Chris Lattner51727be2002-06-04 21:58:56 +0000976INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000977INTVAL : UINTVAL {
978 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
979 ThrowException("Value too large for type!");
980 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000981};
Chris Lattner00950542001-06-06 20:29:01 +0000982
983
Chris Lattner51727be2002-06-04 21:58:56 +0000984EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000985EINT64VAL : EUINT64VAL {
986 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
987 ThrowException("Value too large for type!");
988 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000989};
Chris Lattner00950542001-06-06 20:29:01 +0000990
Chris Lattner00950542001-06-06 20:29:01 +0000991// Operations that are notably excluded from this list include:
992// RET, BR, & SWITCH because they end basic blocks and are treated specially.
993//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000994ArithmeticOps: ADD | SUB | MUL | DIV | REM;
995LogicalOps : AND | OR | XOR;
996SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
997BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
998
Chris Lattner51727be2002-06-04 21:58:56 +0000999ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001000
Chris Lattnere98dda62001-07-14 06:10:16 +00001001// These are some types that allow classification if we only want a particular
1002// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001003SIntType : LONG | INT | SHORT | SBYTE;
1004UIntType : ULONG | UINT | USHORT | UBYTE;
1005IntType : SIntType | UIntType;
1006FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001007
Chris Lattnere98dda62001-07-14 06:10:16 +00001008// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001009OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001010 $$ = $1;
1011 }
1012 | /*empty*/ {
1013 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001014 };
Chris Lattner00950542001-06-06 20:29:01 +00001015
Chris Lattner4ad02e72003-04-16 20:28:45 +00001016OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1017 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001018 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001019 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1020 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001021
1022//===----------------------------------------------------------------------===//
1023// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001024// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001025// access to it, a user must explicitly use TypesV.
1026//
1027
1028// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001029TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1030UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001031
1032Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001033 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001034 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1035 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001036 };
Chris Lattner30c89792001-09-07 16:35:17 +00001037
1038
1039// Derived types are added later...
1040//
Chris Lattner51727be2002-06-04 21:58:56 +00001041PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1042PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001043UpRTypes : OPAQUE {
1044 $$ = new PATypeHolder(OpaqueType::get());
1045 }
1046 | PrimType {
1047 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001048 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001049UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001050 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001051};
Chris Lattner30c89792001-09-07 16:35:17 +00001052
Chris Lattner30c89792001-09-07 16:35:17 +00001053// Include derived types in the Types production.
1054//
1055UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001056 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001057 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001058 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001059 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001060 UR_OUT("New Upreference!\n");
1061 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001062 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001063 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001064 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001065 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001066 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1067 if (isVarArg) Params.pop_back();
1068
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001069 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001070 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001071 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001072 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001073 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001074 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001075 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001076 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001077 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001078 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001079 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001080 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001081
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001082 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001083 delete $2;
1084 }
1085 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001086 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001087 }
1088 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001089 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001090 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001091 };
Chris Lattner30c89792001-09-07 16:35:17 +00001092
Chris Lattner7e708292002-06-25 16:13:24 +00001093// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001094// declaration type lists
1095//
1096TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001097 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001098 $$->push_back(*$1); delete $1;
1099 }
1100 | TypeListI ',' UpRTypes {
1101 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001102 };
Chris Lattner30c89792001-09-07 16:35:17 +00001103
Chris Lattner7e708292002-06-25 16:13:24 +00001104// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001105ArgTypeListI : TypeListI
1106 | TypeListI ',' DOTDOTDOT {
1107 ($$=$1)->push_back(Type::VoidTy);
1108 }
1109 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001110 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001111 }
1112 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001113 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001114 };
Chris Lattner30c89792001-09-07 16:35:17 +00001115
Chris Lattnere98dda62001-07-14 06:10:16 +00001116// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001117// production is used ONLY to represent constants that show up AFTER a 'const',
1118// 'constant' or 'global' token at global scope. Constants that can be inlined
1119// into other expressions (such as integers and constexprs) are handled by the
1120// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001121//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001122ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001123 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001124 if (ATy == 0)
1125 ThrowException("Cannot make array constant with type: '" +
1126 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001127 const Type *ETy = ATy->getElementType();
1128 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001129
Chris Lattner30c89792001-09-07 16:35:17 +00001130 // Verify that we have the correct size...
1131 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001132 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001133 utostr($3->size()) + " arguments, but has size of " +
1134 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001135
Chris Lattner30c89792001-09-07 16:35:17 +00001136 // Verify all elements are correct type!
1137 for (unsigned i = 0; i < $3->size(); i++) {
1138 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001139 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001140 ETy->getDescription() +"' as required!\nIt is of type '"+
1141 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001142 }
1143
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001144 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001145 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001146 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001147 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001148 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001149 if (ATy == 0)
1150 ThrowException("Cannot make array constant with type: '" +
1151 (*$1)->getDescription() + "'!");
1152
1153 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001154 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001155 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001156 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001157 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001158 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001159 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001160 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001161 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001162 if (ATy == 0)
1163 ThrowException("Cannot make array constant with type: '" +
1164 (*$1)->getDescription() + "'!");
1165
Chris Lattner30c89792001-09-07 16:35:17 +00001166 int NumElements = ATy->getNumElements();
1167 const Type *ETy = ATy->getElementType();
1168 char *EndStr = UnEscapeLexed($3, true);
1169 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001170 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001171 itostr((int)(EndStr-$3)) +
1172 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001173 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001174 if (ETy == Type::SByteTy) {
1175 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001176 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001177 } else if (ETy == Type::UByteTy) {
1178 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001179 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001180 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001181 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001182 ThrowException("Cannot build string arrays of non byte sized elements!");
1183 }
Chris Lattner30c89792001-09-07 16:35:17 +00001184 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001185 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001186 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001187 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001188 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001189 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001190 if (STy == 0)
1191 ThrowException("Cannot make struct constant with type: '" +
1192 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001193
Chris Lattnerc6212f12003-05-21 16:06:56 +00001194 if ($3->size() != STy->getNumContainedTypes())
1195 ThrowException("Illegal number of initializers for structure type!");
1196
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001197 // Check to ensure that constants are compatible with the type initializer!
1198 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001199 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001200 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001201 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001202 "' for element #" + utostr(i) +
1203 " of structure initializer!");
1204
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001205 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001206 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001207 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001208 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001209 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001210 if (STy == 0)
1211 ThrowException("Cannot make struct constant with type: '" +
1212 (*$1)->getDescription() + "'!");
1213
1214 if (STy->getNumContainedTypes() != 0)
1215 ThrowException("Illegal number of initializers for structure type!");
1216
1217 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1218 delete $1;
1219 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001220 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001221 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001222 if (PTy == 0)
1223 ThrowException("Cannot make null pointer constant with type: '" +
1224 (*$1)->getDescription() + "'!");
1225
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001226 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001227 delete $1;
1228 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001229 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001230 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001231 if (Ty == 0)
1232 ThrowException("Global const reference must be a pointer type!");
1233
Chris Lattner3101c252002-08-15 17:58:33 +00001234 // ConstExprs can exist in the body of a function, thus creating
1235 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1236 // the context of a function, getValNonImprovising will search the functions
1237 // symbol table instead of the module symbol table for the global symbol,
1238 // which throws things all off. To get around this, we just tell
1239 // getValNonImprovising that we are at global scope here.
1240 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001241 Function *SavedCurFn = CurFun.CurrentFunction;
1242 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001243
Chris Lattner2079fde2001-10-13 06:41:08 +00001244 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001245
Chris Lattner394f2eb2003-10-16 20:12:13 +00001246 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001247
Chris Lattner2079fde2001-10-13 06:41:08 +00001248 // If this is an initializer for a constant pointer, which is referencing a
1249 // (currently) undefined variable, create a stub now that shall be replaced
1250 // in the future with the right type of variable.
1251 //
1252 if (V == 0) {
1253 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1254 const PointerType *PT = cast<PointerType>(Ty);
1255
1256 // First check to see if the forward references value is already created!
1257 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001258 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001259
1260 if (I != CurModule.GlobalRefs.end()) {
1261 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001262 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001263 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001264 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001265 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001266 false,
1267 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001268 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001269 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001270
1271 // Must temporarily push this value into the module table...
1272 CurModule.CurrentModule->getGlobalList().push_back(GV);
1273 V = GV;
1274 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001275 }
1276
Chris Lattner2079fde2001-10-13 06:41:08 +00001277 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001278 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001279 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001280 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001281 | Types ConstExpr {
1282 if ($1->get() != $2->getType())
1283 ThrowException("Mismatched types for constant expression!");
1284 $$ = $2;
1285 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001286 }
1287 | Types ZEROINITIALIZER {
1288 $$ = Constant::getNullValue($1->get());
1289 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001290 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001291
Chris Lattnere43f40b2002-10-09 00:25:32 +00001292ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001293 if (!ConstantSInt::isValueValidForType($1, $2))
1294 ThrowException("Constant value doesn't fit in type!");
1295 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001296 }
1297 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001298 if (!ConstantUInt::isValueValidForType($1, $2))
1299 ThrowException("Constant value doesn't fit in type!");
1300 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001301 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001302 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001303 $$ = ConstantBool::True;
1304 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001305 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001306 $$ = ConstantBool::False;
1307 }
1308 | FPType FPVAL { // Float & Double constants
1309 $$ = ConstantFP::get($1, $2);
1310 };
1311
Chris Lattner00950542001-06-06 20:29:01 +00001312
Chris Lattnerd78700d2002-08-16 21:14:40 +00001313ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001314 if (!$3->getType()->isFirstClassType())
1315 ThrowException("cast constant expression from a non-primitive type: '" +
1316 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001317 if (!$5->get()->isFirstClassType())
1318 ThrowException("cast constant expression to a non-primitive type: '" +
1319 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001320 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001321 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001322 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001323 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1324 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001325 ThrowException("GetElementPtr requires a pointer operand!");
1326
Chris Lattner830b6f92004-04-05 01:30:04 +00001327 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1328 // indices to uint struct indices for compatibility.
1329 generic_gep_type_iterator<std::vector<Value*>::iterator>
1330 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1331 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1332 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1333 if (isa<StructType>(*GTI)) // Only change struct indices
1334 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1335 if (CUI->getType() == Type::UByteTy)
1336 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1337
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001338 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001339 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001340 if (!IdxTy)
1341 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001342
Chris Lattner9232b992003-04-22 18:42:41 +00001343 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001344 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1345 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001346 IdxVec.push_back(C);
1347 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001348 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001349
Chris Lattnerd78700d2002-08-16 21:14:40 +00001350 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001351
Chris Lattnerd78700d2002-08-16 21:14:40 +00001352 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001353 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001354 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1355 if ($3->getType() != Type::BoolTy)
1356 ThrowException("Select condition must be of boolean type!");
1357 if ($5->getType() != $7->getType())
1358 ThrowException("Select operand types must match!");
1359 $$ = ConstantExpr::getSelect($3, $5, $7);
1360 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001361 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001362 if ($3->getType() != $5->getType())
1363 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001364 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001365 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001366 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001367 if ($5->getType() != Type::UByteTy)
1368 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001369 if (!$3->getType()->isInteger())
1370 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001371 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001372 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001373
1374
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001375// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001376ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001377 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001378 }
1379 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001380 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001381 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001382 };
Chris Lattner00950542001-06-06 20:29:01 +00001383
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001384
Chris Lattner1781aca2001-09-18 04:00:54 +00001385// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001386GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001387
Chris Lattner00950542001-06-06 20:29:01 +00001388
Chris Lattner0e73ce62002-05-02 19:11:13 +00001389//===----------------------------------------------------------------------===//
1390// Rules to match Modules
1391//===----------------------------------------------------------------------===//
1392
1393// Module rule: Capture the result of parsing the whole file into a result
1394// variable...
1395//
1396Module : FunctionList {
1397 $$ = ParserResult = $1;
1398 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001399};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001400
Chris Lattner7e708292002-06-25 16:13:24 +00001401// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001402//
1403FunctionList : FunctionList Function {
1404 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001405 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001406 }
1407 | FunctionList FunctionProto {
1408 $$ = $1;
1409 }
1410 | FunctionList IMPLEMENTATION {
1411 $$ = $1;
1412 }
1413 | ConstPool {
1414 $$ = CurModule.CurrentModule;
1415 // Resolve circular types before we parse the body of the module
1416 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001417 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001418
Chris Lattnere98dda62001-07-14 06:10:16 +00001419// ConstPool - Constants with optional names assigned to them.
Chris Lattnere0026942004-07-14 06:44:56 +00001420ConstPool : ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001421 // Eagerly resolve types. This is not an optimization, this is a
1422 // requirement that is due to the fact that we could have this:
1423 //
1424 // %list = type { %list * }
1425 // %list = type { %list * } ; repeated type decl
1426 //
1427 // If types are not resolved eagerly, then the two types will not be
1428 // determined to be the same type!
1429 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001430 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001431
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001432 if (!setTypeName(*$4, $2) && !$2) {
1433 // If this is a named type that is not a redefinition, add it to the slot
1434 // table.
Chris Lattner64116f92004-07-14 01:33:11 +00001435 if (inFunctionScope())
1436 CurFun.Types.push_back(*$4);
1437 else
1438 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001439 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001440
1441 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001442 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001443 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001444 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001445 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001446 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001447 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001448 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001449 if (Initializer == 0)
1450 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001451
Chris Lattnerdda71962001-11-26 18:54:16 +00001452 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001453 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001454 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001455 int Slot = InsertValue(GV, CurModule.Values);
1456
1457 if (Slot != -1) {
1458 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1459 } else {
1460 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1461 (char*)GV->getName().c_str()));
1462 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001463 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001464 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001465 | ConstPool OptAssign EXTERNAL GlobalType Types {
1466 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001467 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001468 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001469 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001470 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001471 int Slot = InsertValue(GV, CurModule.Values);
1472
1473 if (Slot != -1) {
1474 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1475 } else {
1476 assert(GV->hasName() && "Not named and not numbered!?");
1477 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1478 (char*)GV->getName().c_str()));
1479 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001480 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001481 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001482 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001483 | ConstPool TARGET TargetDefinition {
1484 }
Chris Lattner00950542001-06-06 20:29:01 +00001485 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001486 };
Chris Lattner00950542001-06-06 20:29:01 +00001487
1488
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001489
1490BigOrLittle : BIG { $$ = Module::BigEndian; };
1491BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1492
1493TargetDefinition : ENDIAN '=' BigOrLittle {
1494 CurModule.CurrentModule->setEndianness($3);
1495 }
1496 | POINTERSIZE '=' EUINT64VAL {
1497 if ($3 == 32)
1498 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1499 else if ($3 == 64)
1500 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1501 else
1502 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1503 };
1504
1505
Chris Lattner00950542001-06-06 20:29:01 +00001506//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001507// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001508//===----------------------------------------------------------------------===//
1509
Chris Lattner6c23f572003-08-22 05:42:10 +00001510Name : VAR_ID | STRINGCONSTANT;
1511OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001512
Chris Lattner6c23f572003-08-22 05:42:10 +00001513ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001514 if (*$1 == Type::VoidTy)
1515 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001516 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001517};
Chris Lattner00950542001-06-06 20:29:01 +00001518
Chris Lattner69da5cf2002-10-13 20:57:00 +00001519ArgListH : ArgListH ',' ArgVal {
1520 $$ = $1;
1521 $1->push_back(*$3);
1522 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001523 }
1524 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001525 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001526 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001527 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001528 };
Chris Lattner00950542001-06-06 20:29:01 +00001529
1530ArgList : ArgListH {
1531 $$ = $1;
1532 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001533 | ArgListH ',' DOTDOTDOT {
1534 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001535 $$->push_back(std::pair<PATypeHolder*,
1536 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001537 }
1538 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001539 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1540 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001541 }
Chris Lattner00950542001-06-06 20:29:01 +00001542 | /* empty */ {
1543 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001544 };
Chris Lattner00950542001-06-06 20:29:01 +00001545
Chris Lattner6c23f572003-08-22 05:42:10 +00001546FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001547 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001548 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001549
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001550 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1551 ThrowException("LLVM functions cannot return aggregate types!");
1552
Chris Lattner9232b992003-04-22 18:42:41 +00001553 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001554 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001555 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001556 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001557 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001558 }
Chris Lattner00950542001-06-06 20:29:01 +00001559
Chris Lattner2079fde2001-10-13 06:41:08 +00001560 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1561 if (isVarArg) ParamTypeList.pop_back();
1562
Chris Lattner1f862af2003-04-16 18:13:57 +00001563 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001564 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001565 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001566
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001567 // Is the function already in symtab?
Chris Lattner66028f22004-07-14 07:12:48 +00001568 if (CurModule.CurrentModule->getFunction(FunctionName, FT))
1569 ThrowException("Redefinition of function '" + FunctionName + "'!");
Chris Lattner5659dd12002-07-15 00:10:33 +00001570
Chris Lattner66028f22004-07-14 07:12:48 +00001571 Function *Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1572 CurModule.CurrentModule);
1573 InsertValue(Fn, CurModule.Values);
1574 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattner1f862af2003-04-16 18:13:57 +00001575 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001576
Chris Lattner394f2eb2003-10-16 20:12:13 +00001577 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001578
Chris Lattner7e708292002-06-25 16:13:24 +00001579 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001580 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001581 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001582 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001583 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001584 delete $4->back().first;
1585 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001586 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001587 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001588 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001589 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001590 delete I->first; // Delete the typeholder...
1591
Chris Lattner8ab406d2004-07-13 07:59:27 +00001592 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001593 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001594 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001595
Chris Lattner1f862af2003-04-16 18:13:57 +00001596 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001597 }
Chris Lattner51727be2002-06-04 21:58:56 +00001598};
Chris Lattner00950542001-06-06 20:29:01 +00001599
Chris Lattner9b02cc32002-05-03 18:23:48 +00001600BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1601
Chris Lattner4ad02e72003-04-16 20:28:45 +00001602FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001603 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001604
Chris Lattner4ad02e72003-04-16 20:28:45 +00001605 // Make sure that we keep track of the linkage type even if there was a
1606 // previous "declare".
1607 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001608
Chris Lattner7e708292002-06-25 16:13:24 +00001609 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001610 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001611};
Chris Lattner00950542001-06-06 20:29:01 +00001612
Chris Lattner9b02cc32002-05-03 18:23:48 +00001613END : ENDTOK | '}'; // Allow end of '}' to end a function
1614
Chris Lattner79df7c02002-03-26 18:01:55 +00001615Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001616 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001617};
Chris Lattner00950542001-06-06 20:29:01 +00001618
Chris Lattner394f2eb2003-10-16 20:12:13 +00001619FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1620 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001621 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001622};
Chris Lattner00950542001-06-06 20:29:01 +00001623
1624//===----------------------------------------------------------------------===//
1625// Rules to match Basic Blocks
1626//===----------------------------------------------------------------------===//
1627
1628ConstValueRef : ESINT64VAL { // A reference to a direct constant
1629 $$ = ValID::create($1);
1630 }
1631 | EUINT64VAL {
1632 $$ = ValID::create($1);
1633 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001634 | FPVAL { // Perhaps it's an FP constant?
1635 $$ = ValID::create($1);
1636 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001637 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001638 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001639 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001640 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001641 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001642 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001643 | NULL_TOK {
1644 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001645 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001646 | ConstExpr {
1647 $$ = ValID::create($1);
1648 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001649
Chris Lattner2079fde2001-10-13 06:41:08 +00001650// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1651// another value.
1652//
1653SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001654 $$ = ValID::create($1);
1655 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001656 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001657 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001658 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001659
1660// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001661ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001662
Chris Lattner00950542001-06-06 20:29:01 +00001663
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001664// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1665// type immediately preceeds the value reference, and allows complex constant
1666// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001667ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001668 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001669 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001670
Chris Lattner00950542001-06-06 20:29:01 +00001671BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001672 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001673 }
Chris Lattner7e708292002-06-25 16:13:24 +00001674 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001675 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001676 };
Chris Lattner00950542001-06-06 20:29:01 +00001677
1678
1679// Basic blocks are terminated by branching instructions:
1680// br, br/cc, switch, ret
1681//
Chris Lattner2079fde2001-10-13 06:41:08 +00001682BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001683 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001684 InsertValue($3);
1685
1686 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001687 InsertValue($1);
1688 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001689 };
Chris Lattner00950542001-06-06 20:29:01 +00001690
1691InstructionList : InstructionList Inst {
1692 $1->getInstList().push_back($2);
1693 $$ = $1;
1694 }
1695 | /* empty */ {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001696 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner22ae2322004-07-13 08:39:15 +00001697 }
1698 | LABELSTR {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001699 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner51727be2002-06-04 21:58:56 +00001700 };
Chris Lattner00950542001-06-06 20:29:01 +00001701
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001702BBTerminatorInst : RET ResolvedVal { // Return with a result...
1703 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001704 }
1705 | RET VOID { // Return with no result...
1706 $$ = new ReturnInst();
1707 }
1708 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001709 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001710 } // Conditional Branch...
1711 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001712 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001713 }
1714 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001715 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner00950542001-06-06 20:29:01 +00001716 $$ = S;
1717
Chris Lattner9232b992003-04-22 18:42:41 +00001718 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001719 E = $8->end();
1720 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001721 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001722 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001723 }
Chris Lattner196850c2003-05-15 21:30:00 +00001724 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001725 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner196850c2003-05-15 21:30:00 +00001726 $$ = S;
1727 }
Chris Lattner64116f92004-07-14 01:33:11 +00001728 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1729 UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001730 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001731 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001732
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001733 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1734 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001735 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001736 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001737 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001738 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1739 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001740 ParamTypes.push_back((*I)->getType());
1741 }
1742
1743 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1744 if (isVarArg) ParamTypes.pop_back();
1745
Chris Lattner79df7c02002-03-26 18:01:55 +00001746 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001747 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001748 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001749
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001750 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001751
Chris Lattner64116f92004-07-14 01:33:11 +00001752 BasicBlock *Normal = getBBVal($9);
1753 BasicBlock *Except = getBBVal($12);
Chris Lattner2079fde2001-10-13 06:41:08 +00001754
1755 // Create the call node...
1756 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001757 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001758 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001759 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001760 // correctly!
1761 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001762 FunctionType::param_iterator I = Ty->param_begin();
1763 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001764 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001765
1766 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1767 if ((*ArgI)->getType() != *I)
1768 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001769 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001770
1771 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1772 ThrowException("Invalid number of parameters detected!");
1773
Chris Lattner6cdb0112001-11-26 16:54:11 +00001774 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001775 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001776 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001777 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001778 }
1779 | UNWIND {
1780 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001781 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001782
1783
Chris Lattner00950542001-06-06 20:29:01 +00001784
1785JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1786 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001787 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001788 if (V == 0)
1789 ThrowException("May only switch on a constant pool value!");
1790
Chris Lattner64116f92004-07-14 01:33:11 +00001791 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001792 }
1793 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001794 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001795 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001796
1797 if (V == 0)
1798 ThrowException("May only switch on a constant pool value!");
1799
Chris Lattner64116f92004-07-14 01:33:11 +00001800 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001801 };
Chris Lattner00950542001-06-06 20:29:01 +00001802
1803Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001804 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001805 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001806 InsertValue($2);
1807 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001808};
Chris Lattner00950542001-06-06 20:29:01 +00001809
Chris Lattnerc24d2082001-06-11 15:04:20 +00001810PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001811 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001812 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001813 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001814 }
1815 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1816 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001817 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001818 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001819 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001820
1821
Chris Lattner30c89792001-09-07 16:35:17 +00001822ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001823 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001824 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001825 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001826 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001827 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001828 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001829 };
Chris Lattner00950542001-06-06 20:29:01 +00001830
1831// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001832ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001833
Chris Lattner4a6482b2002-09-10 19:57:26 +00001834InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1835 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1836 ThrowException("Arithmetic operator requires integer or FP operands!");
1837 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1838 if ($$ == 0)
1839 ThrowException("binary operator returned null!");
1840 delete $2;
1841 }
1842 | LogicalOps Types ValueRef ',' ValueRef {
1843 if (!(*$2)->isIntegral())
1844 ThrowException("Logical operator requires integral operands!");
1845 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1846 if ($$ == 0)
1847 ThrowException("binary operator returned null!");
1848 delete $2;
1849 }
1850 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001851 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001852 if ($$ == 0)
1853 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001854 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001855 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001856 | NOT ResolvedVal {
1857 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1858 << " Replacing with 'xor'.\n";
1859
1860 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1861 if (Ones == 0)
1862 ThrowException("Expected integral type for not instruction!");
1863
1864 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001865 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001866 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001867 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001868 | ShiftOps ResolvedVal ',' ResolvedVal {
1869 if ($4->getType() != Type::UByteTy)
1870 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001871 if (!$2->getType()->isInteger())
1872 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001873 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001874 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001875 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001876 if (!$4->get()->isFirstClassType())
1877 ThrowException("cast instruction to a non-primitive type: '" +
1878 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001879 $$ = new CastInst($2, *$4);
1880 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001881 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001882 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1883 if ($2->getType() != Type::BoolTy)
1884 ThrowException("select condition must be boolean!");
1885 if ($4->getType() != $6->getType())
1886 ThrowException("select value types should match!");
1887 $$ = new SelectInst($2, $4, $6);
1888 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001889 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001890 // FIXME: This is emulation code for an obsolete syntax. This should be
1891 // removed at some point.
1892 if (!ObsoleteVarArgs) {
1893 std::cerr << "WARNING: this file uses obsolete features. "
1894 << "Assemble and disassemble to update it.\n";
1895 ObsoleteVarArgs = true;
1896 }
1897
1898 // First, load the valist...
1899 Instruction *CurVAList = new LoadInst($2, "");
1900 CurBB->getInstList().push_back(CurVAList);
1901
1902 // Emit the vaarg instruction.
1903 $$ = new VAArgInst(CurVAList, *$4);
1904
1905 // Now we must advance the pointer and update it in memory.
1906 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1907 CurBB->getInstList().push_back(TheVANext);
1908
1909 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1910 delete $4;
1911 }
1912 | VAARG ResolvedVal ',' Types {
1913 $$ = new VAArgInst($2, *$4);
1914 delete $4;
1915 }
1916 | VANEXT ResolvedVal ',' Types {
1917 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001918 delete $4;
1919 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001920 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001921 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001922 if (!Ty->isFirstClassType())
1923 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001924 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001925 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001926 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001927 if ($2->front().first->getType() != Ty)
1928 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001929 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001930 $2->pop_front();
1931 }
1932 delete $2; // Free the list...
1933 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001934 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001935 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001936 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001937
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001938 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1939 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001940 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001941 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001942 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001943 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1944 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001945 ParamTypes.push_back((*I)->getType());
1946 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001947
1948 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1949 if (isVarArg) ParamTypes.pop_back();
1950
Chris Lattner79df7c02002-03-26 18:01:55 +00001951 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001952 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001953 }
Chris Lattner00950542001-06-06 20:29:01 +00001954
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001955 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001956
Chris Lattner8b81bf52001-07-25 22:47:46 +00001957 // Create the call node...
1958 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001959 // Make sure no arguments is a good thing!
1960 if (Ty->getNumParams() != 0)
1961 ThrowException("No arguments passed to a function that "
1962 "expects arguments!");
1963
Chris Lattner9232b992003-04-22 18:42:41 +00001964 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001965 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001966 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001967 // correctly!
1968 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001969 FunctionType::param_iterator I = Ty->param_begin();
1970 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001971 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001972
1973 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1974 if ((*ArgI)->getType() != *I)
1975 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001976 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001977
Chris Lattner8b81bf52001-07-25 22:47:46 +00001978 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001979 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001980
Chris Lattner6cdb0112001-11-26 16:54:11 +00001981 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001982 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001983 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001984 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001985 }
1986 | MemoryInst {
1987 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001988 };
Chris Lattner00950542001-06-06 20:29:01 +00001989
Chris Lattner6cdb0112001-11-26 16:54:11 +00001990
1991// IndexList - List of indices for GEP based instructions...
1992IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001993 $$ = $2;
1994 } | /* empty */ {
1995 $$ = new std::vector<Value*>();
1996 };
1997
1998OptVolatile : VOLATILE {
1999 $$ = true;
2000 }
2001 | /* empty */ {
2002 $$ = false;
2003 };
2004
Chris Lattner027dcc52001-07-08 21:10:27 +00002005
Chris Lattner00950542001-06-06 20:29:01 +00002006MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002007 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002008 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002009 }
2010 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002011 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002012 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002013 }
2014 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002015 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002016 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002017 }
2018 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002019 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002020 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002021 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002022 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002023 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002024 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002025 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002026 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002027 }
2028
Chris Lattner15c9c032003-09-08 18:20:29 +00002029 | OptVolatile LOAD Types ValueRef {
2030 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002031 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002032 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002033 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002034 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002035 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002036 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2037 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002038 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002039 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002040 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002041 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002042 if (ElTy != $3->getType())
2043 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002044 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002045
Chris Lattner79ad13802003-09-08 20:29:46 +00002046 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002047 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002048 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002049 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002050 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002051 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002052
2053 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2054 // indices to uint struct indices for compatibility.
2055 generic_gep_type_iterator<std::vector<Value*>::iterator>
2056 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2057 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2058 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2059 if (isa<StructType>(*GTI)) // Only change struct indices
2060 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2061 if (CUI->getType() == Type::UByteTy)
2062 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2063
Chris Lattner30c89792001-09-07 16:35:17 +00002064 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002065 ThrowException("Invalid getelementptr indices for type '" +
2066 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002067 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2068 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002069 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002070
Brian Gaeked0fde302003-11-11 22:41:34 +00002071
Chris Lattner00950542001-06-06 20:29:01 +00002072%%
Chris Lattner09083092001-07-08 04:57:15 +00002073int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002074 std::string where
2075 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002076 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002077 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002078 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002079 errMsg += "end-of-file.";
2080 else
Chris Lattner9232b992003-04-22 18:42:41 +00002081 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002082 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002083 return 0;
2084}