blob: 4724a0f1030f2b456b7712954c2f91adbecfce2d [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000020#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/STLExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000024#include <algorithm>
25#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000026#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000027#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000028
Chris Lattner386a3b72001-10-16 19:54:17 +000029int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000030int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000031int yyparse();
32
Brian Gaeked0fde302003-11-11 22:41:34 +000033namespace llvm {
34
Chris Lattner00950542001-06-06 20:29:01 +000035static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000036std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000043#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000044#else
45#define UR_OUT(X)
46#endif
47
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000048#define YYERROR_VERBOSE 1
49
Chris Lattner99e7ab72003-10-18 05:53:13 +000050// HACK ALERT: This variable is used to implement the automatic conversion of
51// variable argument instructions from their old to new forms. When this
52// compatiblity "Feature" is removed, this should be too.
53//
54static BasicBlock *CurBB;
55static bool ObsoleteVarArgs;
56
57
Chris Lattner7e708292002-06-25 16:13:24 +000058// This contains info used when building the body of a function. It is
59// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000060//
Chris Lattner9232b992003-04-22 18:42:41 +000061typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner735f2702004-07-08 22:30:50 +000062static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
63 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000064
65static struct PerModuleInfo {
66 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000067 std::map<const Type *, ValueList> Values; // Module level numbered definitions
68 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000069 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000070 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000071
Chris Lattnerbc721ed2004-07-13 08:28:21 +000072 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
73 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000074 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000075 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
76
Chris Lattner2079fde2001-10-13 06:41:08 +000077 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
78 // references to global values. Global values may be referenced before they
79 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000080 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000081 //
Chris Lattner9232b992003-04-22 18:42:41 +000082 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000083 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000084 GlobalRefsType GlobalRefs;
85
Chris Lattner00950542001-06-06 20:29:01 +000086 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000087 // If we could not resolve some functions at function compilation time
88 // (calls to functions before they are defined), resolve them now... Types
89 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000090 //
Chris Lattner00950542001-06-06 20:29:01 +000091 ResolveDefinitions(LateResolveValues);
92
Chris Lattner2079fde2001-10-13 06:41:08 +000093 // Check to make sure that all global value forward references have been
94 // resolved!
95 //
96 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000097 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000098
99 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
100 I != E; ++I) {
101 UndefinedReferences += " " + I->first.first->getDescription() + " " +
102 I->first.second.getName() + "\n";
103 }
104 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000105 }
106
Chris Lattner7e708292002-06-25 16:13:24 +0000107 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000108 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000109 CurrentModule = 0;
110 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000111
112
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000113 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000114 // is used to remove things from the forward declaration map, resolving them
115 // to the correct thing as needed.
116 //
117 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
118 // Check to see if there is a forward reference to this global variable...
119 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000120 GlobalRefsType::iterator I =
121 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000122
123 if (I != GlobalRefs.end()) {
Chris Lattnerbc207592004-03-08 06:09:57 +0000124 GlobalValue *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000125 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner38ab6bf2004-07-13 06:58:07 +0000126
127 // Replace all uses of the placeholder with the new GV
128 OldGV->replaceAllUsesWith(GV);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000129
130 // Remove OldGV from the module...
Chris Lattnerbc207592004-03-08 06:09:57 +0000131 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
132 CurrentModule->getGlobalList().erase(GVar);
133 else
134 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000135
Chris Lattner2079fde2001-10-13 06:41:08 +0000136 // Remove the map entry for the global now that it has been created...
137 GlobalRefs.erase(I);
138 }
139 }
140
Chris Lattner00950542001-06-06 20:29:01 +0000141} CurModule;
142
Chris Lattner79df7c02002-03-26 18:01:55 +0000143static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000144 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000145
Chris Lattner735f2702004-07-08 22:30:50 +0000146 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
147 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000148 std::vector<PATypeHolder> Types;
149 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000150 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000151
Chris Lattner2e2ed292004-07-14 06:28:35 +0000152 /// BBForwardRefs - When we see forward references to basic blocks, keep
153 /// track of them here.
154 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
155 std::vector<BasicBlock*> NumberedBlocks;
156 unsigned NextBBNum;
157
Chris Lattner79df7c02002-03-26 18:01:55 +0000158 inline PerFunctionInfo() {
159 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000160 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000161 }
162
Chris Lattner79df7c02002-03-26 18:01:55 +0000163 inline void FunctionStart(Function *M) {
164 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000165 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000166 }
167
Chris Lattner79df7c02002-03-26 18:01:55 +0000168 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000169 NumberedBlocks.clear();
170
171 // Any forward referenced blocks left?
172 if (!BBForwardRefs.empty())
173 ThrowException("Undefined reference to label " +
174 BBForwardRefs.begin()->second.first.getName());
175
176 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000177 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000178
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000179 // Make sure to resolve any constant expr references that might exist within
180 // the function we just declared itself.
181 ValID FID;
182 if (CurrentFunction->hasName()) {
183 FID = ValID::create((char*)CurrentFunction->getName().c_str());
184 } else {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000185 // Figure out which slot number if is...
Chris Lattner735f2702004-07-08 22:30:50 +0000186 ValueList &List = CurModule.Values[CurrentFunction->getType()];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000187 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000188 assert(i < List.size() && "Function not found!");
189 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000190 FID = ValID::create((int)i);
191 break;
192 }
193 }
194 }
195 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
196
Chris Lattner7e708292002-06-25 16:13:24 +0000197 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000198 Types.clear(); // Clear out function local types
Chris Lattner79df7c02002-03-26 18:01:55 +0000199 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000200 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000201 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000202} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000203
Chris Lattner394f2eb2003-10-16 20:12:13 +0000204static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000205
Chris Lattner00950542001-06-06 20:29:01 +0000206
207//===----------------------------------------------------------------------===//
208// Code to handle definitions of all the types
209//===----------------------------------------------------------------------===//
210
Chris Lattner735f2702004-07-08 22:30:50 +0000211static int InsertValue(Value *V,
212 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
213 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000214
215 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000216 ValueList &List = ValueTab[V->getType()];
217 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000218 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000219}
220
Chris Lattner30c89792001-09-07 16:35:17 +0000221static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000222 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000223 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000224 unsigned Num = (unsigned)D.Num;
225
226 // Module constants occupy the lowest numbered slots...
227 if (Num < CurModule.Types.size())
228 return CurModule.Types[Num];
229
230 Num -= CurModule.Types.size();
231
232 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000233 if (Num <= CurFun.Types.size())
234 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000235 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000236 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000237 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000238 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000239 SymbolTable *SymTab = 0;
Reid Spencer8ce1da72004-07-04 12:19:05 +0000240 Type *N = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000241 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000242 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000243 N = SymTab->lookupType(Name);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000244 }
Chris Lattner30c89792001-09-07 16:35:17 +0000245
246 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000247 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000248 // hasn't been added to the module...
249 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000250 SymTab = &CurModule.CurrentModule->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000251 N = SymTab->lookupType(Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000252 if (N == 0) break;
253 }
254
255 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000256 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000257 }
258 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000259 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000260 }
261
262 // If we reached here, we referenced either a symbol that we don't know about
263 // or an id number that hasn't been read yet. We may be referencing something
264 // forward, so just create an entry to be resolved later and get to it...
265 //
266 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
267
Chris Lattner9232b992003-04-22 18:42:41 +0000268 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000269 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000270
Chris Lattner9232b992003-04-22 18:42:41 +0000271 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000272 if (I != LateResolver.end()) {
273 return I->second;
274 }
Chris Lattner30c89792001-09-07 16:35:17 +0000275
Chris Lattner82269592001-10-22 06:01:08 +0000276 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000277 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000278 return Typ;
279}
280
Chris Lattner9232b992003-04-22 18:42:41 +0000281static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000282 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000283 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000284 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000285 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000286}
287
Chris Lattner2079fde2001-10-13 06:41:08 +0000288// getValNonImprovising - Look up the value specified by the provided type and
289// the provided ValID. If the value exists and has already been defined, return
290// it. Otherwise return null.
291//
292static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000293 if (isa<FunctionType>(Ty))
294 ThrowException("Functions are not values and "
295 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000296
Chris Lattner30c89792001-09-07 16:35:17 +0000297 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000298 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000299 unsigned Num = (unsigned)D.Num;
300
301 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000302 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000303 if (VI != CurModule.Values.end()) {
304 if (Num < VI->second.size())
305 return VI->second[Num];
306 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000307 }
308
309 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000310 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000311 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000312
313 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000314 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000315
Chris Lattner16af11d2004-02-09 21:03:38 +0000316 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000317 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000318
Chris Lattner1a1cb112001-09-30 22:46:54 +0000319 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000320 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000321 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000322
323 D.destroy(); // Free old strdup'd memory...
324 return N;
325 }
326
Chris Lattner2079fde2001-10-13 06:41:08 +0000327 // Check to make sure that "Ty" is an integral type, and that our
328 // value will fit into the specified type...
329 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000330 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
331 ThrowException("Signed integral constant '" +
332 itostr(D.ConstPool64) + "' is invalid for type '" +
333 Ty->getDescription() + "'!");
334 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000335
336 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000337 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
338 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000339 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
340 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000341 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000342 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000343 }
344 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000345 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000346 }
347
Chris Lattner2079fde2001-10-13 06:41:08 +0000348 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000349 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000350 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000351 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000352
353 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000354 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000355 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000356 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000357
Chris Lattnerd78700d2002-08-16 21:14:40 +0000358 case ValID::ConstantVal: // Fully resolved constant?
359 if (D.ConstantValue->getType() != Ty)
360 ThrowException("Constant expression type different from required type!");
361 return D.ConstantValue;
362
Chris Lattner30c89792001-09-07 16:35:17 +0000363 default:
364 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000365 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000366 } // End of switch
367
Chris Lattner2079fde2001-10-13 06:41:08 +0000368 assert(0 && "Unhandled case!");
369 return 0;
370}
371
Chris Lattner2079fde2001-10-13 06:41:08 +0000372// getVal - This function is identical to getValNonImprovising, except that if a
373// value is not already defined, it "improvises" by creating a placeholder var
374// that looks and acts just like the requested variable. When the value is
375// defined later, all uses of the placeholder variable are replaced with the
376// real thing.
377//
Chris Lattner64116f92004-07-14 01:33:11 +0000378static Value *getVal(const Type *Ty, const ValID &ID) {
379 if (Ty == Type::LabelTy)
380 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000381
Chris Lattner64116f92004-07-14 01:33:11 +0000382 // See if the value has already been defined.
383 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000384 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000385
386 // If we reached here, we referenced either a symbol that we don't know about
387 // or an id number that hasn't been read yet. We may be referencing something
388 // forward, so just create an entry to be resolved later and get to it...
389 //
Chris Lattner64116f92004-07-14 01:33:11 +0000390 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000391
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000392 // Remember where this forward reference came from. FIXME, shouldn't we try
393 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000394 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000395 llvmAsmlineno)));
396
Chris Lattner79df7c02002-03-26 18:01:55 +0000397 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000398 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000399 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000400 InsertValue(V, CurModule.LateResolveValues);
401 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000402}
403
Chris Lattner2e2ed292004-07-14 06:28:35 +0000404/// getBBVal - This is used for two purposes:
405/// * If isDefinition is true, a new basic block with the specified ID is being
406/// defined.
407/// * If isDefinition is true, this is a reference to a basic block, which may
408/// or may not be a forward reference.
409///
410static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000411 assert(inFunctionScope() && "Can't get basic block at global scope!");
412
Chris Lattner2e2ed292004-07-14 06:28:35 +0000413 std::string Name;
414 BasicBlock *BB = 0;
415 switch (ID.Type) {
416 default: ThrowException("Illegal label reference " + ID.getName());
417 case ValID::NumberVal: // Is it a numbered definition?
418 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
419 CurFun.NumberedBlocks.resize(ID.Num+1);
420 BB = CurFun.NumberedBlocks[ID.Num];
421 break;
422 case ValID::NameVal: // Is it a named definition?
423 Name = ID.Name;
424 if (Value *N = lookupInSymbolTable(Type::LabelTy, Name))
425 BB = cast<BasicBlock>(N);
426 break;
427 }
Chris Lattner64116f92004-07-14 01:33:11 +0000428
Chris Lattner2e2ed292004-07-14 06:28:35 +0000429 // See if the block has already been defined.
430 if (BB) {
431 // If this is the definition of the block, make sure the existing value was
432 // just a forward reference. If it was a forward reference, there will be
433 // an entry for it in the PlaceHolderInfo map.
434 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
435 // The existing value was a definition, not a forward reference.
436 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000437
Chris Lattner2e2ed292004-07-14 06:28:35 +0000438 ID.destroy(); // Free strdup'd memory.
439 return BB;
440 }
441
442 // Otherwise this block has not been seen before.
443 BB = new BasicBlock("", CurFun.CurrentFunction);
444 if (ID.Type == ValID::NameVal) {
445 BB->setName(ID.Name);
446 } else {
447 CurFun.NumberedBlocks[ID.Num] = BB;
448 }
449
450 // If this is not a definition, keep track of it so we can use it as a forward
451 // reference.
452 if (!isDefinition) {
453 // Remember where this forward reference came from.
454 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
455 } else {
456 // The forward declaration could have been inserted anywhere in the
457 // function: insert it into the correct place now.
458 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
459 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
460 }
461
Chris Lattner64116f92004-07-14 01:33:11 +0000462 return BB;
463}
464
Chris Lattner00950542001-06-06 20:29:01 +0000465
466//===----------------------------------------------------------------------===//
467// Code to handle forward references in instructions
468//===----------------------------------------------------------------------===//
469//
470// This code handles the late binding needed with statements that reference
471// values not defined yet... for example, a forward branch, or the PHI node for
472// a loop body.
473//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000474// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000475// and back patchs after we are done.
476//
477
478// ResolveDefinitions - If we could not resolve some defs at parsing
479// time (forward branches, phi functions for loops, etc...) resolve the
480// defs now...
481//
Chris Lattner735f2702004-07-08 22:30:50 +0000482static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
483 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000484 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000485 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000486 E = LateResolvers.end(); LRI != E; ++LRI) {
487 ValueList &List = LRI->second;
488 while (!List.empty()) {
489 Value *V = List.back();
490 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000491
492 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
493 CurModule.PlaceHolderInfo.find(V);
494 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
495
496 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000497
Chris Lattner735f2702004-07-08 22:30:50 +0000498 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000499 if (TheRealValue) {
500 V->replaceAllUsesWith(TheRealValue);
501 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000502 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000503 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000504 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000505 // resolver table
506 InsertValue(V, *FutureLateResolvers);
507 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000508 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000509 ThrowException("Reference to an invalid definition: '" +DID.getName()+
510 "' of type '" + V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000511 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000512 else
513 ThrowException("Reference to an invalid definition: #" +
514 itostr(DID.Num) + " of type '" +
515 V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000516 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000517 }
Chris Lattner00950542001-06-06 20:29:01 +0000518 }
519 }
520
521 LateResolvers.clear();
522}
523
Chris Lattner4a42e902001-10-22 05:56:09 +0000524// ResolveTypeTo - A brand new type was just declared. This means that (if
525// name is not null) things referencing Name can be resolved. Otherwise, things
526// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000527//
Chris Lattner4a42e902001-10-22 05:56:09 +0000528static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000529 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000530 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000531
Chris Lattner4a42e902001-10-22 05:56:09 +0000532 ValID D;
533 if (Name) D = ValID::create(Name);
534 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000535
Chris Lattner9232b992003-04-22 18:42:41 +0000536 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000537 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000538
Chris Lattner9232b992003-04-22 18:42:41 +0000539 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000540 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000541 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000542 LateResolver.erase(I);
543 }
544}
545
546// ResolveTypes - At this point, all types should be resolved. Any that aren't
547// are errors.
548//
Chris Lattner9232b992003-04-22 18:42:41 +0000549static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000550 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000551 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000552
553 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000554 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000555 else
Chris Lattner82269592001-10-22 06:01:08 +0000556 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000557 }
558}
559
Chris Lattner1781aca2001-09-18 04:00:54 +0000560// setValueName - Set the specified value to the name given. The name may be
561// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000562// assumed to be a malloc'd string buffer, and is free'd by this function.
563//
564static void setValueName(Value *V, char *NameStr) {
565 if (NameStr) {
566 std::string Name(NameStr); // Copy string
567 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000568
569 if (V->getType() == Type::VoidTy)
570 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000571
572 assert(inFunctionScope() && "Must be in function scope!");
573 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
574 if (ST.lookup(V->getType(), Name))
575 ThrowException("Redefinition of value named '" + Name + "' in the '" +
576 V->getType()->getDescription() + "' type plane!");
577
Chris Lattnerc9aea522004-07-13 08:12:39 +0000578 // Set the name.
579 V->setName(Name, &ST);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000580 }
581}
582
583// setValueNameMergingDuplicates - Set the specified value to the name given.
584// The name may be null potentially, in which case this is a noop. The string
585// passed in is assumed to be a malloc'd string buffer, and is free'd by this
586// function.
Chris Lattner1781aca2001-09-18 04:00:54 +0000587//
Chris Lattnerb7474512001-10-03 15:39:04 +0000588// This function returns true if the value has already been defined, but is
589// allowed to be redefined in the specified context. If the name is a new name
590// for the typeplane, false is returned.
591//
Chris Lattner8ab406d2004-07-13 07:59:27 +0000592static bool setValueNameMergingDuplicates(Value *V, char *NameStr) {
Chris Lattnerc9aea522004-07-13 08:12:39 +0000593 assert(V->getType() != Type::VoidTy && "Global or constant of type void?");
594
Chris Lattnerb7474512001-10-03 15:39:04 +0000595 if (NameStr == 0) return false;
Chris Lattner8ab406d2004-07-13 07:59:27 +0000596
Chris Lattner9232b992003-04-22 18:42:41 +0000597 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000598 free(NameStr); // Free old string
599
Chris Lattner8ab406d2004-07-13 07:59:27 +0000600 // FIXME: If we eliminated the function constant pool (which we should), this
601 // would just unconditionally look at the module symtab.
Chris Lattner6e6026b2002-11-20 18:36:02 +0000602 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000603 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000604 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000605
Reid Spencer75719bf2004-05-26 21:48:31 +0000606 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000607 if (Existing) { // Inserting a name that is already defined???
Chris Lattner8ab406d2004-07-13 07:59:27 +0000608 // We are a simple redefinition of a value, check to see if it is defined
609 // the same as the old one...
610 if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000611 // We are allowed to redefine a global variable in two circumstances:
612 // 1. If at least one of the globals is uninitialized or
613 // 2. If both initializers have the same value.
614 //
Chris Lattner89219832001-10-03 19:35:04 +0000615 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000616 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
617 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000618
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000619 // Make sure the existing global version gets the initializer! Make
620 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000621 if (GV->hasInitializer() && !EGV->hasInitializer())
622 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000623 if (GV->isConstant())
624 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000625 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000626
Chris Lattner2079fde2001-10-13 06:41:08 +0000627 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000628 return true; // They are equivalent!
629 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000630 }
Chris Lattner8ab406d2004-07-13 07:59:27 +0000631 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
632 if (C == V) return true; // Constants are equal to themselves
Chris Lattner9636a912001-10-01 16:18:37 +0000633 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000634
Chris Lattner2079fde2001-10-13 06:41:08 +0000635 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000636 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000637 }
Chris Lattnerc9aea522004-07-13 08:12:39 +0000638
639 // Set the name.
640 V->setName(Name, &ST);
Chris Lattnerb7474512001-10-03 15:39:04 +0000641 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000642}
643
Chris Lattner8ab406d2004-07-13 07:59:27 +0000644
645
Reid Spencer75719bf2004-05-26 21:48:31 +0000646// setTypeName - Set the specified type to the name given. The name may be
647// null potentially, in which case this is a noop. The string passed in is
648// assumed to be a malloc'd string buffer, and is freed by this function.
649//
650// This function returns true if the type has already been defined, but is
651// allowed to be redefined in the specified context. If the name is a new name
652// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000653static bool setTypeName(const Type *T, char *NameStr) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000654 if (NameStr == 0) return false;
655
656 std::string Name(NameStr); // Copy string
657 free(NameStr); // Free old string
658
659 // We don't allow assigning names to void type
660 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000661 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000662
663 SymbolTable &ST = inFunctionScope() ?
664 CurFun.CurrentFunction->getSymbolTable() :
665 CurModule.CurrentModule->getSymbolTable();
666
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000667 // Inserting a name that is already defined???
668 if (Type *Existing = ST.lookupType(Name)) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000669 // There is only one case where this is allowed: when we are refining an
670 // opaque type. In this case, Existing will be an opaque type.
671 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
672 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000673 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000674 return true;
675 }
676
677 // Otherwise, this is an attempt to redefine a type. That's okay if
678 // the redefinition is identical to the original. This will be so if
679 // Existing and T point to the same Type object. In this one case we
680 // allow the equivalent redefinition.
681 if (Existing == T) return true; // Yes, it's equal.
682
683 // Any other kind of (non-equivalent) redefinition is an error.
684 ThrowException("Redefinition of type named '" + Name + "' in the '" +
685 T->getDescription() + "' type plane!");
686 }
687
688 // Okay, its a newly named type. Set its name.
Chris Lattner8ca2dc02004-07-09 16:43:55 +0000689 if (!Name.empty()) ST.insert(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000690
Reid Spencer75719bf2004-05-26 21:48:31 +0000691 return false;
692}
Chris Lattner8896eda2001-07-09 19:38:36 +0000693
Chris Lattner30c89792001-09-07 16:35:17 +0000694//===----------------------------------------------------------------------===//
695// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000696//
Chris Lattner8896eda2001-07-09 19:38:36 +0000697
Chris Lattner3b073862004-02-09 03:19:29 +0000698// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000699//
700static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000701 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
702}
703
704namespace {
705 struct UpRefRecord {
706 // NestingLevel - The number of nesting levels that need to be popped before
707 // this type is resolved.
708 unsigned NestingLevel;
709
710 // LastContainedTy - This is the type at the current binding level for the
711 // type. Every time we reduce the nesting level, this gets updated.
712 const Type *LastContainedTy;
713
714 // UpRefTy - This is the actual opaque type that the upreference is
715 // represented with.
716 OpaqueType *UpRefTy;
717
718 UpRefRecord(unsigned NL, OpaqueType *URTy)
719 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
720 };
Chris Lattner30c89792001-09-07 16:35:17 +0000721}
Chris Lattner698b56e2001-07-20 19:15:08 +0000722
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000723// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000724static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000725
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000726/// HandleUpRefs - Every time we finish a new layer of types, this function is
727/// called. It loops through the UpRefs vector, which is a list of the
728/// currently active types. For each type, if the up reference is contained in
729/// the newly completed type, we decrement the level count. When the level
730/// count reaches zero, the upreferenced type is the type that is passed in:
731/// thus we can complete the cycle.
732///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000733static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000734 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000735 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000736 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000737 "' newly formed. Resolving upreferences.\n" <<
738 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000739
740 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
741 // to zero), we resolve them all together before we resolve them to Ty. At
742 // the end of the loop, if there is anything to resolve to Ty, it will be in
743 // this variable.
744 OpaqueType *TypeToResolve = 0;
745
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000746 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000747 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000748 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000749 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000750 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
751 // Decrement level of upreference
752 unsigned Level = --UpRefs[i].NestingLevel;
753 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000754 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000755 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000756 if (!TypeToResolve) {
757 TypeToResolve = UpRefs[i].UpRefTy;
758 } else {
759 UR_OUT(" * Resolving upreference for "
760 << UpRefs[i].second->getDescription() << "\n";
761 std::string OldName = UpRefs[i].UpRefTy->getDescription());
762 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
763 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
764 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
765 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000766 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
767 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000768 }
769 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000770 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000771
Chris Lattner026a8ce2004-02-09 18:53:54 +0000772 if (TypeToResolve) {
773 UR_OUT(" * Resolving upreference for "
774 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000775 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000776 TypeToResolve->refineAbstractTypeTo(Ty);
777 }
778
Chris Lattner8896eda2001-07-09 19:38:36 +0000779 return Ty;
780}
781
Chris Lattner30c89792001-09-07 16:35:17 +0000782
Chris Lattner00950542001-06-06 20:29:01 +0000783//===----------------------------------------------------------------------===//
784// RunVMAsmParser - Define an interface to this parser
785//===----------------------------------------------------------------------===//
786//
Chris Lattner9232b992003-04-22 18:42:41 +0000787Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000788 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000789 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000790 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000791 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000792
Chris Lattner75f20532003-04-22 18:02:52 +0000793 // Allocate a new module to read
794 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000795
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000796 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000797
Chris Lattner00950542001-06-06 20:29:01 +0000798 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000799
800 // Check to see if they called va_start but not va_arg..
801 if (!ObsoleteVarArgs)
802 if (Function *F = Result->getNamedFunction("llvm.va_start"))
803 if (F->asize() == 1) {
804 std::cerr << "WARNING: this file uses obsolete features. "
805 << "Assemble and disassemble to update it.\n";
806 ObsoleteVarArgs = true;
807 }
808
Chris Lattner99e7ab72003-10-18 05:53:13 +0000809 if (ObsoleteVarArgs) {
810 // If the user is making use of obsolete varargs intrinsics, adjust them for
811 // the user.
812 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
813 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
814
815 const Type *RetTy = F->getFunctionType()->getParamType(0);
816 RetTy = cast<PointerType>(RetTy)->getElementType();
817 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
818
819 while (!F->use_empty()) {
820 CallInst *CI = cast<CallInst>(F->use_back());
821 Value *V = new CallInst(NF, "", CI);
822 new StoreInst(V, CI->getOperand(1), CI);
823 CI->getParent()->getInstList().erase(CI);
824 }
825 Result->getFunctionList().erase(F);
826 }
827
828 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
829 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
830 const Type *ArgTy = F->getFunctionType()->getParamType(0);
831 ArgTy = cast<PointerType>(ArgTy)->getElementType();
832 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
833 ArgTy, 0);
834
835 while (!F->use_empty()) {
836 CallInst *CI = cast<CallInst>(F->use_back());
837 Value *V = new LoadInst(CI->getOperand(1), "", CI);
838 new CallInst(NF, V, "", CI);
839 CI->getParent()->getInstList().erase(CI);
840 }
841 Result->getFunctionList().erase(F);
842 }
843
844 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
845 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
846 const Type *ArgTy = F->getFunctionType()->getParamType(0);
847 ArgTy = cast<PointerType>(ArgTy)->getElementType();
848 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
849 ArgTy, 0);
850
851 while (!F->use_empty()) {
852 CallInst *CI = cast<CallInst>(F->use_back());
853 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
854 new StoreInst(V, CI->getOperand(1), CI);
855 CI->getParent()->getInstList().erase(CI);
856 }
857 Result->getFunctionList().erase(F);
858 }
859 }
860
Chris Lattner00950542001-06-06 20:29:01 +0000861 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
862 ParserResult = 0;
863
864 return Result;
865}
866
Brian Gaeked0fde302003-11-11 22:41:34 +0000867} // End llvm namespace
868
869using namespace llvm;
870
Chris Lattner00950542001-06-06 20:29:01 +0000871%}
872
873%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000874 llvm::Module *ModuleVal;
875 llvm::Function *FunctionVal;
876 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
877 llvm::BasicBlock *BasicBlockVal;
878 llvm::TerminatorInst *TermInstVal;
879 llvm::Instruction *InstVal;
880 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000881
Brian Gaeked0fde302003-11-11 22:41:34 +0000882 const llvm::Type *PrimType;
883 llvm::PATypeHolder *TypeVal;
884 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000885
Brian Gaeked0fde302003-11-11 22:41:34 +0000886 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
887 std::vector<llvm::Value*> *ValueList;
888 std::list<llvm::PATypeHolder> *TypeList;
889 std::list<std::pair<llvm::Value*,
890 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
891 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
892 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000893
Brian Gaeked0fde302003-11-11 22:41:34 +0000894 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000895 int64_t SInt64Val;
896 uint64_t UInt64Val;
897 int SIntVal;
898 unsigned UIntVal;
899 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000900 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000901
Chris Lattner30c89792001-09-07 16:35:17 +0000902 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000903 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000904
Brian Gaeked0fde302003-11-11 22:41:34 +0000905 llvm::Instruction::BinaryOps BinaryOpVal;
906 llvm::Instruction::TermOps TermOpVal;
907 llvm::Instruction::MemoryOps MemOpVal;
908 llvm::Instruction::OtherOps OtherOpVal;
909 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000910}
911
Chris Lattner79df7c02002-03-26 18:01:55 +0000912%type <ModuleVal> Module FunctionList
913%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000914%type <BasicBlockVal> BasicBlock InstructionList
915%type <TermInstVal> BBTerminatorInst
916%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000917%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000918%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000919%type <ArgList> ArgList ArgListH
920%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000921%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000922%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000923%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000924%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000925%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000926%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000927%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000928%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000929%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000930
Chris Lattner2079fde2001-10-13 06:41:08 +0000931// ValueRef - Unresolved reference to a definition or BB
932%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000933%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000934// Tokens and types for handling constant integer values
935//
936// ESINT64VAL - A negative number within long long range
937%token <SInt64Val> ESINT64VAL
938
939// EUINT64VAL - A positive number within uns. long long range
940%token <UInt64Val> EUINT64VAL
941%type <SInt64Val> EINT64VAL
942
943%token <SIntVal> SINTVAL // Signed 32 bit ints...
944%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
945%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000946%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000947
948// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000949%type <TypeVal> Types TypesV UpRTypes UpRTypesV
950%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000951%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
952%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000953
Chris Lattner6c23f572003-08-22 05:42:10 +0000954%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
955%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000956
957
Chris Lattner91ef4602004-03-31 03:49:47 +0000958%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000959%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000960%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000961%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000962
963// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000964%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000965
Chris Lattner00950542001-06-06 20:29:01 +0000966// Binary Operators
967%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000968%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000969%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000970%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000971
972// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000973%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000974
Chris Lattner027dcc52001-07-08 21:10:27 +0000975// Other Operators
976%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000977%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000978%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000979
Chris Lattner00950542001-06-06 20:29:01 +0000980%start Module
981%%
982
983// Handle constant integer size restriction and conversion...
984//
Chris Lattner51727be2002-06-04 21:58:56 +0000985INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000986INTVAL : UINTVAL {
987 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
988 ThrowException("Value too large for type!");
989 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000990};
Chris Lattner00950542001-06-06 20:29:01 +0000991
992
Chris Lattner51727be2002-06-04 21:58:56 +0000993EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000994EINT64VAL : EUINT64VAL {
995 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
996 ThrowException("Value too large for type!");
997 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000998};
Chris Lattner00950542001-06-06 20:29:01 +0000999
Chris Lattner00950542001-06-06 20:29:01 +00001000// Operations that are notably excluded from this list include:
1001// RET, BR, & SWITCH because they end basic blocks and are treated specially.
1002//
Chris Lattner4a6482b2002-09-10 19:57:26 +00001003ArithmeticOps: ADD | SUB | MUL | DIV | REM;
1004LogicalOps : AND | OR | XOR;
1005SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
1006BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
1007
Chris Lattner51727be2002-06-04 21:58:56 +00001008ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +00001009
Chris Lattnere98dda62001-07-14 06:10:16 +00001010// These are some types that allow classification if we only want a particular
1011// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +00001012SIntType : LONG | INT | SHORT | SBYTE;
1013UIntType : ULONG | UINT | USHORT | UBYTE;
1014IntType : SIntType | UIntType;
1015FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +00001016
Chris Lattnere98dda62001-07-14 06:10:16 +00001017// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +00001018OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +00001019 $$ = $1;
1020 }
1021 | /*empty*/ {
1022 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001023 };
Chris Lattner00950542001-06-06 20:29:01 +00001024
Chris Lattner4ad02e72003-04-16 20:28:45 +00001025OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
1026 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +00001027 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +00001028 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
1029 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001030
1031//===----------------------------------------------------------------------===//
1032// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001033// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001034// access to it, a user must explicitly use TypesV.
1035//
1036
1037// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001038TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1039UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001040
1041Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001042 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001043 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1044 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001045 };
Chris Lattner30c89792001-09-07 16:35:17 +00001046
1047
1048// Derived types are added later...
1049//
Chris Lattner51727be2002-06-04 21:58:56 +00001050PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1051PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001052UpRTypes : OPAQUE {
1053 $$ = new PATypeHolder(OpaqueType::get());
1054 }
1055 | PrimType {
1056 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001057 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001058UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001059 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001060};
Chris Lattner30c89792001-09-07 16:35:17 +00001061
Chris Lattner30c89792001-09-07 16:35:17 +00001062// Include derived types in the Types production.
1063//
1064UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001065 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001066 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001067 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001068 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001069 UR_OUT("New Upreference!\n");
1070 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001071 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001072 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001073 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001074 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001075 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1076 if (isVarArg) Params.pop_back();
1077
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001078 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001079 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001080 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001081 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001082 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001083 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001084 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001085 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001086 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001087 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001088 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001089 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001090
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001091 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001092 delete $2;
1093 }
1094 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001095 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001096 }
1097 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001098 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001099 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001100 };
Chris Lattner30c89792001-09-07 16:35:17 +00001101
Chris Lattner7e708292002-06-25 16:13:24 +00001102// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001103// declaration type lists
1104//
1105TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001106 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001107 $$->push_back(*$1); delete $1;
1108 }
1109 | TypeListI ',' UpRTypes {
1110 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001111 };
Chris Lattner30c89792001-09-07 16:35:17 +00001112
Chris Lattner7e708292002-06-25 16:13:24 +00001113// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001114ArgTypeListI : TypeListI
1115 | TypeListI ',' DOTDOTDOT {
1116 ($$=$1)->push_back(Type::VoidTy);
1117 }
1118 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001119 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001120 }
1121 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001122 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001123 };
Chris Lattner30c89792001-09-07 16:35:17 +00001124
Chris Lattnere98dda62001-07-14 06:10:16 +00001125// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001126// production is used ONLY to represent constants that show up AFTER a 'const',
1127// 'constant' or 'global' token at global scope. Constants that can be inlined
1128// into other expressions (such as integers and constexprs) are handled by the
1129// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001130//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001131ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001132 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001133 if (ATy == 0)
1134 ThrowException("Cannot make array constant with type: '" +
1135 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001136 const Type *ETy = ATy->getElementType();
1137 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001138
Chris Lattner30c89792001-09-07 16:35:17 +00001139 // Verify that we have the correct size...
1140 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001141 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001142 utostr($3->size()) + " arguments, but has size of " +
1143 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001144
Chris Lattner30c89792001-09-07 16:35:17 +00001145 // Verify all elements are correct type!
1146 for (unsigned i = 0; i < $3->size(); i++) {
1147 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001148 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001149 ETy->getDescription() +"' as required!\nIt is of type '"+
1150 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001151 }
1152
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001153 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001154 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001155 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001156 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001157 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001158 if (ATy == 0)
1159 ThrowException("Cannot make array constant with type: '" +
1160 (*$1)->getDescription() + "'!");
1161
1162 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001163 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001164 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001165 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001166 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001167 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001168 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001169 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001170 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001171 if (ATy == 0)
1172 ThrowException("Cannot make array constant with type: '" +
1173 (*$1)->getDescription() + "'!");
1174
Chris Lattner30c89792001-09-07 16:35:17 +00001175 int NumElements = ATy->getNumElements();
1176 const Type *ETy = ATy->getElementType();
1177 char *EndStr = UnEscapeLexed($3, true);
1178 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001179 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001180 itostr((int)(EndStr-$3)) +
1181 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001182 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001183 if (ETy == Type::SByteTy) {
1184 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001185 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001186 } else if (ETy == Type::UByteTy) {
1187 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001188 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001189 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001190 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001191 ThrowException("Cannot build string arrays of non byte sized elements!");
1192 }
Chris Lattner30c89792001-09-07 16:35:17 +00001193 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001194 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001195 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001196 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001197 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001198 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001199 if (STy == 0)
1200 ThrowException("Cannot make struct constant with type: '" +
1201 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001202
Chris Lattnerc6212f12003-05-21 16:06:56 +00001203 if ($3->size() != STy->getNumContainedTypes())
1204 ThrowException("Illegal number of initializers for structure type!");
1205
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001206 // Check to ensure that constants are compatible with the type initializer!
1207 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001208 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001209 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001210 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001211 "' for element #" + utostr(i) +
1212 " of structure initializer!");
1213
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001214 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001215 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001216 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001217 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001218 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001219 if (STy == 0)
1220 ThrowException("Cannot make struct constant with type: '" +
1221 (*$1)->getDescription() + "'!");
1222
1223 if (STy->getNumContainedTypes() != 0)
1224 ThrowException("Illegal number of initializers for structure type!");
1225
1226 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1227 delete $1;
1228 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001229 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001230 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001231 if (PTy == 0)
1232 ThrowException("Cannot make null pointer constant with type: '" +
1233 (*$1)->getDescription() + "'!");
1234
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001235 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001236 delete $1;
1237 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001238 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001239 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001240 if (Ty == 0)
1241 ThrowException("Global const reference must be a pointer type!");
1242
Chris Lattner3101c252002-08-15 17:58:33 +00001243 // ConstExprs can exist in the body of a function, thus creating
1244 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1245 // the context of a function, getValNonImprovising will search the functions
1246 // symbol table instead of the module symbol table for the global symbol,
1247 // which throws things all off. To get around this, we just tell
1248 // getValNonImprovising that we are at global scope here.
1249 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001250 Function *SavedCurFn = CurFun.CurrentFunction;
1251 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001252
Chris Lattner2079fde2001-10-13 06:41:08 +00001253 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001254
Chris Lattner394f2eb2003-10-16 20:12:13 +00001255 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001256
Chris Lattner2079fde2001-10-13 06:41:08 +00001257 // If this is an initializer for a constant pointer, which is referencing a
1258 // (currently) undefined variable, create a stub now that shall be replaced
1259 // in the future with the right type of variable.
1260 //
1261 if (V == 0) {
1262 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1263 const PointerType *PT = cast<PointerType>(Ty);
1264
1265 // First check to see if the forward references value is already created!
1266 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001267 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001268
1269 if (I != CurModule.GlobalRefs.end()) {
1270 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001271 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001272 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001273 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001274 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001275 false,
1276 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001277 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001278 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001279
1280 // Must temporarily push this value into the module table...
1281 CurModule.CurrentModule->getGlobalList().push_back(GV);
1282 V = GV;
1283 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001284 }
1285
Chris Lattner2079fde2001-10-13 06:41:08 +00001286 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001287 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001288 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001289 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001290 | Types ConstExpr {
1291 if ($1->get() != $2->getType())
1292 ThrowException("Mismatched types for constant expression!");
1293 $$ = $2;
1294 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001295 }
1296 | Types ZEROINITIALIZER {
1297 $$ = Constant::getNullValue($1->get());
1298 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001299 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001300
Chris Lattnere43f40b2002-10-09 00:25:32 +00001301ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001302 if (!ConstantSInt::isValueValidForType($1, $2))
1303 ThrowException("Constant value doesn't fit in type!");
1304 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001305 }
1306 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001307 if (!ConstantUInt::isValueValidForType($1, $2))
1308 ThrowException("Constant value doesn't fit in type!");
1309 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001310 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001311 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001312 $$ = ConstantBool::True;
1313 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001314 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001315 $$ = ConstantBool::False;
1316 }
1317 | FPType FPVAL { // Float & Double constants
1318 $$ = ConstantFP::get($1, $2);
1319 };
1320
Chris Lattner00950542001-06-06 20:29:01 +00001321
Chris Lattnerd78700d2002-08-16 21:14:40 +00001322ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001323 if (!$3->getType()->isFirstClassType())
1324 ThrowException("cast constant expression from a non-primitive type: '" +
1325 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001326 if (!$5->get()->isFirstClassType())
1327 ThrowException("cast constant expression to a non-primitive type: '" +
1328 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001329 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001330 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001331 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001332 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1333 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001334 ThrowException("GetElementPtr requires a pointer operand!");
1335
Chris Lattner830b6f92004-04-05 01:30:04 +00001336 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1337 // indices to uint struct indices for compatibility.
1338 generic_gep_type_iterator<std::vector<Value*>::iterator>
1339 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1340 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1341 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1342 if (isa<StructType>(*GTI)) // Only change struct indices
1343 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1344 if (CUI->getType() == Type::UByteTy)
1345 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1346
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001347 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001348 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001349 if (!IdxTy)
1350 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001351
Chris Lattner9232b992003-04-22 18:42:41 +00001352 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001353 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1354 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001355 IdxVec.push_back(C);
1356 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001357 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001358
Chris Lattnerd78700d2002-08-16 21:14:40 +00001359 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001360
Chris Lattnerd78700d2002-08-16 21:14:40 +00001361 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001362 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001363 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1364 if ($3->getType() != Type::BoolTy)
1365 ThrowException("Select condition must be of boolean type!");
1366 if ($5->getType() != $7->getType())
1367 ThrowException("Select operand types must match!");
1368 $$ = ConstantExpr::getSelect($3, $5, $7);
1369 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001370 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001371 if ($3->getType() != $5->getType())
1372 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001373 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001374 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001375 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001376 if ($5->getType() != Type::UByteTy)
1377 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001378 if (!$3->getType()->isInteger())
1379 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001380 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001381 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001382
1383
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001384// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001385ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001386 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001387 }
1388 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001389 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001390 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001391 };
Chris Lattner00950542001-06-06 20:29:01 +00001392
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001393
Chris Lattner1781aca2001-09-18 04:00:54 +00001394// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001395GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001396
Chris Lattner00950542001-06-06 20:29:01 +00001397
Chris Lattner0e73ce62002-05-02 19:11:13 +00001398//===----------------------------------------------------------------------===//
1399// Rules to match Modules
1400//===----------------------------------------------------------------------===//
1401
1402// Module rule: Capture the result of parsing the whole file into a result
1403// variable...
1404//
1405Module : FunctionList {
1406 $$ = ParserResult = $1;
1407 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001408};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001409
Chris Lattner7e708292002-06-25 16:13:24 +00001410// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001411//
1412FunctionList : FunctionList Function {
1413 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001414 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001415 }
1416 | FunctionList FunctionProto {
1417 $$ = $1;
1418 }
1419 | FunctionList IMPLEMENTATION {
1420 $$ = $1;
1421 }
1422 | ConstPool {
1423 $$ = CurModule.CurrentModule;
1424 // Resolve circular types before we parse the body of the module
1425 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001426 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001427
Chris Lattnere98dda62001-07-14 06:10:16 +00001428// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001429ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001430 // FIXME: THIS SHOULD REALLY BE ELIMINATED. It is totally unneeded.
1431 if (!setValueNameMergingDuplicates($4, $2))
Chris Lattneradf99702003-03-03 23:28:55 +00001432 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001433 }
Chris Lattner30c89792001-09-07 16:35:17 +00001434 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001435 // Eagerly resolve types. This is not an optimization, this is a
1436 // requirement that is due to the fact that we could have this:
1437 //
1438 // %list = type { %list * }
1439 // %list = type { %list * } ; repeated type decl
1440 //
1441 // If types are not resolved eagerly, then the two types will not be
1442 // determined to be the same type!
1443 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001444 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001445
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001446 if (!setTypeName(*$4, $2) && !$2) {
1447 // If this is a named type that is not a redefinition, add it to the slot
1448 // table.
Chris Lattner64116f92004-07-14 01:33:11 +00001449 if (inFunctionScope())
1450 CurFun.Types.push_back(*$4);
1451 else
1452 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001453 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001454
1455 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001456 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001457 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001458 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001459 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001460 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001461 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001462 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001463 if (Initializer == 0)
1464 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001465
Chris Lattnerdda71962001-11-26 18:54:16 +00001466 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001467 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001468 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001469 int Slot = InsertValue(GV, CurModule.Values);
1470
1471 if (Slot != -1) {
1472 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1473 } else {
1474 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1475 (char*)GV->getName().c_str()));
1476 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001477 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001478 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001479 | ConstPool OptAssign EXTERNAL GlobalType Types {
1480 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001481 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001482 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattner8ab406d2004-07-13 07:59:27 +00001483 if (!setValueNameMergingDuplicates(GV, $2)) { // If not redefining...
Chris Lattnerb7474512001-10-03 15:39:04 +00001484 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001485 int Slot = InsertValue(GV, CurModule.Values);
1486
1487 if (Slot != -1) {
1488 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1489 } else {
1490 assert(GV->hasName() && "Not named and not numbered!?");
1491 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1492 (char*)GV->getName().c_str()));
1493 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001494 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001495 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001496 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001497 | ConstPool TARGET TargetDefinition {
1498 }
Chris Lattner00950542001-06-06 20:29:01 +00001499 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001500 };
Chris Lattner00950542001-06-06 20:29:01 +00001501
1502
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001503
1504BigOrLittle : BIG { $$ = Module::BigEndian; };
1505BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1506
1507TargetDefinition : ENDIAN '=' BigOrLittle {
1508 CurModule.CurrentModule->setEndianness($3);
1509 }
1510 | POINTERSIZE '=' EUINT64VAL {
1511 if ($3 == 32)
1512 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1513 else if ($3 == 64)
1514 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1515 else
1516 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1517 };
1518
1519
Chris Lattner00950542001-06-06 20:29:01 +00001520//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001521// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001522//===----------------------------------------------------------------------===//
1523
Chris Lattner6c23f572003-08-22 05:42:10 +00001524Name : VAR_ID | STRINGCONSTANT;
1525OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001526
Chris Lattner6c23f572003-08-22 05:42:10 +00001527ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001528 if (*$1 == Type::VoidTy)
1529 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001530 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001531};
Chris Lattner00950542001-06-06 20:29:01 +00001532
Chris Lattner69da5cf2002-10-13 20:57:00 +00001533ArgListH : ArgListH ',' ArgVal {
1534 $$ = $1;
1535 $1->push_back(*$3);
1536 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001537 }
1538 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001539 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001540 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001541 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001542 };
Chris Lattner00950542001-06-06 20:29:01 +00001543
1544ArgList : ArgListH {
1545 $$ = $1;
1546 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001547 | ArgListH ',' DOTDOTDOT {
1548 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001549 $$->push_back(std::pair<PATypeHolder*,
1550 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001551 }
1552 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001553 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1554 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001555 }
Chris Lattner00950542001-06-06 20:29:01 +00001556 | /* empty */ {
1557 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001558 };
Chris Lattner00950542001-06-06 20:29:01 +00001559
Chris Lattner6c23f572003-08-22 05:42:10 +00001560FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001561 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001562 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001563
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001564 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1565 ThrowException("LLVM functions cannot return aggregate types!");
1566
Chris Lattner9232b992003-04-22 18:42:41 +00001567 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001568 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001569 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001570 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001571 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001572 }
Chris Lattner00950542001-06-06 20:29:01 +00001573
Chris Lattner2079fde2001-10-13 06:41:08 +00001574 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1575 if (isVarArg) ParamTypeList.pop_back();
1576
Chris Lattner1f862af2003-04-16 18:13:57 +00001577 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001578 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001579 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001580
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001581 Function *Fn = 0;
1582 // Is the function already in symtab?
1583 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1584 // Yes it is. If this is the case, either we need to be a forward decl,
1585 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001586 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001587 ThrowException("Redefinition of function '" + FunctionName + "'!");
1588
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001589 // Make sure to strip off any argument names so we can't get conflicts...
1590 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1591 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001592
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001593 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001594 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1595 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001596 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001597 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001598 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001599 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001600
Chris Lattner394f2eb2003-10-16 20:12:13 +00001601 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001602
Chris Lattner7e708292002-06-25 16:13:24 +00001603 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001604 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001605 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001606 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001607 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001608 delete $4->back().first;
1609 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001610 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001611 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001612 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001613 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001614 delete I->first; // Delete the typeholder...
1615
Chris Lattner8ab406d2004-07-13 07:59:27 +00001616 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001617 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001618 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001619
Chris Lattner1f862af2003-04-16 18:13:57 +00001620 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001621 }
Chris Lattner51727be2002-06-04 21:58:56 +00001622};
Chris Lattner00950542001-06-06 20:29:01 +00001623
Chris Lattner9b02cc32002-05-03 18:23:48 +00001624BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1625
Chris Lattner4ad02e72003-04-16 20:28:45 +00001626FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001627 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001628
Chris Lattner4ad02e72003-04-16 20:28:45 +00001629 // Make sure that we keep track of the linkage type even if there was a
1630 // previous "declare".
1631 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001632
Chris Lattner7e708292002-06-25 16:13:24 +00001633 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001634 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001635};
Chris Lattner00950542001-06-06 20:29:01 +00001636
Chris Lattner9b02cc32002-05-03 18:23:48 +00001637END : ENDTOK | '}'; // Allow end of '}' to end a function
1638
Chris Lattner79df7c02002-03-26 18:01:55 +00001639Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001640 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001641};
Chris Lattner00950542001-06-06 20:29:01 +00001642
Chris Lattner394f2eb2003-10-16 20:12:13 +00001643FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1644 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001645 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001646};
Chris Lattner00950542001-06-06 20:29:01 +00001647
1648//===----------------------------------------------------------------------===//
1649// Rules to match Basic Blocks
1650//===----------------------------------------------------------------------===//
1651
1652ConstValueRef : ESINT64VAL { // A reference to a direct constant
1653 $$ = ValID::create($1);
1654 }
1655 | EUINT64VAL {
1656 $$ = ValID::create($1);
1657 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001658 | FPVAL { // Perhaps it's an FP constant?
1659 $$ = ValID::create($1);
1660 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001661 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001662 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001663 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001664 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001665 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001666 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001667 | NULL_TOK {
1668 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001669 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001670 | ConstExpr {
1671 $$ = ValID::create($1);
1672 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001673
Chris Lattner2079fde2001-10-13 06:41:08 +00001674// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1675// another value.
1676//
1677SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001678 $$ = ValID::create($1);
1679 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001680 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001681 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001682 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001683
1684// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001685ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001686
Chris Lattner00950542001-06-06 20:29:01 +00001687
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001688// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1689// type immediately preceeds the value reference, and allows complex constant
1690// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001691ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001692 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001693 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001694
Chris Lattner00950542001-06-06 20:29:01 +00001695BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001696 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001697 }
Chris Lattner7e708292002-06-25 16:13:24 +00001698 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001699 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001700 };
Chris Lattner00950542001-06-06 20:29:01 +00001701
1702
1703// Basic blocks are terminated by branching instructions:
1704// br, br/cc, switch, ret
1705//
Chris Lattner2079fde2001-10-13 06:41:08 +00001706BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001707 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001708 InsertValue($3);
1709
1710 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001711 InsertValue($1);
1712 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001713 };
Chris Lattner00950542001-06-06 20:29:01 +00001714
1715InstructionList : InstructionList Inst {
1716 $1->getInstList().push_back($2);
1717 $$ = $1;
1718 }
1719 | /* empty */ {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001720 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner22ae2322004-07-13 08:39:15 +00001721 }
1722 | LABELSTR {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001723 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner51727be2002-06-04 21:58:56 +00001724 };
Chris Lattner00950542001-06-06 20:29:01 +00001725
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001726BBTerminatorInst : RET ResolvedVal { // Return with a result...
1727 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001728 }
1729 | RET VOID { // Return with no result...
1730 $$ = new ReturnInst();
1731 }
1732 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001733 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001734 } // Conditional Branch...
1735 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001736 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001737 }
1738 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001739 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner00950542001-06-06 20:29:01 +00001740 $$ = S;
1741
Chris Lattner9232b992003-04-22 18:42:41 +00001742 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001743 E = $8->end();
1744 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001745 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001746 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001747 }
Chris Lattner196850c2003-05-15 21:30:00 +00001748 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001749 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner196850c2003-05-15 21:30:00 +00001750 $$ = S;
1751 }
Chris Lattner64116f92004-07-14 01:33:11 +00001752 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1753 UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001754 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001755 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001756
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001757 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1758 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001759 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001760 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001761 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001762 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1763 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001764 ParamTypes.push_back((*I)->getType());
1765 }
1766
1767 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1768 if (isVarArg) ParamTypes.pop_back();
1769
Chris Lattner79df7c02002-03-26 18:01:55 +00001770 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001771 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001772 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001773
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001774 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001775
Chris Lattner64116f92004-07-14 01:33:11 +00001776 BasicBlock *Normal = getBBVal($9);
1777 BasicBlock *Except = getBBVal($12);
Chris Lattner2079fde2001-10-13 06:41:08 +00001778
1779 // Create the call node...
1780 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001781 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001782 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001783 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001784 // correctly!
1785 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001786 FunctionType::param_iterator I = Ty->param_begin();
1787 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001788 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001789
1790 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1791 if ((*ArgI)->getType() != *I)
1792 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001793 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001794
1795 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1796 ThrowException("Invalid number of parameters detected!");
1797
Chris Lattner6cdb0112001-11-26 16:54:11 +00001798 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001799 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001800 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001801 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001802 }
1803 | UNWIND {
1804 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001805 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001806
1807
Chris Lattner00950542001-06-06 20:29:01 +00001808
1809JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1810 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001811 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001812 if (V == 0)
1813 ThrowException("May only switch on a constant pool value!");
1814
Chris Lattner64116f92004-07-14 01:33:11 +00001815 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001816 }
1817 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001818 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001819 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001820
1821 if (V == 0)
1822 ThrowException("May only switch on a constant pool value!");
1823
Chris Lattner64116f92004-07-14 01:33:11 +00001824 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001825 };
Chris Lattner00950542001-06-06 20:29:01 +00001826
1827Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001828 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001829 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001830 InsertValue($2);
1831 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001832};
Chris Lattner00950542001-06-06 20:29:01 +00001833
Chris Lattnerc24d2082001-06-11 15:04:20 +00001834PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001835 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001836 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001837 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001838 }
1839 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1840 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001841 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001842 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001843 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001844
1845
Chris Lattner30c89792001-09-07 16:35:17 +00001846ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001847 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001848 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001849 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001850 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001851 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001852 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001853 };
Chris Lattner00950542001-06-06 20:29:01 +00001854
1855// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001856ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001857
Chris Lattner4a6482b2002-09-10 19:57:26 +00001858InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1859 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1860 ThrowException("Arithmetic operator requires integer or FP operands!");
1861 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1862 if ($$ == 0)
1863 ThrowException("binary operator returned null!");
1864 delete $2;
1865 }
1866 | LogicalOps Types ValueRef ',' ValueRef {
1867 if (!(*$2)->isIntegral())
1868 ThrowException("Logical operator requires integral operands!");
1869 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1870 if ($$ == 0)
1871 ThrowException("binary operator returned null!");
1872 delete $2;
1873 }
1874 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001875 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001876 if ($$ == 0)
1877 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001878 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001879 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001880 | NOT ResolvedVal {
1881 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1882 << " Replacing with 'xor'.\n";
1883
1884 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1885 if (Ones == 0)
1886 ThrowException("Expected integral type for not instruction!");
1887
1888 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001889 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001890 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001891 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001892 | ShiftOps ResolvedVal ',' ResolvedVal {
1893 if ($4->getType() != Type::UByteTy)
1894 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001895 if (!$2->getType()->isInteger())
1896 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001897 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001898 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001899 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001900 if (!$4->get()->isFirstClassType())
1901 ThrowException("cast instruction to a non-primitive type: '" +
1902 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001903 $$ = new CastInst($2, *$4);
1904 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001905 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001906 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1907 if ($2->getType() != Type::BoolTy)
1908 ThrowException("select condition must be boolean!");
1909 if ($4->getType() != $6->getType())
1910 ThrowException("select value types should match!");
1911 $$ = new SelectInst($2, $4, $6);
1912 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001913 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001914 // FIXME: This is emulation code for an obsolete syntax. This should be
1915 // removed at some point.
1916 if (!ObsoleteVarArgs) {
1917 std::cerr << "WARNING: this file uses obsolete features. "
1918 << "Assemble and disassemble to update it.\n";
1919 ObsoleteVarArgs = true;
1920 }
1921
1922 // First, load the valist...
1923 Instruction *CurVAList = new LoadInst($2, "");
1924 CurBB->getInstList().push_back(CurVAList);
1925
1926 // Emit the vaarg instruction.
1927 $$ = new VAArgInst(CurVAList, *$4);
1928
1929 // Now we must advance the pointer and update it in memory.
1930 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1931 CurBB->getInstList().push_back(TheVANext);
1932
1933 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1934 delete $4;
1935 }
1936 | VAARG ResolvedVal ',' Types {
1937 $$ = new VAArgInst($2, *$4);
1938 delete $4;
1939 }
1940 | VANEXT ResolvedVal ',' Types {
1941 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001942 delete $4;
1943 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001944 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001945 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001946 if (!Ty->isFirstClassType())
1947 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001948 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001949 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001950 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001951 if ($2->front().first->getType() != Ty)
1952 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001953 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001954 $2->pop_front();
1955 }
1956 delete $2; // Free the list...
1957 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001958 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001959 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001960 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001961
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001962 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1963 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001964 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001965 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001966 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001967 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1968 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001969 ParamTypes.push_back((*I)->getType());
1970 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001971
1972 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1973 if (isVarArg) ParamTypes.pop_back();
1974
Chris Lattner79df7c02002-03-26 18:01:55 +00001975 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001976 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001977 }
Chris Lattner00950542001-06-06 20:29:01 +00001978
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001979 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001980
Chris Lattner8b81bf52001-07-25 22:47:46 +00001981 // Create the call node...
1982 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001983 // Make sure no arguments is a good thing!
1984 if (Ty->getNumParams() != 0)
1985 ThrowException("No arguments passed to a function that "
1986 "expects arguments!");
1987
Chris Lattner9232b992003-04-22 18:42:41 +00001988 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001989 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001990 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001991 // correctly!
1992 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001993 FunctionType::param_iterator I = Ty->param_begin();
1994 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001995 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001996
1997 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1998 if ((*ArgI)->getType() != *I)
1999 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00002000 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00002001
Chris Lattner8b81bf52001-07-25 22:47:46 +00002002 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00002003 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00002004
Chris Lattner6cdb0112001-11-26 16:54:11 +00002005 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00002006 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00002007 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00002008 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00002009 }
2010 | MemoryInst {
2011 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00002012 };
Chris Lattner00950542001-06-06 20:29:01 +00002013
Chris Lattner6cdb0112001-11-26 16:54:11 +00002014
2015// IndexList - List of indices for GEP based instructions...
2016IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002017 $$ = $2;
2018 } | /* empty */ {
2019 $$ = new std::vector<Value*>();
2020 };
2021
2022OptVolatile : VOLATILE {
2023 $$ = true;
2024 }
2025 | /* empty */ {
2026 $$ = false;
2027 };
2028
Chris Lattner027dcc52001-07-08 21:10:27 +00002029
Chris Lattner00950542001-06-06 20:29:01 +00002030MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002031 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002032 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002033 }
2034 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002035 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002036 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002037 }
2038 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002039 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002040 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002041 }
2042 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002043 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002044 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002045 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002046 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002047 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002048 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002049 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002050 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002051 }
2052
Chris Lattner15c9c032003-09-08 18:20:29 +00002053 | OptVolatile LOAD Types ValueRef {
2054 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002055 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002056 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002057 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002058 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002059 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002060 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2061 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002062 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002063 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002064 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002065 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002066 if (ElTy != $3->getType())
2067 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002068 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002069
Chris Lattner79ad13802003-09-08 20:29:46 +00002070 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002071 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002072 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002073 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002074 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002075 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002076
2077 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2078 // indices to uint struct indices for compatibility.
2079 generic_gep_type_iterator<std::vector<Value*>::iterator>
2080 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2081 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2082 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2083 if (isa<StructType>(*GTI)) // Only change struct indices
2084 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2085 if (CUI->getType() == Type::UByteTy)
2086 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2087
Chris Lattner30c89792001-09-07 16:35:17 +00002088 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002089 ThrowException("Invalid getelementptr indices for type '" +
2090 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002091 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2092 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002093 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002094
Brian Gaeked0fde302003-11-11 22:41:34 +00002095
Chris Lattner00950542001-06-06 20:29:01 +00002096%%
Chris Lattner09083092001-07-08 04:57:15 +00002097int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002098 std::string where
2099 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002100 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002101 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002102 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002103 errMsg += "end-of-file.";
2104 else
Chris Lattner9232b992003-04-22 18:42:41 +00002105 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002106 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002107 return 0;
2108}