blob: dbb4546b4f3d00062e59cba52bc45c3ae2c51df6 [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000020#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/STLExtras.h"
Reid Spencer8ce1da72004-07-04 12:19:05 +000024#include <algorithm>
25#include <iostream>
Chris Lattner00950542001-06-06 20:29:01 +000026#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000027#include <utility>
Chris Lattner00950542001-06-06 20:29:01 +000028
Chris Lattner386a3b72001-10-16 19:54:17 +000029int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000030int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000031int yyparse();
32
Brian Gaeked0fde302003-11-11 22:41:34 +000033namespace llvm {
34
Chris Lattner00950542001-06-06 20:29:01 +000035static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000036std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner30c89792001-09-07 16:35:17 +000038// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
39// relating to upreferences in the input stream.
40//
41//#define DEBUG_UPREFS 1
42#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000043#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000044#else
45#define UR_OUT(X)
46#endif
47
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000048#define YYERROR_VERBOSE 1
49
Chris Lattner99e7ab72003-10-18 05:53:13 +000050// HACK ALERT: This variable is used to implement the automatic conversion of
51// variable argument instructions from their old to new forms. When this
52// compatiblity "Feature" is removed, this should be too.
53//
54static BasicBlock *CurBB;
55static bool ObsoleteVarArgs;
56
57
Chris Lattner7e708292002-06-25 16:13:24 +000058// This contains info used when building the body of a function. It is
59// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000060//
Chris Lattner9232b992003-04-22 18:42:41 +000061typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner735f2702004-07-08 22:30:50 +000062static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
63 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000064
65static struct PerModuleInfo {
66 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000067 std::map<const Type *, ValueList> Values; // Module level numbered definitions
68 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000069 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000070 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000071
Chris 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 Lattner735f2702004-07-08 22:30:50 +0000149 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
150 std::map<const Type*, 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 {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000176 // Figure out which slot number if is...
Chris Lattner735f2702004-07-08 22:30:50 +0000177 ValueList &List = CurModule.Values[CurrentFunction->getType()];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000178 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000179 assert(i < List.size() && "Function not found!");
180 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000181 FID = ValID::create((int)i);
182 break;
183 }
184 }
185 }
186 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
187
Chris Lattner7e708292002-06-25 16:13:24 +0000188 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000189 Types.clear(); // Clear out function local types
190 LocalSymtab.clear(); // Clear out function local symbol table
Chris Lattner79df7c02002-03-26 18:01:55 +0000191 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000192 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000193 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000194} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000195
Chris Lattner394f2eb2003-10-16 20:12:13 +0000196static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000197
Chris Lattner00950542001-06-06 20:29:01 +0000198
199//===----------------------------------------------------------------------===//
200// Code to handle definitions of all the types
201//===----------------------------------------------------------------------===//
202
Chris Lattner735f2702004-07-08 22:30:50 +0000203static int InsertValue(Value *V,
204 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
205 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000206
207 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000208 ValueList &List = ValueTab[V->getType()];
209 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000210 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000211}
212
Chris Lattner30c89792001-09-07 16:35:17 +0000213// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000214static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000215 Types.push_back(Ty);
216}
217
218static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000219 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000220 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000221 unsigned Num = (unsigned)D.Num;
222
223 // Module constants occupy the lowest numbered slots...
224 if (Num < CurModule.Types.size())
225 return CurModule.Types[Num];
226
227 Num -= CurModule.Types.size();
228
229 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000230 if (Num <= CurFun.Types.size())
231 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000232 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000233 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000234 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000235 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000236 SymbolTable *SymTab = 0;
Reid Spencer8ce1da72004-07-04 12:19:05 +0000237 Type *N = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000238 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000239 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000240 N = SymTab->lookupType(Name);
Chris Lattner6e6026b2002-11-20 18:36:02 +0000241 }
Chris Lattner30c89792001-09-07 16:35:17 +0000242
243 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000244 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000245 // hasn't been added to the module...
246 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000247 SymTab = &CurModule.CurrentModule->getSymbolTable();
Reid Spencerb152f9f2004-05-25 17:29:21 +0000248 N = SymTab->lookupType(Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000249 if (N == 0) break;
250 }
251
252 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000253 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000254 }
255 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000256 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000257 }
258
259 // If we reached here, we referenced either a symbol that we don't know about
260 // or an id number that hasn't been read yet. We may be referencing something
261 // forward, so just create an entry to be resolved later and get to it...
262 //
263 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
264
Chris Lattner9232b992003-04-22 18:42:41 +0000265 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000266 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000267
Chris Lattner9232b992003-04-22 18:42:41 +0000268 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000269 if (I != LateResolver.end()) {
270 return I->second;
271 }
Chris Lattner30c89792001-09-07 16:35:17 +0000272
Chris Lattner82269592001-10-22 06:01:08 +0000273 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000274 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000275 return Typ;
276}
277
Chris Lattner9232b992003-04-22 18:42:41 +0000278static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000279 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000280 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000281 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000282 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000283}
284
Chris Lattner2079fde2001-10-13 06:41:08 +0000285// getValNonImprovising - Look up the value specified by the provided type and
286// the provided ValID. If the value exists and has already been defined, return
287// it. Otherwise return null.
288//
289static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000290 if (isa<FunctionType>(Ty))
291 ThrowException("Functions are not values and "
292 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000293
Chris Lattner30c89792001-09-07 16:35:17 +0000294 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000295 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000296 unsigned Num = (unsigned)D.Num;
297
298 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000299 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000300 if (VI != CurModule.Values.end()) {
301 if (Num < VI->second.size())
302 return VI->second[Num];
303 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000304 }
305
306 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000307 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000308 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000309
310 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000311 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000312
Chris Lattner16af11d2004-02-09 21:03:38 +0000313 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000314 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000315
Chris Lattner1a1cb112001-09-30 22:46:54 +0000316 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000317 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000318 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000319
320 D.destroy(); // Free old strdup'd memory...
321 return N;
322 }
323
Chris Lattner2079fde2001-10-13 06:41:08 +0000324 // Check to make sure that "Ty" is an integral type, and that our
325 // value will fit into the specified type...
326 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000327 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
328 ThrowException("Signed integral constant '" +
329 itostr(D.ConstPool64) + "' is invalid for type '" +
330 Ty->getDescription() + "'!");
331 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000332
333 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000334 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
335 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000336 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
337 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000338 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000339 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000340 }
341 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000342 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000343 }
344
Chris Lattner2079fde2001-10-13 06:41:08 +0000345 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000346 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000347 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000348 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000349
350 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000351 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000352 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000353 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000354
Chris Lattnerd78700d2002-08-16 21:14:40 +0000355 case ValID::ConstantVal: // Fully resolved constant?
356 if (D.ConstantValue->getType() != Ty)
357 ThrowException("Constant expression type different from required type!");
358 return D.ConstantValue;
359
Chris Lattner30c89792001-09-07 16:35:17 +0000360 default:
361 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000362 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000363 } // End of switch
364
Chris Lattner2079fde2001-10-13 06:41:08 +0000365 assert(0 && "Unhandled case!");
366 return 0;
367}
368
369
370// getVal - This function is identical to getValNonImprovising, except that if a
371// value is not already defined, it "improvises" by creating a placeholder var
372// that looks and acts just like the requested variable. When the value is
373// defined later, all uses of the placeholder variable are replaced with the
374// real thing.
375//
376static Value *getVal(const Type *Ty, const ValID &D) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000377
378 // See if the value has already been defined...
379 Value *V = getValNonImprovising(Ty, D);
380 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000381
382 // If we reached here, we referenced either a symbol that we don't know about
383 // or an id number that hasn't been read yet. We may be referencing something
384 // forward, so just create an entry to be resolved later and get to it...
385 //
Chris Lattner00950542001-06-06 20:29:01 +0000386 Value *d = 0;
Chris Lattnerf70c22b2004-06-17 18:19:28 +0000387 switch (Ty->getTypeID()) {
Chris Lattner30c89792001-09-07 16:35:17 +0000388 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000389 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000390 }
391
392 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000393 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000394 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000395 else
396 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000397 return d;
398}
399
400
401//===----------------------------------------------------------------------===//
402// Code to handle forward references in instructions
403//===----------------------------------------------------------------------===//
404//
405// This code handles the late binding needed with statements that reference
406// values not defined yet... for example, a forward branch, or the PHI node for
407// a loop body.
408//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000409// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000410// and back patchs after we are done.
411//
412
413// ResolveDefinitions - If we could not resolve some defs at parsing
414// time (forward branches, phi functions for loops, etc...) resolve the
415// defs now...
416//
Chris Lattner735f2702004-07-08 22:30:50 +0000417static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
418 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000419 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000420 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000421 E = LateResolvers.end(); LRI != E; ++LRI) {
422 ValueList &List = LRI->second;
423 while (!List.empty()) {
424 Value *V = List.back();
425 List.pop_back();
Chris Lattner00950542001-06-06 20:29:01 +0000426 ValID &DID = getValIDFromPlaceHolder(V);
427
Chris Lattner735f2702004-07-08 22:30:50 +0000428 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000429 if (TheRealValue) {
430 V->replaceAllUsesWith(TheRealValue);
431 delete V;
432 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000433 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000434 // resolver table
435 InsertValue(V, *FutureLateResolvers);
436 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000437 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000438 ThrowException("Reference to an invalid definition: '" +DID.getName()+
439 "' of type '" + V->getType()->getDescription() + "'",
440 getLineNumFromPlaceHolder(V));
441 else
442 ThrowException("Reference to an invalid definition: #" +
443 itostr(DID.Num) + " of type '" +
444 V->getType()->getDescription() + "'",
445 getLineNumFromPlaceHolder(V));
446 }
Chris Lattner00950542001-06-06 20:29:01 +0000447 }
448 }
449
450 LateResolvers.clear();
451}
452
Chris Lattner4a42e902001-10-22 05:56:09 +0000453// ResolveTypeTo - A brand new type was just declared. This means that (if
454// name is not null) things referencing Name can be resolved. Otherwise, things
455// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000456//
Chris Lattner4a42e902001-10-22 05:56:09 +0000457static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000458 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000459 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000460
Chris Lattner4a42e902001-10-22 05:56:09 +0000461 ValID D;
462 if (Name) D = ValID::create(Name);
463 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000464
Chris Lattner9232b992003-04-22 18:42:41 +0000465 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000466 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000467
Chris Lattner9232b992003-04-22 18:42:41 +0000468 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000469 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000470 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000471 LateResolver.erase(I);
472 }
473}
474
475// ResolveTypes - At this point, all types should be resolved. Any that aren't
476// are errors.
477//
Chris Lattner9232b992003-04-22 18:42:41 +0000478static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000479 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000480 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000481
482 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000483 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000484 else
Chris Lattner82269592001-10-22 06:01:08 +0000485 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000486 }
487}
488
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000489
Chris Lattner1781aca2001-09-18 04:00:54 +0000490// setValueName - Set the specified value to the name given. The name may be
491// null potentially, in which case this is a noop. The string passed in is
492// assumed to be a malloc'd string buffer, and is freed by this function.
493//
Chris Lattnerb7474512001-10-03 15:39:04 +0000494// This function returns true if the value has already been defined, but is
495// allowed to be redefined in the specified context. If the name is a new name
496// for the typeplane, false is returned.
497//
498static bool setValueName(Value *V, char *NameStr) {
499 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000500
Chris Lattner9232b992003-04-22 18:42:41 +0000501 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000502 free(NameStr); // Free old string
503
Chris Lattner2079fde2001-10-13 06:41:08 +0000504 if (V->getType() == Type::VoidTy)
505 ThrowException("Can't assign name '" + Name +
506 "' to a null valued instruction!");
507
Chris Lattner6e6026b2002-11-20 18:36:02 +0000508 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000509 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000510 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000511
Reid Spencer75719bf2004-05-26 21:48:31 +0000512 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner42fbc7e2004-05-26 17:08:25 +0000513
Chris Lattner30c89792001-09-07 16:35:17 +0000514 if (Existing) { // Inserting a name that is already defined???
Reid Spencer9ed0f172004-05-27 22:05:50 +0000515 // We are a simple redefinition of a value, check to see if it
Chris Lattner9636a912001-10-01 16:18:37 +0000516 // is defined the same as the old one...
Reid Spencer4e6620c2004-05-28 00:21:06 +0000517 if (const Constant *C = dyn_cast<Constant>(Existing)) {
Chris Lattneradf99702003-03-03 23:28:55 +0000518 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000519 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000520 // We are allowed to redefine a global variable in two circumstances:
521 // 1. If at least one of the globals is uninitialized or
522 // 2. If both initializers have the same value.
523 //
Chris Lattner89219832001-10-03 19:35:04 +0000524 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000525 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
526 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000527
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000528 // Make sure the existing global version gets the initializer! Make
529 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000530 if (GV->hasInitializer() && !EGV->hasInitializer())
531 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000532 if (GV->isConstant())
533 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000534 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000535
Chris Lattner2079fde2001-10-13 06:41:08 +0000536 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000537 return true; // They are equivalent!
538 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000539 }
Chris Lattner9636a912001-10-01 16:18:37 +0000540 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000541
Chris Lattner2079fde2001-10-13 06:41:08 +0000542 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000543 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000544 }
Chris Lattner00950542001-06-06 20:29:01 +0000545
Chris Lattner15b69722003-11-12 04:40:30 +0000546 // Set the name
Chris Lattner6e6026b2002-11-20 18:36:02 +0000547 V->setName(Name, &ST);
Chris Lattner15b69722003-11-12 04:40:30 +0000548
549 // If we're in function scope
550 if (inFunctionScope()) {
551 // Look up the symbol in the function's local symboltable
552 Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
553
554 // If it already exists
555 if (Existing) {
Chris Lattner15b69722003-11-12 04:40:30 +0000556 // Bail
557 ThrowException("Redefinition of value named '" + Name + "' in the '" +
558 V->getType()->getDescription() + "' type plane!");
559
560 // otherwise, since it doesn't exist
561 } else {
562 // Insert it.
563 CurFun.LocalSymtab.insert(V);
564 }
565 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000566 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000567}
568
Reid Spencer75719bf2004-05-26 21:48:31 +0000569// setTypeName - Set the specified type to the name given. The name may be
570// null potentially, in which case this is a noop. The string passed in is
571// assumed to be a malloc'd string buffer, and is freed by this function.
572//
573// This function returns true if the type has already been defined, but is
574// allowed to be redefined in the specified context. If the name is a new name
575// for the type plane, it is inserted and false is returned.
576static bool setTypeName(Type *T, char *NameStr) {
577 if (NameStr == 0) return false;
578
579 std::string Name(NameStr); // Copy string
580 free(NameStr); // Free old string
581
582 // We don't allow assigning names to void type
583 if (T == Type::VoidTy)
584 ThrowException("Can't assign name '" + Name + "' to the null type!");
585
586 SymbolTable &ST = inFunctionScope() ?
587 CurFun.CurrentFunction->getSymbolTable() :
588 CurModule.CurrentModule->getSymbolTable();
589
590 Type *Existing = ST.lookupType(Name);
591
592 if (Existing) { // Inserting a name that is already defined???
593 // There is only one case where this is allowed: when we are refining an
594 // opaque type. In this case, Existing will be an opaque type.
595 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
596 // We ARE replacing an opaque type!
597 ((OpaqueType*)OpTy)->refineAbstractTypeTo(T);
598 return true;
599 }
600
601 // Otherwise, this is an attempt to redefine a type. That's okay if
602 // the redefinition is identical to the original. This will be so if
603 // Existing and T point to the same Type object. In this one case we
604 // allow the equivalent redefinition.
605 if (Existing == T) return true; // Yes, it's equal.
606
607 // Any other kind of (non-equivalent) redefinition is an error.
608 ThrowException("Redefinition of type named '" + Name + "' in the '" +
609 T->getDescription() + "' type plane!");
610 }
611
612 // Okay, its a newly named type. Set its name.
613 T->setName(Name,&ST);
614
615 // If we're in function scope
616 if (inFunctionScope()) {
617 // Look up the symbol in the function's local symboltable
618 Existing = CurFun.LocalSymtab.lookupType(Name);
619
620 // If it already exists
621 if (Existing) {
622 // Bail
623 ThrowException("Redefinition of type named '" + Name + "' in the '" +
624 T->getDescription() + "' type plane in function scope!");
625
626 // otherwise, since it doesn't exist
627 } else {
628 // Insert it.
629 CurFun.LocalSymtab.insert(Name,T);
630 }
631 }
632 return false;
633}
Chris Lattner8896eda2001-07-09 19:38:36 +0000634
Chris Lattner30c89792001-09-07 16:35:17 +0000635//===----------------------------------------------------------------------===//
636// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000637//
Chris Lattner8896eda2001-07-09 19:38:36 +0000638
Chris Lattner3b073862004-02-09 03:19:29 +0000639// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000640//
641static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000642 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
643}
644
645namespace {
646 struct UpRefRecord {
647 // NestingLevel - The number of nesting levels that need to be popped before
648 // this type is resolved.
649 unsigned NestingLevel;
650
651 // LastContainedTy - This is the type at the current binding level for the
652 // type. Every time we reduce the nesting level, this gets updated.
653 const Type *LastContainedTy;
654
655 // UpRefTy - This is the actual opaque type that the upreference is
656 // represented with.
657 OpaqueType *UpRefTy;
658
659 UpRefRecord(unsigned NL, OpaqueType *URTy)
660 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
661 };
Chris Lattner30c89792001-09-07 16:35:17 +0000662}
Chris Lattner698b56e2001-07-20 19:15:08 +0000663
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000664// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000665static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000666
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000667/// HandleUpRefs - Every time we finish a new layer of types, this function is
668/// called. It loops through the UpRefs vector, which is a list of the
669/// currently active types. For each type, if the up reference is contained in
670/// the newly completed type, we decrement the level count. When the level
671/// count reaches zero, the upreferenced type is the type that is passed in:
672/// thus we can complete the cycle.
673///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000674static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000675 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000676 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000677 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000678 "' newly formed. Resolving upreferences.\n" <<
679 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000680
681 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
682 // to zero), we resolve them all together before we resolve them to Ty. At
683 // the end of the loop, if there is anything to resolve to Ty, it will be in
684 // this variable.
685 OpaqueType *TypeToResolve = 0;
686
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000687 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000688 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000689 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000690 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000691 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
692 // Decrement level of upreference
693 unsigned Level = --UpRefs[i].NestingLevel;
694 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000695 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000696 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000697 if (!TypeToResolve) {
698 TypeToResolve = UpRefs[i].UpRefTy;
699 } else {
700 UR_OUT(" * Resolving upreference for "
701 << UpRefs[i].second->getDescription() << "\n";
702 std::string OldName = UpRefs[i].UpRefTy->getDescription());
703 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
704 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
705 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
706 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000707 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
708 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000709 }
710 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000711 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000712
Chris Lattner026a8ce2004-02-09 18:53:54 +0000713 if (TypeToResolve) {
714 UR_OUT(" * Resolving upreference for "
715 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000716 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000717 TypeToResolve->refineAbstractTypeTo(Ty);
718 }
719
Chris Lattner8896eda2001-07-09 19:38:36 +0000720 return Ty;
721}
722
Chris Lattner30c89792001-09-07 16:35:17 +0000723
Chris Lattner00950542001-06-06 20:29:01 +0000724//===----------------------------------------------------------------------===//
725// RunVMAsmParser - Define an interface to this parser
726//===----------------------------------------------------------------------===//
727//
Chris Lattner9232b992003-04-22 18:42:41 +0000728Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000729 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000730 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000731 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000732 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000733
Chris Lattner75f20532003-04-22 18:02:52 +0000734 // Allocate a new module to read
735 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000736
737 try {
738 yyparse(); // Parse the file.
739 } catch (...) {
740 // Clear the symbol table so it doesn't complain when it
741 // gets destructed
742 CurFun.LocalSymtab.clear();
743 throw;
744 }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000745
Chris Lattner00950542001-06-06 20:29:01 +0000746 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000747
748 // Check to see if they called va_start but not va_arg..
749 if (!ObsoleteVarArgs)
750 if (Function *F = Result->getNamedFunction("llvm.va_start"))
751 if (F->asize() == 1) {
752 std::cerr << "WARNING: this file uses obsolete features. "
753 << "Assemble and disassemble to update it.\n";
754 ObsoleteVarArgs = true;
755 }
756
757
758 if (ObsoleteVarArgs) {
759 // If the user is making use of obsolete varargs intrinsics, adjust them for
760 // the user.
761 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
762 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
763
764 const Type *RetTy = F->getFunctionType()->getParamType(0);
765 RetTy = cast<PointerType>(RetTy)->getElementType();
766 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
767
768 while (!F->use_empty()) {
769 CallInst *CI = cast<CallInst>(F->use_back());
770 Value *V = new CallInst(NF, "", CI);
771 new StoreInst(V, CI->getOperand(1), CI);
772 CI->getParent()->getInstList().erase(CI);
773 }
774 Result->getFunctionList().erase(F);
775 }
776
777 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
778 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
779 const Type *ArgTy = F->getFunctionType()->getParamType(0);
780 ArgTy = cast<PointerType>(ArgTy)->getElementType();
781 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
782 ArgTy, 0);
783
784 while (!F->use_empty()) {
785 CallInst *CI = cast<CallInst>(F->use_back());
786 Value *V = new LoadInst(CI->getOperand(1), "", CI);
787 new CallInst(NF, V, "", CI);
788 CI->getParent()->getInstList().erase(CI);
789 }
790 Result->getFunctionList().erase(F);
791 }
792
793 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
794 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
795 const Type *ArgTy = F->getFunctionType()->getParamType(0);
796 ArgTy = cast<PointerType>(ArgTy)->getElementType();
797 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
798 ArgTy, 0);
799
800 while (!F->use_empty()) {
801 CallInst *CI = cast<CallInst>(F->use_back());
802 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
803 new StoreInst(V, CI->getOperand(1), CI);
804 CI->getParent()->getInstList().erase(CI);
805 }
806 Result->getFunctionList().erase(F);
807 }
808 }
809
Chris Lattner00950542001-06-06 20:29:01 +0000810 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
811 ParserResult = 0;
812
813 return Result;
814}
815
Brian Gaeked0fde302003-11-11 22:41:34 +0000816} // End llvm namespace
817
818using namespace llvm;
819
Chris Lattner00950542001-06-06 20:29:01 +0000820%}
821
822%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000823 llvm::Module *ModuleVal;
824 llvm::Function *FunctionVal;
825 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
826 llvm::BasicBlock *BasicBlockVal;
827 llvm::TerminatorInst *TermInstVal;
828 llvm::Instruction *InstVal;
829 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000830
Brian Gaeked0fde302003-11-11 22:41:34 +0000831 const llvm::Type *PrimType;
832 llvm::PATypeHolder *TypeVal;
833 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000834
Brian Gaeked0fde302003-11-11 22:41:34 +0000835 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
836 std::vector<llvm::Value*> *ValueList;
837 std::list<llvm::PATypeHolder> *TypeList;
838 std::list<std::pair<llvm::Value*,
839 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
840 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
841 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000842
Brian Gaeked0fde302003-11-11 22:41:34 +0000843 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000844 int64_t SInt64Val;
845 uint64_t UInt64Val;
846 int SIntVal;
847 unsigned UIntVal;
848 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000849 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000850
Chris Lattner30c89792001-09-07 16:35:17 +0000851 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000852 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000853
Brian Gaeked0fde302003-11-11 22:41:34 +0000854 llvm::Instruction::BinaryOps BinaryOpVal;
855 llvm::Instruction::TermOps TermOpVal;
856 llvm::Instruction::MemoryOps MemOpVal;
857 llvm::Instruction::OtherOps OtherOpVal;
858 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000859}
860
Chris Lattner79df7c02002-03-26 18:01:55 +0000861%type <ModuleVal> Module FunctionList
862%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000863%type <BasicBlockVal> BasicBlock InstructionList
864%type <TermInstVal> BBTerminatorInst
865%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000866%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000867%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000868%type <ArgList> ArgList ArgListH
869%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000870%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000871%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000872%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000873%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000874%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000875%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000876%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000877%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000878%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000879
Chris Lattner2079fde2001-10-13 06:41:08 +0000880// ValueRef - Unresolved reference to a definition or BB
881%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000882%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000883// Tokens and types for handling constant integer values
884//
885// ESINT64VAL - A negative number within long long range
886%token <SInt64Val> ESINT64VAL
887
888// EUINT64VAL - A positive number within uns. long long range
889%token <UInt64Val> EUINT64VAL
890%type <SInt64Val> EINT64VAL
891
892%token <SIntVal> SINTVAL // Signed 32 bit ints...
893%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
894%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000895%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000896
897// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000898%type <TypeVal> Types TypesV UpRTypes UpRTypesV
899%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000900%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
901%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000902
Chris Lattner6c23f572003-08-22 05:42:10 +0000903%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
904%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000905
906
Chris Lattner91ef4602004-03-31 03:49:47 +0000907%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000908%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000909%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000910%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000911
912// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000913%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000914
Chris Lattner00950542001-06-06 20:29:01 +0000915// Binary Operators
916%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000917%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000918%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000919%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000920
921// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000922%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000923
Chris Lattner027dcc52001-07-08 21:10:27 +0000924// Other Operators
925%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000926%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000927%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000928
Chris Lattner00950542001-06-06 20:29:01 +0000929%start Module
930%%
931
932// Handle constant integer size restriction and conversion...
933//
Chris Lattner51727be2002-06-04 21:58:56 +0000934INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000935INTVAL : UINTVAL {
936 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
937 ThrowException("Value too large for type!");
938 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000939};
Chris Lattner00950542001-06-06 20:29:01 +0000940
941
Chris Lattner51727be2002-06-04 21:58:56 +0000942EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000943EINT64VAL : EUINT64VAL {
944 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
945 ThrowException("Value too large for type!");
946 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000947};
Chris Lattner00950542001-06-06 20:29:01 +0000948
Chris Lattner00950542001-06-06 20:29:01 +0000949// Operations that are notably excluded from this list include:
950// RET, BR, & SWITCH because they end basic blocks and are treated specially.
951//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000952ArithmeticOps: ADD | SUB | MUL | DIV | REM;
953LogicalOps : AND | OR | XOR;
954SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
955BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
956
Chris Lattner51727be2002-06-04 21:58:56 +0000957ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000958
Chris Lattnere98dda62001-07-14 06:10:16 +0000959// These are some types that allow classification if we only want a particular
960// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000961SIntType : LONG | INT | SHORT | SBYTE;
962UIntType : ULONG | UINT | USHORT | UBYTE;
963IntType : SIntType | UIntType;
964FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000965
Chris Lattnere98dda62001-07-14 06:10:16 +0000966// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000967OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000968 $$ = $1;
969 }
970 | /*empty*/ {
971 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000972 };
Chris Lattner00950542001-06-06 20:29:01 +0000973
Chris Lattner4ad02e72003-04-16 20:28:45 +0000974OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
975 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000976 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000977 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
978 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000979
980//===----------------------------------------------------------------------===//
981// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000982// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000983// access to it, a user must explicitly use TypesV.
984//
985
986// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000987TypesV : Types | VOID { $$ = new PATypeHolder($1); };
988UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000989
990Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000991 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000992 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
993 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000994 };
Chris Lattner30c89792001-09-07 16:35:17 +0000995
996
997// Derived types are added later...
998//
Chris Lattner51727be2002-06-04 21:58:56 +0000999PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1000PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001001UpRTypes : OPAQUE {
1002 $$ = new PATypeHolder(OpaqueType::get());
1003 }
1004 | PrimType {
1005 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001006 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001007UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001008 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001009};
Chris Lattner30c89792001-09-07 16:35:17 +00001010
Chris Lattner30c89792001-09-07 16:35:17 +00001011// Include derived types in the Types production.
1012//
1013UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001014 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001015 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001016 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001017 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001018 UR_OUT("New Upreference!\n");
1019 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001020 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001021 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001022 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001023 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001024 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1025 if (isVarArg) Params.pop_back();
1026
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001027 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001028 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001029 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001030 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001031 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001032 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001033 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001034 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001035 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001036 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001037 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001038 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001039
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001040 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001041 delete $2;
1042 }
1043 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001044 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001045 }
1046 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001047 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001048 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001049 };
Chris Lattner30c89792001-09-07 16:35:17 +00001050
Chris Lattner7e708292002-06-25 16:13:24 +00001051// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001052// declaration type lists
1053//
1054TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001055 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001056 $$->push_back(*$1); delete $1;
1057 }
1058 | TypeListI ',' UpRTypes {
1059 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001060 };
Chris Lattner30c89792001-09-07 16:35:17 +00001061
Chris Lattner7e708292002-06-25 16:13:24 +00001062// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001063ArgTypeListI : TypeListI
1064 | TypeListI ',' DOTDOTDOT {
1065 ($$=$1)->push_back(Type::VoidTy);
1066 }
1067 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001068 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001069 }
1070 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001071 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001072 };
Chris Lattner30c89792001-09-07 16:35:17 +00001073
Chris Lattnere98dda62001-07-14 06:10:16 +00001074// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001075// production is used ONLY to represent constants that show up AFTER a 'const',
1076// 'constant' or 'global' token at global scope. Constants that can be inlined
1077// into other expressions (such as integers and constexprs) are handled by the
1078// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001079//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001080ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001081 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001082 if (ATy == 0)
1083 ThrowException("Cannot make array constant with type: '" +
1084 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001085 const Type *ETy = ATy->getElementType();
1086 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001087
Chris Lattner30c89792001-09-07 16:35:17 +00001088 // Verify that we have the correct size...
1089 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001090 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001091 utostr($3->size()) + " arguments, but has size of " +
1092 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001093
Chris Lattner30c89792001-09-07 16:35:17 +00001094 // Verify all elements are correct type!
1095 for (unsigned i = 0; i < $3->size(); i++) {
1096 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001097 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001098 ETy->getDescription() +"' as required!\nIt is of type '"+
1099 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001100 }
1101
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001102 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001103 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001104 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001105 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001106 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001107 if (ATy == 0)
1108 ThrowException("Cannot make array constant with type: '" +
1109 (*$1)->getDescription() + "'!");
1110
1111 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001112 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001113 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001114 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001115 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001116 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001117 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001118 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001119 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001120 if (ATy == 0)
1121 ThrowException("Cannot make array constant with type: '" +
1122 (*$1)->getDescription() + "'!");
1123
Chris Lattner30c89792001-09-07 16:35:17 +00001124 int NumElements = ATy->getNumElements();
1125 const Type *ETy = ATy->getElementType();
1126 char *EndStr = UnEscapeLexed($3, true);
1127 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001128 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001129 itostr((int)(EndStr-$3)) +
1130 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001131 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001132 if (ETy == Type::SByteTy) {
1133 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001134 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001135 } else if (ETy == Type::UByteTy) {
1136 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001137 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001138 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001139 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001140 ThrowException("Cannot build string arrays of non byte sized elements!");
1141 }
Chris Lattner30c89792001-09-07 16:35:17 +00001142 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001143 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001144 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001145 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001146 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001147 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001148 if (STy == 0)
1149 ThrowException("Cannot make struct constant with type: '" +
1150 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001151
Chris Lattnerc6212f12003-05-21 16:06:56 +00001152 if ($3->size() != STy->getNumContainedTypes())
1153 ThrowException("Illegal number of initializers for structure type!");
1154
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001155 // Check to ensure that constants are compatible with the type initializer!
1156 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001157 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001158 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001159 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001160 "' for element #" + utostr(i) +
1161 " of structure initializer!");
1162
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001163 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001164 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001165 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001166 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001167 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001168 if (STy == 0)
1169 ThrowException("Cannot make struct constant with type: '" +
1170 (*$1)->getDescription() + "'!");
1171
1172 if (STy->getNumContainedTypes() != 0)
1173 ThrowException("Illegal number of initializers for structure type!");
1174
1175 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1176 delete $1;
1177 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001178 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001179 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001180 if (PTy == 0)
1181 ThrowException("Cannot make null pointer constant with type: '" +
1182 (*$1)->getDescription() + "'!");
1183
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001184 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001185 delete $1;
1186 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001187 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001188 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001189 if (Ty == 0)
1190 ThrowException("Global const reference must be a pointer type!");
1191
Chris Lattner3101c252002-08-15 17:58:33 +00001192 // ConstExprs can exist in the body of a function, thus creating
1193 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1194 // the context of a function, getValNonImprovising will search the functions
1195 // symbol table instead of the module symbol table for the global symbol,
1196 // which throws things all off. To get around this, we just tell
1197 // getValNonImprovising that we are at global scope here.
1198 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001199 Function *SavedCurFn = CurFun.CurrentFunction;
1200 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001201
Chris Lattner2079fde2001-10-13 06:41:08 +00001202 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001203
Chris Lattner394f2eb2003-10-16 20:12:13 +00001204 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001205
Chris Lattner2079fde2001-10-13 06:41:08 +00001206 // If this is an initializer for a constant pointer, which is referencing a
1207 // (currently) undefined variable, create a stub now that shall be replaced
1208 // in the future with the right type of variable.
1209 //
1210 if (V == 0) {
1211 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1212 const PointerType *PT = cast<PointerType>(Ty);
1213
1214 // First check to see if the forward references value is already created!
1215 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001216 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001217
1218 if (I != CurModule.GlobalRefs.end()) {
1219 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001220 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001221 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001222 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001223 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001224 false,
1225 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001226 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001227 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001228
1229 // Must temporarily push this value into the module table...
1230 CurModule.CurrentModule->getGlobalList().push_back(GV);
1231 V = GV;
1232 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001233 }
1234
Chris Lattner2079fde2001-10-13 06:41:08 +00001235 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001236 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001237 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001238 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001239 | Types ConstExpr {
1240 if ($1->get() != $2->getType())
1241 ThrowException("Mismatched types for constant expression!");
1242 $$ = $2;
1243 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001244 }
1245 | Types ZEROINITIALIZER {
1246 $$ = Constant::getNullValue($1->get());
1247 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001248 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001249
Chris Lattnere43f40b2002-10-09 00:25:32 +00001250ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001251 if (!ConstantSInt::isValueValidForType($1, $2))
1252 ThrowException("Constant value doesn't fit in type!");
1253 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001254 }
1255 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001256 if (!ConstantUInt::isValueValidForType($1, $2))
1257 ThrowException("Constant value doesn't fit in type!");
1258 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001259 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001260 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001261 $$ = ConstantBool::True;
1262 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001263 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001264 $$ = ConstantBool::False;
1265 }
1266 | FPType FPVAL { // Float & Double constants
1267 $$ = ConstantFP::get($1, $2);
1268 };
1269
Chris Lattner00950542001-06-06 20:29:01 +00001270
Chris Lattnerd78700d2002-08-16 21:14:40 +00001271ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001272 if (!$3->getType()->isFirstClassType())
1273 ThrowException("cast constant expression from a non-primitive type: '" +
1274 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001275 if (!$5->get()->isFirstClassType())
1276 ThrowException("cast constant expression to a non-primitive type: '" +
1277 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001278 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001279 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001280 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001281 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1282 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001283 ThrowException("GetElementPtr requires a pointer operand!");
1284
Chris Lattner830b6f92004-04-05 01:30:04 +00001285 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1286 // indices to uint struct indices for compatibility.
1287 generic_gep_type_iterator<std::vector<Value*>::iterator>
1288 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1289 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1290 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1291 if (isa<StructType>(*GTI)) // Only change struct indices
1292 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1293 if (CUI->getType() == Type::UByteTy)
1294 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1295
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001296 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001297 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001298 if (!IdxTy)
1299 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001300
Chris Lattner9232b992003-04-22 18:42:41 +00001301 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001302 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1303 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001304 IdxVec.push_back(C);
1305 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001306 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001307
Chris Lattnerd78700d2002-08-16 21:14:40 +00001308 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001309
Chris Lattnerd78700d2002-08-16 21:14:40 +00001310 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001311 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001312 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1313 if ($3->getType() != Type::BoolTy)
1314 ThrowException("Select condition must be of boolean type!");
1315 if ($5->getType() != $7->getType())
1316 ThrowException("Select operand types must match!");
1317 $$ = ConstantExpr::getSelect($3, $5, $7);
1318 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001319 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001320 if ($3->getType() != $5->getType())
1321 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001322 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001323 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001324 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001325 if ($5->getType() != Type::UByteTy)
1326 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001327 if (!$3->getType()->isInteger())
1328 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001329 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001330 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001331
1332
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001333// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001334ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001335 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001336 }
1337 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001338 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001339 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001340 };
Chris Lattner00950542001-06-06 20:29:01 +00001341
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001342
Chris Lattner1781aca2001-09-18 04:00:54 +00001343// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001344GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001345
Chris Lattner00950542001-06-06 20:29:01 +00001346
Chris Lattner0e73ce62002-05-02 19:11:13 +00001347//===----------------------------------------------------------------------===//
1348// Rules to match Modules
1349//===----------------------------------------------------------------------===//
1350
1351// Module rule: Capture the result of parsing the whole file into a result
1352// variable...
1353//
1354Module : FunctionList {
1355 $$ = ParserResult = $1;
1356 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001357};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001358
Chris Lattner7e708292002-06-25 16:13:24 +00001359// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001360//
1361FunctionList : FunctionList Function {
1362 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001363 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001364 }
1365 | FunctionList FunctionProto {
1366 $$ = $1;
1367 }
1368 | FunctionList IMPLEMENTATION {
1369 $$ = $1;
1370 }
1371 | ConstPool {
1372 $$ = CurModule.CurrentModule;
1373 // Resolve circular types before we parse the body of the module
1374 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001375 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001376
Chris Lattnere98dda62001-07-14 06:10:16 +00001377// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001378ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001379 if (!setValueName($4, $2))
1380 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001381 }
Chris Lattner30c89792001-09-07 16:35:17 +00001382 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001383 // Eagerly resolve types. This is not an optimization, this is a
1384 // requirement that is due to the fact that we could have this:
1385 //
1386 // %list = type { %list * }
1387 // %list = type { %list * } ; repeated type decl
1388 //
1389 // If types are not resolved eagerly, then the two types will not be
1390 // determined to be the same type!
1391 //
1392 ResolveTypeTo($2, $4->get());
1393
Chris Lattner1781aca2001-09-18 04:00:54 +00001394 // TODO: FIXME when Type are not const
Reid Spencer75719bf2004-05-26 21:48:31 +00001395 if (!setTypeName(const_cast<Type*>($4->get()), $2)) {
Chris Lattnerb7474512001-10-03 15:39:04 +00001396 // If this is not a redefinition of a type...
1397 if (!$2) {
1398 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001399 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001400 }
Chris Lattner30c89792001-09-07 16:35:17 +00001401 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001402
1403 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001404 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001405 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001406 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001407 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001408 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001409 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001410 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001411 if (Initializer == 0)
1412 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001413
Chris Lattnerdda71962001-11-26 18:54:16 +00001414 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001415 if (!setValueName(GV, $2)) { // If not redefining...
1416 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001417 int Slot = InsertValue(GV, CurModule.Values);
1418
1419 if (Slot != -1) {
1420 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1421 } else {
1422 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1423 (char*)GV->getName().c_str()));
1424 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001425 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001426 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001427 | ConstPool OptAssign EXTERNAL GlobalType Types {
1428 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001429 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001430 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001431 if (!setValueName(GV, $2)) { // If not redefining...
1432 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001433 int Slot = InsertValue(GV, CurModule.Values);
1434
1435 if (Slot != -1) {
1436 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1437 } else {
1438 assert(GV->hasName() && "Not named and not numbered!?");
1439 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1440 (char*)GV->getName().c_str()));
1441 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001442 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001443 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001444 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001445 | ConstPool TARGET TargetDefinition {
1446 }
Chris Lattner00950542001-06-06 20:29:01 +00001447 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001448 };
Chris Lattner00950542001-06-06 20:29:01 +00001449
1450
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001451
1452BigOrLittle : BIG { $$ = Module::BigEndian; };
1453BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1454
1455TargetDefinition : ENDIAN '=' BigOrLittle {
1456 CurModule.CurrentModule->setEndianness($3);
1457 }
1458 | POINTERSIZE '=' EUINT64VAL {
1459 if ($3 == 32)
1460 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1461 else if ($3 == 64)
1462 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1463 else
1464 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1465 };
1466
1467
Chris Lattner00950542001-06-06 20:29:01 +00001468//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001469// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001470//===----------------------------------------------------------------------===//
1471
Chris Lattner6c23f572003-08-22 05:42:10 +00001472Name : VAR_ID | STRINGCONSTANT;
1473OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001474
Chris Lattner6c23f572003-08-22 05:42:10 +00001475ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001476 if (*$1 == Type::VoidTy)
1477 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001478 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001479};
Chris Lattner00950542001-06-06 20:29:01 +00001480
Chris Lattner69da5cf2002-10-13 20:57:00 +00001481ArgListH : ArgListH ',' ArgVal {
1482 $$ = $1;
1483 $1->push_back(*$3);
1484 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001485 }
1486 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001487 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001488 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001489 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001490 };
Chris Lattner00950542001-06-06 20:29:01 +00001491
1492ArgList : ArgListH {
1493 $$ = $1;
1494 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001495 | ArgListH ',' DOTDOTDOT {
1496 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001497 $$->push_back(std::pair<PATypeHolder*,
1498 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001499 }
1500 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001501 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1502 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001503 }
Chris Lattner00950542001-06-06 20:29:01 +00001504 | /* empty */ {
1505 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001506 };
Chris Lattner00950542001-06-06 20:29:01 +00001507
Chris Lattner6c23f572003-08-22 05:42:10 +00001508FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001509 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001510 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001511
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001512 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1513 ThrowException("LLVM functions cannot return aggregate types!");
1514
Chris Lattner9232b992003-04-22 18:42:41 +00001515 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001516 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001517 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001518 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001519 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001520 }
Chris Lattner00950542001-06-06 20:29:01 +00001521
Chris Lattner2079fde2001-10-13 06:41:08 +00001522 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1523 if (isVarArg) ParamTypeList.pop_back();
1524
Chris Lattner1f862af2003-04-16 18:13:57 +00001525 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001526 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001527 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001528
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001529 Function *Fn = 0;
1530 // Is the function already in symtab?
1531 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1532 // Yes it is. If this is the case, either we need to be a forward decl,
1533 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001534 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001535 ThrowException("Redefinition of function '" + FunctionName + "'!");
1536
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001537 // Make sure to strip off any argument names so we can't get conflicts...
1538 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1539 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001540
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001541 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001542 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1543 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001544 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001545 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001546 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001547 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001548
Chris Lattner394f2eb2003-10-16 20:12:13 +00001549 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001550
Chris Lattner7e708292002-06-25 16:13:24 +00001551 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001552 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001553 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001554 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001555 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001556 delete $4->back().first;
1557 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001558 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001559 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001560 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001561 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001562 delete I->first; // Delete the typeholder...
1563
1564 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001565 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001566
Chris Lattner69da5cf2002-10-13 20:57:00 +00001567 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001568 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001569
Chris Lattner1f862af2003-04-16 18:13:57 +00001570 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001571 }
Chris Lattner51727be2002-06-04 21:58:56 +00001572};
Chris Lattner00950542001-06-06 20:29:01 +00001573
Chris Lattner9b02cc32002-05-03 18:23:48 +00001574BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1575
Chris Lattner4ad02e72003-04-16 20:28:45 +00001576FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001577 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001578
Chris Lattner4ad02e72003-04-16 20:28:45 +00001579 // Make sure that we keep track of the linkage type even if there was a
1580 // previous "declare".
1581 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001582
Chris Lattner7e708292002-06-25 16:13:24 +00001583 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001584 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001585};
Chris Lattner00950542001-06-06 20:29:01 +00001586
Chris Lattner9b02cc32002-05-03 18:23:48 +00001587END : ENDTOK | '}'; // Allow end of '}' to end a function
1588
Chris Lattner79df7c02002-03-26 18:01:55 +00001589Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001590 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001591};
Chris Lattner00950542001-06-06 20:29:01 +00001592
Chris Lattner394f2eb2003-10-16 20:12:13 +00001593FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1594 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001595 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001596};
Chris Lattner00950542001-06-06 20:29:01 +00001597
1598//===----------------------------------------------------------------------===//
1599// Rules to match Basic Blocks
1600//===----------------------------------------------------------------------===//
1601
1602ConstValueRef : ESINT64VAL { // A reference to a direct constant
1603 $$ = ValID::create($1);
1604 }
1605 | EUINT64VAL {
1606 $$ = ValID::create($1);
1607 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001608 | FPVAL { // Perhaps it's an FP constant?
1609 $$ = ValID::create($1);
1610 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001611 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001612 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001613 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001614 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001615 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001616 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001617 | NULL_TOK {
1618 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001619 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001620 | ConstExpr {
1621 $$ = ValID::create($1);
1622 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001623
Chris Lattner2079fde2001-10-13 06:41:08 +00001624// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1625// another value.
1626//
1627SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001628 $$ = ValID::create($1);
1629 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001630 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001631 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001632 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001633
1634// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001635ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001636
Chris Lattner00950542001-06-06 20:29:01 +00001637
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001638// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1639// type immediately preceeds the value reference, and allows complex constant
1640// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001641ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001642 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001643 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001644
Chris Lattner00950542001-06-06 20:29:01 +00001645BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001646 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001647 }
Chris Lattner7e708292002-06-25 16:13:24 +00001648 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1649 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001650 };
Chris Lattner00950542001-06-06 20:29:01 +00001651
1652
1653// Basic blocks are terminated by branching instructions:
1654// br, br/cc, switch, ret
1655//
Chris Lattner2079fde2001-10-13 06:41:08 +00001656BasicBlock : InstructionList OptAssign BBTerminatorInst {
1657 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1658 InsertValue($3);
1659
1660 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001661 InsertValue($1);
1662 $$ = $1;
1663 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001664 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1665 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1666 InsertValue($4);
1667
1668 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001669 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001670
1671 InsertValue($2);
1672 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001673 };
Chris Lattner00950542001-06-06 20:29:01 +00001674
1675InstructionList : InstructionList Inst {
1676 $1->getInstList().push_back($2);
1677 $$ = $1;
1678 }
1679 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001680 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001681 };
Chris Lattner00950542001-06-06 20:29:01 +00001682
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001683BBTerminatorInst : RET ResolvedVal { // Return with a result...
1684 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001685 }
1686 | RET VOID { // Return with no result...
1687 $$ = new ReturnInst();
1688 }
1689 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001690 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001691 } // Conditional Branch...
1692 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001693 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1694 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001695 getVal(Type::BoolTy, $3));
1696 }
1697 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1698 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001699 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001700 $$ = S;
1701
Chris Lattner9232b992003-04-22 18:42:41 +00001702 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001703 E = $8->end();
1704 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001705 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001706 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001707 }
Chris Lattner196850c2003-05-15 21:30:00 +00001708 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1709 SwitchInst *S = new SwitchInst(getVal($2, $3),
1710 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1711 $$ = S;
1712 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001713 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001714 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001715 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001716 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001717
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001718 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1719 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001720 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001721 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001722 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001723 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1724 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001725 ParamTypes.push_back((*I)->getType());
1726 }
1727
1728 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1729 if (isVarArg) ParamTypes.pop_back();
1730
Chris Lattner79df7c02002-03-26 18:01:55 +00001731 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001732 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001733 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001734
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001735 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001736
1737 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1738 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1739
1740 if (Normal == 0 || Except == 0)
1741 ThrowException("Invoke instruction without label destinations!");
1742
1743 // Create the call node...
1744 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001745 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001746 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001747 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001748 // correctly!
1749 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001750 FunctionType::param_iterator I = Ty->param_begin();
1751 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001752 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001753
1754 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1755 if ((*ArgI)->getType() != *I)
1756 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001757 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001758
1759 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1760 ThrowException("Invalid number of parameters detected!");
1761
Chris Lattner6cdb0112001-11-26 16:54:11 +00001762 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001763 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001764 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001765 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001766 }
1767 | UNWIND {
1768 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001769 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001770
1771
Chris Lattner00950542001-06-06 20:29:01 +00001772
1773JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1774 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001775 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001776 if (V == 0)
1777 ThrowException("May only switch on a constant pool value!");
1778
Chris Lattner9232b992003-04-22 18:42:41 +00001779 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001780 }
1781 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001782 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001783 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001784
1785 if (V == 0)
1786 ThrowException("May only switch on a constant pool value!");
1787
Chris Lattner9232b992003-04-22 18:42:41 +00001788 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001789 };
Chris Lattner00950542001-06-06 20:29:01 +00001790
1791Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001792 // Is this definition named?? if so, assign the name...
1793 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001794 InsertValue($2);
1795 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001796};
Chris Lattner00950542001-06-06 20:29:01 +00001797
Chris Lattnerc24d2082001-06-11 15:04:20 +00001798PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001799 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1800 $$->push_back(std::make_pair(getVal(*$1, $3),
1801 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001802 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001803 }
1804 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1805 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001806 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1807 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001808 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001809
1810
Chris Lattner30c89792001-09-07 16:35:17 +00001811ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001812 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001813 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001814 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001815 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001816 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001817 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001818 };
Chris Lattner00950542001-06-06 20:29:01 +00001819
1820// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001821ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001822
Chris Lattner4a6482b2002-09-10 19:57:26 +00001823InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1824 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1825 ThrowException("Arithmetic operator requires integer or FP operands!");
1826 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1827 if ($$ == 0)
1828 ThrowException("binary operator returned null!");
1829 delete $2;
1830 }
1831 | LogicalOps Types ValueRef ',' ValueRef {
1832 if (!(*$2)->isIntegral())
1833 ThrowException("Logical operator requires integral operands!");
1834 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1835 if ($$ == 0)
1836 ThrowException("binary operator returned null!");
1837 delete $2;
1838 }
1839 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001840 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001841 if ($$ == 0)
1842 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001843 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001844 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001845 | NOT ResolvedVal {
1846 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1847 << " Replacing with 'xor'.\n";
1848
1849 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1850 if (Ones == 0)
1851 ThrowException("Expected integral type for not instruction!");
1852
1853 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001854 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001855 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001856 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001857 | ShiftOps ResolvedVal ',' ResolvedVal {
1858 if ($4->getType() != Type::UByteTy)
1859 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001860 if (!$2->getType()->isInteger())
1861 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001862 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001863 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001864 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001865 if (!$4->get()->isFirstClassType())
1866 ThrowException("cast instruction to a non-primitive type: '" +
1867 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001868 $$ = new CastInst($2, *$4);
1869 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001870 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001871 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1872 if ($2->getType() != Type::BoolTy)
1873 ThrowException("select condition must be boolean!");
1874 if ($4->getType() != $6->getType())
1875 ThrowException("select value types should match!");
1876 $$ = new SelectInst($2, $4, $6);
1877 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001878 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001879 // FIXME: This is emulation code for an obsolete syntax. This should be
1880 // removed at some point.
1881 if (!ObsoleteVarArgs) {
1882 std::cerr << "WARNING: this file uses obsolete features. "
1883 << "Assemble and disassemble to update it.\n";
1884 ObsoleteVarArgs = true;
1885 }
1886
1887 // First, load the valist...
1888 Instruction *CurVAList = new LoadInst($2, "");
1889 CurBB->getInstList().push_back(CurVAList);
1890
1891 // Emit the vaarg instruction.
1892 $$ = new VAArgInst(CurVAList, *$4);
1893
1894 // Now we must advance the pointer and update it in memory.
1895 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1896 CurBB->getInstList().push_back(TheVANext);
1897
1898 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1899 delete $4;
1900 }
1901 | VAARG ResolvedVal ',' Types {
1902 $$ = new VAArgInst($2, *$4);
1903 delete $4;
1904 }
1905 | VANEXT ResolvedVal ',' Types {
1906 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001907 delete $4;
1908 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001909 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001910 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001911 if (!Ty->isFirstClassType())
1912 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001913 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001914 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001915 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001916 if ($2->front().first->getType() != Ty)
1917 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001918 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001919 $2->pop_front();
1920 }
1921 delete $2; // Free the list...
1922 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001923 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001924 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001925 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001926
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001927 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1928 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001929 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001930 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001931 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001932 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1933 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001934 ParamTypes.push_back((*I)->getType());
1935 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001936
1937 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1938 if (isVarArg) ParamTypes.pop_back();
1939
Chris Lattner79df7c02002-03-26 18:01:55 +00001940 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001941 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001942 }
Chris Lattner00950542001-06-06 20:29:01 +00001943
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001944 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001945
Chris Lattner8b81bf52001-07-25 22:47:46 +00001946 // Create the call node...
1947 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001948 // Make sure no arguments is a good thing!
1949 if (Ty->getNumParams() != 0)
1950 ThrowException("No arguments passed to a function that "
1951 "expects arguments!");
1952
Chris Lattner9232b992003-04-22 18:42:41 +00001953 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001954 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001955 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001956 // correctly!
1957 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001958 FunctionType::param_iterator I = Ty->param_begin();
1959 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001960 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001961
1962 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1963 if ((*ArgI)->getType() != *I)
1964 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001965 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001966
Chris Lattner8b81bf52001-07-25 22:47:46 +00001967 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001968 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001969
Chris Lattner6cdb0112001-11-26 16:54:11 +00001970 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001971 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001972 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001973 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001974 }
1975 | MemoryInst {
1976 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001977 };
Chris Lattner00950542001-06-06 20:29:01 +00001978
Chris Lattner6cdb0112001-11-26 16:54:11 +00001979
1980// IndexList - List of indices for GEP based instructions...
1981IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001982 $$ = $2;
1983 } | /* empty */ {
1984 $$ = new std::vector<Value*>();
1985 };
1986
1987OptVolatile : VOLATILE {
1988 $$ = true;
1989 }
1990 | /* empty */ {
1991 $$ = false;
1992 };
1993
Chris Lattner027dcc52001-07-08 21:10:27 +00001994
Chris Lattner00950542001-06-06 20:29:01 +00001995MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001996 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001997 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001998 }
1999 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002000 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002001 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002002 }
2003 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00002004 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00002005 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002006 }
2007 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00002008 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002009 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002010 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002011 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002012 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002013 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002014 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002015 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002016 }
2017
Chris Lattner15c9c032003-09-08 18:20:29 +00002018 | OptVolatile LOAD Types ValueRef {
2019 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002020 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002021 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002022 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002023 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002024 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002025 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2026 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002027 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002028 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002029 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002030 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002031 if (ElTy != $3->getType())
2032 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002033 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002034
Chris Lattner79ad13802003-09-08 20:29:46 +00002035 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002036 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002037 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002038 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002039 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002040 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002041
2042 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2043 // indices to uint struct indices for compatibility.
2044 generic_gep_type_iterator<std::vector<Value*>::iterator>
2045 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2046 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2047 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2048 if (isa<StructType>(*GTI)) // Only change struct indices
2049 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2050 if (CUI->getType() == Type::UByteTy)
2051 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2052
Chris Lattner30c89792001-09-07 16:35:17 +00002053 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002054 ThrowException("Invalid getelementptr indices for type '" +
2055 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002056 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2057 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002058 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002059
Brian Gaeked0fde302003-11-11 22:41:34 +00002060
Chris Lattner00950542001-06-06 20:29:01 +00002061%%
Chris Lattner09083092001-07-08 04:57:15 +00002062int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002063 std::string where
2064 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002065 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002066 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002067 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002068 errMsg += "end-of-file.";
2069 else
Chris Lattner9232b992003-04-22 18:42:41 +00002070 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002071 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002072 return 0;
2073}