blob: 8c571f4d7792a15ac41feb7d840d7ebfb8da0023 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000020#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/STLExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000024#include <algorithm>
25#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000026#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000027#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000028
Chris Lattner386a3b72001-10-16 19:54:17 +000029int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000030int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000031int yyparse();
32
Brian Gaeked0fde302003-11-11 22:41:34 +000033namespace llvm {
34
Chris Lattner00950542001-06-06 20:29:01 +000035static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000036std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000043#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000044#else
45#define UR_OUT(X)
46#endif
47
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000048#define YYERROR_VERBOSE 1
49
Chris Lattner99e7ab72003-10-18 05:53:13 +000050// HACK ALERT: This variable is used to implement the automatic conversion of
51// variable argument instructions from their old to new forms. When this
52// compatiblity "Feature" is removed, this should be too.
53//
54static BasicBlock *CurBB;
55static bool ObsoleteVarArgs;
56
57
Chris Lattner7e708292002-06-25 16:13:24 +000058// This contains info used when building the body of a function. It is
59// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000060//
Chris Lattner9232b992003-04-22 18:42:41 +000061typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner16af11d2004-02-09 21:03:38 +000062static void ResolveDefinitions(std::map<unsigned,ValueList> &LateResolvers,
63 std::map<unsigned,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000064
65static struct PerModuleInfo {
66 Module *CurrentModule;
Chris Lattner16af11d2004-02-09 21:03:38 +000067 std::map<unsigned,ValueList> Values; // Module level numbered definitions
68 std::map<unsigned,ValueList> LateResolveValues;
69 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000070 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000071
Chris Lattner2079fde2001-10-13 06:41:08 +000072 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
73 // references to global values. Global values may be referenced before they
74 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000075 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000076 //
Chris Lattner9232b992003-04-22 18:42:41 +000077 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000078 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000079 GlobalRefsType GlobalRefs;
80
Chris Lattner00950542001-06-06 20:29:01 +000081 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000082 // If we could not resolve some functions at function compilation time
83 // (calls to functions before they are defined), resolve them now... Types
84 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000085 //
Chris Lattner00950542001-06-06 20:29:01 +000086 ResolveDefinitions(LateResolveValues);
87
Chris Lattner2079fde2001-10-13 06:41:08 +000088 // Check to make sure that all global value forward references have been
89 // resolved!
90 //
91 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000092 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000093
94 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
95 I != E; ++I) {
96 UndefinedReferences += " " + I->first.first->getDescription() + " " +
97 I->first.second.getName() + "\n";
98 }
99 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000100 }
101
Chris Lattner7e708292002-06-25 16:13:24 +0000102 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000103 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000104 CurrentModule = 0;
105 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000106
107
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000108 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000109 // is used to remove things from the forward declaration map, resolving them
110 // to the correct thing as needed.
111 //
112 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
113 // Check to see if there is a forward reference to this global variable...
114 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000115 GlobalRefsType::iterator I =
116 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000117
118 if (I != GlobalRefs.end()) {
Chris Lattnerbc207592004-03-08 06:09:57 +0000119 GlobalValue *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000120 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner2079fde2001-10-13 06:41:08 +0000121
122 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000123 // allowed to be is ConstantPointerRef's.
Chris Lattnerfd059242003-10-15 16:48:29 +0000124 assert(OldGV->hasOneUse() && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000125 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
126 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000127
Chris Lattner9e932bd2002-10-14 03:28:42 +0000128 // Change the const pool reference to point to the real global variable
129 // now. This should drop a use from the OldGV.
Chris Lattnerbc207592004-03-08 06:09:57 +0000130 CPR->replaceUsesOfWithOnConstant(OldGV, GV);
Chris Lattner9e932bd2002-10-14 03:28:42 +0000131 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000132
133 // Remove OldGV from the module...
Chris Lattnerbc207592004-03-08 06:09:57 +0000134 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
135 CurrentModule->getGlobalList().erase(GVar);
136 else
137 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000138
Chris Lattner2079fde2001-10-13 06:41:08 +0000139 // Remove the map entry for the global now that it has been created...
140 GlobalRefs.erase(I);
141 }
142 }
143
Chris Lattner00950542001-06-06 20:29:01 +0000144} CurModule;
145
Chris Lattner79df7c02002-03-26 18:01:55 +0000146static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000147 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000148
Chris Lattner16af11d2004-02-09 21:03:38 +0000149 std::map<unsigned,ValueList> Values; // Keep track of numbered definitions
150 std::map<unsigned,ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000151 std::vector<PATypeHolder> Types;
152 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner15b69722003-11-12 04:40:30 +0000153 SymbolTable LocalSymtab;
Chris Lattner7e708292002-06-25 16:13:24 +0000154 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000155
Chris Lattner79df7c02002-03-26 18:01:55 +0000156 inline PerFunctionInfo() {
157 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000158 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000159 }
160
Chris Lattner79df7c02002-03-26 18:01:55 +0000161 inline void FunctionStart(Function *M) {
162 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000163 }
164
Chris Lattner79df7c02002-03-26 18:01:55 +0000165 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000166 // If we could not resolve some blocks at parsing time (forward branches)
167 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000168 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000169
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000170 // Make sure to resolve any constant expr references that might exist within
171 // the function we just declared itself.
172 ValID FID;
173 if (CurrentFunction->hasName()) {
174 FID = ValID::create((char*)CurrentFunction->getName().c_str());
175 } else {
176 unsigned Slot = CurrentFunction->getType()->getUniqueID();
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000177 // Figure out which slot number if is...
Chris Lattner16af11d2004-02-09 21:03:38 +0000178 ValueList &List = CurModule.Values[Slot];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000179 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000180 assert(i < List.size() && "Function not found!");
181 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000182 FID = ValID::create((int)i);
183 break;
184 }
185 }
186 }
187 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
188
Chris Lattner7e708292002-06-25 16:13:24 +0000189 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000190 Types.clear(); // Clear out function local types
191 LocalSymtab.clear(); // Clear out function local symbol table
Chris Lattner79df7c02002-03-26 18:01:55 +0000192 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000193 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000194 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000195} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000196
Chris Lattner394f2eb2003-10-16 20:12:13 +0000197static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000198
Chris Lattner00950542001-06-06 20:29:01 +0000199
200//===----------------------------------------------------------------------===//
201// Code to handle definitions of all the types
202//===----------------------------------------------------------------------===//
203
Chris Lattner9232b992003-04-22 18:42:41 +0000204static int InsertValue(Value *D,
Chris Lattner16af11d2004-02-09 21:03:38 +0000205 std::map<unsigned,ValueList> &ValueTab = CurFun.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000206 if (D->hasName()) return -1; // Is this a numbered definition?
207
208 // Yes, insert the value into the value table...
209 unsigned type = D->getType()->getUniqueID();
Chris Lattner2079fde2001-10-13 06:41:08 +0000210 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
Chris Lattner16af11d2004-02-09 21:03:38 +0000211 ValueList &List = ValueTab[type];
212 List.push_back(D);
213 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000214}
215
Chris Lattner30c89792001-09-07 16:35:17 +0000216// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000217static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000218 Types.push_back(Ty);
219}
220
221static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000222 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000223 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000224 unsigned Num = (unsigned)D.Num;
225
226 // Module constants occupy the lowest numbered slots...
227 if (Num < CurModule.Types.size())
228 return CurModule.Types[Num];
229
230 Num -= CurModule.Types.size();
231
232 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000233 if (Num <= CurFun.Types.size())
234 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000235 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000236 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000237 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000238 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000239 SymbolTable *SymTab = 0;
Reid Spencer8ce1da72004-07-04 12:19:05 +0000240 Type *N = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000241 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000242 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000243 N = SymTab->lookupType(Name);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000244 }
Chris Lattner30c89792001-09-07 16:35:17 +0000245
246 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000247 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000248 // hasn't been added to the module...
249 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000250 SymTab = &CurModule.CurrentModule->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000251 N = SymTab->lookupType(Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000252 if (N == 0) break;
253 }
254
255 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000256 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000257 }
258 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000259 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000260 }
261
262 // If we reached here, we referenced either a symbol that we don't know about
263 // or an id number that hasn't been read yet. We may be referencing something
264 // forward, so just create an entry to be resolved later and get to it...
265 //
266 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
267
Chris Lattner9232b992003-04-22 18:42:41 +0000268 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000269 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000270
Chris Lattner9232b992003-04-22 18:42:41 +0000271 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000272 if (I != LateResolver.end()) {
273 return I->second;
274 }
Chris Lattner30c89792001-09-07 16:35:17 +0000275
Chris Lattner82269592001-10-22 06:01:08 +0000276 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000277 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000278 return Typ;
279}
280
Chris Lattner9232b992003-04-22 18:42:41 +0000281static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000282 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000283 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000284 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000285 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000286}
287
Chris Lattner2079fde2001-10-13 06:41:08 +0000288// getValNonImprovising - Look up the value specified by the provided type and
289// the provided ValID. If the value exists and has already been defined, return
290// it. Otherwise return null.
291//
292static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000293 if (isa<FunctionType>(Ty))
294 ThrowException("Functions are not values and "
295 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000296
Chris Lattner30c89792001-09-07 16:35:17 +0000297 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000298 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000299 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000300 unsigned Num = (unsigned)D.Num;
301
302 // Module constants occupy the lowest numbered slots...
Chris Lattner16af11d2004-02-09 21:03:38 +0000303 std::map<unsigned,ValueList>::iterator VI = CurModule.Values.find(type);
304 if (VI != CurModule.Values.end()) {
305 if (Num < VI->second.size())
306 return VI->second[Num];
307 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000308 }
309
310 // Make sure that our type is within bounds
Chris Lattner16af11d2004-02-09 21:03:38 +0000311 VI = CurFun.Values.find(type);
312 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000313
314 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000315 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000316
Chris Lattner16af11d2004-02-09 21:03:38 +0000317 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000318 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000319
Chris Lattner1a1cb112001-09-30 22:46:54 +0000320 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000321 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000322 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000323
324 D.destroy(); // Free old strdup'd memory...
325 return N;
326 }
327
Chris Lattner2079fde2001-10-13 06:41:08 +0000328 // Check to make sure that "Ty" is an integral type, and that our
329 // value will fit into the specified type...
330 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000331 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
332 ThrowException("Signed integral constant '" +
333 itostr(D.ConstPool64) + "' is invalid for type '" +
334 Ty->getDescription() + "'!");
335 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000336
337 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000338 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
339 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000340 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
341 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000342 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000343 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000344 }
345 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000346 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000347 }
348
Chris Lattner2079fde2001-10-13 06:41:08 +0000349 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000350 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000351 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000352 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000353
354 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000355 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000356 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000357 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000358
Chris Lattnerd78700d2002-08-16 21:14:40 +0000359 case ValID::ConstantVal: // Fully resolved constant?
360 if (D.ConstantValue->getType() != Ty)
361 ThrowException("Constant expression type different from required type!");
362 return D.ConstantValue;
363
Chris Lattner30c89792001-09-07 16:35:17 +0000364 default:
365 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000366 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000367 } // End of switch
368
Chris Lattner2079fde2001-10-13 06:41:08 +0000369 assert(0 && "Unhandled case!");
370 return 0;
371}
372
373
374// getVal - This function is identical to getValNonImprovising, except that if a
375// value is not already defined, it "improvises" by creating a placeholder var
376// that looks and acts just like the requested variable. When the value is
377// defined later, all uses of the placeholder variable are replaced with the
378// real thing.
379//
380static Value *getVal(const Type *Ty, const ValID &D) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000381
382 // See if the value has already been defined...
383 Value *V = getValNonImprovising(Ty, D);
384 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000385
386 // If we reached here, we referenced either a symbol that we don't know about
387 // or an id number that hasn't been read yet. We may be referencing something
388 // forward, so just create an entry to be resolved later and get to it...
389 //
Chris Lattner00950542001-06-06 20:29:01 +0000390 Value *d = 0;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000391 switch (Ty->getTypeID()) {
Chris Lattner30c89792001-09-07 16:35:17 +0000392 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000393 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000394 }
395
396 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000397 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000398 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000399 else
400 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000401 return d;
402}
403
404
405//===----------------------------------------------------------------------===//
406// Code to handle forward references in instructions
407//===----------------------------------------------------------------------===//
408//
409// This code handles the late binding needed with statements that reference
410// values not defined yet... for example, a forward branch, or the PHI node for
411// a loop body.
412//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000413// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000414// and back patchs after we are done.
415//
416
417// ResolveDefinitions - If we could not resolve some defs at parsing
418// time (forward branches, phi functions for loops, etc...) resolve the
419// defs now...
420//
Chris Lattner16af11d2004-02-09 21:03:38 +0000421static void ResolveDefinitions(std::map<unsigned,ValueList> &LateResolvers,
422 std::map<unsigned,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000423 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner16af11d2004-02-09 21:03:38 +0000424 for (std::map<unsigned,ValueList>::iterator LRI = LateResolvers.begin(),
425 E = LateResolvers.end(); LRI != E; ++LRI) {
426 ValueList &List = LRI->second;
427 while (!List.empty()) {
428 Value *V = List.back();
429 List.pop_back();
Chris 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) {
504 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000505
Chris Lattner9232b992003-04-22 18:42:41 +0000506 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000507 free(NameStr); // Free old string
508
Chris Lattner2079fde2001-10-13 06:41:08 +0000509 if (V->getType() == Type::VoidTy)
510 ThrowException("Can't assign name '" + Name +
511 "' to a null valued instruction!");
512
Chris Lattner6e6026b2002-11-20 18:36:02 +0000513 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000514 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000515 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000516
Reid Spencer75719bf2004-05-26 21:48:31 +0000517 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner42fbc7e2004-05-26 17:08:25 +0000518
Chris Lattner30c89792001-09-07 16:35:17 +0000519 if (Existing) { // Inserting a name that is already defined???
Reid Spencer9ed0f172004-05-27 22:05:50 +0000520 // We are a simple redefinition of a value, check to see if it
Chris Lattner9636a912001-10-01 16:18:37 +0000521 // is defined the same as the old one...
Reid Spencer4e6620c2004-05-28 00:21:06 +0000522 if (const Constant *C = dyn_cast<Constant>(Existing)) {
Chris Lattneradf99702003-03-03 23:28:55 +0000523 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000524 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000525 // We are allowed to redefine a global variable in two circumstances:
526 // 1. If at least one of the globals is uninitialized or
527 // 2. If both initializers have the same value.
528 //
Chris Lattner89219832001-10-03 19:35:04 +0000529 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000530 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
531 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000532
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000533 // Make sure the existing global version gets the initializer! Make
534 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000535 if (GV->hasInitializer() && !EGV->hasInitializer())
536 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000537 if (GV->isConstant())
538 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000539 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000540
Chris Lattner2079fde2001-10-13 06:41:08 +0000541 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000542 return true; // They are equivalent!
543 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000544 }
Chris Lattner9636a912001-10-01 16:18:37 +0000545 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000546
Chris Lattner2079fde2001-10-13 06:41:08 +0000547 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000548 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000549 }
Chris Lattner00950542001-06-06 20:29:01 +0000550
Chris Lattner15b69722003-11-12 04:40:30 +0000551 // Set the name
Chris Lattner6e6026b2002-11-20 18:36:02 +0000552 V->setName(Name, &ST);
Chris Lattner15b69722003-11-12 04:40:30 +0000553
554 // If we're in function scope
555 if (inFunctionScope()) {
556 // Look up the symbol in the function's local symboltable
557 Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
558
559 // If it already exists
560 if (Existing) {
Chris Lattner15b69722003-11-12 04:40:30 +0000561 // Bail
562 ThrowException("Redefinition of value named '" + Name + "' in the '" +
563 V->getType()->getDescription() + "' type plane!");
564
565 // otherwise, since it doesn't exist
566 } else {
567 // Insert it.
568 CurFun.LocalSymtab.insert(V);
569 }
570 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000571 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000572}
573
Reid Spencer75719bf2004-05-26 21:48:31 +0000574// setTypeName - Set the specified type to the name given. The name may be
575// null potentially, in which case this is a noop. The string passed in is
576// assumed to be a malloc'd string buffer, and is freed by this function.
577//
578// This function returns true if the type has already been defined, but is
579// allowed to be redefined in the specified context. If the name is a new name
580// for the type plane, it is inserted and false is returned.
581static bool setTypeName(Type *T, char *NameStr) {
582 if (NameStr == 0) return false;
583
584 std::string Name(NameStr); // Copy string
585 free(NameStr); // Free old string
586
587 // We don't allow assigning names to void type
588 if (T == Type::VoidTy)
589 ThrowException("Can't assign name '" + Name + "' to the null type!");
590
591 SymbolTable &ST = inFunctionScope() ?
592 CurFun.CurrentFunction->getSymbolTable() :
593 CurModule.CurrentModule->getSymbolTable();
594
595 Type *Existing = ST.lookupType(Name);
596
597 if (Existing) { // Inserting a name that is already defined???
598 // There is only one case where this is allowed: when we are refining an
599 // opaque type. In this case, Existing will be an opaque type.
600 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
601 // We ARE replacing an opaque type!
602 ((OpaqueType*)OpTy)->refineAbstractTypeTo(T);
603 return true;
604 }
605
606 // Otherwise, this is an attempt to redefine a type. That's okay if
607 // the redefinition is identical to the original. This will be so if
608 // Existing and T point to the same Type object. In this one case we
609 // allow the equivalent redefinition.
610 if (Existing == T) return true; // Yes, it's equal.
611
612 // Any other kind of (non-equivalent) redefinition is an error.
613 ThrowException("Redefinition of type named '" + Name + "' in the '" +
614 T->getDescription() + "' type plane!");
615 }
616
617 // Okay, its a newly named type. Set its name.
618 T->setName(Name,&ST);
619
620 // If we're in function scope
621 if (inFunctionScope()) {
622 // Look up the symbol in the function's local symboltable
623 Existing = CurFun.LocalSymtab.lookupType(Name);
624
625 // If it already exists
626 if (Existing) {
627 // Bail
628 ThrowException("Redefinition of type named '" + Name + "' in the '" +
629 T->getDescription() + "' type plane in function scope!");
630
631 // otherwise, since it doesn't exist
632 } else {
633 // Insert it.
634 CurFun.LocalSymtab.insert(Name,T);
635 }
636 }
637 return false;
638}
Chris Lattner8896eda2001-07-09 19:38:36 +0000639
Chris Lattner30c89792001-09-07 16:35:17 +0000640//===----------------------------------------------------------------------===//
641// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000642//
Chris Lattner8896eda2001-07-09 19:38:36 +0000643
Chris Lattner3b073862004-02-09 03:19:29 +0000644// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000645//
646static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000647 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
648}
649
650namespace {
651 struct UpRefRecord {
652 // NestingLevel - The number of nesting levels that need to be popped before
653 // this type is resolved.
654 unsigned NestingLevel;
655
656 // LastContainedTy - This is the type at the current binding level for the
657 // type. Every time we reduce the nesting level, this gets updated.
658 const Type *LastContainedTy;
659
660 // UpRefTy - This is the actual opaque type that the upreference is
661 // represented with.
662 OpaqueType *UpRefTy;
663
664 UpRefRecord(unsigned NL, OpaqueType *URTy)
665 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
666 };
Chris Lattner30c89792001-09-07 16:35:17 +0000667}
Chris Lattner698b56e2001-07-20 19:15:08 +0000668
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000669// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000670static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000671
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000672/// HandleUpRefs - Every time we finish a new layer of types, this function is
673/// called. It loops through the UpRefs vector, which is a list of the
674/// currently active types. For each type, if the up reference is contained in
675/// the newly completed type, we decrement the level count. When the level
676/// count reaches zero, the upreferenced type is the type that is passed in:
677/// thus we can complete the cycle.
678///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000679static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000680 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000681 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000682 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000683 "' newly formed. Resolving upreferences.\n" <<
684 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000685
686 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
687 // to zero), we resolve them all together before we resolve them to Ty. At
688 // the end of the loop, if there is anything to resolve to Ty, it will be in
689 // this variable.
690 OpaqueType *TypeToResolve = 0;
691
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000692 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000693 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000694 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000695 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000696 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
697 // Decrement level of upreference
698 unsigned Level = --UpRefs[i].NestingLevel;
699 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000700 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000701 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000702 if (!TypeToResolve) {
703 TypeToResolve = UpRefs[i].UpRefTy;
704 } else {
705 UR_OUT(" * Resolving upreference for "
706 << UpRefs[i].second->getDescription() << "\n";
707 std::string OldName = UpRefs[i].UpRefTy->getDescription());
708 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
709 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
710 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
711 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000712 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
713 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000714 }
715 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000716 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000717
Chris Lattner026a8ce2004-02-09 18:53:54 +0000718 if (TypeToResolve) {
719 UR_OUT(" * Resolving upreference for "
720 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000721 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000722 TypeToResolve->refineAbstractTypeTo(Ty);
723 }
724
Chris Lattner8896eda2001-07-09 19:38:36 +0000725 return Ty;
726}
727
Chris Lattner30c89792001-09-07 16:35:17 +0000728
Chris Lattner00950542001-06-06 20:29:01 +0000729//===----------------------------------------------------------------------===//
730// RunVMAsmParser - Define an interface to this parser
731//===----------------------------------------------------------------------===//
732//
Chris Lattner9232b992003-04-22 18:42:41 +0000733Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000734 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000735 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000736 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000737 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000738
Chris Lattner75f20532003-04-22 18:02:52 +0000739 // Allocate a new module to read
740 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000741
742 try {
743 yyparse(); // Parse the file.
744 } catch (...) {
745 // Clear the symbol table so it doesn't complain when it
746 // gets destructed
747 CurFun.LocalSymtab.clear();
748 throw;
749 }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000750
Chris Lattner00950542001-06-06 20:29:01 +0000751 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000752
753 // Check to see if they called va_start but not va_arg..
754 if (!ObsoleteVarArgs)
755 if (Function *F = Result->getNamedFunction("llvm.va_start"))
756 if (F->asize() == 1) {
757 std::cerr << "WARNING: this file uses obsolete features. "
758 << "Assemble and disassemble to update it.\n";
759 ObsoleteVarArgs = true;
760 }
761
762
763 if (ObsoleteVarArgs) {
764 // If the user is making use of obsolete varargs intrinsics, adjust them for
765 // the user.
766 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
767 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
768
769 const Type *RetTy = F->getFunctionType()->getParamType(0);
770 RetTy = cast<PointerType>(RetTy)->getElementType();
771 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
772
773 while (!F->use_empty()) {
774 CallInst *CI = cast<CallInst>(F->use_back());
775 Value *V = new CallInst(NF, "", CI);
776 new StoreInst(V, CI->getOperand(1), CI);
777 CI->getParent()->getInstList().erase(CI);
778 }
779 Result->getFunctionList().erase(F);
780 }
781
782 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
783 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
784 const Type *ArgTy = F->getFunctionType()->getParamType(0);
785 ArgTy = cast<PointerType>(ArgTy)->getElementType();
786 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
787 ArgTy, 0);
788
789 while (!F->use_empty()) {
790 CallInst *CI = cast<CallInst>(F->use_back());
791 Value *V = new LoadInst(CI->getOperand(1), "", CI);
792 new CallInst(NF, V, "", CI);
793 CI->getParent()->getInstList().erase(CI);
794 }
795 Result->getFunctionList().erase(F);
796 }
797
798 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
799 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
800 const Type *ArgTy = F->getFunctionType()->getParamType(0);
801 ArgTy = cast<PointerType>(ArgTy)->getElementType();
802 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
803 ArgTy, 0);
804
805 while (!F->use_empty()) {
806 CallInst *CI = cast<CallInst>(F->use_back());
807 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
808 new StoreInst(V, CI->getOperand(1), CI);
809 CI->getParent()->getInstList().erase(CI);
810 }
811 Result->getFunctionList().erase(F);
812 }
813 }
814
Chris Lattner00950542001-06-06 20:29:01 +0000815 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
816 ParserResult = 0;
817
818 return Result;
819}
820
Brian Gaeked0fde302003-11-11 22:41:34 +0000821} // End llvm namespace
822
823using namespace llvm;
824
Chris Lattner00950542001-06-06 20:29:01 +0000825%}
826
827%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000828 llvm::Module *ModuleVal;
829 llvm::Function *FunctionVal;
830 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
831 llvm::BasicBlock *BasicBlockVal;
832 llvm::TerminatorInst *TermInstVal;
833 llvm::Instruction *InstVal;
834 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000835
Brian Gaeked0fde302003-11-11 22:41:34 +0000836 const llvm::Type *PrimType;
837 llvm::PATypeHolder *TypeVal;
838 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000839
Brian Gaeked0fde302003-11-11 22:41:34 +0000840 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
841 std::vector<llvm::Value*> *ValueList;
842 std::list<llvm::PATypeHolder> *TypeList;
843 std::list<std::pair<llvm::Value*,
844 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
845 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
846 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000847
Brian Gaeked0fde302003-11-11 22:41:34 +0000848 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000849 int64_t SInt64Val;
850 uint64_t UInt64Val;
851 int SIntVal;
852 unsigned UIntVal;
853 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000854 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000855
Chris Lattner30c89792001-09-07 16:35:17 +0000856 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000857 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000858
Brian Gaeked0fde302003-11-11 22:41:34 +0000859 llvm::Instruction::BinaryOps BinaryOpVal;
860 llvm::Instruction::TermOps TermOpVal;
861 llvm::Instruction::MemoryOps MemOpVal;
862 llvm::Instruction::OtherOps OtherOpVal;
863 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000864}
865
Chris Lattner79df7c02002-03-26 18:01:55 +0000866%type <ModuleVal> Module FunctionList
867%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000868%type <BasicBlockVal> BasicBlock InstructionList
869%type <TermInstVal> BBTerminatorInst
870%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000871%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000872%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000873%type <ArgList> ArgList ArgListH
874%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000875%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000876%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000877%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000878%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000879%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000880%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000881%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000882%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000883%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000884
Chris Lattner2079fde2001-10-13 06:41:08 +0000885// ValueRef - Unresolved reference to a definition or BB
886%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000887%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000888// Tokens and types for handling constant integer values
889//
890// ESINT64VAL - A negative number within long long range
891%token <SInt64Val> ESINT64VAL
892
893// EUINT64VAL - A positive number within uns. long long range
894%token <UInt64Val> EUINT64VAL
895%type <SInt64Val> EINT64VAL
896
897%token <SIntVal> SINTVAL // Signed 32 bit ints...
898%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
899%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000900%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000901
902// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000903%type <TypeVal> Types TypesV UpRTypes UpRTypesV
904%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000905%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
906%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000907
Chris Lattner6c23f572003-08-22 05:42:10 +0000908%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
909%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000910
911
Chris Lattner91ef4602004-03-31 03:49:47 +0000912%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000913%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000914%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000915%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000916
917// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000918%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000919
Chris Lattner00950542001-06-06 20:29:01 +0000920// Binary Operators
921%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000922%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000923%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000924%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000925
926// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000927%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000928
Chris Lattner027dcc52001-07-08 21:10:27 +0000929// Other Operators
930%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000931%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000932%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000933
Chris Lattner00950542001-06-06 20:29:01 +0000934%start Module
935%%
936
937// Handle constant integer size restriction and conversion...
938//
Chris Lattner51727be2002-06-04 21:58:56 +0000939INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000940INTVAL : UINTVAL {
941 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
942 ThrowException("Value too large for type!");
943 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000944};
Chris Lattner00950542001-06-06 20:29:01 +0000945
946
Chris Lattner51727be2002-06-04 21:58:56 +0000947EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000948EINT64VAL : EUINT64VAL {
949 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
950 ThrowException("Value too large for type!");
951 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000952};
Chris Lattner00950542001-06-06 20:29:01 +0000953
Chris Lattner00950542001-06-06 20:29:01 +0000954// Operations that are notably excluded from this list include:
955// RET, BR, & SWITCH because they end basic blocks and are treated specially.
956//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000957ArithmeticOps: ADD | SUB | MUL | DIV | REM;
958LogicalOps : AND | OR | XOR;
959SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
960BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
961
Chris Lattner51727be2002-06-04 21:58:56 +0000962ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000963
Chris Lattnere98dda62001-07-14 06:10:16 +0000964// These are some types that allow classification if we only want a particular
965// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000966SIntType : LONG | INT | SHORT | SBYTE;
967UIntType : ULONG | UINT | USHORT | UBYTE;
968IntType : SIntType | UIntType;
969FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000970
Chris Lattnere98dda62001-07-14 06:10:16 +0000971// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000972OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000973 $$ = $1;
974 }
975 | /*empty*/ {
976 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000977 };
Chris Lattner00950542001-06-06 20:29:01 +0000978
Chris Lattner4ad02e72003-04-16 20:28:45 +0000979OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
980 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000981 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000982 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
983 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000984
985//===----------------------------------------------------------------------===//
986// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000987// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000988// access to it, a user must explicitly use TypesV.
989//
990
991// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000992TypesV : Types | VOID { $$ = new PATypeHolder($1); };
993UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000994
995Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000996 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000997 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
998 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000999 };
Chris Lattner30c89792001-09-07 16:35:17 +00001000
1001
1002// Derived types are added later...
1003//
Chris Lattner51727be2002-06-04 21:58:56 +00001004PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1005PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001006UpRTypes : OPAQUE {
1007 $$ = new PATypeHolder(OpaqueType::get());
1008 }
1009 | PrimType {
1010 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001011 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001012UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001013 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001014};
Chris Lattner30c89792001-09-07 16:35:17 +00001015
Chris Lattner30c89792001-09-07 16:35:17 +00001016// Include derived types in the Types production.
1017//
1018UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001019 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001020 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001021 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001022 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001023 UR_OUT("New Upreference!\n");
1024 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001025 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001026 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001027 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001028 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001029 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1030 if (isVarArg) Params.pop_back();
1031
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001032 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001033 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001034 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001035 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001036 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001037 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001038 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001039 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001040 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001041 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001042 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001043 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001044
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001045 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001046 delete $2;
1047 }
1048 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001049 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001050 }
1051 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001052 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001053 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001054 };
Chris Lattner30c89792001-09-07 16:35:17 +00001055
Chris Lattner7e708292002-06-25 16:13:24 +00001056// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001057// declaration type lists
1058//
1059TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001060 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001061 $$->push_back(*$1); delete $1;
1062 }
1063 | TypeListI ',' UpRTypes {
1064 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001065 };
Chris Lattner30c89792001-09-07 16:35:17 +00001066
Chris Lattner7e708292002-06-25 16:13:24 +00001067// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001068ArgTypeListI : TypeListI
1069 | TypeListI ',' DOTDOTDOT {
1070 ($$=$1)->push_back(Type::VoidTy);
1071 }
1072 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001073 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001074 }
1075 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001076 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001077 };
Chris Lattner30c89792001-09-07 16:35:17 +00001078
Chris Lattnere98dda62001-07-14 06:10:16 +00001079// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001080// production is used ONLY to represent constants that show up AFTER a 'const',
1081// 'constant' or 'global' token at global scope. Constants that can be inlined
1082// into other expressions (such as integers and constexprs) are handled by the
1083// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001084//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001085ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001086 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001087 if (ATy == 0)
1088 ThrowException("Cannot make array constant with type: '" +
1089 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001090 const Type *ETy = ATy->getElementType();
1091 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001092
Chris Lattner30c89792001-09-07 16:35:17 +00001093 // Verify that we have the correct size...
1094 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001095 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001096 utostr($3->size()) + " arguments, but has size of " +
1097 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001098
Chris Lattner30c89792001-09-07 16:35:17 +00001099 // Verify all elements are correct type!
1100 for (unsigned i = 0; i < $3->size(); i++) {
1101 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001102 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001103 ETy->getDescription() +"' as required!\nIt is of type '"+
1104 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001105 }
1106
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001107 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001108 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001109 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001110 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001111 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001112 if (ATy == 0)
1113 ThrowException("Cannot make array constant with type: '" +
1114 (*$1)->getDescription() + "'!");
1115
1116 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001117 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001118 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001119 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001120 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001121 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001122 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001123 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001124 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001125 if (ATy == 0)
1126 ThrowException("Cannot make array constant with type: '" +
1127 (*$1)->getDescription() + "'!");
1128
Chris Lattner30c89792001-09-07 16:35:17 +00001129 int NumElements = ATy->getNumElements();
1130 const Type *ETy = ATy->getElementType();
1131 char *EndStr = UnEscapeLexed($3, true);
1132 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001133 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001134 itostr((int)(EndStr-$3)) +
1135 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001136 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001137 if (ETy == Type::SByteTy) {
1138 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001139 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001140 } else if (ETy == Type::UByteTy) {
1141 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001142 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001143 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001144 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001145 ThrowException("Cannot build string arrays of non byte sized elements!");
1146 }
Chris Lattner30c89792001-09-07 16:35:17 +00001147 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001148 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001149 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001150 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001151 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001152 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001153 if (STy == 0)
1154 ThrowException("Cannot make struct constant with type: '" +
1155 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001156
Chris Lattnerc6212f12003-05-21 16:06:56 +00001157 if ($3->size() != STy->getNumContainedTypes())
1158 ThrowException("Illegal number of initializers for structure type!");
1159
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001160 // Check to ensure that constants are compatible with the type initializer!
1161 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001162 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001163 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001164 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001165 "' for element #" + utostr(i) +
1166 " of structure initializer!");
1167
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001168 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001169 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001170 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001171 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001172 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001173 if (STy == 0)
1174 ThrowException("Cannot make struct constant with type: '" +
1175 (*$1)->getDescription() + "'!");
1176
1177 if (STy->getNumContainedTypes() != 0)
1178 ThrowException("Illegal number of initializers for structure type!");
1179
1180 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1181 delete $1;
1182 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001183 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001184 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001185 if (PTy == 0)
1186 ThrowException("Cannot make null pointer constant with type: '" +
1187 (*$1)->getDescription() + "'!");
1188
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001189 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001190 delete $1;
1191 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001192 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001193 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001194 if (Ty == 0)
1195 ThrowException("Global const reference must be a pointer type!");
1196
Chris Lattner3101c252002-08-15 17:58:33 +00001197 // ConstExprs can exist in the body of a function, thus creating
1198 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1199 // the context of a function, getValNonImprovising will search the functions
1200 // symbol table instead of the module symbol table for the global symbol,
1201 // which throws things all off. To get around this, we just tell
1202 // getValNonImprovising that we are at global scope here.
1203 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001204 Function *SavedCurFn = CurFun.CurrentFunction;
1205 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001206
Chris Lattner2079fde2001-10-13 06:41:08 +00001207 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001208
Chris Lattner394f2eb2003-10-16 20:12:13 +00001209 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001210
Chris Lattner2079fde2001-10-13 06:41:08 +00001211 // If this is an initializer for a constant pointer, which is referencing a
1212 // (currently) undefined variable, create a stub now that shall be replaced
1213 // in the future with the right type of variable.
1214 //
1215 if (V == 0) {
1216 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1217 const PointerType *PT = cast<PointerType>(Ty);
1218
1219 // First check to see if the forward references value is already created!
1220 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001221 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001222
1223 if (I != CurModule.GlobalRefs.end()) {
1224 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001225 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001226 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001227 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001228 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001229 false,
1230 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001231 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001232 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001233
1234 // Must temporarily push this value into the module table...
1235 CurModule.CurrentModule->getGlobalList().push_back(GV);
1236 V = GV;
1237 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001238 }
1239
Chris Lattner2079fde2001-10-13 06:41:08 +00001240 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001241 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001242 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001243 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001244 | Types ConstExpr {
1245 if ($1->get() != $2->getType())
1246 ThrowException("Mismatched types for constant expression!");
1247 $$ = $2;
1248 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001249 }
1250 | Types ZEROINITIALIZER {
1251 $$ = Constant::getNullValue($1->get());
1252 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001253 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001254
Chris Lattnere43f40b2002-10-09 00:25:32 +00001255ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001256 if (!ConstantSInt::isValueValidForType($1, $2))
1257 ThrowException("Constant value doesn't fit in type!");
1258 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001259 }
1260 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001261 if (!ConstantUInt::isValueValidForType($1, $2))
1262 ThrowException("Constant value doesn't fit in type!");
1263 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001264 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001265 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001266 $$ = ConstantBool::True;
1267 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001268 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001269 $$ = ConstantBool::False;
1270 }
1271 | FPType FPVAL { // Float & Double constants
1272 $$ = ConstantFP::get($1, $2);
1273 };
1274
Chris Lattner00950542001-06-06 20:29:01 +00001275
Chris Lattnerd78700d2002-08-16 21:14:40 +00001276ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001277 if (!$3->getType()->isFirstClassType())
1278 ThrowException("cast constant expression from a non-primitive type: '" +
1279 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001280 if (!$5->get()->isFirstClassType())
1281 ThrowException("cast constant expression to a non-primitive type: '" +
1282 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001283 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001284 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001285 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001286 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1287 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001288 ThrowException("GetElementPtr requires a pointer operand!");
1289
Chris Lattner830b6f92004-04-05 01:30:04 +00001290 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1291 // indices to uint struct indices for compatibility.
1292 generic_gep_type_iterator<std::vector<Value*>::iterator>
1293 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1294 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1295 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1296 if (isa<StructType>(*GTI)) // Only change struct indices
1297 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1298 if (CUI->getType() == Type::UByteTy)
1299 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1300
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001301 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001302 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001303 if (!IdxTy)
1304 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001305
Chris Lattner9232b992003-04-22 18:42:41 +00001306 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001307 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1308 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001309 IdxVec.push_back(C);
1310 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001311 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001312
Chris Lattnerd78700d2002-08-16 21:14:40 +00001313 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001314
Chris Lattnerd78700d2002-08-16 21:14:40 +00001315 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001316 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001317 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1318 if ($3->getType() != Type::BoolTy)
1319 ThrowException("Select condition must be of boolean type!");
1320 if ($5->getType() != $7->getType())
1321 ThrowException("Select operand types must match!");
1322 $$ = ConstantExpr::getSelect($3, $5, $7);
1323 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001324 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001325 if ($3->getType() != $5->getType())
1326 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001327 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001328 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001329 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001330 if ($5->getType() != Type::UByteTy)
1331 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001332 if (!$3->getType()->isInteger())
1333 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001334 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001335 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001336
1337
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001338// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001339ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001340 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001341 }
1342 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001343 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001344 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001345 };
Chris Lattner00950542001-06-06 20:29:01 +00001346
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001347
Chris Lattner1781aca2001-09-18 04:00:54 +00001348// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001349GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001350
Chris Lattner00950542001-06-06 20:29:01 +00001351
Chris Lattner0e73ce62002-05-02 19:11:13 +00001352//===----------------------------------------------------------------------===//
1353// Rules to match Modules
1354//===----------------------------------------------------------------------===//
1355
1356// Module rule: Capture the result of parsing the whole file into a result
1357// variable...
1358//
1359Module : FunctionList {
1360 $$ = ParserResult = $1;
1361 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001362};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001363
Chris Lattner7e708292002-06-25 16:13:24 +00001364// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001365//
1366FunctionList : FunctionList Function {
1367 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001368 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001369 }
1370 | FunctionList FunctionProto {
1371 $$ = $1;
1372 }
1373 | FunctionList IMPLEMENTATION {
1374 $$ = $1;
1375 }
1376 | ConstPool {
1377 $$ = CurModule.CurrentModule;
1378 // Resolve circular types before we parse the body of the module
1379 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001380 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001381
Chris Lattnere98dda62001-07-14 06:10:16 +00001382// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001383ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001384 if (!setValueName($4, $2))
1385 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001386 }
Chris Lattner30c89792001-09-07 16:35:17 +00001387 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001388 // Eagerly resolve types. This is not an optimization, this is a
1389 // requirement that is due to the fact that we could have this:
1390 //
1391 // %list = type { %list * }
1392 // %list = type { %list * } ; repeated type decl
1393 //
1394 // If types are not resolved eagerly, then the two types will not be
1395 // determined to be the same type!
1396 //
1397 ResolveTypeTo($2, $4->get());
1398
Chris Lattner1781aca2001-09-18 04:00:54 +00001399 // TODO: FIXME when Type are not const
Reid Spencer75719bf2004-05-26 21:48:31 +00001400 if (!setTypeName(const_cast<Type*>($4->get()), $2)) {
Chris Lattnerb7474512001-10-03 15:39:04 +00001401 // If this is not a redefinition of a type...
1402 if (!$2) {
1403 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001404 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001405 }
Chris Lattner30c89792001-09-07 16:35:17 +00001406 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001407
1408 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001409 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001410 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001411 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001412 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001413 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001414 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001415 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001416 if (Initializer == 0)
1417 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001418
Chris Lattnerdda71962001-11-26 18:54:16 +00001419 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001420 if (!setValueName(GV, $2)) { // If not redefining...
1421 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001422 int Slot = InsertValue(GV, CurModule.Values);
1423
1424 if (Slot != -1) {
1425 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1426 } else {
1427 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1428 (char*)GV->getName().c_str()));
1429 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001430 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001431 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001432 | ConstPool OptAssign EXTERNAL GlobalType Types {
1433 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001434 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001435 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001436 if (!setValueName(GV, $2)) { // If not redefining...
1437 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001438 int Slot = InsertValue(GV, CurModule.Values);
1439
1440 if (Slot != -1) {
1441 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1442 } else {
1443 assert(GV->hasName() && "Not named and not numbered!?");
1444 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1445 (char*)GV->getName().c_str()));
1446 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001447 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001448 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001449 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001450 | ConstPool TARGET TargetDefinition {
1451 }
Chris Lattner00950542001-06-06 20:29:01 +00001452 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001453 };
Chris Lattner00950542001-06-06 20:29:01 +00001454
1455
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001456
1457BigOrLittle : BIG { $$ = Module::BigEndian; };
1458BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1459
1460TargetDefinition : ENDIAN '=' BigOrLittle {
1461 CurModule.CurrentModule->setEndianness($3);
1462 }
1463 | POINTERSIZE '=' EUINT64VAL {
1464 if ($3 == 32)
1465 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1466 else if ($3 == 64)
1467 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1468 else
1469 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1470 };
1471
1472
Chris Lattner00950542001-06-06 20:29:01 +00001473//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001474// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001475//===----------------------------------------------------------------------===//
1476
Chris Lattner6c23f572003-08-22 05:42:10 +00001477Name : VAR_ID | STRINGCONSTANT;
1478OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001479
Chris Lattner6c23f572003-08-22 05:42:10 +00001480ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001481 if (*$1 == Type::VoidTy)
1482 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001483 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001484};
Chris Lattner00950542001-06-06 20:29:01 +00001485
Chris Lattner69da5cf2002-10-13 20:57:00 +00001486ArgListH : ArgListH ',' ArgVal {
1487 $$ = $1;
1488 $1->push_back(*$3);
1489 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001490 }
1491 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001492 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001493 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001494 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001495 };
Chris Lattner00950542001-06-06 20:29:01 +00001496
1497ArgList : ArgListH {
1498 $$ = $1;
1499 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001500 | ArgListH ',' DOTDOTDOT {
1501 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001502 $$->push_back(std::pair<PATypeHolder*,
1503 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001504 }
1505 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001506 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1507 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001508 }
Chris Lattner00950542001-06-06 20:29:01 +00001509 | /* empty */ {
1510 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001511 };
Chris Lattner00950542001-06-06 20:29:01 +00001512
Chris Lattner6c23f572003-08-22 05:42:10 +00001513FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001514 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001515 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001516
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001517 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1518 ThrowException("LLVM functions cannot return aggregate types!");
1519
Chris Lattner9232b992003-04-22 18:42:41 +00001520 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001521 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001522 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001523 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001524 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001525 }
Chris Lattner00950542001-06-06 20:29:01 +00001526
Chris Lattner2079fde2001-10-13 06:41:08 +00001527 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1528 if (isVarArg) ParamTypeList.pop_back();
1529
Chris Lattner1f862af2003-04-16 18:13:57 +00001530 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001531 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001532 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001533
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001534 Function *Fn = 0;
1535 // Is the function already in symtab?
1536 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1537 // Yes it is. If this is the case, either we need to be a forward decl,
1538 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001539 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001540 ThrowException("Redefinition of function '" + FunctionName + "'!");
1541
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001542 // Make sure to strip off any argument names so we can't get conflicts...
1543 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1544 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001545
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001546 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001547 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1548 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001549 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001550 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001551 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001552 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001553
Chris Lattner394f2eb2003-10-16 20:12:13 +00001554 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001555
Chris Lattner7e708292002-06-25 16:13:24 +00001556 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001557 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001558 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001559 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001560 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001561 delete $4->back().first;
1562 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001563 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001564 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001565 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001566 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001567 delete I->first; // Delete the typeholder...
1568
1569 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001570 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001571
Chris Lattner69da5cf2002-10-13 20:57:00 +00001572 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001573 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001574
Chris Lattner1f862af2003-04-16 18:13:57 +00001575 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001576 }
Chris Lattner51727be2002-06-04 21:58:56 +00001577};
Chris Lattner00950542001-06-06 20:29:01 +00001578
Chris Lattner9b02cc32002-05-03 18:23:48 +00001579BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1580
Chris Lattner4ad02e72003-04-16 20:28:45 +00001581FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001582 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001583
Chris Lattner4ad02e72003-04-16 20:28:45 +00001584 // Make sure that we keep track of the linkage type even if there was a
1585 // previous "declare".
1586 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001587
Chris Lattner7e708292002-06-25 16:13:24 +00001588 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001589 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001590};
Chris Lattner00950542001-06-06 20:29:01 +00001591
Chris Lattner9b02cc32002-05-03 18:23:48 +00001592END : ENDTOK | '}'; // Allow end of '}' to end a function
1593
Chris Lattner79df7c02002-03-26 18:01:55 +00001594Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001595 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001596};
Chris Lattner00950542001-06-06 20:29:01 +00001597
Chris Lattner394f2eb2003-10-16 20:12:13 +00001598FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1599 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001600 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001601};
Chris Lattner00950542001-06-06 20:29:01 +00001602
1603//===----------------------------------------------------------------------===//
1604// Rules to match Basic Blocks
1605//===----------------------------------------------------------------------===//
1606
1607ConstValueRef : ESINT64VAL { // A reference to a direct constant
1608 $$ = ValID::create($1);
1609 }
1610 | EUINT64VAL {
1611 $$ = ValID::create($1);
1612 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001613 | FPVAL { // Perhaps it's an FP constant?
1614 $$ = ValID::create($1);
1615 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001616 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001617 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001618 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001619 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001620 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001621 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001622 | NULL_TOK {
1623 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001624 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001625 | ConstExpr {
1626 $$ = ValID::create($1);
1627 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001628
Chris Lattner2079fde2001-10-13 06:41:08 +00001629// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1630// another value.
1631//
1632SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001633 $$ = ValID::create($1);
1634 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001635 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001636 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001637 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001638
1639// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001640ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001641
Chris Lattner00950542001-06-06 20:29:01 +00001642
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001643// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1644// type immediately preceeds the value reference, and allows complex constant
1645// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001646ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001647 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001648 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001649
Chris Lattner00950542001-06-06 20:29:01 +00001650BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001651 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001652 }
Chris Lattner7e708292002-06-25 16:13:24 +00001653 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1654 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001655 };
Chris Lattner00950542001-06-06 20:29:01 +00001656
1657
1658// Basic blocks are terminated by branching instructions:
1659// br, br/cc, switch, ret
1660//
Chris Lattner2079fde2001-10-13 06:41:08 +00001661BasicBlock : InstructionList OptAssign BBTerminatorInst {
1662 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1663 InsertValue($3);
1664
1665 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001666 InsertValue($1);
1667 $$ = $1;
1668 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001669 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1670 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1671 InsertValue($4);
1672
1673 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001674 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001675
1676 InsertValue($2);
1677 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001678 };
Chris Lattner00950542001-06-06 20:29:01 +00001679
1680InstructionList : InstructionList Inst {
1681 $1->getInstList().push_back($2);
1682 $$ = $1;
1683 }
1684 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001685 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001686 };
Chris Lattner00950542001-06-06 20:29:01 +00001687
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001688BBTerminatorInst : RET ResolvedVal { // Return with a result...
1689 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001690 }
1691 | RET VOID { // Return with no result...
1692 $$ = new ReturnInst();
1693 }
1694 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001695 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001696 } // Conditional Branch...
1697 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001698 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1699 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001700 getVal(Type::BoolTy, $3));
1701 }
1702 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1703 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001704 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001705 $$ = S;
1706
Chris Lattner9232b992003-04-22 18:42:41 +00001707 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001708 E = $8->end();
1709 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001710 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001711 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001712 }
Chris Lattner196850c2003-05-15 21:30:00 +00001713 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1714 SwitchInst *S = new SwitchInst(getVal($2, $3),
1715 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1716 $$ = S;
1717 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001718 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001719 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001720 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001721 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001722
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001723 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1724 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001725 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001726 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001727 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001728 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1729 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001730 ParamTypes.push_back((*I)->getType());
1731 }
1732
1733 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1734 if (isVarArg) ParamTypes.pop_back();
1735
Chris Lattner79df7c02002-03-26 18:01:55 +00001736 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001737 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001738 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001739
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001740 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001741
1742 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1743 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1744
1745 if (Normal == 0 || Except == 0)
1746 ThrowException("Invoke instruction without label destinations!");
1747
1748 // Create the call node...
1749 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001750 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001751 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001752 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001753 // correctly!
1754 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001755 FunctionType::param_iterator I = Ty->param_begin();
1756 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001757 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001758
1759 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1760 if ((*ArgI)->getType() != *I)
1761 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001762 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001763
1764 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1765 ThrowException("Invalid number of parameters detected!");
1766
Chris Lattner6cdb0112001-11-26 16:54:11 +00001767 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001768 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001769 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001770 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001771 }
1772 | UNWIND {
1773 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001774 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001775
1776
Chris Lattner00950542001-06-06 20:29:01 +00001777
1778JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1779 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001780 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001781 if (V == 0)
1782 ThrowException("May only switch on a constant pool value!");
1783
Chris Lattner9232b992003-04-22 18:42:41 +00001784 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001785 }
1786 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001787 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001788 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001789
1790 if (V == 0)
1791 ThrowException("May only switch on a constant pool value!");
1792
Chris Lattner9232b992003-04-22 18:42:41 +00001793 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001794 };
Chris Lattner00950542001-06-06 20:29:01 +00001795
1796Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001797 // Is this definition named?? if so, assign the name...
1798 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001799 InsertValue($2);
1800 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001801};
Chris Lattner00950542001-06-06 20:29:01 +00001802
Chris Lattnerc24d2082001-06-11 15:04:20 +00001803PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001804 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1805 $$->push_back(std::make_pair(getVal(*$1, $3),
1806 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001807 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001808 }
1809 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1810 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001811 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1812 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001813 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001814
1815
Chris Lattner30c89792001-09-07 16:35:17 +00001816ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001817 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001818 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001819 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001820 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001821 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001822 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001823 };
Chris Lattner00950542001-06-06 20:29:01 +00001824
1825// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001826ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001827
Chris Lattner4a6482b2002-09-10 19:57:26 +00001828InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1829 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1830 ThrowException("Arithmetic operator requires integer or FP operands!");
1831 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1832 if ($$ == 0)
1833 ThrowException("binary operator returned null!");
1834 delete $2;
1835 }
1836 | LogicalOps Types ValueRef ',' ValueRef {
1837 if (!(*$2)->isIntegral())
1838 ThrowException("Logical operator requires integral operands!");
1839 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1840 if ($$ == 0)
1841 ThrowException("binary operator returned null!");
1842 delete $2;
1843 }
1844 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001845 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001846 if ($$ == 0)
1847 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001848 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001849 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001850 | NOT ResolvedVal {
1851 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1852 << " Replacing with 'xor'.\n";
1853
1854 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1855 if (Ones == 0)
1856 ThrowException("Expected integral type for not instruction!");
1857
1858 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001859 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001860 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001861 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001862 | ShiftOps ResolvedVal ',' ResolvedVal {
1863 if ($4->getType() != Type::UByteTy)
1864 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001865 if (!$2->getType()->isInteger())
1866 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001867 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001868 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001869 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001870 if (!$4->get()->isFirstClassType())
1871 ThrowException("cast instruction to a non-primitive type: '" +
1872 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001873 $$ = new CastInst($2, *$4);
1874 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001875 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001876 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1877 if ($2->getType() != Type::BoolTy)
1878 ThrowException("select condition must be boolean!");
1879 if ($4->getType() != $6->getType())
1880 ThrowException("select value types should match!");
1881 $$ = new SelectInst($2, $4, $6);
1882 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001883 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001884 // FIXME: This is emulation code for an obsolete syntax. This should be
1885 // removed at some point.
1886 if (!ObsoleteVarArgs) {
1887 std::cerr << "WARNING: this file uses obsolete features. "
1888 << "Assemble and disassemble to update it.\n";
1889 ObsoleteVarArgs = true;
1890 }
1891
1892 // First, load the valist...
1893 Instruction *CurVAList = new LoadInst($2, "");
1894 CurBB->getInstList().push_back(CurVAList);
1895
1896 // Emit the vaarg instruction.
1897 $$ = new VAArgInst(CurVAList, *$4);
1898
1899 // Now we must advance the pointer and update it in memory.
1900 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1901 CurBB->getInstList().push_back(TheVANext);
1902
1903 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1904 delete $4;
1905 }
1906 | VAARG ResolvedVal ',' Types {
1907 $$ = new VAArgInst($2, *$4);
1908 delete $4;
1909 }
1910 | VANEXT ResolvedVal ',' Types {
1911 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001912 delete $4;
1913 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001914 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001915 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001916 if (!Ty->isFirstClassType())
1917 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001918 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001919 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001920 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001921 if ($2->front().first->getType() != Ty)
1922 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001923 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001924 $2->pop_front();
1925 }
1926 delete $2; // Free the list...
1927 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001928 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001929 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001930 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001931
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001932 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1933 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001934 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001935 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001936 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001937 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1938 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001939 ParamTypes.push_back((*I)->getType());
1940 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001941
1942 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1943 if (isVarArg) ParamTypes.pop_back();
1944
Chris Lattner79df7c02002-03-26 18:01:55 +00001945 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001946 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001947 }
Chris Lattner00950542001-06-06 20:29:01 +00001948
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001949 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001950
Chris Lattner8b81bf52001-07-25 22:47:46 +00001951 // Create the call node...
1952 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001953 // Make sure no arguments is a good thing!
1954 if (Ty->getNumParams() != 0)
1955 ThrowException("No arguments passed to a function that "
1956 "expects arguments!");
1957
Chris Lattner9232b992003-04-22 18:42:41 +00001958 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001959 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001960 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001961 // correctly!
1962 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001963 FunctionType::param_iterator I = Ty->param_begin();
1964 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001965 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001966
1967 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1968 if ((*ArgI)->getType() != *I)
1969 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001970 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001971
Chris Lattner8b81bf52001-07-25 22:47:46 +00001972 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001973 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001974
Chris Lattner6cdb0112001-11-26 16:54:11 +00001975 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001976 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001977 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001978 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001979 }
1980 | MemoryInst {
1981 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001982 };
Chris Lattner00950542001-06-06 20:29:01 +00001983
Chris Lattner6cdb0112001-11-26 16:54:11 +00001984
1985// IndexList - List of indices for GEP based instructions...
1986IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001987 $$ = $2;
1988 } | /* empty */ {
1989 $$ = new std::vector<Value*>();
1990 };
1991
1992OptVolatile : VOLATILE {
1993 $$ = true;
1994 }
1995 | /* empty */ {
1996 $$ = false;
1997 };
1998
Chris Lattner027dcc52001-07-08 21:10:27 +00001999
Chris Lattner00950542001-06-06 20:29:01 +00002000MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002001 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002002 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002003 }
2004 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002005 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002006 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002007 }
2008 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002009 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002010 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002011 }
2012 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002013 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002014 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002015 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002016 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002017 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002018 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002019 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002020 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002021 }
2022
Chris Lattner15c9c032003-09-08 18:20:29 +00002023 | OptVolatile LOAD Types ValueRef {
2024 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002025 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002026 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002027 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002028 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002029 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002030 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2031 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002032 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002033 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002034 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002035 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002036 if (ElTy != $3->getType())
2037 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002038 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002039
Chris Lattner79ad13802003-09-08 20:29:46 +00002040 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002041 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002042 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002043 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002044 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002045 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002046
2047 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2048 // indices to uint struct indices for compatibility.
2049 generic_gep_type_iterator<std::vector<Value*>::iterator>
2050 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2051 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2052 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2053 if (isa<StructType>(*GTI)) // Only change struct indices
2054 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2055 if (CUI->getType() == Type::UByteTy)
2056 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2057
Chris Lattner30c89792001-09-07 16:35:17 +00002058 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002059 ThrowException("Invalid getelementptr indices for type '" +
2060 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002061 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2062 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002063 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002064
Brian Gaeked0fde302003-11-11 22:41:34 +00002065
Chris Lattner00950542001-06-06 20:29:01 +00002066%%
Chris Lattner09083092001-07-08 04:57:15 +00002067int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002068 std::string where
2069 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002070 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002071 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002072 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002073 errMsg += "end-of-file.";
2074 else
Chris Lattner9232b992003-04-22 18:42:41 +00002075 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002076 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002077 return 0;
2078}