blob: ea06f875ad08bfb3e2f44ded3ca7ce16ed8ba1ae [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 Lattner8ab406d2004-07-13 07:59:27 +0000594static bool setValueNameMergingDuplicates(Value *V, char *NameStr) {
Chris Lattnerc9aea522004-07-13 08:12:39 +0000595 assert(V->getType() != Type::VoidTy && "Global or constant of type void?");
596
Chris Lattnerb7474512001-10-03 15:39:04 +0000597 if (NameStr == 0) return false;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000598
Chris Lattner9232b992003-04-22 18:42:41 +0000599 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000600 free(NameStr); // Free old string
601
Chris Lattner8ab406d2004-07-13 07:59:27 +0000602 // FIXME: If we eliminated the function constant pool (which we should), this
603 // would just unconditionally look at the module symtab.
Chris Lattner6e6026b2002-11-20 18:36:02 +0000604 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000605 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000606 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000607
Reid Spencer75719bf2004-05-26 21:48:31 +0000608 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000609 if (Existing) { // Inserting a name that is already defined???
Chris Lattner8ab406d2004-07-13 07:59:27 +0000610 // We are a simple redefinition of a value, check to see if it is defined
611 // the same as the old one...
612 if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000613 // We are allowed to redefine a global variable in two circumstances:
614 // 1. If at least one of the globals is uninitialized or
615 // 2. If both initializers have the same value.
616 //
Chris Lattner89219832001-10-03 19:35:04 +0000617 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000618 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
619 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000620
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000621 // Make sure the existing global version gets the initializer! Make
622 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000623 if (GV->hasInitializer() && !EGV->hasInitializer())
624 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000625 if (GV->isConstant())
626 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000627 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000628
Chris Lattner2079fde2001-10-13 06:41:08 +0000629 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000630 return true; // They are equivalent!
631 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000632 }
Chris Lattner8ab406d2004-07-13 07:59:27 +0000633 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
634 if (C == V) return true; // Constants are equal to themselves
Chris Lattner9636a912001-10-01 16:18:37 +0000635 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000636
Chris Lattner2079fde2001-10-13 06:41:08 +0000637 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000638 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000639 }
Chris Lattnerc9aea522004-07-13 08:12:39 +0000640
641 // Set the name.
642 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000643 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000644}
645
Chris Lattner8ab406d2004-07-13 07:59:27 +0000646
647
Reid Spencer75719bf2004-05-26 21:48:31 +0000648// setTypeName - Set the specified type to the name given. The name may be
649// null potentially, in which case this is a noop. The string passed in is
650// assumed to be a malloc'd string buffer, and is freed by this function.
651//
652// This function returns true if the type has already been defined, but is
653// allowed to be redefined in the specified context. If the name is a new name
654// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000655static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000656 if (NameStr == 0) return false;
657
658 std::string Name(NameStr); // Copy string
659 free(NameStr); // Free old string
660
661 // We don't allow assigning names to void type
662 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000663 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000664
665 SymbolTable &ST = inFunctionScope() ?
666 CurFun.CurrentFunction->getSymbolTable() :
667 CurModule.CurrentModule->getSymbolTable();
668
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000669 // Inserting a name that is already defined???
670 if (Type *Existing = ST.lookupType(Name)) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000671 // There is only one case where this is allowed: when we are refining an
672 // opaque type. In this case, Existing will be an opaque type.
673 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
674 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000675 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000676 return true;
677 }
678
679 // Otherwise, this is an attempt to redefine a type. That's okay if
680 // the redefinition is identical to the original. This will be so if
681 // Existing and T point to the same Type object. In this one case we
682 // allow the equivalent redefinition.
683 if (Existing == T) return true; // Yes, it's equal.
684
685 // Any other kind of (non-equivalent) redefinition is an error.
686 ThrowException("Redefinition of type named '" + Name + "' in the '" +
687 T->getDescription() + "' type plane!");
688 }
689
690 // Okay, its a newly named type. Set its name.
Chris Lattner8ca2dc02004-07-09 16:43:55 +0000691 if (!Name.empty()) ST.insert(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000692
Reid Spencer75719bf2004-05-26 21:48:31 +0000693 return false;
694}
Chris Lattner8896eda2001-07-09 19:38:36 +0000695
Chris Lattner30c89792001-09-07 16:35:17 +0000696//===----------------------------------------------------------------------===//
697// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000698//
Chris Lattner8896eda2001-07-09 19:38:36 +0000699
Chris Lattner3b073862004-02-09 03:19:29 +0000700// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000701//
702static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000703 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
704}
705
706namespace {
707 struct UpRefRecord {
708 // NestingLevel - The number of nesting levels that need to be popped before
709 // this type is resolved.
710 unsigned NestingLevel;
711
712 // LastContainedTy - This is the type at the current binding level for the
713 // type. Every time we reduce the nesting level, this gets updated.
714 const Type *LastContainedTy;
715
716 // UpRefTy - This is the actual opaque type that the upreference is
717 // represented with.
718 OpaqueType *UpRefTy;
719
720 UpRefRecord(unsigned NL, OpaqueType *URTy)
721 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
722 };
Chris Lattner30c89792001-09-07 16:35:17 +0000723}
Chris Lattner698b56e2001-07-20 19:15:08 +0000724
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000725// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000726static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000727
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000728/// HandleUpRefs - Every time we finish a new layer of types, this function is
729/// called. It loops through the UpRefs vector, which is a list of the
730/// currently active types. For each type, if the up reference is contained in
731/// the newly completed type, we decrement the level count. When the level
732/// count reaches zero, the upreferenced type is the type that is passed in:
733/// thus we can complete the cycle.
734///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000735static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000736 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000737 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000738 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000739 "' newly formed. Resolving upreferences.\n" <<
740 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000741
742 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
743 // to zero), we resolve them all together before we resolve them to Ty. At
744 // the end of the loop, if there is anything to resolve to Ty, it will be in
745 // this variable.
746 OpaqueType *TypeToResolve = 0;
747
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000748 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000749 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000750 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000751 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000752 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
753 // Decrement level of upreference
754 unsigned Level = --UpRefs[i].NestingLevel;
755 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000756 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000757 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000758 if (!TypeToResolve) {
759 TypeToResolve = UpRefs[i].UpRefTy;
760 } else {
761 UR_OUT(" * Resolving upreference for "
762 << UpRefs[i].second->getDescription() << "\n";
763 std::string OldName = UpRefs[i].UpRefTy->getDescription());
764 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
765 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
766 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
767 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000768 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
769 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000770 }
771 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000772 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000773
Chris Lattner026a8ce2004-02-09 18:53:54 +0000774 if (TypeToResolve) {
775 UR_OUT(" * Resolving upreference for "
776 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000777 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000778 TypeToResolve->refineAbstractTypeTo(Ty);
779 }
780
Chris Lattner8896eda2001-07-09 19:38:36 +0000781 return Ty;
782}
783
Chris Lattner30c89792001-09-07 16:35:17 +0000784
Chris Lattner00950542001-06-06 20:29:01 +0000785//===----------------------------------------------------------------------===//
786// RunVMAsmParser - Define an interface to this parser
787//===----------------------------------------------------------------------===//
788//
Chris Lattner86427632004-07-14 06:39:48 +0000789Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000790 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000791 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000792 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000793 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000794
Chris Lattner75f20532003-04-22 18:02:52 +0000795 // Allocate a new module to read
796 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000797
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000798 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000799
Chris Lattner00950542001-06-06 20:29:01 +0000800 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000801
802 // Check to see if they called va_start but not va_arg..
803 if (!ObsoleteVarArgs)
804 if (Function *F = Result->getNamedFunction("llvm.va_start"))
805 if (F->asize() == 1) {
806 std::cerr << "WARNING: this file uses obsolete features. "
807 << "Assemble and disassemble to update it.\n";
808 ObsoleteVarArgs = true;
809 }
810
Chris Lattner99e7ab72003-10-18 05:53:13 +0000811 if (ObsoleteVarArgs) {
812 // If the user is making use of obsolete varargs intrinsics, adjust them for
813 // the user.
814 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
815 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
816
817 const Type *RetTy = F->getFunctionType()->getParamType(0);
818 RetTy = cast<PointerType>(RetTy)->getElementType();
819 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
820
821 while (!F->use_empty()) {
822 CallInst *CI = cast<CallInst>(F->use_back());
823 Value *V = new CallInst(NF, "", CI);
824 new StoreInst(V, CI->getOperand(1), CI);
825 CI->getParent()->getInstList().erase(CI);
826 }
827 Result->getFunctionList().erase(F);
828 }
829
830 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
831 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
832 const Type *ArgTy = F->getFunctionType()->getParamType(0);
833 ArgTy = cast<PointerType>(ArgTy)->getElementType();
834 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
835 ArgTy, 0);
836
837 while (!F->use_empty()) {
838 CallInst *CI = cast<CallInst>(F->use_back());
839 Value *V = new LoadInst(CI->getOperand(1), "", CI);
840 new CallInst(NF, V, "", CI);
841 CI->getParent()->getInstList().erase(CI);
842 }
843 Result->getFunctionList().erase(F);
844 }
845
846 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
847 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
848 const Type *ArgTy = F->getFunctionType()->getParamType(0);
849 ArgTy = cast<PointerType>(ArgTy)->getElementType();
850 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
851 ArgTy, 0);
852
853 while (!F->use_empty()) {
854 CallInst *CI = cast<CallInst>(F->use_back());
855 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
856 new StoreInst(V, CI->getOperand(1), CI);
857 CI->getParent()->getInstList().erase(CI);
858 }
859 Result->getFunctionList().erase(F);
860 }
861 }
862
Chris Lattner00950542001-06-06 20:29:01 +0000863 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
864 ParserResult = 0;
865
866 return Result;
867}
868
869%}
870
871%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000872 llvm::Module *ModuleVal;
873 llvm::Function *FunctionVal;
874 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
875 llvm::BasicBlock *BasicBlockVal;
876 llvm::TerminatorInst *TermInstVal;
877 llvm::Instruction *InstVal;
878 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000879
Brian Gaeked0fde302003-11-11 22:41:34 +0000880 const llvm::Type *PrimType;
881 llvm::PATypeHolder *TypeVal;
882 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000883
Brian Gaeked0fde302003-11-11 22:41:34 +0000884 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
885 std::vector<llvm::Value*> *ValueList;
886 std::list<llvm::PATypeHolder> *TypeList;
887 std::list<std::pair<llvm::Value*,
888 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
889 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
890 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000891
Brian Gaeked0fde302003-11-11 22:41:34 +0000892 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000893 int64_t SInt64Val;
894 uint64_t UInt64Val;
895 int SIntVal;
896 unsigned UIntVal;
897 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000898 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000899
Chris Lattner30c89792001-09-07 16:35:17 +0000900 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000901 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000902
Brian Gaeked0fde302003-11-11 22:41:34 +0000903 llvm::Instruction::BinaryOps BinaryOpVal;
904 llvm::Instruction::TermOps TermOpVal;
905 llvm::Instruction::MemoryOps MemOpVal;
906 llvm::Instruction::OtherOps OtherOpVal;
907 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000908}
909
Chris Lattner79df7c02002-03-26 18:01:55 +0000910%type <ModuleVal> Module FunctionList
911%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000912%type <BasicBlockVal> BasicBlock InstructionList
913%type <TermInstVal> BBTerminatorInst
914%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000915%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000916%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000917%type <ArgList> ArgList ArgListH
918%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000919%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000920%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000921%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000922%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000923%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000924%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000925%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000926%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000927%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000928
Chris Lattner2079fde2001-10-13 06:41:08 +0000929// ValueRef - Unresolved reference to a definition or BB
930%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000931%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000932// Tokens and types for handling constant integer values
933//
934// ESINT64VAL - A negative number within long long range
935%token <SInt64Val> ESINT64VAL
936
937// EUINT64VAL - A positive number within uns. long long range
938%token <UInt64Val> EUINT64VAL
939%type <SInt64Val> EINT64VAL
940
941%token <SIntVal> SINTVAL // Signed 32 bit ints...
942%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
943%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000944%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000945
946// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000947%type <TypeVal> Types TypesV UpRTypes UpRTypesV
948%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000949%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
950%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000951
Chris Lattner6c23f572003-08-22 05:42:10 +0000952%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
953%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000954
955
Chris Lattner91ef4602004-03-31 03:49:47 +0000956%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000957%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000958%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000959%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000960
961// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000962%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000963
Chris Lattner00950542001-06-06 20:29:01 +0000964// Binary Operators
965%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000966%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000967%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000968%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000969
970// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000971%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000972
Chris Lattner027dcc52001-07-08 21:10:27 +0000973// Other Operators
974%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000975%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000976%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000977
Chris Lattner00950542001-06-06 20:29:01 +0000978%start Module
979%%
980
981// Handle constant integer size restriction and conversion...
982//
Chris Lattner51727be2002-06-04 21:58:56 +0000983INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000984INTVAL : UINTVAL {
985 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
986 ThrowException("Value too large for type!");
987 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000988};
Chris Lattner00950542001-06-06 20:29:01 +0000989
990
Chris Lattner51727be2002-06-04 21:58:56 +0000991EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000992EINT64VAL : EUINT64VAL {
993 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
994 ThrowException("Value too large for type!");
995 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000996};
Chris Lattner00950542001-06-06 20:29:01 +0000997
Chris Lattner00950542001-06-06 20:29:01 +0000998// Operations that are notably excluded from this list include:
999// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1000//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001001ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1002LogicalOps : AND | OR | XOR;
1003SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
1004BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
1005
Chris Lattner51727be2002-06-04 21:58:56 +00001006ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001007
Chris Lattnere98dda62001-07-14 06:10:16 +00001008// These are some types that allow classification if we only want a particular
1009// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001010SIntType : LONG | INT | SHORT | SBYTE;
1011UIntType : ULONG | UINT | USHORT | UBYTE;
1012IntType : SIntType | UIntType;
1013FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001014
Chris Lattnere98dda62001-07-14 06:10:16 +00001015// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001016OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001017 $$ = $1;
1018 }
1019 | /*empty*/ {
1020 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001021 };
Chris Lattner00950542001-06-06 20:29:01 +00001022
Chris Lattner4ad02e72003-04-16 20:28:45 +00001023OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1024 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001025 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001026 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1027 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001028
1029//===----------------------------------------------------------------------===//
1030// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001031// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001032// access to it, a user must explicitly use TypesV.
1033//
1034
1035// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001036TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1037UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001038
1039Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001040 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001041 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1042 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001043 };
Chris Lattner30c89792001-09-07 16:35:17 +00001044
1045
1046// Derived types are added later...
1047//
Chris Lattner51727be2002-06-04 21:58:56 +00001048PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1049PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001050UpRTypes : OPAQUE {
1051 $$ = new PATypeHolder(OpaqueType::get());
1052 }
1053 | PrimType {
1054 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001055 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001056UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001057 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001058};
Chris Lattner30c89792001-09-07 16:35:17 +00001059
Chris Lattner30c89792001-09-07 16:35:17 +00001060// Include derived types in the Types production.
1061//
1062UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001063 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001064 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001065 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001066 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001067 UR_OUT("New Upreference!\n");
1068 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001069 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001070 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001071 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001072 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001073 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1074 if (isVarArg) Params.pop_back();
1075
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001076 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001077 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001078 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001079 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001080 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001081 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001082 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001083 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001084 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001085 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001086 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001087 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001088
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001089 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001090 delete $2;
1091 }
1092 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001093 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001094 }
1095 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001096 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001097 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001098 };
Chris Lattner30c89792001-09-07 16:35:17 +00001099
Chris Lattner7e708292002-06-25 16:13:24 +00001100// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001101// declaration type lists
1102//
1103TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001104 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001105 $$->push_back(*$1); delete $1;
1106 }
1107 | TypeListI ',' UpRTypes {
1108 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001109 };
Chris Lattner30c89792001-09-07 16:35:17 +00001110
Chris Lattner7e708292002-06-25 16:13:24 +00001111// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001112ArgTypeListI : TypeListI
1113 | TypeListI ',' DOTDOTDOT {
1114 ($$=$1)->push_back(Type::VoidTy);
1115 }
1116 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001117 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001118 }
1119 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001120 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001121 };
Chris Lattner30c89792001-09-07 16:35:17 +00001122
Chris Lattnere98dda62001-07-14 06:10:16 +00001123// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001124// production is used ONLY to represent constants that show up AFTER a 'const',
1125// 'constant' or 'global' token at global scope. Constants that can be inlined
1126// into other expressions (such as integers and constexprs) are handled by the
1127// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001128//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001129ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001130 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001131 if (ATy == 0)
1132 ThrowException("Cannot make array constant with type: '" +
1133 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001134 const Type *ETy = ATy->getElementType();
1135 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001136
Chris Lattner30c89792001-09-07 16:35:17 +00001137 // Verify that we have the correct size...
1138 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001139 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001140 utostr($3->size()) + " arguments, but has size of " +
1141 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001142
Chris Lattner30c89792001-09-07 16:35:17 +00001143 // Verify all elements are correct type!
1144 for (unsigned i = 0; i < $3->size(); i++) {
1145 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001146 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001147 ETy->getDescription() +"' as required!\nIt is of type '"+
1148 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001149 }
1150
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001151 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001152 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001153 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001154 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001155 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001156 if (ATy == 0)
1157 ThrowException("Cannot make array constant with type: '" +
1158 (*$1)->getDescription() + "'!");
1159
1160 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001161 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001162 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001163 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001164 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001165 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001166 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001167 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001168 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001169 if (ATy == 0)
1170 ThrowException("Cannot make array constant with type: '" +
1171 (*$1)->getDescription() + "'!");
1172
Chris Lattner30c89792001-09-07 16:35:17 +00001173 int NumElements = ATy->getNumElements();
1174 const Type *ETy = ATy->getElementType();
1175 char *EndStr = UnEscapeLexed($3, true);
1176 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001177 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001178 itostr((int)(EndStr-$3)) +
1179 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001180 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001181 if (ETy == Type::SByteTy) {
1182 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001183 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001184 } else if (ETy == Type::UByteTy) {
1185 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001186 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001187 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001188 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001189 ThrowException("Cannot build string arrays of non byte sized elements!");
1190 }
Chris Lattner30c89792001-09-07 16:35:17 +00001191 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001192 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001193 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001194 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001195 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001196 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001197 if (STy == 0)
1198 ThrowException("Cannot make struct constant with type: '" +
1199 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001200
Chris Lattnerc6212f12003-05-21 16:06:56 +00001201 if ($3->size() != STy->getNumContainedTypes())
1202 ThrowException("Illegal number of initializers for structure type!");
1203
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001204 // Check to ensure that constants are compatible with the type initializer!
1205 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001206 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001207 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001208 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001209 "' for element #" + utostr(i) +
1210 " of structure initializer!");
1211
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001212 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001213 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001214 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001215 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001216 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001217 if (STy == 0)
1218 ThrowException("Cannot make struct constant with type: '" +
1219 (*$1)->getDescription() + "'!");
1220
1221 if (STy->getNumContainedTypes() != 0)
1222 ThrowException("Illegal number of initializers for structure type!");
1223
1224 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1225 delete $1;
1226 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001227 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001228 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001229 if (PTy == 0)
1230 ThrowException("Cannot make null pointer constant with type: '" +
1231 (*$1)->getDescription() + "'!");
1232
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001233 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001234 delete $1;
1235 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001236 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001237 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001238 if (Ty == 0)
1239 ThrowException("Global const reference must be a pointer type!");
1240
Chris Lattner3101c252002-08-15 17:58:33 +00001241 // ConstExprs can exist in the body of a function, thus creating
1242 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1243 // the context of a function, getValNonImprovising will search the functions
1244 // symbol table instead of the module symbol table for the global symbol,
1245 // which throws things all off. To get around this, we just tell
1246 // getValNonImprovising that we are at global scope here.
1247 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001248 Function *SavedCurFn = CurFun.CurrentFunction;
1249 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001250
Chris Lattner2079fde2001-10-13 06:41:08 +00001251 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001252
Chris Lattner394f2eb2003-10-16 20:12:13 +00001253 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001254
Chris Lattner2079fde2001-10-13 06:41:08 +00001255 // If this is an initializer for a constant pointer, which is referencing a
1256 // (currently) undefined variable, create a stub now that shall be replaced
1257 // in the future with the right type of variable.
1258 //
1259 if (V == 0) {
1260 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1261 const PointerType *PT = cast<PointerType>(Ty);
1262
1263 // First check to see if the forward references value is already created!
1264 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001265 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001266
1267 if (I != CurModule.GlobalRefs.end()) {
1268 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001269 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001270 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001271 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001272 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001273 false,
1274 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001275 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001276 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001277
1278 // Must temporarily push this value into the module table...
1279 CurModule.CurrentModule->getGlobalList().push_back(GV);
1280 V = GV;
1281 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001282 }
1283
Chris Lattner2079fde2001-10-13 06:41:08 +00001284 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001285 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001286 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001287 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001288 | Types ConstExpr {
1289 if ($1->get() != $2->getType())
1290 ThrowException("Mismatched types for constant expression!");
1291 $$ = $2;
1292 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001293 }
1294 | Types ZEROINITIALIZER {
1295 $$ = Constant::getNullValue($1->get());
1296 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001297 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001298
Chris Lattnere43f40b2002-10-09 00:25:32 +00001299ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001300 if (!ConstantSInt::isValueValidForType($1, $2))
1301 ThrowException("Constant value doesn't fit in type!");
1302 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001303 }
1304 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001305 if (!ConstantUInt::isValueValidForType($1, $2))
1306 ThrowException("Constant value doesn't fit in type!");
1307 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001308 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001309 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001310 $$ = ConstantBool::True;
1311 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001312 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001313 $$ = ConstantBool::False;
1314 }
1315 | FPType FPVAL { // Float & Double constants
1316 $$ = ConstantFP::get($1, $2);
1317 };
1318
Chris Lattner00950542001-06-06 20:29:01 +00001319
Chris Lattnerd78700d2002-08-16 21:14:40 +00001320ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001321 if (!$3->getType()->isFirstClassType())
1322 ThrowException("cast constant expression from a non-primitive type: '" +
1323 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001324 if (!$5->get()->isFirstClassType())
1325 ThrowException("cast constant expression to a non-primitive type: '" +
1326 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001327 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001328 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001329 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001330 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1331 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001332 ThrowException("GetElementPtr requires a pointer operand!");
1333
Chris Lattner830b6f92004-04-05 01:30:04 +00001334 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1335 // indices to uint struct indices for compatibility.
1336 generic_gep_type_iterator<std::vector<Value*>::iterator>
1337 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1338 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1339 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1340 if (isa<StructType>(*GTI)) // Only change struct indices
1341 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1342 if (CUI->getType() == Type::UByteTy)
1343 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1344
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001345 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001346 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001347 if (!IdxTy)
1348 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001349
Chris Lattner9232b992003-04-22 18:42:41 +00001350 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001351 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1352 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001353 IdxVec.push_back(C);
1354 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001355 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001356
Chris Lattnerd78700d2002-08-16 21:14:40 +00001357 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001358
Chris Lattnerd78700d2002-08-16 21:14:40 +00001359 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001360 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001361 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1362 if ($3->getType() != Type::BoolTy)
1363 ThrowException("Select condition must be of boolean type!");
1364 if ($5->getType() != $7->getType())
1365 ThrowException("Select operand types must match!");
1366 $$ = ConstantExpr::getSelect($3, $5, $7);
1367 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001368 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001369 if ($3->getType() != $5->getType())
1370 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001371 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001372 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001373 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001374 if ($5->getType() != Type::UByteTy)
1375 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001376 if (!$3->getType()->isInteger())
1377 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001378 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001379 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001380
1381
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001382// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001383ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001384 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001385 }
1386 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001387 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001388 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001389 };
Chris Lattner00950542001-06-06 20:29:01 +00001390
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001391
Chris Lattner1781aca2001-09-18 04:00:54 +00001392// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001393GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001394
Chris Lattner00950542001-06-06 20:29:01 +00001395
Chris Lattner0e73ce62002-05-02 19:11:13 +00001396//===----------------------------------------------------------------------===//
1397// Rules to match Modules
1398//===----------------------------------------------------------------------===//
1399
1400// Module rule: Capture the result of parsing the whole file into a result
1401// variable...
1402//
1403Module : FunctionList {
1404 $$ = ParserResult = $1;
1405 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001406};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001407
Chris Lattner7e708292002-06-25 16:13:24 +00001408// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001409//
1410FunctionList : FunctionList Function {
1411 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001412 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001413 }
1414 | FunctionList FunctionProto {
1415 $$ = $1;
1416 }
1417 | FunctionList IMPLEMENTATION {
1418 $$ = $1;
1419 }
1420 | ConstPool {
1421 $$ = CurModule.CurrentModule;
1422 // Resolve circular types before we parse the body of the module
1423 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001424 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001425
Chris Lattnere98dda62001-07-14 06:10:16 +00001426// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001427ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001428 // FIXME: THIS SHOULD REALLY BE ELIMINATED. It is totally unneeded.
1429 if (!setValueNameMergingDuplicates($4, $2))
Chris Lattneradf99702003-03-03 23:28:55 +00001430 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001431 }
Chris Lattner30c89792001-09-07 16:35:17 +00001432 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001433 // Eagerly resolve types. This is not an optimization, this is a
1434 // requirement that is due to the fact that we could have this:
1435 //
1436 // %list = type { %list * }
1437 // %list = type { %list * } ; repeated type decl
1438 //
1439 // If types are not resolved eagerly, then the two types will not be
1440 // determined to be the same type!
1441 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001442 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001443
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001444 if (!setTypeName(*$4, $2) && !$2) {
1445 // If this is a named type that is not a redefinition, add it to the slot
1446 // table.
Chris Lattner64116f92004-07-14 01:33:11 +00001447 if (inFunctionScope())
1448 CurFun.Types.push_back(*$4);
1449 else
1450 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001451 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001452
1453 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001454 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001455 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001456 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001457 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001458 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001459 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001460 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001461 if (Initializer == 0)
1462 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001463
Chris Lattnerdda71962001-11-26 18:54:16 +00001464 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001465 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001466 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001467 int Slot = InsertValue(GV, CurModule.Values);
1468
1469 if (Slot != -1) {
1470 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1471 } else {
1472 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1473 (char*)GV->getName().c_str()));
1474 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001475 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001476 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001477 | ConstPool OptAssign EXTERNAL GlobalType Types {
1478 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001479 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001480 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001481 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001482 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001483 int Slot = InsertValue(GV, CurModule.Values);
1484
1485 if (Slot != -1) {
1486 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1487 } else {
1488 assert(GV->hasName() && "Not named and not numbered!?");
1489 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1490 (char*)GV->getName().c_str()));
1491 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001492 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001493 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001494 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001495 | ConstPool TARGET TargetDefinition {
1496 }
Chris Lattner00950542001-06-06 20:29:01 +00001497 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001498 };
Chris Lattner00950542001-06-06 20:29:01 +00001499
1500
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001501
1502BigOrLittle : BIG { $$ = Module::BigEndian; };
1503BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1504
1505TargetDefinition : ENDIAN '=' BigOrLittle {
1506 CurModule.CurrentModule->setEndianness($3);
1507 }
1508 | POINTERSIZE '=' EUINT64VAL {
1509 if ($3 == 32)
1510 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1511 else if ($3 == 64)
1512 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1513 else
1514 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1515 };
1516
1517
Chris Lattner00950542001-06-06 20:29:01 +00001518//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001519// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001520//===----------------------------------------------------------------------===//
1521
Chris Lattner6c23f572003-08-22 05:42:10 +00001522Name : VAR_ID | STRINGCONSTANT;
1523OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001524
Chris Lattner6c23f572003-08-22 05:42:10 +00001525ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001526 if (*$1 == Type::VoidTy)
1527 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001528 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001529};
Chris Lattner00950542001-06-06 20:29:01 +00001530
Chris Lattner69da5cf2002-10-13 20:57:00 +00001531ArgListH : ArgListH ',' ArgVal {
1532 $$ = $1;
1533 $1->push_back(*$3);
1534 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001535 }
1536 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001537 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001538 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001539 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001540 };
Chris Lattner00950542001-06-06 20:29:01 +00001541
1542ArgList : ArgListH {
1543 $$ = $1;
1544 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001545 | ArgListH ',' DOTDOTDOT {
1546 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001547 $$->push_back(std::pair<PATypeHolder*,
1548 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001549 }
1550 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001551 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1552 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001553 }
Chris Lattner00950542001-06-06 20:29:01 +00001554 | /* empty */ {
1555 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001556 };
Chris Lattner00950542001-06-06 20:29:01 +00001557
Chris Lattner6c23f572003-08-22 05:42:10 +00001558FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001559 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001560 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001561
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001562 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1563 ThrowException("LLVM functions cannot return aggregate types!");
1564
Chris Lattner9232b992003-04-22 18:42:41 +00001565 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001566 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001567 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001568 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001569 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001570 }
Chris Lattner00950542001-06-06 20:29:01 +00001571
Chris Lattner2079fde2001-10-13 06:41:08 +00001572 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1573 if (isVarArg) ParamTypeList.pop_back();
1574
Chris Lattner1f862af2003-04-16 18:13:57 +00001575 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001576 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001577 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001578
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001579 Function *Fn = 0;
1580 // Is the function already in symtab?
1581 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1582 // Yes it is. If this is the case, either we need to be a forward decl,
1583 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001584 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001585 ThrowException("Redefinition of function '" + FunctionName + "'!");
1586
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001587 // Make sure to strip off any argument names so we can't get conflicts...
1588 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1589 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001590
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001591 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001592 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1593 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001594 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001595 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001596 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001597 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001598
Chris Lattner394f2eb2003-10-16 20:12:13 +00001599 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001600
Chris Lattner7e708292002-06-25 16:13:24 +00001601 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001602 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001603 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001604 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001605 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001606 delete $4->back().first;
1607 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001608 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001609 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001610 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001611 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001612 delete I->first; // Delete the typeholder...
1613
Chris Lattner8ab406d2004-07-13 07:59:27 +00001614 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001615 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001616 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001617
Chris Lattner1f862af2003-04-16 18:13:57 +00001618 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001619 }
Chris Lattner51727be2002-06-04 21:58:56 +00001620};
Chris Lattner00950542001-06-06 20:29:01 +00001621
Chris Lattner9b02cc32002-05-03 18:23:48 +00001622BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1623
Chris Lattner4ad02e72003-04-16 20:28:45 +00001624FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001625 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001626
Chris Lattner4ad02e72003-04-16 20:28:45 +00001627 // Make sure that we keep track of the linkage type even if there was a
1628 // previous "declare".
1629 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001630
Chris Lattner7e708292002-06-25 16:13:24 +00001631 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001632 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001633};
Chris Lattner00950542001-06-06 20:29:01 +00001634
Chris Lattner9b02cc32002-05-03 18:23:48 +00001635END : ENDTOK | '}'; // Allow end of '}' to end a function
1636
Chris Lattner79df7c02002-03-26 18:01:55 +00001637Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001638 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001639};
Chris Lattner00950542001-06-06 20:29:01 +00001640
Chris Lattner394f2eb2003-10-16 20:12:13 +00001641FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1642 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001643 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001644};
Chris Lattner00950542001-06-06 20:29:01 +00001645
1646//===----------------------------------------------------------------------===//
1647// Rules to match Basic Blocks
1648//===----------------------------------------------------------------------===//
1649
1650ConstValueRef : ESINT64VAL { // A reference to a direct constant
1651 $$ = ValID::create($1);
1652 }
1653 | EUINT64VAL {
1654 $$ = ValID::create($1);
1655 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001656 | FPVAL { // Perhaps it's an FP constant?
1657 $$ = ValID::create($1);
1658 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001659 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001660 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001661 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001662 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001663 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001664 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001665 | NULL_TOK {
1666 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001667 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001668 | ConstExpr {
1669 $$ = ValID::create($1);
1670 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001671
Chris Lattner2079fde2001-10-13 06:41:08 +00001672// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1673// another value.
1674//
1675SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001676 $$ = ValID::create($1);
1677 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001678 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001679 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001680 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001681
1682// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001683ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001684
Chris Lattner00950542001-06-06 20:29:01 +00001685
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001686// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1687// type immediately preceeds the value reference, and allows complex constant
1688// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001689ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001690 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001691 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001692
Chris Lattner00950542001-06-06 20:29:01 +00001693BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001694 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001695 }
Chris Lattner7e708292002-06-25 16:13:24 +00001696 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001697 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001698 };
Chris Lattner00950542001-06-06 20:29:01 +00001699
1700
1701// Basic blocks are terminated by branching instructions:
1702// br, br/cc, switch, ret
1703//
Chris Lattner2079fde2001-10-13 06:41:08 +00001704BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001705 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001706 InsertValue($3);
1707
1708 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001709 InsertValue($1);
1710 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001711 };
Chris Lattner00950542001-06-06 20:29:01 +00001712
1713InstructionList : InstructionList Inst {
1714 $1->getInstList().push_back($2);
1715 $$ = $1;
1716 }
1717 | /* empty */ {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001718 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner22ae2322004-07-13 08:39:15 +00001719 }
1720 | LABELSTR {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001721 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner51727be2002-06-04 21:58:56 +00001722 };
Chris Lattner00950542001-06-06 20:29:01 +00001723
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001724BBTerminatorInst : RET ResolvedVal { // Return with a result...
1725 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001726 }
1727 | RET VOID { // Return with no result...
1728 $$ = new ReturnInst();
1729 }
1730 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001731 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001732 } // Conditional Branch...
1733 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001734 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001735 }
1736 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001737 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner00950542001-06-06 20:29:01 +00001738 $$ = S;
1739
Chris Lattner9232b992003-04-22 18:42:41 +00001740 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001741 E = $8->end();
1742 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001743 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001744 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001745 }
Chris Lattner196850c2003-05-15 21:30:00 +00001746 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001747 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner196850c2003-05-15 21:30:00 +00001748 $$ = S;
1749 }
Chris Lattner64116f92004-07-14 01:33:11 +00001750 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1751 UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001752 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001753 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001754
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001755 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1756 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001757 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001758 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001759 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001760 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1761 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001762 ParamTypes.push_back((*I)->getType());
1763 }
1764
1765 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1766 if (isVarArg) ParamTypes.pop_back();
1767
Chris Lattner79df7c02002-03-26 18:01:55 +00001768 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001769 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001770 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001771
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001772 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001773
Chris Lattner64116f92004-07-14 01:33:11 +00001774 BasicBlock *Normal = getBBVal($9);
1775 BasicBlock *Except = getBBVal($12);
Chris Lattner2079fde2001-10-13 06:41:08 +00001776
1777 // Create the call node...
1778 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001779 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001780 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001781 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001782 // correctly!
1783 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001784 FunctionType::param_iterator I = Ty->param_begin();
1785 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001786 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001787
1788 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1789 if ((*ArgI)->getType() != *I)
1790 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001791 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001792
1793 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1794 ThrowException("Invalid number of parameters detected!");
1795
Chris Lattner6cdb0112001-11-26 16:54:11 +00001796 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001797 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001798 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001799 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001800 }
1801 | UNWIND {
1802 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001803 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001804
1805
Chris Lattner00950542001-06-06 20:29:01 +00001806
1807JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1808 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001809 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001810 if (V == 0)
1811 ThrowException("May only switch on a constant pool value!");
1812
Chris Lattner64116f92004-07-14 01:33:11 +00001813 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001814 }
1815 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001816 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001817 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001818
1819 if (V == 0)
1820 ThrowException("May only switch on a constant pool value!");
1821
Chris Lattner64116f92004-07-14 01:33:11 +00001822 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001823 };
Chris Lattner00950542001-06-06 20:29:01 +00001824
1825Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001826 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001827 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001828 InsertValue($2);
1829 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001830};
Chris Lattner00950542001-06-06 20:29:01 +00001831
Chris Lattnerc24d2082001-06-11 15:04:20 +00001832PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001833 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001834 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001835 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001836 }
1837 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1838 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001839 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001840 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001841 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001842
1843
Chris Lattner30c89792001-09-07 16:35:17 +00001844ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001845 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001846 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001847 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001848 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001849 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001850 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001851 };
Chris Lattner00950542001-06-06 20:29:01 +00001852
1853// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001854ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001855
Chris Lattner4a6482b2002-09-10 19:57:26 +00001856InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1857 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1858 ThrowException("Arithmetic operator requires integer or FP operands!");
1859 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1860 if ($$ == 0)
1861 ThrowException("binary operator returned null!");
1862 delete $2;
1863 }
1864 | LogicalOps Types ValueRef ',' ValueRef {
1865 if (!(*$2)->isIntegral())
1866 ThrowException("Logical operator requires integral operands!");
1867 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1868 if ($$ == 0)
1869 ThrowException("binary operator returned null!");
1870 delete $2;
1871 }
1872 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001873 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001874 if ($$ == 0)
1875 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001876 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001877 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001878 | NOT ResolvedVal {
1879 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1880 << " Replacing with 'xor'.\n";
1881
1882 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1883 if (Ones == 0)
1884 ThrowException("Expected integral type for not instruction!");
1885
1886 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001887 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001888 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001889 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001890 | ShiftOps ResolvedVal ',' ResolvedVal {
1891 if ($4->getType() != Type::UByteTy)
1892 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001893 if (!$2->getType()->isInteger())
1894 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001895 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001896 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001897 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001898 if (!$4->get()->isFirstClassType())
1899 ThrowException("cast instruction to a non-primitive type: '" +
1900 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001901 $$ = new CastInst($2, *$4);
1902 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001903 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001904 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1905 if ($2->getType() != Type::BoolTy)
1906 ThrowException("select condition must be boolean!");
1907 if ($4->getType() != $6->getType())
1908 ThrowException("select value types should match!");
1909 $$ = new SelectInst($2, $4, $6);
1910 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001911 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001912 // FIXME: This is emulation code for an obsolete syntax. This should be
1913 // removed at some point.
1914 if (!ObsoleteVarArgs) {
1915 std::cerr << "WARNING: this file uses obsolete features. "
1916 << "Assemble and disassemble to update it.\n";
1917 ObsoleteVarArgs = true;
1918 }
1919
1920 // First, load the valist...
1921 Instruction *CurVAList = new LoadInst($2, "");
1922 CurBB->getInstList().push_back(CurVAList);
1923
1924 // Emit the vaarg instruction.
1925 $$ = new VAArgInst(CurVAList, *$4);
1926
1927 // Now we must advance the pointer and update it in memory.
1928 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1929 CurBB->getInstList().push_back(TheVANext);
1930
1931 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1932 delete $4;
1933 }
1934 | VAARG ResolvedVal ',' Types {
1935 $$ = new VAArgInst($2, *$4);
1936 delete $4;
1937 }
1938 | VANEXT ResolvedVal ',' Types {
1939 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001940 delete $4;
1941 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001942 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001943 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001944 if (!Ty->isFirstClassType())
1945 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001946 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001947 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001948 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001949 if ($2->front().first->getType() != Ty)
1950 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001951 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001952 $2->pop_front();
1953 }
1954 delete $2; // Free the list...
1955 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001956 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001957 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001958 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001959
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001960 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1961 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001962 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001963 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001964 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001965 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1966 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001967 ParamTypes.push_back((*I)->getType());
1968 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001969
1970 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1971 if (isVarArg) ParamTypes.pop_back();
1972
Chris Lattner79df7c02002-03-26 18:01:55 +00001973 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001974 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001975 }
Chris Lattner00950542001-06-06 20:29:01 +00001976
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001977 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001978
Chris Lattner8b81bf52001-07-25 22:47:46 +00001979 // Create the call node...
1980 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001981 // Make sure no arguments is a good thing!
1982 if (Ty->getNumParams() != 0)
1983 ThrowException("No arguments passed to a function that "
1984 "expects arguments!");
1985
Chris Lattner9232b992003-04-22 18:42:41 +00001986 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001987 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001988 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001989 // correctly!
1990 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001991 FunctionType::param_iterator I = Ty->param_begin();
1992 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001993 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001994
1995 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1996 if ((*ArgI)->getType() != *I)
1997 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001998 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001999
Chris Lattner8b81bf52001-07-25 22:47:46 +00002000 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00002001 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002002
Chris Lattner6cdb0112001-11-26 16:54:11 +00002003 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002004 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00002005 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002006 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00002007 }
2008 | MemoryInst {
2009 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002010 };
Chris Lattner00950542001-06-06 20:29:01 +00002011
Chris Lattner6cdb0112001-11-26 16:54:11 +00002012
2013// IndexList - List of indices for GEP based instructions...
2014IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002015 $$ = $2;
2016 } | /* empty */ {
2017 $$ = new std::vector<Value*>();
2018 };
2019
2020OptVolatile : VOLATILE {
2021 $$ = true;
2022 }
2023 | /* empty */ {
2024 $$ = false;
2025 };
2026
Chris Lattner027dcc52001-07-08 21:10:27 +00002027
Chris Lattner00950542001-06-06 20:29:01 +00002028MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002029 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002030 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002031 }
2032 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002033 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002034 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002035 }
2036 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002037 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002038 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002039 }
2040 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002041 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002042 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002043 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002044 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002045 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002046 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002047 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002048 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002049 }
2050
Chris Lattner15c9c032003-09-08 18:20:29 +00002051 | OptVolatile LOAD Types ValueRef {
2052 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002053 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002054 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002055 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002056 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002057 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002058 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2059 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002060 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002061 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002062 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002063 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002064 if (ElTy != $3->getType())
2065 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002066 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002067
Chris Lattner79ad13802003-09-08 20:29:46 +00002068 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002069 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002070 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002071 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002072 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002073 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002074
2075 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2076 // indices to uint struct indices for compatibility.
2077 generic_gep_type_iterator<std::vector<Value*>::iterator>
2078 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2079 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2080 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2081 if (isa<StructType>(*GTI)) // Only change struct indices
2082 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2083 if (CUI->getType() == Type::UByteTy)
2084 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2085
Chris Lattner30c89792001-09-07 16:35:17 +00002086 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002087 ThrowException("Invalid getelementptr indices for type '" +
2088 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002089 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2090 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002091 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002092
Brian Gaeked0fde302003-11-11 22:41:34 +00002093
Chris Lattner00950542001-06-06 20:29:01 +00002094%%
Chris Lattner09083092001-07-08 04:57:15 +00002095int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002096 std::string where
2097 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002098 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002099 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002100 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002101 errMsg += "end-of-file.";
2102 else
Chris Lattner9232b992003-04-22 18:42:41 +00002103 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002104 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002105 return 0;
2106}