blob: f06b5b02e944b999bddcd178d7a466b4aaad79b3 [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) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000380
381 // See if the value has already been defined...
382 Value *V = getValNonImprovising(Ty, D);
383 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000384
385 // If we reached here, we referenced either a symbol that we don't know about
386 // or an id number that hasn't been read yet. We may be referencing something
387 // forward, so just create an entry to be resolved later and get to it...
388 //
Chris Lattner00950542001-06-06 20:29:01 +0000389 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000390 switch (Ty->getPrimitiveID()) {
391 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000392 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000393 }
394
395 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000396 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000397 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000398 else
399 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000400 return d;
401}
402
403
404//===----------------------------------------------------------------------===//
405// Code to handle forward references in instructions
406//===----------------------------------------------------------------------===//
407//
408// This code handles the late binding needed with statements that reference
409// values not defined yet... for example, a forward branch, or the PHI node for
410// a loop body.
411//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000412// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000413// and back patchs after we are done.
414//
415
416// ResolveDefinitions - If we could not resolve some defs at parsing
417// time (forward branches, phi functions for loops, etc...) resolve the
418// defs now...
419//
Chris Lattner16af11d2004-02-09 21:03:38 +0000420static void ResolveDefinitions(std::map<unsigned,ValueList> &LateResolvers,
421 std::map<unsigned,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000422 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner16af11d2004-02-09 21:03:38 +0000423 for (std::map<unsigned,ValueList>::iterator LRI = LateResolvers.begin(),
424 E = LateResolvers.end(); LRI != E; ++LRI) {
425 ValueList &List = LRI->second;
426 while (!List.empty()) {
427 Value *V = List.back();
428 List.pop_back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000429 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000430 ValID &DID = getValIDFromPlaceHolder(V);
431
Chris Lattner16af11d2004-02-09 21:03:38 +0000432 Value *TheRealValue =
433 getValNonImprovising(Type::getUniqueIDType(LRI->first), DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000434 if (TheRealValue) {
435 V->replaceAllUsesWith(TheRealValue);
436 delete V;
437 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000438 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000439 // resolver table
440 InsertValue(V, *FutureLateResolvers);
441 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000442 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000443 ThrowException("Reference to an invalid definition: '" +DID.getName()+
444 "' of type '" + V->getType()->getDescription() + "'",
445 getLineNumFromPlaceHolder(V));
446 else
447 ThrowException("Reference to an invalid definition: #" +
448 itostr(DID.Num) + " of type '" +
449 V->getType()->getDescription() + "'",
450 getLineNumFromPlaceHolder(V));
451 }
Chris Lattner00950542001-06-06 20:29:01 +0000452 }
453 }
454
455 LateResolvers.clear();
456}
457
Chris Lattner4a42e902001-10-22 05:56:09 +0000458// ResolveTypeTo - A brand new type was just declared. This means that (if
459// name is not null) things referencing Name can be resolved. Otherwise, things
460// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000461//
Chris Lattner4a42e902001-10-22 05:56:09 +0000462static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000463 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000464 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000465
Chris Lattner4a42e902001-10-22 05:56:09 +0000466 ValID D;
467 if (Name) D = ValID::create(Name);
468 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000469
Chris Lattner9232b992003-04-22 18:42:41 +0000470 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000471 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000472
Chris Lattner9232b992003-04-22 18:42:41 +0000473 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000474 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000475 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000476 LateResolver.erase(I);
477 }
478}
479
480// ResolveTypes - At this point, all types should be resolved. Any that aren't
481// are errors.
482//
Chris Lattner9232b992003-04-22 18:42:41 +0000483static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000484 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000485 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000486
487 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000488 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000489 else
Chris Lattner82269592001-10-22 06:01:08 +0000490 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000491 }
492}
493
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000494
Chris Lattner1781aca2001-09-18 04:00:54 +0000495// setValueName - Set the specified value to the name given. The name may be
496// null potentially, in which case this is a noop. The string passed in is
497// assumed to be a malloc'd string buffer, and is freed by this function.
498//
Chris Lattnerb7474512001-10-03 15:39:04 +0000499// This function returns true if the value has already been defined, but is
500// allowed to be redefined in the specified context. If the name is a new name
501// for the typeplane, false is returned.
502//
503static bool setValueName(Value *V, char *NameStr) {
Reid Spencer75719bf2004-05-26 21:48:31 +0000504 assert(!isa<Type>(V) && "Can't set name of a Type with setValueName");
Chris Lattnerb7474512001-10-03 15:39:04 +0000505 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000506
Chris Lattner9232b992003-04-22 18:42:41 +0000507 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000508 free(NameStr); // Free old string
509
Chris Lattner2079fde2001-10-13 06:41:08 +0000510 if (V->getType() == Type::VoidTy)
511 ThrowException("Can't assign name '" + Name +
512 "' to a null valued instruction!");
513
Chris Lattner6e6026b2002-11-20 18:36:02 +0000514 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000515 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000516 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000517
Reid Spencer75719bf2004-05-26 21:48:31 +0000518 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner42fbc7e2004-05-26 17:08:25 +0000519
Chris Lattner30c89792001-09-07 16:35:17 +0000520 if (Existing) { // Inserting a name that is already defined???
Reid Spencer9ed0f172004-05-27 22:05:50 +0000521 // We are a simple redefinition of a value, check to see if it
Chris Lattner9636a912001-10-01 16:18:37 +0000522 // is defined the same as the old one...
Reid Spencer4e6620c2004-05-28 00:21:06 +0000523 if (const Constant *C = dyn_cast<Constant>(Existing)) {
Chris Lattneradf99702003-03-03 23:28:55 +0000524 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000525 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000526 // We are allowed to redefine a global variable in two circumstances:
527 // 1. If at least one of the globals is uninitialized or
528 // 2. If both initializers have the same value.
529 //
Chris Lattner89219832001-10-03 19:35:04 +0000530 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000531 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
532 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000533
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000534 // Make sure the existing global version gets the initializer! Make
535 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000536 if (GV->hasInitializer() && !EGV->hasInitializer())
537 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000538 if (GV->isConstant())
539 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000540 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000541
Chris Lattner2079fde2001-10-13 06:41:08 +0000542 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000543 return true; // They are equivalent!
544 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000545 }
Chris Lattner9636a912001-10-01 16:18:37 +0000546 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000547
Chris Lattner2079fde2001-10-13 06:41:08 +0000548 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000549 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000550 }
Chris Lattner00950542001-06-06 20:29:01 +0000551
Chris Lattner15b69722003-11-12 04:40:30 +0000552 // Set the name
Chris Lattner6e6026b2002-11-20 18:36:02 +0000553 V->setName(Name, &ST);
Chris Lattner15b69722003-11-12 04:40:30 +0000554
555 // If we're in function scope
556 if (inFunctionScope()) {
557 // Look up the symbol in the function's local symboltable
558 Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
559
560 // If it already exists
561 if (Existing) {
Chris Lattner15b69722003-11-12 04:40:30 +0000562 // Bail
563 ThrowException("Redefinition of value named '" + Name + "' in the '" +
564 V->getType()->getDescription() + "' type plane!");
565
566 // otherwise, since it doesn't exist
567 } else {
568 // Insert it.
569 CurFun.LocalSymtab.insert(V);
570 }
571 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000572 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000573}
574
Reid Spencer75719bf2004-05-26 21:48:31 +0000575// setTypeName - Set the specified type to the name given. The name may be
576// null potentially, in which case this is a noop. The string passed in is
577// assumed to be a malloc'd string buffer, and is freed by this function.
578//
579// This function returns true if the type has already been defined, but is
580// allowed to be redefined in the specified context. If the name is a new name
581// for the type plane, it is inserted and false is returned.
582static bool setTypeName(Type *T, char *NameStr) {
583 if (NameStr == 0) return false;
584
585 std::string Name(NameStr); // Copy string
586 free(NameStr); // Free old string
587
588 // We don't allow assigning names to void type
589 if (T == Type::VoidTy)
590 ThrowException("Can't assign name '" + Name + "' to the null type!");
591
592 SymbolTable &ST = inFunctionScope() ?
593 CurFun.CurrentFunction->getSymbolTable() :
594 CurModule.CurrentModule->getSymbolTable();
595
596 Type *Existing = ST.lookupType(Name);
597
598 if (Existing) { // Inserting a name that is already defined???
599 // There is only one case where this is allowed: when we are refining an
600 // opaque type. In this case, Existing will be an opaque type.
601 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
602 // We ARE replacing an opaque type!
603 ((OpaqueType*)OpTy)->refineAbstractTypeTo(T);
604 return true;
605 }
606
607 // Otherwise, this is an attempt to redefine a type. That's okay if
608 // the redefinition is identical to the original. This will be so if
609 // Existing and T point to the same Type object. In this one case we
610 // allow the equivalent redefinition.
611 if (Existing == T) return true; // Yes, it's equal.
612
613 // Any other kind of (non-equivalent) redefinition is an error.
614 ThrowException("Redefinition of type named '" + Name + "' in the '" +
615 T->getDescription() + "' type plane!");
616 }
617
618 // Okay, its a newly named type. Set its name.
619 T->setName(Name,&ST);
620
621 // If we're in function scope
622 if (inFunctionScope()) {
623 // Look up the symbol in the function's local symboltable
624 Existing = CurFun.LocalSymtab.lookupType(Name);
625
626 // If it already exists
627 if (Existing) {
628 // Bail
629 ThrowException("Redefinition of type named '" + Name + "' in the '" +
630 T->getDescription() + "' type plane in function scope!");
631
632 // otherwise, since it doesn't exist
633 } else {
634 // Insert it.
635 CurFun.LocalSymtab.insert(Name,T);
636 }
637 }
638 return false;
639}
Chris Lattner8896eda2001-07-09 19:38:36 +0000640
Chris Lattner30c89792001-09-07 16:35:17 +0000641//===----------------------------------------------------------------------===//
642// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000643//
Chris Lattner8896eda2001-07-09 19:38:36 +0000644
Chris Lattner3b073862004-02-09 03:19:29 +0000645// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000646//
647static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000648 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
649}
650
651namespace {
652 struct UpRefRecord {
653 // NestingLevel - The number of nesting levels that need to be popped before
654 // this type is resolved.
655 unsigned NestingLevel;
656
657 // LastContainedTy - This is the type at the current binding level for the
658 // type. Every time we reduce the nesting level, this gets updated.
659 const Type *LastContainedTy;
660
661 // UpRefTy - This is the actual opaque type that the upreference is
662 // represented with.
663 OpaqueType *UpRefTy;
664
665 UpRefRecord(unsigned NL, OpaqueType *URTy)
666 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
667 };
Chris Lattner30c89792001-09-07 16:35:17 +0000668}
Chris Lattner698b56e2001-07-20 19:15:08 +0000669
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000670// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000671static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000672
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000673/// HandleUpRefs - Every time we finish a new layer of types, this function is
674/// called. It loops through the UpRefs vector, which is a list of the
675/// currently active types. For each type, if the up reference is contained in
676/// the newly completed type, we decrement the level count. When the level
677/// count reaches zero, the upreferenced type is the type that is passed in:
678/// thus we can complete the cycle.
679///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000680static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000681 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000682 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000683 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000684 "' newly formed. Resolving upreferences.\n" <<
685 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000686
687 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
688 // to zero), we resolve them all together before we resolve them to Ty. At
689 // the end of the loop, if there is anything to resolve to Ty, it will be in
690 // this variable.
691 OpaqueType *TypeToResolve = 0;
692
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000693 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000694 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000695 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000696 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000697 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
698 // Decrement level of upreference
699 unsigned Level = --UpRefs[i].NestingLevel;
700 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000701 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000702 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000703 if (!TypeToResolve) {
704 TypeToResolve = UpRefs[i].UpRefTy;
705 } else {
706 UR_OUT(" * Resolving upreference for "
707 << UpRefs[i].second->getDescription() << "\n";
708 std::string OldName = UpRefs[i].UpRefTy->getDescription());
709 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
710 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
711 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
712 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000713 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
714 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000715 }
716 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000717 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000718
Chris Lattner026a8ce2004-02-09 18:53:54 +0000719 if (TypeToResolve) {
720 UR_OUT(" * Resolving upreference for "
721 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000722 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000723 TypeToResolve->refineAbstractTypeTo(Ty);
724 }
725
Chris Lattner8896eda2001-07-09 19:38:36 +0000726 return Ty;
727}
728
Chris Lattner30c89792001-09-07 16:35:17 +0000729
Chris Lattner00950542001-06-06 20:29:01 +0000730//===----------------------------------------------------------------------===//
731// RunVMAsmParser - Define an interface to this parser
732//===----------------------------------------------------------------------===//
733//
Chris Lattner9232b992003-04-22 18:42:41 +0000734Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000735 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000736 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000737 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000738 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000739
Chris Lattner75f20532003-04-22 18:02:52 +0000740 // Allocate a new module to read
741 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000742
743 try {
744 yyparse(); // Parse the file.
745 } catch (...) {
746 // Clear the symbol table so it doesn't complain when it
747 // gets destructed
748 CurFun.LocalSymtab.clear();
749 throw;
750 }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000751
Chris Lattner00950542001-06-06 20:29:01 +0000752 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000753
754 // Check to see if they called va_start but not va_arg..
755 if (!ObsoleteVarArgs)
756 if (Function *F = Result->getNamedFunction("llvm.va_start"))
757 if (F->asize() == 1) {
758 std::cerr << "WARNING: this file uses obsolete features. "
759 << "Assemble and disassemble to update it.\n";
760 ObsoleteVarArgs = true;
761 }
762
763
764 if (ObsoleteVarArgs) {
765 // If the user is making use of obsolete varargs intrinsics, adjust them for
766 // the user.
767 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
768 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
769
770 const Type *RetTy = F->getFunctionType()->getParamType(0);
771 RetTy = cast<PointerType>(RetTy)->getElementType();
772 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
773
774 while (!F->use_empty()) {
775 CallInst *CI = cast<CallInst>(F->use_back());
776 Value *V = new CallInst(NF, "", CI);
777 new StoreInst(V, CI->getOperand(1), CI);
778 CI->getParent()->getInstList().erase(CI);
779 }
780 Result->getFunctionList().erase(F);
781 }
782
783 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
784 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
785 const Type *ArgTy = F->getFunctionType()->getParamType(0);
786 ArgTy = cast<PointerType>(ArgTy)->getElementType();
787 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
788 ArgTy, 0);
789
790 while (!F->use_empty()) {
791 CallInst *CI = cast<CallInst>(F->use_back());
792 Value *V = new LoadInst(CI->getOperand(1), "", CI);
793 new CallInst(NF, V, "", CI);
794 CI->getParent()->getInstList().erase(CI);
795 }
796 Result->getFunctionList().erase(F);
797 }
798
799 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
800 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
801 const Type *ArgTy = F->getFunctionType()->getParamType(0);
802 ArgTy = cast<PointerType>(ArgTy)->getElementType();
803 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
804 ArgTy, 0);
805
806 while (!F->use_empty()) {
807 CallInst *CI = cast<CallInst>(F->use_back());
808 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
809 new StoreInst(V, CI->getOperand(1), CI);
810 CI->getParent()->getInstList().erase(CI);
811 }
812 Result->getFunctionList().erase(F);
813 }
814 }
815
Chris Lattner00950542001-06-06 20:29:01 +0000816 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
817 ParserResult = 0;
818
819 return Result;
820}
821
Brian Gaeked0fde302003-11-11 22:41:34 +0000822} // End llvm namespace
823
824using namespace llvm;
825
Chris Lattner00950542001-06-06 20:29:01 +0000826%}
827
828%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000829 llvm::Module *ModuleVal;
830 llvm::Function *FunctionVal;
831 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
832 llvm::BasicBlock *BasicBlockVal;
833 llvm::TerminatorInst *TermInstVal;
834 llvm::Instruction *InstVal;
835 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000836
Brian Gaeked0fde302003-11-11 22:41:34 +0000837 const llvm::Type *PrimType;
838 llvm::PATypeHolder *TypeVal;
839 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000840
Brian Gaeked0fde302003-11-11 22:41:34 +0000841 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
842 std::vector<llvm::Value*> *ValueList;
843 std::list<llvm::PATypeHolder> *TypeList;
844 std::list<std::pair<llvm::Value*,
845 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
846 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
847 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000848
Brian Gaeked0fde302003-11-11 22:41:34 +0000849 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000850 int64_t SInt64Val;
851 uint64_t UInt64Val;
852 int SIntVal;
853 unsigned UIntVal;
854 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000855 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000856
Chris Lattner30c89792001-09-07 16:35:17 +0000857 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000858 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000859
Brian Gaeked0fde302003-11-11 22:41:34 +0000860 llvm::Instruction::BinaryOps BinaryOpVal;
861 llvm::Instruction::TermOps TermOpVal;
862 llvm::Instruction::MemoryOps MemOpVal;
863 llvm::Instruction::OtherOps OtherOpVal;
864 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000865}
866
Chris Lattner79df7c02002-03-26 18:01:55 +0000867%type <ModuleVal> Module FunctionList
868%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000869%type <BasicBlockVal> BasicBlock InstructionList
870%type <TermInstVal> BBTerminatorInst
871%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000872%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000873%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000874%type <ArgList> ArgList ArgListH
875%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000876%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000877%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000878%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000879%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000880%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000881%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000882%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000883%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000884%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000885
Chris Lattner2079fde2001-10-13 06:41:08 +0000886// ValueRef - Unresolved reference to a definition or BB
887%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000888%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000889// Tokens and types for handling constant integer values
890//
891// ESINT64VAL - A negative number within long long range
892%token <SInt64Val> ESINT64VAL
893
894// EUINT64VAL - A positive number within uns. long long range
895%token <UInt64Val> EUINT64VAL
896%type <SInt64Val> EINT64VAL
897
898%token <SIntVal> SINTVAL // Signed 32 bit ints...
899%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
900%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000901%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000902
903// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000904%type <TypeVal> Types TypesV UpRTypes UpRTypesV
905%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000906%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
907%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000908
Chris Lattner6c23f572003-08-22 05:42:10 +0000909%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
910%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000911
912
Chris Lattner91ef4602004-03-31 03:49:47 +0000913%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000914%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000915%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000916%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000917
918// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000919%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000920
Chris Lattner00950542001-06-06 20:29:01 +0000921// Binary Operators
922%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000923%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000924%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000925%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000926
927// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000928%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000929
Chris Lattner027dcc52001-07-08 21:10:27 +0000930// Other Operators
931%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000932%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000933%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000934
Chris Lattner00950542001-06-06 20:29:01 +0000935%start Module
936%%
937
938// Handle constant integer size restriction and conversion...
939//
Chris Lattner51727be2002-06-04 21:58:56 +0000940INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000941INTVAL : UINTVAL {
942 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
943 ThrowException("Value too large for type!");
944 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000945};
Chris Lattner00950542001-06-06 20:29:01 +0000946
947
Chris Lattner51727be2002-06-04 21:58:56 +0000948EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000949EINT64VAL : EUINT64VAL {
950 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
951 ThrowException("Value too large for type!");
952 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000953};
Chris Lattner00950542001-06-06 20:29:01 +0000954
Chris Lattner00950542001-06-06 20:29:01 +0000955// Operations that are notably excluded from this list include:
956// RET, BR, & SWITCH because they end basic blocks and are treated specially.
957//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000958ArithmeticOps: ADD | SUB | MUL | DIV | REM;
959LogicalOps : AND | OR | XOR;
960SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
961BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
962
Chris Lattner51727be2002-06-04 21:58:56 +0000963ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000964
Chris Lattnere98dda62001-07-14 06:10:16 +0000965// These are some types that allow classification if we only want a particular
966// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000967SIntType : LONG | INT | SHORT | SBYTE;
968UIntType : ULONG | UINT | USHORT | UBYTE;
969IntType : SIntType | UIntType;
970FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000971
Chris Lattnere98dda62001-07-14 06:10:16 +0000972// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000973OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000974 $$ = $1;
975 }
976 | /*empty*/ {
977 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000978 };
Chris Lattner00950542001-06-06 20:29:01 +0000979
Chris Lattner4ad02e72003-04-16 20:28:45 +0000980OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
981 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000982 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000983 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
984 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000985
986//===----------------------------------------------------------------------===//
987// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000988// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000989// access to it, a user must explicitly use TypesV.
990//
991
992// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000993TypesV : Types | VOID { $$ = new PATypeHolder($1); };
994UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000995
996Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000997 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000998 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
999 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001000 };
Chris Lattner30c89792001-09-07 16:35:17 +00001001
1002
1003// Derived types are added later...
1004//
Chris Lattner51727be2002-06-04 21:58:56 +00001005PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1006PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001007UpRTypes : OPAQUE {
1008 $$ = new PATypeHolder(OpaqueType::get());
1009 }
1010 | PrimType {
1011 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001012 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001013UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001014 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001015};
Chris Lattner30c89792001-09-07 16:35:17 +00001016
Chris Lattner30c89792001-09-07 16:35:17 +00001017// Include derived types in the Types production.
1018//
1019UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001020 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001021 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001022 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001023 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001024 UR_OUT("New Upreference!\n");
1025 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001026 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001027 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001028 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001029 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001030 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1031 if (isVarArg) Params.pop_back();
1032
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001033 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001034 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001035 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001036 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001037 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001038 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001039 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001040 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001041 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001042 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001043 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001044 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001045
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001046 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001047 delete $2;
1048 }
1049 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001050 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001051 }
1052 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001053 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001054 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001055 };
Chris Lattner30c89792001-09-07 16:35:17 +00001056
Chris Lattner7e708292002-06-25 16:13:24 +00001057// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001058// declaration type lists
1059//
1060TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001061 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001062 $$->push_back(*$1); delete $1;
1063 }
1064 | TypeListI ',' UpRTypes {
1065 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001066 };
Chris Lattner30c89792001-09-07 16:35:17 +00001067
Chris Lattner7e708292002-06-25 16:13:24 +00001068// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001069ArgTypeListI : TypeListI
1070 | TypeListI ',' DOTDOTDOT {
1071 ($$=$1)->push_back(Type::VoidTy);
1072 }
1073 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001074 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001075 }
1076 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001077 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001078 };
Chris Lattner30c89792001-09-07 16:35:17 +00001079
Chris Lattnere98dda62001-07-14 06:10:16 +00001080// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001081// production is used ONLY to represent constants that show up AFTER a 'const',
1082// 'constant' or 'global' token at global scope. Constants that can be inlined
1083// into other expressions (such as integers and constexprs) are handled by the
1084// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001085//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001086ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001087 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001088 if (ATy == 0)
1089 ThrowException("Cannot make array constant with type: '" +
1090 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001091 const Type *ETy = ATy->getElementType();
1092 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001093
Chris Lattner30c89792001-09-07 16:35:17 +00001094 // Verify that we have the correct size...
1095 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001096 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001097 utostr($3->size()) + " arguments, but has size of " +
1098 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001099
Chris Lattner30c89792001-09-07 16:35:17 +00001100 // Verify all elements are correct type!
1101 for (unsigned i = 0; i < $3->size(); i++) {
1102 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001103 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001104 ETy->getDescription() +"' as required!\nIt is of type '"+
1105 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001106 }
1107
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001108 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001109 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001110 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001111 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001112 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001113 if (ATy == 0)
1114 ThrowException("Cannot make array constant with type: '" +
1115 (*$1)->getDescription() + "'!");
1116
1117 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001118 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001119 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001120 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001121 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001122 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001123 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001124 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001125 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001126 if (ATy == 0)
1127 ThrowException("Cannot make array constant with type: '" +
1128 (*$1)->getDescription() + "'!");
1129
Chris Lattner30c89792001-09-07 16:35:17 +00001130 int NumElements = ATy->getNumElements();
1131 const Type *ETy = ATy->getElementType();
1132 char *EndStr = UnEscapeLexed($3, true);
1133 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001134 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001135 itostr((int)(EndStr-$3)) +
1136 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001137 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001138 if (ETy == Type::SByteTy) {
1139 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001140 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001141 } else if (ETy == Type::UByteTy) {
1142 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001143 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001144 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001145 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001146 ThrowException("Cannot build string arrays of non byte sized elements!");
1147 }
Chris Lattner30c89792001-09-07 16:35:17 +00001148 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001149 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001150 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001151 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001152 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001153 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001154 if (STy == 0)
1155 ThrowException("Cannot make struct constant with type: '" +
1156 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001157
Chris Lattnerc6212f12003-05-21 16:06:56 +00001158 if ($3->size() != STy->getNumContainedTypes())
1159 ThrowException("Illegal number of initializers for structure type!");
1160
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001161 // Check to ensure that constants are compatible with the type initializer!
1162 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001163 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001164 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001165 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001166 "' for element #" + utostr(i) +
1167 " of structure initializer!");
1168
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001169 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001170 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001171 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001172 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001173 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001174 if (STy == 0)
1175 ThrowException("Cannot make struct constant with type: '" +
1176 (*$1)->getDescription() + "'!");
1177
1178 if (STy->getNumContainedTypes() != 0)
1179 ThrowException("Illegal number of initializers for structure type!");
1180
1181 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1182 delete $1;
1183 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001184 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001185 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001186 if (PTy == 0)
1187 ThrowException("Cannot make null pointer constant with type: '" +
1188 (*$1)->getDescription() + "'!");
1189
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001190 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001191 delete $1;
1192 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001193 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001194 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001195 if (Ty == 0)
1196 ThrowException("Global const reference must be a pointer type!");
1197
Chris Lattner3101c252002-08-15 17:58:33 +00001198 // ConstExprs can exist in the body of a function, thus creating
1199 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1200 // the context of a function, getValNonImprovising will search the functions
1201 // symbol table instead of the module symbol table for the global symbol,
1202 // which throws things all off. To get around this, we just tell
1203 // getValNonImprovising that we are at global scope here.
1204 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001205 Function *SavedCurFn = CurFun.CurrentFunction;
1206 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001207
Chris Lattner2079fde2001-10-13 06:41:08 +00001208 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001209
Chris Lattner394f2eb2003-10-16 20:12:13 +00001210 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001211
Chris Lattner2079fde2001-10-13 06:41:08 +00001212 // If this is an initializer for a constant pointer, which is referencing a
1213 // (currently) undefined variable, create a stub now that shall be replaced
1214 // in the future with the right type of variable.
1215 //
1216 if (V == 0) {
1217 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1218 const PointerType *PT = cast<PointerType>(Ty);
1219
1220 // First check to see if the forward references value is already created!
1221 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001222 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001223
1224 if (I != CurModule.GlobalRefs.end()) {
1225 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001226 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001227 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001228 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001229 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001230 false,
1231 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001232 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001233 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001234
1235 // Must temporarily push this value into the module table...
1236 CurModule.CurrentModule->getGlobalList().push_back(GV);
1237 V = GV;
1238 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001239 }
1240
Chris Lattner2079fde2001-10-13 06:41:08 +00001241 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001242 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001243 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001244 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001245 | Types ConstExpr {
1246 if ($1->get() != $2->getType())
1247 ThrowException("Mismatched types for constant expression!");
1248 $$ = $2;
1249 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001250 }
1251 | Types ZEROINITIALIZER {
1252 $$ = Constant::getNullValue($1->get());
1253 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001254 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001255
Chris Lattnere43f40b2002-10-09 00:25:32 +00001256ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001257 if (!ConstantSInt::isValueValidForType($1, $2))
1258 ThrowException("Constant value doesn't fit in type!");
1259 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001260 }
1261 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001262 if (!ConstantUInt::isValueValidForType($1, $2))
1263 ThrowException("Constant value doesn't fit in type!");
1264 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001265 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001266 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001267 $$ = ConstantBool::True;
1268 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001269 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001270 $$ = ConstantBool::False;
1271 }
1272 | FPType FPVAL { // Float & Double constants
1273 $$ = ConstantFP::get($1, $2);
1274 };
1275
Chris Lattner00950542001-06-06 20:29:01 +00001276
Chris Lattnerd78700d2002-08-16 21:14:40 +00001277ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001278 if (!$3->getType()->isFirstClassType())
1279 ThrowException("cast constant expression from a non-primitive type: '" +
1280 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001281 if (!$5->get()->isFirstClassType())
1282 ThrowException("cast constant expression to a non-primitive type: '" +
1283 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001284 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001285 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001286 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001287 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1288 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001289 ThrowException("GetElementPtr requires a pointer operand!");
1290
Chris Lattner830b6f92004-04-05 01:30:04 +00001291 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1292 // indices to uint struct indices for compatibility.
1293 generic_gep_type_iterator<std::vector<Value*>::iterator>
1294 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1295 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1296 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1297 if (isa<StructType>(*GTI)) // Only change struct indices
1298 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1299 if (CUI->getType() == Type::UByteTy)
1300 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1301
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001302 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001303 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001304 if (!IdxTy)
1305 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001306
Chris Lattner9232b992003-04-22 18:42:41 +00001307 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001308 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1309 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001310 IdxVec.push_back(C);
1311 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001312 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001313
Chris Lattnerd78700d2002-08-16 21:14:40 +00001314 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001315
Chris Lattnerd78700d2002-08-16 21:14:40 +00001316 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001317 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001318 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1319 if ($3->getType() != Type::BoolTy)
1320 ThrowException("Select condition must be of boolean type!");
1321 if ($5->getType() != $7->getType())
1322 ThrowException("Select operand types must match!");
1323 $$ = ConstantExpr::getSelect($3, $5, $7);
1324 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001325 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001326 if ($3->getType() != $5->getType())
1327 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001328 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001329 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001330 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001331 if ($5->getType() != Type::UByteTy)
1332 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001333 if (!$3->getType()->isInteger())
1334 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001335 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001336 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001337
1338
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001339// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001340ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001341 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001342 }
1343 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001344 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001345 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001346 };
Chris Lattner00950542001-06-06 20:29:01 +00001347
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001348
Chris Lattner1781aca2001-09-18 04:00:54 +00001349// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001350GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001351
Chris Lattner00950542001-06-06 20:29:01 +00001352
Chris Lattner0e73ce62002-05-02 19:11:13 +00001353//===----------------------------------------------------------------------===//
1354// Rules to match Modules
1355//===----------------------------------------------------------------------===//
1356
1357// Module rule: Capture the result of parsing the whole file into a result
1358// variable...
1359//
1360Module : FunctionList {
1361 $$ = ParserResult = $1;
1362 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001363};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001364
Chris Lattner7e708292002-06-25 16:13:24 +00001365// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001366//
1367FunctionList : FunctionList Function {
1368 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001369 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001370 }
1371 | FunctionList FunctionProto {
1372 $$ = $1;
1373 }
1374 | FunctionList IMPLEMENTATION {
1375 $$ = $1;
1376 }
1377 | ConstPool {
1378 $$ = CurModule.CurrentModule;
1379 // Resolve circular types before we parse the body of the module
1380 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001381 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001382
Chris Lattnere98dda62001-07-14 06:10:16 +00001383// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001384ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001385 if (!setValueName($4, $2))
1386 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001387 }
Chris Lattner30c89792001-09-07 16:35:17 +00001388 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001389 // Eagerly resolve types. This is not an optimization, this is a
1390 // requirement that is due to the fact that we could have this:
1391 //
1392 // %list = type { %list * }
1393 // %list = type { %list * } ; repeated type decl
1394 //
1395 // If types are not resolved eagerly, then the two types will not be
1396 // determined to be the same type!
1397 //
1398 ResolveTypeTo($2, $4->get());
1399
Chris Lattner1781aca2001-09-18 04:00:54 +00001400 // TODO: FIXME when Type are not const
Reid Spencer75719bf2004-05-26 21:48:31 +00001401 if (!setTypeName(const_cast<Type*>($4->get()), $2)) {
Chris Lattnerb7474512001-10-03 15:39:04 +00001402 // If this is not a redefinition of a type...
1403 if (!$2) {
1404 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001405 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001406 }
Chris Lattner30c89792001-09-07 16:35:17 +00001407 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001408
1409 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001410 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001411 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001412 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001413 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001414 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001415 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001416 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001417 if (Initializer == 0)
1418 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001419
Chris Lattnerdda71962001-11-26 18:54:16 +00001420 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001421 if (!setValueName(GV, $2)) { // If not redefining...
1422 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001423 int Slot = InsertValue(GV, CurModule.Values);
1424
1425 if (Slot != -1) {
1426 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1427 } else {
1428 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1429 (char*)GV->getName().c_str()));
1430 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001431 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001432 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001433 | ConstPool OptAssign EXTERNAL GlobalType Types {
1434 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001435 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001436 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001437 if (!setValueName(GV, $2)) { // If not redefining...
1438 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001439 int Slot = InsertValue(GV, CurModule.Values);
1440
1441 if (Slot != -1) {
1442 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1443 } else {
1444 assert(GV->hasName() && "Not named and not numbered!?");
1445 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1446 (char*)GV->getName().c_str()));
1447 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001448 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001449 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001450 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001451 | ConstPool TARGET TargetDefinition {
1452 }
Chris Lattner00950542001-06-06 20:29:01 +00001453 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001454 };
Chris Lattner00950542001-06-06 20:29:01 +00001455
1456
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001457
1458BigOrLittle : BIG { $$ = Module::BigEndian; };
1459BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1460
1461TargetDefinition : ENDIAN '=' BigOrLittle {
1462 CurModule.CurrentModule->setEndianness($3);
1463 }
1464 | POINTERSIZE '=' EUINT64VAL {
1465 if ($3 == 32)
1466 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1467 else if ($3 == 64)
1468 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1469 else
1470 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1471 };
1472
1473
Chris Lattner00950542001-06-06 20:29:01 +00001474//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001475// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001476//===----------------------------------------------------------------------===//
1477
Chris Lattner6c23f572003-08-22 05:42:10 +00001478Name : VAR_ID | STRINGCONSTANT;
1479OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001480
Chris Lattner6c23f572003-08-22 05:42:10 +00001481ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001482 if (*$1 == Type::VoidTy)
1483 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001484 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001485};
Chris Lattner00950542001-06-06 20:29:01 +00001486
Chris Lattner69da5cf2002-10-13 20:57:00 +00001487ArgListH : ArgListH ',' ArgVal {
1488 $$ = $1;
1489 $1->push_back(*$3);
1490 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001491 }
1492 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001493 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001494 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001495 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001496 };
Chris Lattner00950542001-06-06 20:29:01 +00001497
1498ArgList : ArgListH {
1499 $$ = $1;
1500 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001501 | ArgListH ',' DOTDOTDOT {
1502 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001503 $$->push_back(std::pair<PATypeHolder*,
1504 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001505 }
1506 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001507 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1508 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001509 }
Chris Lattner00950542001-06-06 20:29:01 +00001510 | /* empty */ {
1511 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001512 };
Chris Lattner00950542001-06-06 20:29:01 +00001513
Chris Lattner6c23f572003-08-22 05:42:10 +00001514FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001515 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001516 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001517
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001518 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1519 ThrowException("LLVM functions cannot return aggregate types!");
1520
Chris Lattner9232b992003-04-22 18:42:41 +00001521 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001522 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001523 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001524 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001525 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001526 }
Chris Lattner00950542001-06-06 20:29:01 +00001527
Chris Lattner2079fde2001-10-13 06:41:08 +00001528 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1529 if (isVarArg) ParamTypeList.pop_back();
1530
Chris Lattner1f862af2003-04-16 18:13:57 +00001531 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001532 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001533 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001534
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001535 Function *Fn = 0;
1536 // Is the function already in symtab?
1537 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1538 // Yes it is. If this is the case, either we need to be a forward decl,
1539 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001540 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001541 ThrowException("Redefinition of function '" + FunctionName + "'!");
1542
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001543 // Make sure to strip off any argument names so we can't get conflicts...
1544 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1545 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001546
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001547 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001548 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1549 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001550 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001551 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001552 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001553 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001554
Chris Lattner394f2eb2003-10-16 20:12:13 +00001555 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001556
Chris Lattner7e708292002-06-25 16:13:24 +00001557 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001558 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001559 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001560 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001561 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001562 delete $4->back().first;
1563 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001564 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001565 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001566 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001567 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001568 delete I->first; // Delete the typeholder...
1569
1570 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001571 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001572
Chris Lattner69da5cf2002-10-13 20:57:00 +00001573 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001574 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001575
Chris Lattner1f862af2003-04-16 18:13:57 +00001576 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001577 }
Chris Lattner51727be2002-06-04 21:58:56 +00001578};
Chris Lattner00950542001-06-06 20:29:01 +00001579
Chris Lattner9b02cc32002-05-03 18:23:48 +00001580BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1581
Chris Lattner4ad02e72003-04-16 20:28:45 +00001582FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001583 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001584
Chris Lattner4ad02e72003-04-16 20:28:45 +00001585 // Make sure that we keep track of the linkage type even if there was a
1586 // previous "declare".
1587 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001588
Chris Lattner7e708292002-06-25 16:13:24 +00001589 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001590 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001591};
Chris Lattner00950542001-06-06 20:29:01 +00001592
Chris Lattner9b02cc32002-05-03 18:23:48 +00001593END : ENDTOK | '}'; // Allow end of '}' to end a function
1594
Chris Lattner79df7c02002-03-26 18:01:55 +00001595Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001596 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001597};
Chris Lattner00950542001-06-06 20:29:01 +00001598
Chris Lattner394f2eb2003-10-16 20:12:13 +00001599FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1600 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001601 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001602};
Chris Lattner00950542001-06-06 20:29:01 +00001603
1604//===----------------------------------------------------------------------===//
1605// Rules to match Basic Blocks
1606//===----------------------------------------------------------------------===//
1607
1608ConstValueRef : ESINT64VAL { // A reference to a direct constant
1609 $$ = ValID::create($1);
1610 }
1611 | EUINT64VAL {
1612 $$ = ValID::create($1);
1613 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001614 | FPVAL { // Perhaps it's an FP constant?
1615 $$ = ValID::create($1);
1616 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001617 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001618 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001619 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001620 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001621 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001622 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001623 | NULL_TOK {
1624 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001625 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001626 | ConstExpr {
1627 $$ = ValID::create($1);
1628 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001629
Chris Lattner2079fde2001-10-13 06:41:08 +00001630// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1631// another value.
1632//
1633SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001634 $$ = ValID::create($1);
1635 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001636 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001637 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001638 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001639
1640// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001641ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001642
Chris Lattner00950542001-06-06 20:29:01 +00001643
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001644// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1645// type immediately preceeds the value reference, and allows complex constant
1646// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001647ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001648 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001649 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001650
Chris Lattner00950542001-06-06 20:29:01 +00001651BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001652 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001653 }
Chris Lattner7e708292002-06-25 16:13:24 +00001654 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1655 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001656 };
Chris Lattner00950542001-06-06 20:29:01 +00001657
1658
1659// Basic blocks are terminated by branching instructions:
1660// br, br/cc, switch, ret
1661//
Chris Lattner2079fde2001-10-13 06:41:08 +00001662BasicBlock : InstructionList OptAssign BBTerminatorInst {
1663 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1664 InsertValue($3);
1665
1666 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001667 InsertValue($1);
1668 $$ = $1;
1669 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001670 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1671 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1672 InsertValue($4);
1673
1674 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001675 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001676
1677 InsertValue($2);
1678 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001679 };
Chris Lattner00950542001-06-06 20:29:01 +00001680
1681InstructionList : InstructionList Inst {
1682 $1->getInstList().push_back($2);
1683 $$ = $1;
1684 }
1685 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001686 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001687 };
Chris Lattner00950542001-06-06 20:29:01 +00001688
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001689BBTerminatorInst : RET ResolvedVal { // Return with a result...
1690 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001691 }
1692 | RET VOID { // Return with no result...
1693 $$ = new ReturnInst();
1694 }
1695 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001696 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001697 } // Conditional Branch...
1698 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001699 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1700 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001701 getVal(Type::BoolTy, $3));
1702 }
1703 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1704 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001705 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001706 $$ = S;
1707
Chris Lattner9232b992003-04-22 18:42:41 +00001708 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001709 E = $8->end();
1710 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001711 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001712 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001713 }
Chris Lattner196850c2003-05-15 21:30:00 +00001714 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1715 SwitchInst *S = new SwitchInst(getVal($2, $3),
1716 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1717 $$ = S;
1718 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001719 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001720 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001721 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001722 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001723
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001724 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1725 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001726 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001727 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001728 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001729 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1730 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001731 ParamTypes.push_back((*I)->getType());
1732 }
1733
1734 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1735 if (isVarArg) ParamTypes.pop_back();
1736
Chris Lattner79df7c02002-03-26 18:01:55 +00001737 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001738 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001739 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001740
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001741 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001742
1743 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1744 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1745
1746 if (Normal == 0 || Except == 0)
1747 ThrowException("Invoke instruction without label destinations!");
1748
1749 // Create the call node...
1750 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001751 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001752 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001753 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001754 // correctly!
1755 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001756 FunctionType::param_iterator I = Ty->param_begin();
1757 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001758 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001759
1760 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1761 if ((*ArgI)->getType() != *I)
1762 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001763 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001764
1765 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1766 ThrowException("Invalid number of parameters detected!");
1767
Chris Lattner6cdb0112001-11-26 16:54:11 +00001768 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001769 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001770 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001771 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001772 }
1773 | UNWIND {
1774 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001775 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001776
1777
Chris Lattner00950542001-06-06 20:29:01 +00001778
1779JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1780 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001781 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001782 if (V == 0)
1783 ThrowException("May only switch on a constant pool value!");
1784
Chris Lattner9232b992003-04-22 18:42:41 +00001785 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001786 }
1787 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001788 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001789 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001790
1791 if (V == 0)
1792 ThrowException("May only switch on a constant pool value!");
1793
Chris Lattner9232b992003-04-22 18:42:41 +00001794 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001795 };
Chris Lattner00950542001-06-06 20:29:01 +00001796
1797Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001798 // Is this definition named?? if so, assign the name...
1799 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001800 InsertValue($2);
1801 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001802};
Chris Lattner00950542001-06-06 20:29:01 +00001803
Chris Lattnerc24d2082001-06-11 15:04:20 +00001804PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001805 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1806 $$->push_back(std::make_pair(getVal(*$1, $3),
1807 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001808 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001809 }
1810 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1811 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001812 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1813 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001814 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001815
1816
Chris Lattner30c89792001-09-07 16:35:17 +00001817ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001818 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001819 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001820 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001821 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001822 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001823 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001824 };
Chris Lattner00950542001-06-06 20:29:01 +00001825
1826// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001827ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001828
Chris Lattner4a6482b2002-09-10 19:57:26 +00001829InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1830 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1831 ThrowException("Arithmetic operator requires integer or FP operands!");
1832 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1833 if ($$ == 0)
1834 ThrowException("binary operator returned null!");
1835 delete $2;
1836 }
1837 | LogicalOps Types ValueRef ',' ValueRef {
1838 if (!(*$2)->isIntegral())
1839 ThrowException("Logical operator requires integral operands!");
1840 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1841 if ($$ == 0)
1842 ThrowException("binary operator returned null!");
1843 delete $2;
1844 }
1845 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001846 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001847 if ($$ == 0)
1848 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001849 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001850 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001851 | NOT ResolvedVal {
1852 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1853 << " Replacing with 'xor'.\n";
1854
1855 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1856 if (Ones == 0)
1857 ThrowException("Expected integral type for not instruction!");
1858
1859 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001860 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001861 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001862 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001863 | ShiftOps ResolvedVal ',' ResolvedVal {
1864 if ($4->getType() != Type::UByteTy)
1865 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001866 if (!$2->getType()->isInteger())
1867 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001868 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001869 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001870 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001871 if (!$4->get()->isFirstClassType())
1872 ThrowException("cast instruction to a non-primitive type: '" +
1873 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001874 $$ = new CastInst($2, *$4);
1875 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001876 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001877 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1878 if ($2->getType() != Type::BoolTy)
1879 ThrowException("select condition must be boolean!");
1880 if ($4->getType() != $6->getType())
1881 ThrowException("select value types should match!");
1882 $$ = new SelectInst($2, $4, $6);
1883 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001884 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001885 // FIXME: This is emulation code for an obsolete syntax. This should be
1886 // removed at some point.
1887 if (!ObsoleteVarArgs) {
1888 std::cerr << "WARNING: this file uses obsolete features. "
1889 << "Assemble and disassemble to update it.\n";
1890 ObsoleteVarArgs = true;
1891 }
1892
1893 // First, load the valist...
1894 Instruction *CurVAList = new LoadInst($2, "");
1895 CurBB->getInstList().push_back(CurVAList);
1896
1897 // Emit the vaarg instruction.
1898 $$ = new VAArgInst(CurVAList, *$4);
1899
1900 // Now we must advance the pointer and update it in memory.
1901 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1902 CurBB->getInstList().push_back(TheVANext);
1903
1904 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1905 delete $4;
1906 }
1907 | VAARG ResolvedVal ',' Types {
1908 $$ = new VAArgInst($2, *$4);
1909 delete $4;
1910 }
1911 | VANEXT ResolvedVal ',' Types {
1912 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001913 delete $4;
1914 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001915 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001916 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001917 if (!Ty->isFirstClassType())
1918 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001919 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001920 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001921 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001922 if ($2->front().first->getType() != Ty)
1923 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001924 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001925 $2->pop_front();
1926 }
1927 delete $2; // Free the list...
1928 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001929 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001930 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001931 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001932
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001933 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1934 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001935 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001936 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001937 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001938 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1939 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001940 ParamTypes.push_back((*I)->getType());
1941 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001942
1943 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1944 if (isVarArg) ParamTypes.pop_back();
1945
Chris Lattner79df7c02002-03-26 18:01:55 +00001946 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001947 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001948 }
Chris Lattner00950542001-06-06 20:29:01 +00001949
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001950 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001951
Chris Lattner8b81bf52001-07-25 22:47:46 +00001952 // Create the call node...
1953 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001954 // Make sure no arguments is a good thing!
1955 if (Ty->getNumParams() != 0)
1956 ThrowException("No arguments passed to a function that "
1957 "expects arguments!");
1958
Chris Lattner9232b992003-04-22 18:42:41 +00001959 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001960 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001961 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001962 // correctly!
1963 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001964 FunctionType::param_iterator I = Ty->param_begin();
1965 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001966 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001967
1968 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1969 if ((*ArgI)->getType() != *I)
1970 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001971 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001972
Chris Lattner8b81bf52001-07-25 22:47:46 +00001973 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001974 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001975
Chris Lattner6cdb0112001-11-26 16:54:11 +00001976 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001977 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001978 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001979 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001980 }
1981 | MemoryInst {
1982 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001983 };
Chris Lattner00950542001-06-06 20:29:01 +00001984
Chris Lattner6cdb0112001-11-26 16:54:11 +00001985
1986// IndexList - List of indices for GEP based instructions...
1987IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001988 $$ = $2;
1989 } | /* empty */ {
1990 $$ = new std::vector<Value*>();
1991 };
1992
1993OptVolatile : VOLATILE {
1994 $$ = true;
1995 }
1996 | /* empty */ {
1997 $$ = false;
1998 };
1999
Chris Lattner027dcc52001-07-08 21:10:27 +00002000
Chris Lattner00950542001-06-06 20:29:01 +00002001MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002002 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002003 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002004 }
2005 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002006 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002007 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002008 }
2009 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002010 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002011 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002012 }
2013 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002014 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002015 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002016 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002017 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002018 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002019 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002020 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002021 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002022 }
2023
Chris Lattner15c9c032003-09-08 18:20:29 +00002024 | OptVolatile LOAD Types ValueRef {
2025 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002026 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002027 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002028 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002029 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002030 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002031 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2032 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002033 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002034 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002035 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002036 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002037 if (ElTy != $3->getType())
2038 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002039 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002040
Chris Lattner79ad13802003-09-08 20:29:46 +00002041 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002042 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002043 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002044 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002045 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002046 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002047
2048 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2049 // indices to uint struct indices for compatibility.
2050 generic_gep_type_iterator<std::vector<Value*>::iterator>
2051 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2052 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2053 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2054 if (isa<StructType>(*GTI)) // Only change struct indices
2055 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2056 if (CUI->getType() == Type::UByteTy)
2057 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2058
Chris Lattner30c89792001-09-07 16:35:17 +00002059 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002060 ThrowException("Invalid getelementptr indices for type '" +
2061 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002062 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2063 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002064 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002065
Brian Gaeked0fde302003-11-11 22:41:34 +00002066
Chris Lattner00950542001-06-06 20:29:01 +00002067%%
Chris Lattner09083092001-07-08 04:57:15 +00002068int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002069 std::string where
2070 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002071 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002072 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002073 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002074 errMsg += "end-of-file.";
2075 else
Chris Lattner9232b992003-04-22 18:42:41 +00002076 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002077 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002078 return 0;
2079}