blob: dfdb398f44ae56376655a81953aa3672eaaed4ef [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"
Chris Lattner00950542001-06-06 20:29:01 +000024#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000025#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000026#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner386a3b72001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000030int yyparse();
31
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
33
Chris Lattner00950542001-06-06 20:29:01 +000034static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000035std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattner30c89792001-09-07 16:35:17 +000037// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
38// relating to upreferences in the input stream.
39//
40//#define DEBUG_UPREFS 1
41#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000042#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000043#else
44#define UR_OUT(X)
45#endif
46
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000047#define YYERROR_VERBOSE 1
48
Chris Lattner99e7ab72003-10-18 05:53:13 +000049// HACK ALERT: This variable is used to implement the automatic conversion of
50// variable argument instructions from their old to new forms. When this
51// compatiblity "Feature" is removed, this should be too.
52//
53static BasicBlock *CurBB;
54static bool ObsoleteVarArgs;
55
56
Chris Lattner7e708292002-06-25 16:13:24 +000057// This contains info used when building the body of a function. It is
58// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000059//
Chris Lattner9232b992003-04-22 18:42:41 +000060typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner16af11d2004-02-09 21:03:38 +000061static void ResolveDefinitions(std::map<unsigned,ValueList> &LateResolvers,
62 std::map<unsigned,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000063
64static struct PerModuleInfo {
65 Module *CurrentModule;
Chris Lattner16af11d2004-02-09 21:03:38 +000066 std::map<unsigned,ValueList> Values; // Module level numbered definitions
67 std::map<unsigned,ValueList> LateResolveValues;
68 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000069 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000070
Chris Lattner2079fde2001-10-13 06:41:08 +000071 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
72 // references to global values. Global values may be referenced before they
73 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000074 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000075 //
Chris Lattner9232b992003-04-22 18:42:41 +000076 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000077 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000078 GlobalRefsType GlobalRefs;
79
Chris Lattner00950542001-06-06 20:29:01 +000080 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000081 // If we could not resolve some functions at function compilation time
82 // (calls to functions before they are defined), resolve them now... Types
83 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000084 //
Chris Lattner00950542001-06-06 20:29:01 +000085 ResolveDefinitions(LateResolveValues);
86
Chris Lattner2079fde2001-10-13 06:41:08 +000087 // Check to make sure that all global value forward references have been
88 // resolved!
89 //
90 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000091 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000092
93 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
94 I != E; ++I) {
95 UndefinedReferences += " " + I->first.first->getDescription() + " " +
96 I->first.second.getName() + "\n";
97 }
98 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000099 }
100
Chris Lattner7e708292002-06-25 16:13:24 +0000101 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000102 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000103 CurrentModule = 0;
104 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000105
106
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000107 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000108 // is used to remove things from the forward declaration map, resolving them
109 // to the correct thing as needed.
110 //
111 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
112 // Check to see if there is a forward reference to this global variable...
113 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000114 GlobalRefsType::iterator I =
115 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000116
117 if (I != GlobalRefs.end()) {
Chris Lattnerbc207592004-03-08 06:09:57 +0000118 GlobalValue *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000119 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner2079fde2001-10-13 06:41:08 +0000120
121 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000122 // allowed to be is ConstantPointerRef's.
Chris Lattnerfd059242003-10-15 16:48:29 +0000123 assert(OldGV->hasOneUse() && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000124 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
125 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000126
Chris Lattner9e932bd2002-10-14 03:28:42 +0000127 // Change the const pool reference to point to the real global variable
128 // now. This should drop a use from the OldGV.
Chris Lattnerbc207592004-03-08 06:09:57 +0000129 CPR->replaceUsesOfWithOnConstant(OldGV, GV);
Chris Lattner9e932bd2002-10-14 03:28:42 +0000130 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000131
132 // Remove OldGV from the module...
Chris Lattnerbc207592004-03-08 06:09:57 +0000133 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
134 CurrentModule->getGlobalList().erase(GVar);
135 else
136 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000137
Chris Lattner2079fde2001-10-13 06:41:08 +0000138 // Remove the map entry for the global now that it has been created...
139 GlobalRefs.erase(I);
140 }
141 }
142
Chris Lattner00950542001-06-06 20:29:01 +0000143} CurModule;
144
Chris Lattner79df7c02002-03-26 18:01:55 +0000145static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000146 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000147
Chris Lattner16af11d2004-02-09 21:03:38 +0000148 std::map<unsigned,ValueList> Values; // Keep track of numbered definitions
149 std::map<unsigned,ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000150 std::vector<PATypeHolder> Types;
151 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner15b69722003-11-12 04:40:30 +0000152 SymbolTable LocalSymtab;
Chris Lattner7e708292002-06-25 16:13:24 +0000153 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000154
Chris Lattner79df7c02002-03-26 18:01:55 +0000155 inline PerFunctionInfo() {
156 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000157 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000158 }
159
Chris Lattner79df7c02002-03-26 18:01:55 +0000160 inline void FunctionStart(Function *M) {
161 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000162 }
163
Chris Lattner79df7c02002-03-26 18:01:55 +0000164 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000165 // If we could not resolve some blocks at parsing time (forward branches)
166 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000167 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000168
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000169 // Make sure to resolve any constant expr references that might exist within
170 // the function we just declared itself.
171 ValID FID;
172 if (CurrentFunction->hasName()) {
173 FID = ValID::create((char*)CurrentFunction->getName().c_str());
174 } else {
175 unsigned Slot = CurrentFunction->getType()->getUniqueID();
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000176 // Figure out which slot number if is...
Chris Lattner16af11d2004-02-09 21:03:38 +0000177 ValueList &List = CurModule.Values[Slot];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000178 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000179 assert(i < List.size() && "Function not found!");
180 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000181 FID = ValID::create((int)i);
182 break;
183 }
184 }
185 }
186 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
187
Chris Lattner7e708292002-06-25 16:13:24 +0000188 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000189 Types.clear(); // Clear out function local types
190 LocalSymtab.clear(); // Clear out function local symbol table
Chris Lattner79df7c02002-03-26 18:01:55 +0000191 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000192 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000193 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000194} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000195
Chris Lattner394f2eb2003-10-16 20:12:13 +0000196static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000197
Chris Lattner00950542001-06-06 20:29:01 +0000198
199//===----------------------------------------------------------------------===//
200// Code to handle definitions of all the types
201//===----------------------------------------------------------------------===//
202
Chris Lattner9232b992003-04-22 18:42:41 +0000203static int InsertValue(Value *D,
Chris Lattner16af11d2004-02-09 21:03:38 +0000204 std::map<unsigned,ValueList> &ValueTab = CurFun.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000205 if (D->hasName()) return -1; // Is this a numbered definition?
206
207 // Yes, insert the value into the value table...
208 unsigned type = D->getType()->getUniqueID();
Chris Lattner2079fde2001-10-13 06:41:08 +0000209 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
Chris Lattner16af11d2004-02-09 21:03:38 +0000210 ValueList &List = ValueTab[type];
211 List.push_back(D);
212 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000213}
214
Chris Lattner30c89792001-09-07 16:35:17 +0000215// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000216static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000217 Types.push_back(Ty);
218}
219
220static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000221 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000222 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000223 unsigned Num = (unsigned)D.Num;
224
225 // Module constants occupy the lowest numbered slots...
226 if (Num < CurModule.Types.size())
227 return CurModule.Types[Num];
228
229 Num -= CurModule.Types.size();
230
231 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000232 if (Num <= CurFun.Types.size())
233 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000234 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000235 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000236 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000237 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000238 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000239 Value *N = 0;
240 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000241 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000242 N = SymTab->lookupType(Name);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000243 }
Chris Lattner30c89792001-09-07 16:35:17 +0000244
245 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000246 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000247 // hasn't been added to the module...
248 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000249 SymTab = &CurModule.CurrentModule->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000250 N = SymTab->lookupType(Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000251 if (N == 0) break;
252 }
253
254 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000255 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000256 }
257 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000258 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000259 }
260
261 // If we reached here, we referenced either a symbol that we don't know about
262 // or an id number that hasn't been read yet. We may be referencing something
263 // forward, so just create an entry to be resolved later and get to it...
264 //
265 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
266
Chris Lattner9232b992003-04-22 18:42:41 +0000267 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000268 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000269
Chris Lattner9232b992003-04-22 18:42:41 +0000270 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000271 if (I != LateResolver.end()) {
272 return I->second;
273 }
Chris Lattner30c89792001-09-07 16:35:17 +0000274
Chris Lattner82269592001-10-22 06:01:08 +0000275 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000276 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000277 return Typ;
278}
279
Chris Lattner9232b992003-04-22 18:42:41 +0000280static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000281 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000282 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000283 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000284 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000285}
286
Chris Lattner2079fde2001-10-13 06:41:08 +0000287// getValNonImprovising - Look up the value specified by the provided type and
288// the provided ValID. If the value exists and has already been defined, return
289// it. Otherwise return null.
290//
291static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000292 if (isa<FunctionType>(Ty))
293 ThrowException("Functions are not values and "
294 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000295
Chris Lattner30c89792001-09-07 16:35:17 +0000296 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000297 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000298 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000299 unsigned Num = (unsigned)D.Num;
300
301 // Module constants occupy the lowest numbered slots...
Chris Lattner16af11d2004-02-09 21:03:38 +0000302 std::map<unsigned,ValueList>::iterator VI = CurModule.Values.find(type);
303 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 Lattner16af11d2004-02-09 21:03:38 +0000310 VI = CurFun.Values.find(type);
311 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
372
373// getVal - This function is identical to getValNonImprovising, except that if a
374// value is not already defined, it "improvises" by creating a placeholder var
375// that looks and acts just like the requested variable. When the value is
376// defined later, all uses of the placeholder variable are replaced with the
377// real thing.
378//
379static Value *getVal(const Type *Ty, const ValID &D) {
380 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
381
382 // See if the value has already been defined...
383 Value *V = getValNonImprovising(Ty, D);
384 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 Lattner00950542001-06-06 20:29:01 +0000390 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000391 switch (Ty->getPrimitiveID()) {
392 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000393 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000394 }
395
396 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000397 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000398 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000399 else
400 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000401 return d;
402}
403
404
405//===----------------------------------------------------------------------===//
406// Code to handle forward references in instructions
407//===----------------------------------------------------------------------===//
408//
409// This code handles the late binding needed with statements that reference
410// values not defined yet... for example, a forward branch, or the PHI node for
411// a loop body.
412//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000413// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000414// and back patchs after we are done.
415//
416
417// ResolveDefinitions - If we could not resolve some defs at parsing
418// time (forward branches, phi functions for loops, etc...) resolve the
419// defs now...
420//
Chris Lattner16af11d2004-02-09 21:03:38 +0000421static void ResolveDefinitions(std::map<unsigned,ValueList> &LateResolvers,
422 std::map<unsigned,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000423 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner16af11d2004-02-09 21:03:38 +0000424 for (std::map<unsigned,ValueList>::iterator LRI = LateResolvers.begin(),
425 E = LateResolvers.end(); LRI != E; ++LRI) {
426 ValueList &List = LRI->second;
427 while (!List.empty()) {
428 Value *V = List.back();
429 List.pop_back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000430 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000431 ValID &DID = getValIDFromPlaceHolder(V);
432
Chris Lattner16af11d2004-02-09 21:03:38 +0000433 Value *TheRealValue =
434 getValNonImprovising(Type::getUniqueIDType(LRI->first), DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000435 if (TheRealValue) {
436 V->replaceAllUsesWith(TheRealValue);
437 delete V;
438 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000439 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000440 // resolver table
441 InsertValue(V, *FutureLateResolvers);
442 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000443 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000444 ThrowException("Reference to an invalid definition: '" +DID.getName()+
445 "' of type '" + V->getType()->getDescription() + "'",
446 getLineNumFromPlaceHolder(V));
447 else
448 ThrowException("Reference to an invalid definition: #" +
449 itostr(DID.Num) + " of type '" +
450 V->getType()->getDescription() + "'",
451 getLineNumFromPlaceHolder(V));
452 }
Chris Lattner00950542001-06-06 20:29:01 +0000453 }
454 }
455
456 LateResolvers.clear();
457}
458
Chris Lattner4a42e902001-10-22 05:56:09 +0000459// ResolveTypeTo - A brand new type was just declared. This means that (if
460// name is not null) things referencing Name can be resolved. Otherwise, things
461// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000462//
Chris Lattner4a42e902001-10-22 05:56:09 +0000463static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000464 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000465 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000466
Chris Lattner4a42e902001-10-22 05:56:09 +0000467 ValID D;
468 if (Name) D = ValID::create(Name);
469 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000470
Chris Lattner9232b992003-04-22 18:42:41 +0000471 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000472 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000473
Chris Lattner9232b992003-04-22 18:42:41 +0000474 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000475 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000476 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000477 LateResolver.erase(I);
478 }
479}
480
481// ResolveTypes - At this point, all types should be resolved. Any that aren't
482// are errors.
483//
Chris Lattner9232b992003-04-22 18:42:41 +0000484static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000485 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000486 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000487
488 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000489 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000490 else
Chris Lattner82269592001-10-22 06:01:08 +0000491 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000492 }
493}
494
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000495
Chris Lattner1781aca2001-09-18 04:00:54 +0000496// setValueName - Set the specified value to the name given. The name may be
497// null potentially, in which case this is a noop. The string passed in is
498// assumed to be a malloc'd string buffer, and is freed by this function.
499//
Chris Lattnerb7474512001-10-03 15:39:04 +0000500// This function returns true if the value has already been defined, but is
501// allowed to be redefined in the specified context. If the name is a new name
502// for the typeplane, false is returned.
503//
504static bool setValueName(Value *V, char *NameStr) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000505 assert(!isa<Type>(V) && "Can't set name of a Type with setValueName");
Chris Lattnerb7474512001-10-03 15:39:04 +0000506 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000507
Chris Lattner9232b992003-04-22 18:42:41 +0000508 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000509 free(NameStr); // Free old string
510
Chris Lattner2079fde2001-10-13 06:41:08 +0000511 if (V->getType() == Type::VoidTy)
512 ThrowException("Can't assign name '" + Name +
513 "' to a null valued instruction!");
514
Chris Lattner6e6026b2002-11-20 18:36:02 +0000515 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000516 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000517 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000518
Reid Spencer75719bf2004-05-26 21:48:31 +0000519 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner42fbc7e2004-05-26 17:08:25 +0000520
Chris Lattner30c89792001-09-07 16:35:17 +0000521 if (Existing) { // Inserting a name that is already defined???
522 // There is only one case where this is allowed: when we are refining an
523 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner949a3622003-07-23 15:30:06 +0000524 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000525 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000526 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000527 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000528 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000529 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000530 }
Chris Lattner30c89792001-09-07 16:35:17 +0000531
Chris Lattner9636a912001-10-01 16:18:37 +0000532 // Otherwise, we are a simple redefinition of a value, check to see if it
533 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000534 if (const Type *Ty = dyn_cast<Type>(Existing)) {
535 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000536 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattner949a3622003-07-23 15:30:06 +0000537 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000538 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
539 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000540 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000541 // We are allowed to redefine a global variable in two circumstances:
542 // 1. If at least one of the globals is uninitialized or
543 // 2. If both initializers have the same value.
544 //
Chris Lattner89219832001-10-03 19:35:04 +0000545 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000546 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
547 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000548
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000549 // Make sure the existing global version gets the initializer! Make
550 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000551 if (GV->hasInitializer() && !EGV->hasInitializer())
552 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000553 if (GV->isConstant())
554 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000555 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000556
Chris Lattner2079fde2001-10-13 06:41:08 +0000557 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000558 return true; // They are equivalent!
559 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000560 }
Chris Lattner9636a912001-10-01 16:18:37 +0000561 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000562
Chris Lattner2079fde2001-10-13 06:41:08 +0000563 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000564 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000565 }
Chris Lattner00950542001-06-06 20:29:01 +0000566
Chris Lattner15b69722003-11-12 04:40:30 +0000567 // Set the name
Chris Lattner6e6026b2002-11-20 18:36:02 +0000568 V->setName(Name, &ST);
Chris Lattner15b69722003-11-12 04:40:30 +0000569
570 // If we're in function scope
571 if (inFunctionScope()) {
572 // Look up the symbol in the function's local symboltable
573 Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
574
575 // If it already exists
576 if (Existing) {
Chris Lattner15b69722003-11-12 04:40:30 +0000577 // Bail
578 ThrowException("Redefinition of value named '" + Name + "' in the '" +
579 V->getType()->getDescription() + "' type plane!");
580
581 // otherwise, since it doesn't exist
582 } else {
583 // Insert it.
584 CurFun.LocalSymtab.insert(V);
585 }
586 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000587 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000588}
589
Reid Spencer75719bf2004-05-26 21:48:31 +0000590// setTypeName - Set the specified type to the name given. The name may be
591// null potentially, in which case this is a noop. The string passed in is
592// assumed to be a malloc'd string buffer, and is freed by this function.
593//
594// This function returns true if the type has already been defined, but is
595// allowed to be redefined in the specified context. If the name is a new name
596// for the type plane, it is inserted and false is returned.
597static bool setTypeName(Type *T, char *NameStr) {
598 if (NameStr == 0) return false;
599
600 std::string Name(NameStr); // Copy string
601 free(NameStr); // Free old string
602
603 // We don't allow assigning names to void type
604 if (T == Type::VoidTy)
605 ThrowException("Can't assign name '" + Name + "' to the null type!");
606
607 SymbolTable &ST = inFunctionScope() ?
608 CurFun.CurrentFunction->getSymbolTable() :
609 CurModule.CurrentModule->getSymbolTable();
610
611 Type *Existing = ST.lookupType(Name);
612
613 if (Existing) { // Inserting a name that is already defined???
614 // There is only one case where this is allowed: when we are refining an
615 // opaque type. In this case, Existing will be an opaque type.
616 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
617 // We ARE replacing an opaque type!
618 ((OpaqueType*)OpTy)->refineAbstractTypeTo(T);
619 return true;
620 }
621
622 // Otherwise, this is an attempt to redefine a type. That's okay if
623 // the redefinition is identical to the original. This will be so if
624 // Existing and T point to the same Type object. In this one case we
625 // allow the equivalent redefinition.
626 if (Existing == T) return true; // Yes, it's equal.
627
628 // Any other kind of (non-equivalent) redefinition is an error.
629 ThrowException("Redefinition of type named '" + Name + "' in the '" +
630 T->getDescription() + "' type plane!");
631 }
632
633 // Okay, its a newly named type. Set its name.
634 T->setName(Name,&ST);
635
636 // If we're in function scope
637 if (inFunctionScope()) {
638 // Look up the symbol in the function's local symboltable
639 Existing = CurFun.LocalSymtab.lookupType(Name);
640
641 // If it already exists
642 if (Existing) {
643 // Bail
644 ThrowException("Redefinition of type named '" + Name + "' in the '" +
645 T->getDescription() + "' type plane in function scope!");
646
647 // otherwise, since it doesn't exist
648 } else {
649 // Insert it.
650 CurFun.LocalSymtab.insert(Name,T);
651 }
652 }
653 return false;
654}
Chris Lattner8896eda2001-07-09 19:38:36 +0000655
Chris Lattner30c89792001-09-07 16:35:17 +0000656//===----------------------------------------------------------------------===//
657// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000658//
Chris Lattner8896eda2001-07-09 19:38:36 +0000659
Chris Lattner3b073862004-02-09 03:19:29 +0000660// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000661//
662static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000663 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
664}
665
666namespace {
667 struct UpRefRecord {
668 // NestingLevel - The number of nesting levels that need to be popped before
669 // this type is resolved.
670 unsigned NestingLevel;
671
672 // LastContainedTy - This is the type at the current binding level for the
673 // type. Every time we reduce the nesting level, this gets updated.
674 const Type *LastContainedTy;
675
676 // UpRefTy - This is the actual opaque type that the upreference is
677 // represented with.
678 OpaqueType *UpRefTy;
679
680 UpRefRecord(unsigned NL, OpaqueType *URTy)
681 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
682 };
Chris Lattner30c89792001-09-07 16:35:17 +0000683}
Chris Lattner698b56e2001-07-20 19:15:08 +0000684
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000685// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000686static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000687
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000688/// HandleUpRefs - Every time we finish a new layer of types, this function is
689/// called. It loops through the UpRefs vector, which is a list of the
690/// currently active types. For each type, if the up reference is contained in
691/// the newly completed type, we decrement the level count. When the level
692/// count reaches zero, the upreferenced type is the type that is passed in:
693/// thus we can complete the cycle.
694///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000695static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000696 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000697 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000698 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000699 "' newly formed. Resolving upreferences.\n" <<
700 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000701
702 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
703 // to zero), we resolve them all together before we resolve them to Ty. At
704 // the end of the loop, if there is anything to resolve to Ty, it will be in
705 // this variable.
706 OpaqueType *TypeToResolve = 0;
707
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000708 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000709 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000710 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000711 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000712 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
713 // Decrement level of upreference
714 unsigned Level = --UpRefs[i].NestingLevel;
715 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000716 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000717 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000718 if (!TypeToResolve) {
719 TypeToResolve = UpRefs[i].UpRefTy;
720 } else {
721 UR_OUT(" * Resolving upreference for "
722 << UpRefs[i].second->getDescription() << "\n";
723 std::string OldName = UpRefs[i].UpRefTy->getDescription());
724 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
725 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
726 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
727 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000728 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
729 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000730 }
731 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000732 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000733
Chris Lattner026a8ce2004-02-09 18:53:54 +0000734 if (TypeToResolve) {
735 UR_OUT(" * Resolving upreference for "
736 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000737 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000738 TypeToResolve->refineAbstractTypeTo(Ty);
739 }
740
Chris Lattner8896eda2001-07-09 19:38:36 +0000741 return Ty;
742}
743
Chris Lattner30c89792001-09-07 16:35:17 +0000744
Chris Lattner00950542001-06-06 20:29:01 +0000745//===----------------------------------------------------------------------===//
746// RunVMAsmParser - Define an interface to this parser
747//===----------------------------------------------------------------------===//
748//
Chris Lattner9232b992003-04-22 18:42:41 +0000749Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000750 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000751 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000752 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000753 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000754
Chris Lattner75f20532003-04-22 18:02:52 +0000755 // Allocate a new module to read
756 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000757
758 try {
759 yyparse(); // Parse the file.
760 } catch (...) {
761 // Clear the symbol table so it doesn't complain when it
762 // gets destructed
763 CurFun.LocalSymtab.clear();
764 throw;
765 }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000766
Chris Lattner00950542001-06-06 20:29:01 +0000767 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000768
769 // Check to see if they called va_start but not va_arg..
770 if (!ObsoleteVarArgs)
771 if (Function *F = Result->getNamedFunction("llvm.va_start"))
772 if (F->asize() == 1) {
773 std::cerr << "WARNING: this file uses obsolete features. "
774 << "Assemble and disassemble to update it.\n";
775 ObsoleteVarArgs = true;
776 }
777
778
779 if (ObsoleteVarArgs) {
780 // If the user is making use of obsolete varargs intrinsics, adjust them for
781 // the user.
782 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
783 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
784
785 const Type *RetTy = F->getFunctionType()->getParamType(0);
786 RetTy = cast<PointerType>(RetTy)->getElementType();
787 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
788
789 while (!F->use_empty()) {
790 CallInst *CI = cast<CallInst>(F->use_back());
791 Value *V = new CallInst(NF, "", CI);
792 new StoreInst(V, CI->getOperand(1), CI);
793 CI->getParent()->getInstList().erase(CI);
794 }
795 Result->getFunctionList().erase(F);
796 }
797
798 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
799 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
800 const Type *ArgTy = F->getFunctionType()->getParamType(0);
801 ArgTy = cast<PointerType>(ArgTy)->getElementType();
802 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
803 ArgTy, 0);
804
805 while (!F->use_empty()) {
806 CallInst *CI = cast<CallInst>(F->use_back());
807 Value *V = new LoadInst(CI->getOperand(1), "", CI);
808 new CallInst(NF, V, "", CI);
809 CI->getParent()->getInstList().erase(CI);
810 }
811 Result->getFunctionList().erase(F);
812 }
813
814 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
815 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
816 const Type *ArgTy = F->getFunctionType()->getParamType(0);
817 ArgTy = cast<PointerType>(ArgTy)->getElementType();
818 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
819 ArgTy, 0);
820
821 while (!F->use_empty()) {
822 CallInst *CI = cast<CallInst>(F->use_back());
823 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
824 new StoreInst(V, CI->getOperand(1), CI);
825 CI->getParent()->getInstList().erase(CI);
826 }
827 Result->getFunctionList().erase(F);
828 }
829 }
830
Chris Lattner00950542001-06-06 20:29:01 +0000831 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
832 ParserResult = 0;
833
834 return Result;
835}
836
Brian Gaeked0fde302003-11-11 22:41:34 +0000837} // End llvm namespace
838
839using namespace llvm;
840
Chris Lattner00950542001-06-06 20:29:01 +0000841%}
842
843%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000844 llvm::Module *ModuleVal;
845 llvm::Function *FunctionVal;
846 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
847 llvm::BasicBlock *BasicBlockVal;
848 llvm::TerminatorInst *TermInstVal;
849 llvm::Instruction *InstVal;
850 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000851
Brian Gaeked0fde302003-11-11 22:41:34 +0000852 const llvm::Type *PrimType;
853 llvm::PATypeHolder *TypeVal;
854 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000855
Brian Gaeked0fde302003-11-11 22:41:34 +0000856 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
857 std::vector<llvm::Value*> *ValueList;
858 std::list<llvm::PATypeHolder> *TypeList;
859 std::list<std::pair<llvm::Value*,
860 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
861 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
862 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000863
Brian Gaeked0fde302003-11-11 22:41:34 +0000864 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000865 int64_t SInt64Val;
866 uint64_t UInt64Val;
867 int SIntVal;
868 unsigned UIntVal;
869 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000870 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000871
Chris Lattner30c89792001-09-07 16:35:17 +0000872 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000873 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000874
Brian Gaeked0fde302003-11-11 22:41:34 +0000875 llvm::Instruction::BinaryOps BinaryOpVal;
876 llvm::Instruction::TermOps TermOpVal;
877 llvm::Instruction::MemoryOps MemOpVal;
878 llvm::Instruction::OtherOps OtherOpVal;
879 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000880}
881
Chris Lattner79df7c02002-03-26 18:01:55 +0000882%type <ModuleVal> Module FunctionList
883%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000884%type <BasicBlockVal> BasicBlock InstructionList
885%type <TermInstVal> BBTerminatorInst
886%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000887%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000888%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000889%type <ArgList> ArgList ArgListH
890%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000891%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000892%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000893%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000894%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000895%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000896%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000897%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000898%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000899%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000900
Chris Lattner2079fde2001-10-13 06:41:08 +0000901// ValueRef - Unresolved reference to a definition or BB
902%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000903%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000904// Tokens and types for handling constant integer values
905//
906// ESINT64VAL - A negative number within long long range
907%token <SInt64Val> ESINT64VAL
908
909// EUINT64VAL - A positive number within uns. long long range
910%token <UInt64Val> EUINT64VAL
911%type <SInt64Val> EINT64VAL
912
913%token <SIntVal> SINTVAL // Signed 32 bit ints...
914%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
915%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000916%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000917
918// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000919%type <TypeVal> Types TypesV UpRTypes UpRTypesV
920%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000921%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
922%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000923
Chris Lattner6c23f572003-08-22 05:42:10 +0000924%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
925%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000926
927
Chris Lattner91ef4602004-03-31 03:49:47 +0000928%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000929%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000930%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000931%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000932
933// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000934%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000935
Chris Lattner00950542001-06-06 20:29:01 +0000936// Binary Operators
937%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000938%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000939%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000940%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000941
942// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000943%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000944
Chris Lattner027dcc52001-07-08 21:10:27 +0000945// Other Operators
946%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000947%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000948%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000949
Chris Lattner00950542001-06-06 20:29:01 +0000950%start Module
951%%
952
953// Handle constant integer size restriction and conversion...
954//
Chris Lattner51727be2002-06-04 21:58:56 +0000955INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000956INTVAL : UINTVAL {
957 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
958 ThrowException("Value too large for type!");
959 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000960};
Chris Lattner00950542001-06-06 20:29:01 +0000961
962
Chris Lattner51727be2002-06-04 21:58:56 +0000963EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000964EINT64VAL : EUINT64VAL {
965 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
966 ThrowException("Value too large for type!");
967 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000968};
Chris Lattner00950542001-06-06 20:29:01 +0000969
Chris Lattner00950542001-06-06 20:29:01 +0000970// Operations that are notably excluded from this list include:
971// RET, BR, & SWITCH because they end basic blocks and are treated specially.
972//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000973ArithmeticOps: ADD | SUB | MUL | DIV | REM;
974LogicalOps : AND | OR | XOR;
975SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
976BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
977
Chris Lattner51727be2002-06-04 21:58:56 +0000978ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000979
Chris Lattnere98dda62001-07-14 06:10:16 +0000980// These are some types that allow classification if we only want a particular
981// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000982SIntType : LONG | INT | SHORT | SBYTE;
983UIntType : ULONG | UINT | USHORT | UBYTE;
984IntType : SIntType | UIntType;
985FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000986
Chris Lattnere98dda62001-07-14 06:10:16 +0000987// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000988OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000989 $$ = $1;
990 }
991 | /*empty*/ {
992 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000993 };
Chris Lattner00950542001-06-06 20:29:01 +0000994
Chris Lattner4ad02e72003-04-16 20:28:45 +0000995OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
996 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000997 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000998 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
999 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +00001000
1001//===----------------------------------------------------------------------===//
1002// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001003// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001004// access to it, a user must explicitly use TypesV.
1005//
1006
1007// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001008TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1009UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001010
1011Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001012 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001013 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1014 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001015 };
Chris Lattner30c89792001-09-07 16:35:17 +00001016
1017
1018// Derived types are added later...
1019//
Chris Lattner51727be2002-06-04 21:58:56 +00001020PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1021PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001022UpRTypes : OPAQUE {
1023 $$ = new PATypeHolder(OpaqueType::get());
1024 }
1025 | PrimType {
1026 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001027 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001028UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001029 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001030};
Chris Lattner30c89792001-09-07 16:35:17 +00001031
Chris Lattner30c89792001-09-07 16:35:17 +00001032// Include derived types in the Types production.
1033//
1034UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001035 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001036 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001037 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001038 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001039 UR_OUT("New Upreference!\n");
1040 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001041 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001042 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001043 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001044 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001045 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1046 if (isVarArg) Params.pop_back();
1047
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001048 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001049 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001050 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001051 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001052 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001053 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001054 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001055 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001056 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001057 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001058 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001059 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001060
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001061 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001062 delete $2;
1063 }
1064 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001065 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001066 }
1067 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001068 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001069 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001070 };
Chris Lattner30c89792001-09-07 16:35:17 +00001071
Chris Lattner7e708292002-06-25 16:13:24 +00001072// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001073// declaration type lists
1074//
1075TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001076 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001077 $$->push_back(*$1); delete $1;
1078 }
1079 | TypeListI ',' UpRTypes {
1080 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001081 };
Chris Lattner30c89792001-09-07 16:35:17 +00001082
Chris Lattner7e708292002-06-25 16:13:24 +00001083// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001084ArgTypeListI : TypeListI
1085 | TypeListI ',' DOTDOTDOT {
1086 ($$=$1)->push_back(Type::VoidTy);
1087 }
1088 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001089 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001090 }
1091 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001092 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001093 };
Chris Lattner30c89792001-09-07 16:35:17 +00001094
Chris Lattnere98dda62001-07-14 06:10:16 +00001095// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001096// production is used ONLY to represent constants that show up AFTER a 'const',
1097// 'constant' or 'global' token at global scope. Constants that can be inlined
1098// into other expressions (such as integers and constexprs) are handled by the
1099// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001100//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001101ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001102 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001103 if (ATy == 0)
1104 ThrowException("Cannot make array constant with type: '" +
1105 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001106 const Type *ETy = ATy->getElementType();
1107 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001108
Chris Lattner30c89792001-09-07 16:35:17 +00001109 // Verify that we have the correct size...
1110 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001111 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001112 utostr($3->size()) + " arguments, but has size of " +
1113 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001114
Chris Lattner30c89792001-09-07 16:35:17 +00001115 // Verify all elements are correct type!
1116 for (unsigned i = 0; i < $3->size(); i++) {
1117 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001118 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001119 ETy->getDescription() +"' as required!\nIt is of type '"+
1120 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001121 }
1122
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001123 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001124 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001125 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001126 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001127 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001128 if (ATy == 0)
1129 ThrowException("Cannot make array constant with type: '" +
1130 (*$1)->getDescription() + "'!");
1131
1132 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001133 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001134 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001135 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001136 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001137 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001138 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001139 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001140 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001141 if (ATy == 0)
1142 ThrowException("Cannot make array constant with type: '" +
1143 (*$1)->getDescription() + "'!");
1144
Chris Lattner30c89792001-09-07 16:35:17 +00001145 int NumElements = ATy->getNumElements();
1146 const Type *ETy = ATy->getElementType();
1147 char *EndStr = UnEscapeLexed($3, true);
1148 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001149 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001150 itostr((int)(EndStr-$3)) +
1151 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001152 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001153 if (ETy == Type::SByteTy) {
1154 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001155 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001156 } else if (ETy == Type::UByteTy) {
1157 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001158 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001159 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001160 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001161 ThrowException("Cannot build string arrays of non byte sized elements!");
1162 }
Chris Lattner30c89792001-09-07 16:35:17 +00001163 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001164 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001165 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001166 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001167 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001168 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001169 if (STy == 0)
1170 ThrowException("Cannot make struct constant with type: '" +
1171 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001172
Chris Lattnerc6212f12003-05-21 16:06:56 +00001173 if ($3->size() != STy->getNumContainedTypes())
1174 ThrowException("Illegal number of initializers for structure type!");
1175
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001176 // Check to ensure that constants are compatible with the type initializer!
1177 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001178 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001179 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001180 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001181 "' for element #" + utostr(i) +
1182 " of structure initializer!");
1183
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001184 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001185 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001186 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001187 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001188 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001189 if (STy == 0)
1190 ThrowException("Cannot make struct constant with type: '" +
1191 (*$1)->getDescription() + "'!");
1192
1193 if (STy->getNumContainedTypes() != 0)
1194 ThrowException("Illegal number of initializers for structure type!");
1195
1196 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1197 delete $1;
1198 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001199 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001200 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001201 if (PTy == 0)
1202 ThrowException("Cannot make null pointer constant with type: '" +
1203 (*$1)->getDescription() + "'!");
1204
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001205 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001206 delete $1;
1207 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001208 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001209 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001210 if (Ty == 0)
1211 ThrowException("Global const reference must be a pointer type!");
1212
Chris Lattner3101c252002-08-15 17:58:33 +00001213 // ConstExprs can exist in the body of a function, thus creating
1214 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1215 // the context of a function, getValNonImprovising will search the functions
1216 // symbol table instead of the module symbol table for the global symbol,
1217 // which throws things all off. To get around this, we just tell
1218 // getValNonImprovising that we are at global scope here.
1219 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001220 Function *SavedCurFn = CurFun.CurrentFunction;
1221 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001222
Chris Lattner2079fde2001-10-13 06:41:08 +00001223 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001224
Chris Lattner394f2eb2003-10-16 20:12:13 +00001225 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001226
Chris Lattner2079fde2001-10-13 06:41:08 +00001227 // If this is an initializer for a constant pointer, which is referencing a
1228 // (currently) undefined variable, create a stub now that shall be replaced
1229 // in the future with the right type of variable.
1230 //
1231 if (V == 0) {
1232 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1233 const PointerType *PT = cast<PointerType>(Ty);
1234
1235 // First check to see if the forward references value is already created!
1236 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001237 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001238
1239 if (I != CurModule.GlobalRefs.end()) {
1240 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001241 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001242 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001243 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001244 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001245 false,
1246 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001247 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001248 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001249
1250 // Must temporarily push this value into the module table...
1251 CurModule.CurrentModule->getGlobalList().push_back(GV);
1252 V = GV;
1253 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001254 }
1255
Chris Lattner2079fde2001-10-13 06:41:08 +00001256 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001257 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001258 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001259 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001260 | Types ConstExpr {
1261 if ($1->get() != $2->getType())
1262 ThrowException("Mismatched types for constant expression!");
1263 $$ = $2;
1264 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001265 }
1266 | Types ZEROINITIALIZER {
1267 $$ = Constant::getNullValue($1->get());
1268 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001269 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001270
Chris Lattnere43f40b2002-10-09 00:25:32 +00001271ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001272 if (!ConstantSInt::isValueValidForType($1, $2))
1273 ThrowException("Constant value doesn't fit in type!");
1274 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001275 }
1276 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001277 if (!ConstantUInt::isValueValidForType($1, $2))
1278 ThrowException("Constant value doesn't fit in type!");
1279 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001280 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001281 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001282 $$ = ConstantBool::True;
1283 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001284 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001285 $$ = ConstantBool::False;
1286 }
1287 | FPType FPVAL { // Float & Double constants
1288 $$ = ConstantFP::get($1, $2);
1289 };
1290
Chris Lattner00950542001-06-06 20:29:01 +00001291
Chris Lattnerd78700d2002-08-16 21:14:40 +00001292ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001293 if (!$3->getType()->isFirstClassType())
1294 ThrowException("cast constant expression from a non-primitive type: '" +
1295 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001296 if (!$5->get()->isFirstClassType())
1297 ThrowException("cast constant expression to a non-primitive type: '" +
1298 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001299 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001300 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001301 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001302 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1303 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001304 ThrowException("GetElementPtr requires a pointer operand!");
1305
Chris Lattner830b6f92004-04-05 01:30:04 +00001306 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1307 // indices to uint struct indices for compatibility.
1308 generic_gep_type_iterator<std::vector<Value*>::iterator>
1309 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1310 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1311 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1312 if (isa<StructType>(*GTI)) // Only change struct indices
1313 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1314 if (CUI->getType() == Type::UByteTy)
1315 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1316
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001317 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001318 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001319 if (!IdxTy)
1320 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001321
Chris Lattner9232b992003-04-22 18:42:41 +00001322 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001323 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1324 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001325 IdxVec.push_back(C);
1326 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001327 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001328
Chris Lattnerd78700d2002-08-16 21:14:40 +00001329 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001330
Chris Lattnerd78700d2002-08-16 21:14:40 +00001331 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001332 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001333 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1334 if ($3->getType() != Type::BoolTy)
1335 ThrowException("Select condition must be of boolean type!");
1336 if ($5->getType() != $7->getType())
1337 ThrowException("Select operand types must match!");
1338 $$ = ConstantExpr::getSelect($3, $5, $7);
1339 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001340 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001341 if ($3->getType() != $5->getType())
1342 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001343 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001344 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001345 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001346 if ($5->getType() != Type::UByteTy)
1347 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001348 if (!$3->getType()->isInteger())
1349 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001350 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001351 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001352
1353
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001354// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001355ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001356 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001357 }
1358 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001359 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001360 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001361 };
Chris Lattner00950542001-06-06 20:29:01 +00001362
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001363
Chris Lattner1781aca2001-09-18 04:00:54 +00001364// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001365GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001366
Chris Lattner00950542001-06-06 20:29:01 +00001367
Chris Lattner0e73ce62002-05-02 19:11:13 +00001368//===----------------------------------------------------------------------===//
1369// Rules to match Modules
1370//===----------------------------------------------------------------------===//
1371
1372// Module rule: Capture the result of parsing the whole file into a result
1373// variable...
1374//
1375Module : FunctionList {
1376 $$ = ParserResult = $1;
1377 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001378};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001379
Chris Lattner7e708292002-06-25 16:13:24 +00001380// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001381//
1382FunctionList : FunctionList Function {
1383 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001384 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001385 }
1386 | FunctionList FunctionProto {
1387 $$ = $1;
1388 }
1389 | FunctionList IMPLEMENTATION {
1390 $$ = $1;
1391 }
1392 | ConstPool {
1393 $$ = CurModule.CurrentModule;
1394 // Resolve circular types before we parse the body of the module
1395 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001396 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001397
Chris Lattnere98dda62001-07-14 06:10:16 +00001398// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001399ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001400 if (!setValueName($4, $2))
1401 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001402 }
Chris Lattner30c89792001-09-07 16:35:17 +00001403 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001404 // Eagerly resolve types. This is not an optimization, this is a
1405 // requirement that is due to the fact that we could have this:
1406 //
1407 // %list = type { %list * }
1408 // %list = type { %list * } ; repeated type decl
1409 //
1410 // If types are not resolved eagerly, then the two types will not be
1411 // determined to be the same type!
1412 //
1413 ResolveTypeTo($2, $4->get());
1414
Chris Lattner1781aca2001-09-18 04:00:54 +00001415 // TODO: FIXME when Type are not const
Reid Spencer75719bf2004-05-26 21:48:31 +00001416 if (!setTypeName(const_cast<Type*>($4->get()), $2)) {
Chris Lattnerb7474512001-10-03 15:39:04 +00001417 // If this is not a redefinition of a type...
1418 if (!$2) {
1419 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001420 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001421 }
Chris Lattner30c89792001-09-07 16:35:17 +00001422 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001423
1424 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001425 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001426 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001427 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001428 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001429 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001430 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001431 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001432 if (Initializer == 0)
1433 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001434
Chris Lattnerdda71962001-11-26 18:54:16 +00001435 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001436 if (!setValueName(GV, $2)) { // If not redefining...
1437 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001438 int Slot = InsertValue(GV, CurModule.Values);
1439
1440 if (Slot != -1) {
1441 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1442 } else {
1443 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1444 (char*)GV->getName().c_str()));
1445 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001446 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001447 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001448 | ConstPool OptAssign EXTERNAL GlobalType Types {
1449 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001450 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001451 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001452 if (!setValueName(GV, $2)) { // If not redefining...
1453 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001454 int Slot = InsertValue(GV, CurModule.Values);
1455
1456 if (Slot != -1) {
1457 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1458 } else {
1459 assert(GV->hasName() && "Not named and not numbered!?");
1460 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1461 (char*)GV->getName().c_str()));
1462 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001463 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001464 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001465 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001466 | ConstPool TARGET TargetDefinition {
1467 }
Chris Lattner00950542001-06-06 20:29:01 +00001468 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001469 };
Chris Lattner00950542001-06-06 20:29:01 +00001470
1471
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001472
1473BigOrLittle : BIG { $$ = Module::BigEndian; };
1474BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1475
1476TargetDefinition : ENDIAN '=' BigOrLittle {
1477 CurModule.CurrentModule->setEndianness($3);
1478 }
1479 | POINTERSIZE '=' EUINT64VAL {
1480 if ($3 == 32)
1481 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1482 else if ($3 == 64)
1483 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1484 else
1485 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1486 };
1487
1488
Chris Lattner00950542001-06-06 20:29:01 +00001489//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001490// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001491//===----------------------------------------------------------------------===//
1492
Chris Lattner6c23f572003-08-22 05:42:10 +00001493Name : VAR_ID | STRINGCONSTANT;
1494OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001495
Chris Lattner6c23f572003-08-22 05:42:10 +00001496ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001497 if (*$1 == Type::VoidTy)
1498 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001499 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001500};
Chris Lattner00950542001-06-06 20:29:01 +00001501
Chris Lattner69da5cf2002-10-13 20:57:00 +00001502ArgListH : ArgListH ',' ArgVal {
1503 $$ = $1;
1504 $1->push_back(*$3);
1505 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001506 }
1507 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001508 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001509 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001510 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001511 };
Chris Lattner00950542001-06-06 20:29:01 +00001512
1513ArgList : ArgListH {
1514 $$ = $1;
1515 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001516 | ArgListH ',' DOTDOTDOT {
1517 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001518 $$->push_back(std::pair<PATypeHolder*,
1519 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001520 }
1521 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001522 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1523 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001524 }
Chris Lattner00950542001-06-06 20:29:01 +00001525 | /* empty */ {
1526 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001527 };
Chris Lattner00950542001-06-06 20:29:01 +00001528
Chris Lattner6c23f572003-08-22 05:42:10 +00001529FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001530 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001531 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001532
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001533 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1534 ThrowException("LLVM functions cannot return aggregate types!");
1535
Chris Lattner9232b992003-04-22 18:42:41 +00001536 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001537 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001538 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001539 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001540 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001541 }
Chris Lattner00950542001-06-06 20:29:01 +00001542
Chris Lattner2079fde2001-10-13 06:41:08 +00001543 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1544 if (isVarArg) ParamTypeList.pop_back();
1545
Chris Lattner1f862af2003-04-16 18:13:57 +00001546 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001547 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001548 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001549
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001550 Function *Fn = 0;
1551 // Is the function already in symtab?
1552 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1553 // Yes it is. If this is the case, either we need to be a forward decl,
1554 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001555 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001556 ThrowException("Redefinition of function '" + FunctionName + "'!");
1557
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001558 // Make sure to strip off any argument names so we can't get conflicts...
1559 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1560 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001561
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001562 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001563 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1564 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001565 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001566 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001567 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001568 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001569
Chris Lattner394f2eb2003-10-16 20:12:13 +00001570 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001571
Chris Lattner7e708292002-06-25 16:13:24 +00001572 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001573 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001574 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001575 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001576 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001577 delete $4->back().first;
1578 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001579 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001580 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001581 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001582 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001583 delete I->first; // Delete the typeholder...
1584
1585 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001586 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001587
Chris Lattner69da5cf2002-10-13 20:57:00 +00001588 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001589 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001590
Chris Lattner1f862af2003-04-16 18:13:57 +00001591 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001592 }
Chris Lattner51727be2002-06-04 21:58:56 +00001593};
Chris Lattner00950542001-06-06 20:29:01 +00001594
Chris Lattner9b02cc32002-05-03 18:23:48 +00001595BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1596
Chris Lattner4ad02e72003-04-16 20:28:45 +00001597FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001598 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001599
Chris Lattner4ad02e72003-04-16 20:28:45 +00001600 // Make sure that we keep track of the linkage type even if there was a
1601 // previous "declare".
1602 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001603
Chris Lattner7e708292002-06-25 16:13:24 +00001604 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001605 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001606};
Chris Lattner00950542001-06-06 20:29:01 +00001607
Chris Lattner9b02cc32002-05-03 18:23:48 +00001608END : ENDTOK | '}'; // Allow end of '}' to end a function
1609
Chris Lattner79df7c02002-03-26 18:01:55 +00001610Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001611 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001612};
Chris Lattner00950542001-06-06 20:29:01 +00001613
Chris Lattner394f2eb2003-10-16 20:12:13 +00001614FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1615 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001616 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001617};
Chris Lattner00950542001-06-06 20:29:01 +00001618
1619//===----------------------------------------------------------------------===//
1620// Rules to match Basic Blocks
1621//===----------------------------------------------------------------------===//
1622
1623ConstValueRef : ESINT64VAL { // A reference to a direct constant
1624 $$ = ValID::create($1);
1625 }
1626 | EUINT64VAL {
1627 $$ = ValID::create($1);
1628 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001629 | FPVAL { // Perhaps it's an FP constant?
1630 $$ = ValID::create($1);
1631 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001632 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001633 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001634 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001635 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001636 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001637 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001638 | NULL_TOK {
1639 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001640 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001641 | ConstExpr {
1642 $$ = ValID::create($1);
1643 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001644
Chris Lattner2079fde2001-10-13 06:41:08 +00001645// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1646// another value.
1647//
1648SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001649 $$ = ValID::create($1);
1650 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001651 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001652 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001653 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001654
1655// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001656ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001657
Chris Lattner00950542001-06-06 20:29:01 +00001658
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001659// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1660// type immediately preceeds the value reference, and allows complex constant
1661// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001662ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001663 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001664 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001665
Chris Lattner00950542001-06-06 20:29:01 +00001666BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001667 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001668 }
Chris Lattner7e708292002-06-25 16:13:24 +00001669 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1670 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001671 };
Chris Lattner00950542001-06-06 20:29:01 +00001672
1673
1674// Basic blocks are terminated by branching instructions:
1675// br, br/cc, switch, ret
1676//
Chris Lattner2079fde2001-10-13 06:41:08 +00001677BasicBlock : InstructionList OptAssign BBTerminatorInst {
1678 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1679 InsertValue($3);
1680
1681 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001682 InsertValue($1);
1683 $$ = $1;
1684 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001685 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1686 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1687 InsertValue($4);
1688
1689 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001690 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001691
1692 InsertValue($2);
1693 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001694 };
Chris Lattner00950542001-06-06 20:29:01 +00001695
1696InstructionList : InstructionList Inst {
1697 $1->getInstList().push_back($2);
1698 $$ = $1;
1699 }
1700 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001701 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001702 };
Chris Lattner00950542001-06-06 20:29:01 +00001703
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001704BBTerminatorInst : RET ResolvedVal { // Return with a result...
1705 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001706 }
1707 | RET VOID { // Return with no result...
1708 $$ = new ReturnInst();
1709 }
1710 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001711 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001712 } // Conditional Branch...
1713 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001714 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1715 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001716 getVal(Type::BoolTy, $3));
1717 }
1718 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1719 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001720 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001721 $$ = S;
1722
Chris Lattner9232b992003-04-22 18:42:41 +00001723 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001724 E = $8->end();
1725 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001726 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001727 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001728 }
Chris Lattner196850c2003-05-15 21:30:00 +00001729 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1730 SwitchInst *S = new SwitchInst(getVal($2, $3),
1731 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1732 $$ = S;
1733 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001734 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001735 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001736 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001737 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001738
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001739 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1740 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001741 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001742 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001743 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001744 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1745 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001746 ParamTypes.push_back((*I)->getType());
1747 }
1748
1749 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1750 if (isVarArg) ParamTypes.pop_back();
1751
Chris Lattner79df7c02002-03-26 18:01:55 +00001752 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001753 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001754 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001755
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001756 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001757
1758 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1759 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1760
1761 if (Normal == 0 || Except == 0)
1762 ThrowException("Invoke instruction without label destinations!");
1763
1764 // Create the call node...
1765 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001766 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001767 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001768 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001769 // correctly!
1770 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001771 FunctionType::param_iterator I = Ty->param_begin();
1772 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001773 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001774
1775 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1776 if ((*ArgI)->getType() != *I)
1777 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001778 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001779
1780 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1781 ThrowException("Invalid number of parameters detected!");
1782
Chris Lattner6cdb0112001-11-26 16:54:11 +00001783 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001784 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001785 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001786 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001787 }
1788 | UNWIND {
1789 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001790 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001791
1792
Chris Lattner00950542001-06-06 20:29:01 +00001793
1794JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1795 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001796 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001797 if (V == 0)
1798 ThrowException("May only switch on a constant pool value!");
1799
Chris Lattner9232b992003-04-22 18:42:41 +00001800 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001801 }
1802 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001803 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001804 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001805
1806 if (V == 0)
1807 ThrowException("May only switch on a constant pool value!");
1808
Chris Lattner9232b992003-04-22 18:42:41 +00001809 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001810 };
Chris Lattner00950542001-06-06 20:29:01 +00001811
1812Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001813 // Is this definition named?? if so, assign the name...
1814 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001815 InsertValue($2);
1816 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001817};
Chris Lattner00950542001-06-06 20:29:01 +00001818
Chris Lattnerc24d2082001-06-11 15:04:20 +00001819PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001820 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1821 $$->push_back(std::make_pair(getVal(*$1, $3),
1822 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001823 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001824 }
1825 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1826 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001827 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1828 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001829 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001830
1831
Chris Lattner30c89792001-09-07 16:35:17 +00001832ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001833 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001834 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001835 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001836 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001837 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001838 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001839 };
Chris Lattner00950542001-06-06 20:29:01 +00001840
1841// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001842ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001843
Chris Lattner4a6482b2002-09-10 19:57:26 +00001844InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1845 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1846 ThrowException("Arithmetic operator requires integer or FP operands!");
1847 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1848 if ($$ == 0)
1849 ThrowException("binary operator returned null!");
1850 delete $2;
1851 }
1852 | LogicalOps Types ValueRef ',' ValueRef {
1853 if (!(*$2)->isIntegral())
1854 ThrowException("Logical operator requires integral operands!");
1855 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1856 if ($$ == 0)
1857 ThrowException("binary operator returned null!");
1858 delete $2;
1859 }
1860 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001861 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001862 if ($$ == 0)
1863 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001864 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001865 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001866 | NOT ResolvedVal {
1867 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1868 << " Replacing with 'xor'.\n";
1869
1870 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1871 if (Ones == 0)
1872 ThrowException("Expected integral type for not instruction!");
1873
1874 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001875 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001876 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001877 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001878 | ShiftOps ResolvedVal ',' ResolvedVal {
1879 if ($4->getType() != Type::UByteTy)
1880 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001881 if (!$2->getType()->isInteger())
1882 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001883 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001884 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001885 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001886 if (!$4->get()->isFirstClassType())
1887 ThrowException("cast instruction to a non-primitive type: '" +
1888 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001889 $$ = new CastInst($2, *$4);
1890 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001891 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001892 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1893 if ($2->getType() != Type::BoolTy)
1894 ThrowException("select condition must be boolean!");
1895 if ($4->getType() != $6->getType())
1896 ThrowException("select value types should match!");
1897 $$ = new SelectInst($2, $4, $6);
1898 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001899 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001900 // FIXME: This is emulation code for an obsolete syntax. This should be
1901 // removed at some point.
1902 if (!ObsoleteVarArgs) {
1903 std::cerr << "WARNING: this file uses obsolete features. "
1904 << "Assemble and disassemble to update it.\n";
1905 ObsoleteVarArgs = true;
1906 }
1907
1908 // First, load the valist...
1909 Instruction *CurVAList = new LoadInst($2, "");
1910 CurBB->getInstList().push_back(CurVAList);
1911
1912 // Emit the vaarg instruction.
1913 $$ = new VAArgInst(CurVAList, *$4);
1914
1915 // Now we must advance the pointer and update it in memory.
1916 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1917 CurBB->getInstList().push_back(TheVANext);
1918
1919 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1920 delete $4;
1921 }
1922 | VAARG ResolvedVal ',' Types {
1923 $$ = new VAArgInst($2, *$4);
1924 delete $4;
1925 }
1926 | VANEXT ResolvedVal ',' Types {
1927 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001928 delete $4;
1929 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001930 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001931 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001932 if (!Ty->isFirstClassType())
1933 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001934 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001935 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001936 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001937 if ($2->front().first->getType() != Ty)
1938 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001939 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001940 $2->pop_front();
1941 }
1942 delete $2; // Free the list...
1943 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001944 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001945 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001946 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001947
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001948 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1949 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001950 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001951 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001952 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001953 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1954 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001955 ParamTypes.push_back((*I)->getType());
1956 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001957
1958 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1959 if (isVarArg) ParamTypes.pop_back();
1960
Chris Lattner79df7c02002-03-26 18:01:55 +00001961 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001962 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001963 }
Chris Lattner00950542001-06-06 20:29:01 +00001964
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001965 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001966
Chris Lattner8b81bf52001-07-25 22:47:46 +00001967 // Create the call node...
1968 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001969 // Make sure no arguments is a good thing!
1970 if (Ty->getNumParams() != 0)
1971 ThrowException("No arguments passed to a function that "
1972 "expects arguments!");
1973
Chris Lattner9232b992003-04-22 18:42:41 +00001974 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001975 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001976 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001977 // correctly!
1978 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001979 FunctionType::param_iterator I = Ty->param_begin();
1980 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001981 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001982
1983 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1984 if ((*ArgI)->getType() != *I)
1985 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001986 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001987
Chris Lattner8b81bf52001-07-25 22:47:46 +00001988 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001989 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001990
Chris Lattner6cdb0112001-11-26 16:54:11 +00001991 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001992 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001993 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001994 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001995 }
1996 | MemoryInst {
1997 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001998 };
Chris Lattner00950542001-06-06 20:29:01 +00001999
Chris Lattner6cdb0112001-11-26 16:54:11 +00002000
2001// IndexList - List of indices for GEP based instructions...
2002IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00002003 $$ = $2;
2004 } | /* empty */ {
2005 $$ = new std::vector<Value*>();
2006 };
2007
2008OptVolatile : VOLATILE {
2009 $$ = true;
2010 }
2011 | /* empty */ {
2012 $$ = false;
2013 };
2014
Chris Lattner027dcc52001-07-08 21:10:27 +00002015
Chris Lattner00950542001-06-06 20:29:01 +00002016MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002017 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002018 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002019 }
2020 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002021 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002022 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002023 }
2024 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002025 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002026 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002027 }
2028 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002029 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002030 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002031 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002032 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002033 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002034 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002035 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002036 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002037 }
2038
Chris Lattner15c9c032003-09-08 18:20:29 +00002039 | OptVolatile LOAD Types ValueRef {
2040 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002041 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002042 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002043 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002044 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002045 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002046 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2047 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002048 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002049 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002050 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002051 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002052 if (ElTy != $3->getType())
2053 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002054 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002055
Chris Lattner79ad13802003-09-08 20:29:46 +00002056 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002057 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002058 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002059 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002060 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002061 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002062
2063 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2064 // indices to uint struct indices for compatibility.
2065 generic_gep_type_iterator<std::vector<Value*>::iterator>
2066 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2067 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2068 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2069 if (isa<StructType>(*GTI)) // Only change struct indices
2070 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2071 if (CUI->getType() == Type::UByteTy)
2072 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2073
Chris Lattner30c89792001-09-07 16:35:17 +00002074 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002075 ThrowException("Invalid getelementptr indices for type '" +
2076 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002077 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2078 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002079 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002080
Brian Gaeked0fde302003-11-11 22:41:34 +00002081
Chris Lattner00950542001-06-06 20:29:01 +00002082%%
Chris Lattner09083092001-07-08 04:57:15 +00002083int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002084 std::string where
2085 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002086 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002087 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002088 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002089 errMsg += "end-of-file.";
2090 else
Chris Lattner9232b992003-04-22 18:42:41 +00002091 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002092 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002093 return 0;
2094}