blob: 710c674a77109d4e249023aa5be085a8794ff176 [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 {
Chris Lattner86427632004-07-14 06:39:48 +000034 std::string CurFilename;
35}
36using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000037
Chris Lattner00950542001-06-06 20:29:01 +000038static Module *ParserResult;
Chris Lattner00950542001-06-06 20:29:01 +000039
Chris Lattner30c89792001-09-07 16:35:17 +000040// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
41// relating to upreferences in the input stream.
42//
43//#define DEBUG_UPREFS 1
44#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000045#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000046#else
47#define UR_OUT(X)
48#endif
49
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000050#define YYERROR_VERBOSE 1
51
Chris Lattner99e7ab72003-10-18 05:53:13 +000052// HACK ALERT: This variable is used to implement the automatic conversion of
53// variable argument instructions from their old to new forms. When this
54// compatiblity "Feature" is removed, this should be too.
55//
56static BasicBlock *CurBB;
57static bool ObsoleteVarArgs;
58
59
Chris Lattner7e708292002-06-25 16:13:24 +000060// This contains info used when building the body of a function. It is
61// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000062//
Chris Lattner9232b992003-04-22 18:42:41 +000063typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner735f2702004-07-08 22:30:50 +000064static void ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers,
65 std::map<const Type *,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000066
67static struct PerModuleInfo {
68 Module *CurrentModule;
Chris Lattner735f2702004-07-08 22:30:50 +000069 std::map<const Type *, ValueList> Values; // Module level numbered definitions
70 std::map<const Type *,ValueList> LateResolveValues;
Chris Lattner16af11d2004-02-09 21:03:38 +000071 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000072 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000073
Chris Lattnerbc721ed2004-07-13 08:28:21 +000074 /// PlaceHolderInfo - When temporary placeholder objects are created, remember
75 /// how they were referenced and one which line of the input they came from so
Chris Lattner22ae2322004-07-13 08:39:15 +000076 /// that we can resolve them later and print error messages as appropriate.
Chris Lattnerbc721ed2004-07-13 08:28:21 +000077 std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo;
78
Chris Lattner2079fde2001-10-13 06:41:08 +000079 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
80 // references to global values. Global values may be referenced before they
81 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000082 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000083 //
Chris Lattner9232b992003-04-22 18:42:41 +000084 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000085 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000086 GlobalRefsType GlobalRefs;
87
Chris Lattner00950542001-06-06 20:29:01 +000088 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000089 // If we could not resolve some functions at function compilation time
90 // (calls to functions before they are defined), resolve them now... Types
91 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000092 //
Chris Lattner00950542001-06-06 20:29:01 +000093 ResolveDefinitions(LateResolveValues);
94
Chris Lattner2079fde2001-10-13 06:41:08 +000095 // Check to make sure that all global value forward references have been
96 // resolved!
97 //
98 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000099 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +0000100
101 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
102 I != E; ++I) {
103 UndefinedReferences += " " + I->first.first->getDescription() + " " +
104 I->first.second.getName() + "\n";
105 }
106 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +0000107 }
108
Chris Lattner7e708292002-06-25 16:13:24 +0000109 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000110 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000111 CurrentModule = 0;
112 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000113
114
Chris Lattner8a327842004-07-14 21:44:00 +0000115 // GetForwardRefForGlobal - Check to see if there is a forward reference
116 // for this global. If so, remove it from the GlobalRefs map and return it.
117 // If not, just return null.
118 GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) {
119 // Check to see if there is a forward reference to this global variable...
120 // if there is, eliminate it and patch the reference to use the new def'n.
121 GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID));
122 GlobalValue *Ret = 0;
123 if (I != GlobalRefs.end()) {
124 Ret = I->second;
125 GlobalRefs.erase(I);
126 }
127 return Ret;
128 }
Chris Lattner00950542001-06-06 20:29:01 +0000129} CurModule;
130
Chris Lattner79df7c02002-03-26 18:01:55 +0000131static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000132 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000133
Chris Lattner735f2702004-07-08 22:30:50 +0000134 std::map<const Type*, ValueList> Values; // Keep track of #'d definitions
135 std::map<const Type*, ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000136 std::vector<PATypeHolder> Types;
137 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner7e708292002-06-25 16:13:24 +0000138 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000139
Chris Lattner2e2ed292004-07-14 06:28:35 +0000140 /// BBForwardRefs - When we see forward references to basic blocks, keep
141 /// track of them here.
142 std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs;
143 std::vector<BasicBlock*> NumberedBlocks;
144 unsigned NextBBNum;
145
Chris Lattner79df7c02002-03-26 18:01:55 +0000146 inline PerFunctionInfo() {
147 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000148 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000149 }
150
Chris Lattner79df7c02002-03-26 18:01:55 +0000151 inline void FunctionStart(Function *M) {
152 CurrentFunction = M;
Chris Lattner2e2ed292004-07-14 06:28:35 +0000153 NextBBNum = 0;
Chris Lattner00950542001-06-06 20:29:01 +0000154 }
155
Chris Lattner79df7c02002-03-26 18:01:55 +0000156 void FunctionDone() {
Chris Lattner2e2ed292004-07-14 06:28:35 +0000157 NumberedBlocks.clear();
158
159 // Any forward referenced blocks left?
160 if (!BBForwardRefs.empty())
161 ThrowException("Undefined reference to label " +
162 BBForwardRefs.begin()->second.first.getName());
163
164 // Resolve all forward references now.
Chris Lattner386a3b72001-10-16 19:54:17 +0000165 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000166
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000167 // Make sure to resolve any constant expr references that might exist within
168 // the function we just declared itself.
169 ValID FID;
170 if (CurrentFunction->hasName()) {
171 FID = ValID::create((char*)CurrentFunction->getName().c_str());
172 } else {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000173 // Figure out which slot number if is...
Chris Lattner735f2702004-07-08 22:30:50 +0000174 ValueList &List = CurModule.Values[CurrentFunction->getType()];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000175 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000176 assert(i < List.size() && "Function not found!");
177 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000178 FID = ValID::create((int)i);
179 break;
180 }
181 }
182 }
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000183
Chris Lattner7e708292002-06-25 16:13:24 +0000184 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000185 Types.clear(); // Clear out function local types
Chris Lattner79df7c02002-03-26 18:01:55 +0000186 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000187 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000188 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000189} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000190
Chris Lattner394f2eb2003-10-16 20:12:13 +0000191static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000192
Chris Lattner00950542001-06-06 20:29:01 +0000193
194//===----------------------------------------------------------------------===//
195// Code to handle definitions of all the types
196//===----------------------------------------------------------------------===//
197
Chris Lattner735f2702004-07-08 22:30:50 +0000198static int InsertValue(Value *V,
199 std::map<const Type*,ValueList> &ValueTab = CurFun.Values) {
200 if (V->hasName()) return -1; // Is this a numbered definition?
Chris Lattner2079fde2001-10-13 06:41:08 +0000201
202 // Yes, insert the value into the value table...
Chris Lattner735f2702004-07-08 22:30:50 +0000203 ValueList &List = ValueTab[V->getType()];
204 List.push_back(V);
Chris Lattner16af11d2004-02-09 21:03:38 +0000205 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000206}
207
Chris Lattner30c89792001-09-07 16:35:17 +0000208static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000209 switch (D.Type) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000210 case ValID::NumberVal: // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000211 // Module constants occupy the lowest numbered slots...
Chris Lattnera09000d2004-07-14 23:03:46 +0000212 if ((unsigned)D.Num < CurModule.Types.size())
213 return CurModule.Types[(unsigned)D.Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000214 break;
Chris Lattnera09000d2004-07-14 23:03:46 +0000215 case ValID::NameVal: // Is it a named definition?
216 if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) {
217 D.destroy(); // Free old strdup'd memory...
218 return N;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000219 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000220 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000221 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000222 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000223 }
224
225 // If we reached here, we referenced either a symbol that we don't know about
226 // or an id number that hasn't been read yet. We may be referencing something
227 // forward, so just create an entry to be resolved later and get to it...
228 //
229 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
230
Chris Lattner9232b992003-04-22 18:42:41 +0000231 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000232 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000233
Chris Lattner9232b992003-04-22 18:42:41 +0000234 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000235 if (I != LateResolver.end()) {
236 return I->second;
237 }
Chris Lattner30c89792001-09-07 16:35:17 +0000238
Chris Lattner82269592001-10-22 06:01:08 +0000239 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000240 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000241 return Typ;
242}
243
Chris Lattner9232b992003-04-22 18:42:41 +0000244static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000245 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000246 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000247 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000248 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000249}
250
Chris Lattner2079fde2001-10-13 06:41:08 +0000251// getValNonImprovising - Look up the value specified by the provided type and
252// the provided ValID. If the value exists and has already been defined, return
253// it. Otherwise return null.
254//
255static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000256 if (isa<FunctionType>(Ty))
257 ThrowException("Functions are not values and "
258 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000259
Chris Lattner30c89792001-09-07 16:35:17 +0000260 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000261 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner00950542001-06-06 20:29:01 +0000262 unsigned Num = (unsigned)D.Num;
263
264 // Module constants occupy the lowest numbered slots...
Chris Lattner735f2702004-07-08 22:30:50 +0000265 std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000266 if (VI != CurModule.Values.end()) {
267 if (Num < VI->second.size())
268 return VI->second[Num];
269 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000270 }
271
272 // Make sure that our type is within bounds
Chris Lattner735f2702004-07-08 22:30:50 +0000273 VI = CurFun.Values.find(Ty);
Chris Lattner16af11d2004-02-09 21:03:38 +0000274 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000275
276 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000277 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000278
Chris Lattner16af11d2004-02-09 21:03:38 +0000279 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000280 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000281
Chris Lattner1a1cb112001-09-30 22:46:54 +0000282 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000283 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000284 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000285
286 D.destroy(); // Free old strdup'd memory...
287 return N;
288 }
289
Chris Lattner2079fde2001-10-13 06:41:08 +0000290 // Check to make sure that "Ty" is an integral type, and that our
291 // value will fit into the specified type...
292 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000293 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
294 ThrowException("Signed integral constant '" +
295 itostr(D.ConstPool64) + "' is invalid for type '" +
296 Ty->getDescription() + "'!");
297 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000298
299 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000300 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
301 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000302 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
303 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000304 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000305 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000306 }
307 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000308 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000309 }
310
Chris Lattner2079fde2001-10-13 06:41:08 +0000311 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000312 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000313 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000314 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000315
316 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000317 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000318 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000319 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000320
Chris Lattnerd78700d2002-08-16 21:14:40 +0000321 case ValID::ConstantVal: // Fully resolved constant?
322 if (D.ConstantValue->getType() != Ty)
323 ThrowException("Constant expression type different from required type!");
324 return D.ConstantValue;
325
Chris Lattner30c89792001-09-07 16:35:17 +0000326 default:
327 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000328 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000329 } // End of switch
330
Chris Lattner2079fde2001-10-13 06:41:08 +0000331 assert(0 && "Unhandled case!");
332 return 0;
333}
334
Chris Lattner2079fde2001-10-13 06:41:08 +0000335// getVal - This function is identical to getValNonImprovising, except that if a
336// value is not already defined, it "improvises" by creating a placeholder var
337// that looks and acts just like the requested variable. When the value is
338// defined later, all uses of the placeholder variable are replaced with the
339// real thing.
340//
Chris Lattner64116f92004-07-14 01:33:11 +0000341static Value *getVal(const Type *Ty, const ValID &ID) {
342 if (Ty == Type::LabelTy)
343 ThrowException("Cannot use a basic block here");
Chris Lattner2079fde2001-10-13 06:41:08 +0000344
Chris Lattner64116f92004-07-14 01:33:11 +0000345 // See if the value has already been defined.
346 Value *V = getValNonImprovising(Ty, ID);
Chris Lattner2079fde2001-10-13 06:41:08 +0000347 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000348
349 // If we reached here, we referenced either a symbol that we don't know about
350 // or an id number that hasn't been read yet. We may be referencing something
351 // forward, so just create an entry to be resolved later and get to it...
352 //
Chris Lattner64116f92004-07-14 01:33:11 +0000353 V = new Argument(Ty);
Chris Lattner00950542001-06-06 20:29:01 +0000354
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000355 // Remember where this forward reference came from. FIXME, shouldn't we try
356 // to recycle these things??
Chris Lattner64116f92004-07-14 01:33:11 +0000357 CurModule.PlaceHolderInfo.insert(std::make_pair(V, std::make_pair(ID,
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000358 llvmAsmlineno)));
359
Chris Lattner79df7c02002-03-26 18:01:55 +0000360 if (inFunctionScope())
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000361 InsertValue(V, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000362 else
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000363 InsertValue(V, CurModule.LateResolveValues);
364 return V;
Chris Lattner00950542001-06-06 20:29:01 +0000365}
366
Chris Lattner2e2ed292004-07-14 06:28:35 +0000367/// getBBVal - This is used for two purposes:
368/// * If isDefinition is true, a new basic block with the specified ID is being
369/// defined.
370/// * If isDefinition is true, this is a reference to a basic block, which may
371/// or may not be a forward reference.
372///
373static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) {
Chris Lattner64116f92004-07-14 01:33:11 +0000374 assert(inFunctionScope() && "Can't get basic block at global scope!");
375
Chris Lattner2e2ed292004-07-14 06:28:35 +0000376 std::string Name;
377 BasicBlock *BB = 0;
378 switch (ID.Type) {
379 default: ThrowException("Illegal label reference " + ID.getName());
380 case ValID::NumberVal: // Is it a numbered definition?
381 if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size())
382 CurFun.NumberedBlocks.resize(ID.Num+1);
383 BB = CurFun.NumberedBlocks[ID.Num];
384 break;
385 case ValID::NameVal: // Is it a named definition?
386 Name = ID.Name;
Chris Lattnera09000d2004-07-14 23:03:46 +0000387 if (Value *N = CurFun.CurrentFunction->
388 getSymbolTable().lookup(Type::LabelTy, Name))
Chris Lattner2e2ed292004-07-14 06:28:35 +0000389 BB = cast<BasicBlock>(N);
390 break;
391 }
Chris Lattner64116f92004-07-14 01:33:11 +0000392
Chris Lattner2e2ed292004-07-14 06:28:35 +0000393 // See if the block has already been defined.
394 if (BB) {
395 // If this is the definition of the block, make sure the existing value was
396 // just a forward reference. If it was a forward reference, there will be
397 // an entry for it in the PlaceHolderInfo map.
398 if (isDefinition && !CurFun.BBForwardRefs.erase(BB))
399 // The existing value was a definition, not a forward reference.
400 ThrowException("Redefinition of label " + ID.getName());
Chris Lattner64116f92004-07-14 01:33:11 +0000401
Chris Lattner2e2ed292004-07-14 06:28:35 +0000402 ID.destroy(); // Free strdup'd memory.
403 return BB;
404 }
405
406 // Otherwise this block has not been seen before.
407 BB = new BasicBlock("", CurFun.CurrentFunction);
408 if (ID.Type == ValID::NameVal) {
409 BB->setName(ID.Name);
410 } else {
411 CurFun.NumberedBlocks[ID.Num] = BB;
412 }
413
414 // If this is not a definition, keep track of it so we can use it as a forward
415 // reference.
416 if (!isDefinition) {
417 // Remember where this forward reference came from.
418 CurFun.BBForwardRefs[BB] = std::make_pair(ID, llvmAsmlineno);
419 } else {
420 // The forward declaration could have been inserted anywhere in the
421 // function: insert it into the correct place now.
422 CurFun.CurrentFunction->getBasicBlockList().remove(BB);
423 CurFun.CurrentFunction->getBasicBlockList().push_back(BB);
424 }
425
Chris Lattner64116f92004-07-14 01:33:11 +0000426 return BB;
427}
428
Chris Lattner00950542001-06-06 20:29:01 +0000429
430//===----------------------------------------------------------------------===//
431// Code to handle forward references in instructions
432//===----------------------------------------------------------------------===//
433//
434// This code handles the late binding needed with statements that reference
435// values not defined yet... for example, a forward branch, or the PHI node for
436// a loop body.
437//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000438// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000439// and back patchs after we are done.
440//
441
442// ResolveDefinitions - If we could not resolve some defs at parsing
443// time (forward branches, phi functions for loops, etc...) resolve the
444// defs now...
445//
Chris Lattner735f2702004-07-08 22:30:50 +0000446static void ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers,
447 std::map<const Type*,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000448 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner735f2702004-07-08 22:30:50 +0000449 for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(),
Chris Lattner16af11d2004-02-09 21:03:38 +0000450 E = LateResolvers.end(); LRI != E; ++LRI) {
451 ValueList &List = LRI->second;
452 while (!List.empty()) {
453 Value *V = List.back();
454 List.pop_back();
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000455
456 std::map<Value*, std::pair<ValID, int> >::iterator PHI =
457 CurModule.PlaceHolderInfo.find(V);
458 assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error!");
459
460 ValID &DID = PHI->second.first;
Chris Lattner00950542001-06-06 20:29:01 +0000461
Chris Lattner735f2702004-07-08 22:30:50 +0000462 Value *TheRealValue = getValNonImprovising(LRI->first, DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000463 if (TheRealValue) {
464 V->replaceAllUsesWith(TheRealValue);
465 delete V;
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000466 CurModule.PlaceHolderInfo.erase(PHI);
Chris Lattner386a3b72001-10-16 19:54:17 +0000467 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000468 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000469 // resolver table
470 InsertValue(V, *FutureLateResolvers);
471 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000472 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000473 ThrowException("Reference to an invalid definition: '" +DID.getName()+
474 "' of type '" + V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000475 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000476 else
477 ThrowException("Reference to an invalid definition: #" +
478 itostr(DID.Num) + " of type '" +
479 V->getType()->getDescription() + "'",
Chris Lattnerbc721ed2004-07-13 08:28:21 +0000480 PHI->second.second);
Chris Lattner30c89792001-09-07 16:35:17 +0000481 }
Chris Lattner00950542001-06-06 20:29:01 +0000482 }
483 }
484
485 LateResolvers.clear();
486}
487
Chris Lattner4a42e902001-10-22 05:56:09 +0000488// ResolveTypeTo - A brand new type was just declared. This means that (if
489// name is not null) things referencing Name can be resolved. Otherwise, things
490// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000491//
Chris Lattner4a42e902001-10-22 05:56:09 +0000492static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000493 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000494 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000495
Chris Lattner4a42e902001-10-22 05:56:09 +0000496 ValID D;
497 if (Name) D = ValID::create(Name);
498 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000499
Chris Lattner9232b992003-04-22 18:42:41 +0000500 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000501 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000502
Chris Lattner9232b992003-04-22 18:42:41 +0000503 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000504 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000505 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000506 LateResolver.erase(I);
507 }
508}
509
510// ResolveTypes - At this point, all types should be resolved. Any that aren't
511// are errors.
512//
Chris Lattner9232b992003-04-22 18:42:41 +0000513static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000514 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000515 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000516
517 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000518 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000519 else
Chris Lattner82269592001-10-22 06:01:08 +0000520 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000521 }
522}
523
Chris Lattner1781aca2001-09-18 04:00:54 +0000524// setValueName - Set the specified value to the name given. The name may be
525// null potentially, in which case this is a noop. The string passed in is
Chris Lattner8ab406d2004-07-13 07:59:27 +0000526// assumed to be a malloc'd string buffer, and is free'd by this function.
527//
528static void setValueName(Value *V, char *NameStr) {
529 if (NameStr) {
530 std::string Name(NameStr); // Copy string
531 free(NameStr); // Free old string
Chris Lattnerc9aea522004-07-13 08:12:39 +0000532
533 if (V->getType() == Type::VoidTy)
534 ThrowException("Can't assign name '" + Name+"' to value with void type!");
Chris Lattner8ab406d2004-07-13 07:59:27 +0000535
536 assert(inFunctionScope() && "Must be in function scope!");
537 SymbolTable &ST = CurFun.CurrentFunction->getSymbolTable();
538 if (ST.lookup(V->getType(), Name))
539 ThrowException("Redefinition of value named '" + Name + "' in the '" +
540 V->getType()->getDescription() + "' type plane!");
541
Chris Lattnerc9aea522004-07-13 08:12:39 +0000542 // Set the name.
543 V->setName(Name, &ST);
Chris Lattner8ab406d2004-07-13 07:59:27 +0000544 }
545}
546
Chris Lattnerd88998d2004-07-14 08:23:52 +0000547/// ParseGlobalVariable - Handle parsing of a global. If Initializer is null,
548/// this is a declaration, otherwise it is a definition.
549static void ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage,
550 bool isConstantGlobal, const Type *Ty,
551 Constant *Initializer) {
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000552 if (isa<FunctionType>(Ty))
553 ThrowException("Cannot declare global vars of function type!");
554
Chris Lattnera09000d2004-07-14 23:03:46 +0000555 const PointerType *PTy = PointerType::get(Ty);
556
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000557 std::string Name;
558 if (NameStr) {
559 Name = NameStr; // Copy string
560 free(NameStr); // Free old string
Chris Lattnerd88998d2004-07-14 08:23:52 +0000561 }
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000562
Chris Lattner8a327842004-07-14 21:44:00 +0000563 // See if this global value was forward referenced. If so, recycle the
564 // object.
565 ValID ID;
566 if (!Name.empty()) {
567 ID = ValID::create((char*)Name.c_str());
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000568 } else {
Chris Lattner8a327842004-07-14 21:44:00 +0000569 ID = ValID::create((int)CurModule.Values[PTy].size());
570 }
571
572 if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) {
573 // Move the global to the end of the list, from whereever it was
574 // previously inserted.
575 GlobalVariable *GV = cast<GlobalVariable>(FWGV);
576 CurModule.CurrentModule->getGlobalList().remove(GV);
577 CurModule.CurrentModule->getGlobalList().push_back(GV);
578 GV->setInitializer(Initializer);
579 GV->setLinkage(Linkage);
580 GV->setConstant(isConstantGlobal);
Chris Lattnera09000d2004-07-14 23:03:46 +0000581 return;
Chris Lattnercb9d6a82004-07-14 20:42:57 +0000582 }
Chris Lattnera09000d2004-07-14 23:03:46 +0000583
584 // If this global has a name, check to see if there is already a definition
585 // of this global in the module. If so, merge as appropriate. Note that
586 // this is really just a hack around problems in the CFE. :(
587 if (!Name.empty()) {
588 // We are a simple redefinition of a value, check to see if it is defined
589 // the same as the old one.
590 if (GlobalVariable *EGV =
591 CurModule.CurrentModule->getGlobalVariable(Name, Ty)) {
592 // We are allowed to redefine a global variable in two circumstances:
593 // 1. If at least one of the globals is uninitialized or
594 // 2. If both initializers have the same value.
595 //
596 if (!EGV->hasInitializer() || !Initializer ||
597 EGV->getInitializer() == Initializer) {
598
599 // Make sure the existing global version gets the initializer! Make
600 // sure that it also gets marked const if the new version is.
601 if (Initializer && !EGV->hasInitializer())
602 EGV->setInitializer(Initializer);
603 if (isConstantGlobal)
604 EGV->setConstant(true);
605 EGV->setLinkage(Linkage);
606 return;
607 }
608
609 ThrowException("Redefinition of global variable named '" + Name +
610 "' in the '" + Ty->getDescription() + "' type plane!");
611 }
612 }
613
614 // Otherwise there is no existing GV to use, create one now.
615 new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name,
616 CurModule.CurrentModule);
Chris Lattnerd88998d2004-07-14 08:23:52 +0000617}
Chris Lattner8ab406d2004-07-13 07:59:27 +0000618
Reid Spencer75719bf2004-05-26 21:48:31 +0000619// setTypeName - Set the specified type to the name given. The name may be
620// null potentially, in which case this is a noop. The string passed in is
621// assumed to be a malloc'd string buffer, and is freed by this function.
622//
623// This function returns true if the type has already been defined, but is
624// allowed to be redefined in the specified context. If the name is a new name
625// for the type plane, it is inserted and false is returned.
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000626static bool setTypeName(const Type *T, char *NameStr) {
Chris Lattnera09000d2004-07-14 23:03:46 +0000627 assert(!inFunctionScope() && "Can't give types function-local names!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000628 if (NameStr == 0) return false;
629
630 std::string Name(NameStr); // Copy string
631 free(NameStr); // Free old string
632
633 // We don't allow assigning names to void type
634 if (T == Type::VoidTy)
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000635 ThrowException("Can't assign name '" + Name + "' to the void type!");
Reid Spencer75719bf2004-05-26 21:48:31 +0000636
Chris Lattnera09000d2004-07-14 23:03:46 +0000637 // Set the type name, checking for conflicts as we do so.
638 bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000639
Chris Lattnera09000d2004-07-14 23:03:46 +0000640 if (AlreadyExists) { // Inserting a name that is already defined???
641 const Type *Existing = CurModule.CurrentModule->getTypeByName(Name);
642 assert(Existing && "Conflict but no matching type?");
643
Reid Spencer75719bf2004-05-26 21:48:31 +0000644 // There is only one case where this is allowed: when we are refining an
645 // opaque type. In this case, Existing will be an opaque type.
646 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) {
647 // We ARE replacing an opaque type!
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000648 const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(T);
Reid Spencer75719bf2004-05-26 21:48:31 +0000649 return true;
650 }
651
652 // Otherwise, this is an attempt to redefine a type. That's okay if
653 // the redefinition is identical to the original. This will be so if
654 // Existing and T point to the same Type object. In this one case we
655 // allow the equivalent redefinition.
656 if (Existing == T) return true; // Yes, it's equal.
657
658 // Any other kind of (non-equivalent) redefinition is an error.
659 ThrowException("Redefinition of type named '" + Name + "' in the '" +
660 T->getDescription() + "' type plane!");
661 }
662
Reid Spencer75719bf2004-05-26 21:48:31 +0000663 return false;
664}
Chris Lattner8896eda2001-07-09 19:38:36 +0000665
Chris Lattner30c89792001-09-07 16:35:17 +0000666//===----------------------------------------------------------------------===//
667// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000668//
Chris Lattner8896eda2001-07-09 19:38:36 +0000669
Chris Lattner3b073862004-02-09 03:19:29 +0000670// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000671//
672static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000673 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
674}
675
676namespace {
677 struct UpRefRecord {
678 // NestingLevel - The number of nesting levels that need to be popped before
679 // this type is resolved.
680 unsigned NestingLevel;
681
682 // LastContainedTy - This is the type at the current binding level for the
683 // type. Every time we reduce the nesting level, this gets updated.
684 const Type *LastContainedTy;
685
686 // UpRefTy - This is the actual opaque type that the upreference is
687 // represented with.
688 OpaqueType *UpRefTy;
689
690 UpRefRecord(unsigned NL, OpaqueType *URTy)
691 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
692 };
Chris Lattner30c89792001-09-07 16:35:17 +0000693}
Chris Lattner698b56e2001-07-20 19:15:08 +0000694
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000695// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000696static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000697
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000698/// HandleUpRefs - Every time we finish a new layer of types, this function is
699/// called. It loops through the UpRefs vector, which is a list of the
700/// currently active types. For each type, if the up reference is contained in
701/// the newly completed type, we decrement the level count. When the level
702/// count reaches zero, the upreferenced type is the type that is passed in:
703/// thus we can complete the cycle.
704///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000705static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000706 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000707 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000708 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000709 "' newly formed. Resolving upreferences.\n" <<
710 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000711
712 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
713 // to zero), we resolve them all together before we resolve them to Ty. At
714 // the end of the loop, if there is anything to resolve to Ty, it will be in
715 // this variable.
716 OpaqueType *TypeToResolve = 0;
717
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000718 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000719 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000720 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000721 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000722 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
723 // Decrement level of upreference
724 unsigned Level = --UpRefs[i].NestingLevel;
725 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000726 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000727 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000728 if (!TypeToResolve) {
729 TypeToResolve = UpRefs[i].UpRefTy;
730 } else {
731 UR_OUT(" * Resolving upreference for "
732 << UpRefs[i].second->getDescription() << "\n";
733 std::string OldName = UpRefs[i].UpRefTy->getDescription());
734 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
735 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
736 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
737 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000738 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
739 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000740 }
741 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000742 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000743
Chris Lattner026a8ce2004-02-09 18:53:54 +0000744 if (TypeToResolve) {
745 UR_OUT(" * Resolving upreference for "
746 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000747 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000748 TypeToResolve->refineAbstractTypeTo(Ty);
749 }
750
Chris Lattner8896eda2001-07-09 19:38:36 +0000751 return Ty;
752}
753
Chris Lattner30c89792001-09-07 16:35:17 +0000754
Chris Lattner00950542001-06-06 20:29:01 +0000755//===----------------------------------------------------------------------===//
756// RunVMAsmParser - Define an interface to this parser
757//===----------------------------------------------------------------------===//
758//
Chris Lattner86427632004-07-14 06:39:48 +0000759Module *llvm::RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000760 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000761 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000762 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000763 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000764
Chris Lattner75f20532003-04-22 18:02:52 +0000765 // Allocate a new module to read
766 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000767
Chris Lattner8c89a0a2004-07-13 08:10:10 +0000768 yyparse(); // Parse the file, potentially throwing exception
Chris Lattner99e7ab72003-10-18 05:53:13 +0000769
Chris Lattner00950542001-06-06 20:29:01 +0000770 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000771
772 // Check to see if they called va_start but not va_arg..
773 if (!ObsoleteVarArgs)
774 if (Function *F = Result->getNamedFunction("llvm.va_start"))
775 if (F->asize() == 1) {
776 std::cerr << "WARNING: this file uses obsolete features. "
777 << "Assemble and disassemble to update it.\n";
778 ObsoleteVarArgs = true;
779 }
780
Chris Lattner99e7ab72003-10-18 05:53:13 +0000781 if (ObsoleteVarArgs) {
782 // If the user is making use of obsolete varargs intrinsics, adjust them for
783 // the user.
784 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
785 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
786
787 const Type *RetTy = F->getFunctionType()->getParamType(0);
788 RetTy = cast<PointerType>(RetTy)->getElementType();
789 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
790
791 while (!F->use_empty()) {
792 CallInst *CI = cast<CallInst>(F->use_back());
793 Value *V = new CallInst(NF, "", CI);
794 new StoreInst(V, CI->getOperand(1), CI);
795 CI->getParent()->getInstList().erase(CI);
796 }
797 Result->getFunctionList().erase(F);
798 }
799
800 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
801 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
802 const Type *ArgTy = F->getFunctionType()->getParamType(0);
803 ArgTy = cast<PointerType>(ArgTy)->getElementType();
804 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
805 ArgTy, 0);
806
807 while (!F->use_empty()) {
808 CallInst *CI = cast<CallInst>(F->use_back());
809 Value *V = new LoadInst(CI->getOperand(1), "", CI);
810 new CallInst(NF, V, "", CI);
811 CI->getParent()->getInstList().erase(CI);
812 }
813 Result->getFunctionList().erase(F);
814 }
815
816 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
817 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
818 const Type *ArgTy = F->getFunctionType()->getParamType(0);
819 ArgTy = cast<PointerType>(ArgTy)->getElementType();
820 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
821 ArgTy, 0);
822
823 while (!F->use_empty()) {
824 CallInst *CI = cast<CallInst>(F->use_back());
825 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
826 new StoreInst(V, CI->getOperand(1), CI);
827 CI->getParent()->getInstList().erase(CI);
828 }
829 Result->getFunctionList().erase(F);
830 }
831 }
832
Chris Lattner00950542001-06-06 20:29:01 +0000833 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
834 ParserResult = 0;
835
836 return Result;
837}
838
839%}
840
841%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000842 llvm::Module *ModuleVal;
843 llvm::Function *FunctionVal;
844 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
845 llvm::BasicBlock *BasicBlockVal;
846 llvm::TerminatorInst *TermInstVal;
847 llvm::Instruction *InstVal;
848 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000849
Brian Gaeked0fde302003-11-11 22:41:34 +0000850 const llvm::Type *PrimType;
851 llvm::PATypeHolder *TypeVal;
852 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000853
Brian Gaeked0fde302003-11-11 22:41:34 +0000854 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
855 std::vector<llvm::Value*> *ValueList;
856 std::list<llvm::PATypeHolder> *TypeList;
857 std::list<std::pair<llvm::Value*,
858 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
859 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
860 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000861
Brian Gaeked0fde302003-11-11 22:41:34 +0000862 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000863 int64_t SInt64Val;
864 uint64_t UInt64Val;
865 int SIntVal;
866 unsigned UIntVal;
867 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000868 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000869
Chris Lattner30c89792001-09-07 16:35:17 +0000870 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000871 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000872
Brian Gaeked0fde302003-11-11 22:41:34 +0000873 llvm::Instruction::BinaryOps BinaryOpVal;
874 llvm::Instruction::TermOps TermOpVal;
875 llvm::Instruction::MemoryOps MemOpVal;
876 llvm::Instruction::OtherOps OtherOpVal;
877 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000878}
879
Chris Lattner79df7c02002-03-26 18:01:55 +0000880%type <ModuleVal> Module FunctionList
881%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000882%type <BasicBlockVal> BasicBlock InstructionList
883%type <TermInstVal> BBTerminatorInst
884%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000885%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000886%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000887%type <ArgList> ArgList ArgListH
888%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000889%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000890%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000891%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000892%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000893%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000894%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000895%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000896%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000897%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000898
Chris Lattner2079fde2001-10-13 06:41:08 +0000899// ValueRef - Unresolved reference to a definition or BB
900%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000901%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000902// Tokens and types for handling constant integer values
903//
904// ESINT64VAL - A negative number within long long range
905%token <SInt64Val> ESINT64VAL
906
907// EUINT64VAL - A positive number within uns. long long range
908%token <UInt64Val> EUINT64VAL
909%type <SInt64Val> EINT64VAL
910
911%token <SIntVal> SINTVAL // Signed 32 bit ints...
912%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
913%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000914%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000915
916// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000917%type <TypeVal> Types TypesV UpRTypes UpRTypesV
918%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000919%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
920%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000921
Chris Lattner6c23f572003-08-22 05:42:10 +0000922%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
923%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000924
925
Chris Lattner91ef4602004-03-31 03:49:47 +0000926%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000927%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000928%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000929%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000930
931// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000932%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000933
Chris Lattner00950542001-06-06 20:29:01 +0000934// Binary Operators
935%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000936%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000937%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000938%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000939
940// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000941%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000942
Chris Lattner027dcc52001-07-08 21:10:27 +0000943// Other Operators
944%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000945%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000946%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000947
Chris Lattner00950542001-06-06 20:29:01 +0000948%start Module
949%%
950
951// Handle constant integer size restriction and conversion...
952//
Chris Lattner51727be2002-06-04 21:58:56 +0000953INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000954INTVAL : UINTVAL {
955 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
956 ThrowException("Value too large for type!");
957 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000958};
Chris Lattner00950542001-06-06 20:29:01 +0000959
960
Chris Lattner51727be2002-06-04 21:58:56 +0000961EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000962EINT64VAL : EUINT64VAL {
963 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
964 ThrowException("Value too large for type!");
965 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000966};
Chris Lattner00950542001-06-06 20:29:01 +0000967
Chris Lattner00950542001-06-06 20:29:01 +0000968// Operations that are notably excluded from this list include:
969// RET, BR, & SWITCH because they end basic blocks and are treated specially.
970//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000971ArithmeticOps: ADD | SUB | MUL | DIV | REM;
972LogicalOps : AND | OR | XOR;
973SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
974BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
975
Chris Lattner51727be2002-06-04 21:58:56 +0000976ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000977
Chris Lattnere98dda62001-07-14 06:10:16 +0000978// These are some types that allow classification if we only want a particular
979// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000980SIntType : LONG | INT | SHORT | SBYTE;
981UIntType : ULONG | UINT | USHORT | UBYTE;
982IntType : SIntType | UIntType;
983FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000984
Chris Lattnere98dda62001-07-14 06:10:16 +0000985// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000986OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000987 $$ = $1;
988 }
989 | /*empty*/ {
990 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000991 };
Chris Lattner00950542001-06-06 20:29:01 +0000992
Chris Lattner4ad02e72003-04-16 20:28:45 +0000993OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
994 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000995 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000996 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
997 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000998
999//===----------------------------------------------------------------------===//
1000// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +00001001// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +00001002// access to it, a user must explicitly use TypesV.
1003//
1004
1005// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +00001006TypesV : Types | VOID { $$ = new PATypeHolder($1); };
1007UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +00001008
1009Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001010 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001011 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
1012 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001013 };
Chris Lattner30c89792001-09-07 16:35:17 +00001014
1015
1016// Derived types are added later...
1017//
Chris Lattner51727be2002-06-04 21:58:56 +00001018PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1019PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001020UpRTypes : OPAQUE {
1021 $$ = new PATypeHolder(OpaqueType::get());
1022 }
1023 | PrimType {
1024 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001025 };
Chris Lattnerd78700d2002-08-16 21:14:40 +00001026UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001027 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +00001028};
Chris Lattner30c89792001-09-07 16:35:17 +00001029
Chris Lattner30c89792001-09-07 16:35:17 +00001030// Include derived types in the Types production.
1031//
1032UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001033 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +00001034 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +00001035 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001036 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +00001037 UR_OUT("New Upreference!\n");
1038 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001039 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +00001040 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +00001041 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +00001042 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +00001043 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
1044 if (isVarArg) Params.pop_back();
1045
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001046 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +00001047 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +00001048 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +00001049 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001050 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001051 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001052 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001053 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001054 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001055 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +00001056 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +00001057 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +00001058
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001059 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001060 delete $2;
1061 }
1062 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +00001063 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001064 }
1065 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001066 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001067 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001068 };
Chris Lattner30c89792001-09-07 16:35:17 +00001069
Chris Lattner7e708292002-06-25 16:13:24 +00001070// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001071// declaration type lists
1072//
1073TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001074 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001075 $$->push_back(*$1); delete $1;
1076 }
1077 | TypeListI ',' UpRTypes {
1078 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001079 };
Chris Lattner30c89792001-09-07 16:35:17 +00001080
Chris Lattner7e708292002-06-25 16:13:24 +00001081// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001082ArgTypeListI : TypeListI
1083 | TypeListI ',' DOTDOTDOT {
1084 ($$=$1)->push_back(Type::VoidTy);
1085 }
1086 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001087 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001088 }
1089 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001090 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001091 };
Chris Lattner30c89792001-09-07 16:35:17 +00001092
Chris Lattnere98dda62001-07-14 06:10:16 +00001093// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001094// production is used ONLY to represent constants that show up AFTER a 'const',
1095// 'constant' or 'global' token at global scope. Constants that can be inlined
1096// into other expressions (such as integers and constexprs) are handled by the
1097// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001098//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001099ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001100 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001101 if (ATy == 0)
1102 ThrowException("Cannot make array constant with type: '" +
1103 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001104 const Type *ETy = ATy->getElementType();
1105 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001106
Chris Lattner30c89792001-09-07 16:35:17 +00001107 // Verify that we have the correct size...
1108 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001109 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001110 utostr($3->size()) + " arguments, but has size of " +
1111 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001112
Chris Lattner30c89792001-09-07 16:35:17 +00001113 // Verify all elements are correct type!
1114 for (unsigned i = 0; i < $3->size(); i++) {
1115 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001116 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001117 ETy->getDescription() +"' as required!\nIt is of type '"+
1118 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001119 }
1120
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001121 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001122 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001123 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001124 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001125 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001126 if (ATy == 0)
1127 ThrowException("Cannot make array constant with type: '" +
1128 (*$1)->getDescription() + "'!");
1129
1130 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001131 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001132 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001133 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001134 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001135 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001136 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001137 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001138 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001139 if (ATy == 0)
1140 ThrowException("Cannot make array constant with type: '" +
1141 (*$1)->getDescription() + "'!");
1142
Chris Lattner30c89792001-09-07 16:35:17 +00001143 int NumElements = ATy->getNumElements();
1144 const Type *ETy = ATy->getElementType();
1145 char *EndStr = UnEscapeLexed($3, true);
1146 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001147 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001148 itostr((int)(EndStr-$3)) +
1149 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001150 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001151 if (ETy == Type::SByteTy) {
1152 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001153 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001154 } else if (ETy == Type::UByteTy) {
1155 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001156 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001157 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001158 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001159 ThrowException("Cannot build string arrays of non byte sized elements!");
1160 }
Chris Lattner30c89792001-09-07 16:35:17 +00001161 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001162 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001163 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001164 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001165 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001166 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001167 if (STy == 0)
1168 ThrowException("Cannot make struct constant with type: '" +
1169 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001170
Chris Lattnerc6212f12003-05-21 16:06:56 +00001171 if ($3->size() != STy->getNumContainedTypes())
1172 ThrowException("Illegal number of initializers for structure type!");
1173
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001174 // Check to ensure that constants are compatible with the type initializer!
1175 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001176 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001177 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001178 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001179 "' for element #" + utostr(i) +
1180 " of structure initializer!");
1181
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001182 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001183 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001184 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001185 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001186 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001187 if (STy == 0)
1188 ThrowException("Cannot make struct constant with type: '" +
1189 (*$1)->getDescription() + "'!");
1190
1191 if (STy->getNumContainedTypes() != 0)
1192 ThrowException("Illegal number of initializers for structure type!");
1193
1194 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1195 delete $1;
1196 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001197 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001198 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001199 if (PTy == 0)
1200 ThrowException("Cannot make null pointer constant with type: '" +
1201 (*$1)->getDescription() + "'!");
1202
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001203 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001204 delete $1;
1205 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001206 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001207 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001208 if (Ty == 0)
1209 ThrowException("Global const reference must be a pointer type!");
1210
Chris Lattner3101c252002-08-15 17:58:33 +00001211 // ConstExprs can exist in the body of a function, thus creating
1212 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1213 // the context of a function, getValNonImprovising will search the functions
1214 // symbol table instead of the module symbol table for the global symbol,
1215 // which throws things all off. To get around this, we just tell
1216 // getValNonImprovising that we are at global scope here.
1217 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001218 Function *SavedCurFn = CurFun.CurrentFunction;
1219 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001220
Chris Lattner2079fde2001-10-13 06:41:08 +00001221 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001222
Chris Lattner394f2eb2003-10-16 20:12:13 +00001223 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001224
Chris Lattner2079fde2001-10-13 06:41:08 +00001225 // If this is an initializer for a constant pointer, which is referencing a
1226 // (currently) undefined variable, create a stub now that shall be replaced
1227 // in the future with the right type of variable.
1228 //
1229 if (V == 0) {
1230 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1231 const PointerType *PT = cast<PointerType>(Ty);
1232
1233 // First check to see if the forward references value is already created!
1234 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001235 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001236
1237 if (I != CurModule.GlobalRefs.end()) {
1238 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001239 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001240 } else {
Chris Lattner8a327842004-07-14 21:44:00 +00001241 std::string Name;
Chris Lattnera09000d2004-07-14 23:03:46 +00001242 if ($2.Type == ValID::NameVal) Name = $2.Name;
Chris Lattner8a327842004-07-14 21:44:00 +00001243
Chris Lattnera09000d2004-07-14 23:03:46 +00001244 // Create the forward referenced global.
1245 GlobalValue *GV;
1246 if (const FunctionType *FTy =
1247 dyn_cast<FunctionType>(PT->getElementType())) {
1248 GV = new Function(FTy, GlobalValue::ExternalLinkage, Name,
1249 CurModule.CurrentModule);
1250 } else {
1251 GV = new GlobalVariable(PT->getElementType(), false,
1252 GlobalValue::ExternalLinkage, 0,
1253 Name, CurModule.CurrentModule);
1254 }
Chris Lattner8a327842004-07-14 21:44:00 +00001255
Chris Lattner2079fde2001-10-13 06:41:08 +00001256 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001257 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001258 V = GV;
1259 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001260 }
1261
Chris Lattner2079fde2001-10-13 06:41:08 +00001262 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001263 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001264 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001265 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001266 | Types ConstExpr {
1267 if ($1->get() != $2->getType())
1268 ThrowException("Mismatched types for constant expression!");
1269 $$ = $2;
1270 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001271 }
1272 | Types ZEROINITIALIZER {
1273 $$ = Constant::getNullValue($1->get());
1274 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001275 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001276
Chris Lattnere43f40b2002-10-09 00:25:32 +00001277ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001278 if (!ConstantSInt::isValueValidForType($1, $2))
1279 ThrowException("Constant value doesn't fit in type!");
1280 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001281 }
1282 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001283 if (!ConstantUInt::isValueValidForType($1, $2))
1284 ThrowException("Constant value doesn't fit in type!");
1285 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001286 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001287 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001288 $$ = ConstantBool::True;
1289 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001290 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001291 $$ = ConstantBool::False;
1292 }
1293 | FPType FPVAL { // Float & Double constants
1294 $$ = ConstantFP::get($1, $2);
1295 };
1296
Chris Lattner00950542001-06-06 20:29:01 +00001297
Chris Lattnerd78700d2002-08-16 21:14:40 +00001298ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001299 if (!$3->getType()->isFirstClassType())
1300 ThrowException("cast constant expression from a non-primitive type: '" +
1301 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001302 if (!$5->get()->isFirstClassType())
1303 ThrowException("cast constant expression to a non-primitive type: '" +
1304 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001305 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001306 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001307 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001308 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1309 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001310 ThrowException("GetElementPtr requires a pointer operand!");
1311
Chris Lattner830b6f92004-04-05 01:30:04 +00001312 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1313 // indices to uint struct indices for compatibility.
1314 generic_gep_type_iterator<std::vector<Value*>::iterator>
1315 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1316 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1317 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1318 if (isa<StructType>(*GTI)) // Only change struct indices
1319 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1320 if (CUI->getType() == Type::UByteTy)
1321 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1322
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001323 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001324 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001325 if (!IdxTy)
1326 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001327
Chris Lattner9232b992003-04-22 18:42:41 +00001328 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001329 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1330 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001331 IdxVec.push_back(C);
1332 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001333 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001334
Chris Lattnerd78700d2002-08-16 21:14:40 +00001335 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001336
Chris Lattnerd78700d2002-08-16 21:14:40 +00001337 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001338 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001339 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1340 if ($3->getType() != Type::BoolTy)
1341 ThrowException("Select condition must be of boolean type!");
1342 if ($5->getType() != $7->getType())
1343 ThrowException("Select operand types must match!");
1344 $$ = ConstantExpr::getSelect($3, $5, $7);
1345 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001346 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001347 if ($3->getType() != $5->getType())
1348 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001349 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001350 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001351 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001352 if ($5->getType() != Type::UByteTy)
1353 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001354 if (!$3->getType()->isInteger())
1355 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001356 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001357 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001358
1359
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001360// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001361ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001362 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001363 }
1364 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001365 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001366 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001367 };
Chris Lattner00950542001-06-06 20:29:01 +00001368
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001369
Chris Lattner1781aca2001-09-18 04:00:54 +00001370// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001371GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001372
Chris Lattner00950542001-06-06 20:29:01 +00001373
Chris Lattner0e73ce62002-05-02 19:11:13 +00001374//===----------------------------------------------------------------------===//
1375// Rules to match Modules
1376//===----------------------------------------------------------------------===//
1377
1378// Module rule: Capture the result of parsing the whole file into a result
1379// variable...
1380//
1381Module : FunctionList {
1382 $$ = ParserResult = $1;
1383 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001384};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001385
Chris Lattner7e708292002-06-25 16:13:24 +00001386// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001387//
1388FunctionList : FunctionList Function {
1389 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001390 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001391 }
1392 | FunctionList FunctionProto {
1393 $$ = $1;
1394 }
1395 | FunctionList IMPLEMENTATION {
1396 $$ = $1;
1397 }
1398 | ConstPool {
1399 $$ = CurModule.CurrentModule;
1400 // Resolve circular types before we parse the body of the module
1401 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001402 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001403
Chris Lattnere98dda62001-07-14 06:10:16 +00001404// ConstPool - Constants with optional names assigned to them.
Chris Lattnere0026942004-07-14 06:44:56 +00001405ConstPool : ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001406 // Eagerly resolve types. This is not an optimization, this is a
1407 // requirement that is due to the fact that we could have this:
1408 //
1409 // %list = type { %list * }
1410 // %list = type { %list * } ; repeated type decl
1411 //
1412 // If types are not resolved eagerly, then the two types will not be
1413 // determined to be the same type!
1414 //
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001415 ResolveTypeTo($2, *$4);
Chris Lattner4a42e902001-10-22 05:56:09 +00001416
Chris Lattner8c89a0a2004-07-13 08:10:10 +00001417 if (!setTypeName(*$4, $2) && !$2) {
1418 // If this is a named type that is not a redefinition, add it to the slot
1419 // table.
Chris Lattner64116f92004-07-14 01:33:11 +00001420 if (inFunctionScope())
1421 CurFun.Types.push_back(*$4);
1422 else
1423 CurModule.Types.push_back(*$4);
Chris Lattner30c89792001-09-07 16:35:17 +00001424 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001425
1426 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001427 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001428 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001429 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001430 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001431 if ($5 == 0) ThrowException("Global value initializer is not a constant!");
1432 ParseGlobalVariable($2, $3, $4, $5->getType(), $5);
Chris Lattner1781aca2001-09-18 04:00:54 +00001433 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001434 | ConstPool OptAssign EXTERNAL GlobalType Types {
Chris Lattnerd88998d2004-07-14 08:23:52 +00001435 ParseGlobalVariable($2, GlobalValue::ExternalLinkage, $4, *$5, 0);
Chris Lattner1f862af2003-04-16 18:13:57 +00001436 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001437 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001438 | ConstPool TARGET TargetDefinition {
1439 }
Chris Lattner00950542001-06-06 20:29:01 +00001440 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001441 };
Chris Lattner00950542001-06-06 20:29:01 +00001442
1443
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001444
1445BigOrLittle : BIG { $$ = Module::BigEndian; };
1446BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1447
1448TargetDefinition : ENDIAN '=' BigOrLittle {
1449 CurModule.CurrentModule->setEndianness($3);
1450 }
1451 | POINTERSIZE '=' EUINT64VAL {
1452 if ($3 == 32)
1453 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1454 else if ($3 == 64)
1455 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1456 else
1457 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1458 };
1459
1460
Chris Lattner00950542001-06-06 20:29:01 +00001461//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001462// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001463//===----------------------------------------------------------------------===//
1464
Chris Lattner6c23f572003-08-22 05:42:10 +00001465Name : VAR_ID | STRINGCONSTANT;
1466OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001467
Chris Lattner6c23f572003-08-22 05:42:10 +00001468ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001469 if (*$1 == Type::VoidTy)
1470 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001471 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001472};
Chris Lattner00950542001-06-06 20:29:01 +00001473
Chris Lattner69da5cf2002-10-13 20:57:00 +00001474ArgListH : ArgListH ',' ArgVal {
1475 $$ = $1;
1476 $1->push_back(*$3);
1477 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001478 }
1479 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001480 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001481 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001482 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001483 };
Chris Lattner00950542001-06-06 20:29:01 +00001484
1485ArgList : ArgListH {
1486 $$ = $1;
1487 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001488 | ArgListH ',' DOTDOTDOT {
1489 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001490 $$->push_back(std::pair<PATypeHolder*,
1491 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001492 }
1493 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001494 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1495 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001496 }
Chris Lattner00950542001-06-06 20:29:01 +00001497 | /* empty */ {
1498 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001499 };
Chris Lattner00950542001-06-06 20:29:01 +00001500
Chris Lattner6c23f572003-08-22 05:42:10 +00001501FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001502 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001503 std::string FunctionName($2);
Chris Lattnera09000d2004-07-14 23:03:46 +00001504 free($2); // Free strdup'd memory!
Chris Lattnerdda71962001-11-26 18:54:16 +00001505
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001506 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1507 ThrowException("LLVM functions cannot return aggregate types!");
1508
Chris Lattner9232b992003-04-22 18:42:41 +00001509 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001510 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001511 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001512 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001513 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001514 }
Chris Lattner00950542001-06-06 20:29:01 +00001515
Chris Lattner2079fde2001-10-13 06:41:08 +00001516 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1517 if (isVarArg) ParamTypeList.pop_back();
1518
Chris Lattner1f862af2003-04-16 18:13:57 +00001519 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001520 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001521 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001522
Chris Lattnera09000d2004-07-14 23:03:46 +00001523 ValID ID;
1524 if (!FunctionName.empty()) {
1525 ID = ValID::create((char*)FunctionName.c_str());
1526 } else {
1527 ID = ValID::create((int)CurModule.Values[PFT].size());
1528 }
1529
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001530 Function *Fn = 0;
Chris Lattnera09000d2004-07-14 23:03:46 +00001531 // See if this function was forward referenced. If so, recycle the object.
1532 if (GlobalValue *FWRef = CurModule.GetForwardRefForGlobal(PFT, ID)) {
1533 // Move the function to the end of the list, from whereever it was
1534 // previously inserted.
1535 Fn = cast<Function>(FWRef);
1536 CurModule.CurrentModule->getFunctionList().remove(Fn);
1537 CurModule.CurrentModule->getFunctionList().push_back(Fn);
1538 } else if (!FunctionName.empty() && // Merge with an earlier prototype?
1539 (Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1540 // If this is the case, either we need to be a forward decl, or it needs
1541 // to be.
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001542 if (!CurFun.isDeclare && !Fn->isExternal())
1543 ThrowException("Redefinition of function '" + FunctionName + "'!");
1544
Chris Lattnera09000d2004-07-14 23:03:46 +00001545 // Make sure to strip off any argument names so we can't get conflicts.
1546 if (Fn->isExternal())
1547 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend();
1548 AI != AE; ++AI)
1549 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001550
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001551 } else { // Not already defined?
1552 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1553 CurModule.CurrentModule);
1554 InsertValue(Fn, CurModule.Values);
Chris Lattnerd9ce6ad2004-07-14 19:33:47 +00001555 }
Chris Lattner00950542001-06-06 20:29:01 +00001556
Chris Lattner394f2eb2003-10-16 20:12:13 +00001557 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001558
Chris Lattner7e708292002-06-25 16:13:24 +00001559 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001560 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001561 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001562 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001563 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001564 delete $4->back().first;
1565 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001566 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001567 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001568 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001569 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001570 delete I->first; // Delete the typeholder...
1571
Chris Lattner8ab406d2004-07-13 07:59:27 +00001572 setValueName(ArgIt, I->second); // Insert arg into symtab...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001573 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001574 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001575
Chris Lattner1f862af2003-04-16 18:13:57 +00001576 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001577 }
Chris Lattner51727be2002-06-04 21:58:56 +00001578};
Chris Lattner00950542001-06-06 20:29:01 +00001579
Chris Lattner9b02cc32002-05-03 18:23:48 +00001580BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1581
Chris Lattner4ad02e72003-04-16 20:28:45 +00001582FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001583 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001584
Chris Lattner4ad02e72003-04-16 20:28:45 +00001585 // Make sure that we keep track of the linkage type even if there was a
1586 // previous "declare".
1587 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001588
Chris Lattner7e708292002-06-25 16:13:24 +00001589 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001590 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001591};
Chris Lattner00950542001-06-06 20:29:01 +00001592
Chris Lattner9b02cc32002-05-03 18:23:48 +00001593END : ENDTOK | '}'; // Allow end of '}' to end a function
1594
Chris Lattner79df7c02002-03-26 18:01:55 +00001595Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001596 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001597};
Chris Lattner00950542001-06-06 20:29:01 +00001598
Chris Lattner394f2eb2003-10-16 20:12:13 +00001599FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1600 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001601 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001602};
Chris Lattner00950542001-06-06 20:29:01 +00001603
1604//===----------------------------------------------------------------------===//
1605// Rules to match Basic Blocks
1606//===----------------------------------------------------------------------===//
1607
1608ConstValueRef : ESINT64VAL { // A reference to a direct constant
1609 $$ = ValID::create($1);
1610 }
1611 | EUINT64VAL {
1612 $$ = ValID::create($1);
1613 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001614 | FPVAL { // Perhaps it's an FP constant?
1615 $$ = ValID::create($1);
1616 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001617 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001618 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001619 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001620 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001621 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001622 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001623 | NULL_TOK {
1624 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001625 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001626 | ConstExpr {
1627 $$ = ValID::create($1);
1628 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001629
Chris Lattner2079fde2001-10-13 06:41:08 +00001630// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1631// another value.
1632//
1633SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001634 $$ = ValID::create($1);
1635 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001636 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001637 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001638 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001639
1640// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001641ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001642
Chris Lattner00950542001-06-06 20:29:01 +00001643
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001644// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1645// type immediately preceeds the value reference, and allows complex constant
1646// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001647ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001648 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001649 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001650
Chris Lattner00950542001-06-06 20:29:01 +00001651BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001652 $$ = $1;
Chris Lattner00950542001-06-06 20:29:01 +00001653 }
Chris Lattner7e708292002-06-25 16:13:24 +00001654 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
Chris Lattner8ab406d2004-07-13 07:59:27 +00001655 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001656 };
Chris Lattner00950542001-06-06 20:29:01 +00001657
1658
1659// Basic blocks are terminated by branching instructions:
1660// br, br/cc, switch, ret
1661//
Chris Lattner2079fde2001-10-13 06:41:08 +00001662BasicBlock : InstructionList OptAssign BBTerminatorInst {
Chris Lattner8ab406d2004-07-13 07:59:27 +00001663 setValueName($3, $2);
Chris Lattner2079fde2001-10-13 06:41:08 +00001664 InsertValue($3);
1665
1666 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001667 InsertValue($1);
1668 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001669 };
Chris Lattner00950542001-06-06 20:29:01 +00001670
1671InstructionList : InstructionList Inst {
1672 $1->getInstList().push_back($2);
1673 $$ = $1;
1674 }
1675 | /* empty */ {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001676 $$ = CurBB = getBBVal(ValID::create((int)CurFun.NextBBNum++), true);
Chris Lattner22ae2322004-07-13 08:39:15 +00001677 }
1678 | LABELSTR {
Chris Lattner2e2ed292004-07-14 06:28:35 +00001679 $$ = CurBB = getBBVal(ValID::create($1), true);
Chris Lattner51727be2002-06-04 21:58:56 +00001680 };
Chris Lattner00950542001-06-06 20:29:01 +00001681
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001682BBTerminatorInst : RET ResolvedVal { // Return with a result...
1683 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001684 }
1685 | RET VOID { // Return with no result...
1686 $$ = new ReturnInst();
1687 }
1688 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner64116f92004-07-14 01:33:11 +00001689 $$ = new BranchInst(getBBVal($3));
Chris Lattner00950542001-06-06 20:29:01 +00001690 } // Conditional Branch...
1691 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner64116f92004-07-14 01:33:11 +00001692 $$ = new BranchInst(getBBVal($6), getBBVal($9), getVal(Type::BoolTy, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001693 }
1694 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001695 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner00950542001-06-06 20:29:01 +00001696 $$ = S;
1697
Chris Lattner9232b992003-04-22 18:42:41 +00001698 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001699 E = $8->end();
1700 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001701 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001702 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001703 }
Chris Lattner196850c2003-05-15 21:30:00 +00001704 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Chris Lattner64116f92004-07-14 01:33:11 +00001705 SwitchInst *S = new SwitchInst(getVal($2, $3), getBBVal($6));
Chris Lattner196850c2003-05-15 21:30:00 +00001706 $$ = S;
1707 }
Chris Lattner64116f92004-07-14 01:33:11 +00001708 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO LABEL ValueRef
1709 UNWIND LABEL ValueRef {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001710 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001711 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001712
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001713 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1714 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001715 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001716 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001717 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001718 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1719 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001720 ParamTypes.push_back((*I)->getType());
1721 }
1722
1723 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1724 if (isVarArg) ParamTypes.pop_back();
1725
Chris Lattner79df7c02002-03-26 18:01:55 +00001726 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001727 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001728 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001729
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001730 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001731
Chris Lattner64116f92004-07-14 01:33:11 +00001732 BasicBlock *Normal = getBBVal($9);
1733 BasicBlock *Except = getBBVal($12);
Chris Lattner2079fde2001-10-13 06:41:08 +00001734
1735 // Create the call node...
1736 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001737 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001738 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001739 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001740 // correctly!
1741 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001742 FunctionType::param_iterator I = Ty->param_begin();
1743 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001744 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001745
1746 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1747 if ((*ArgI)->getType() != *I)
1748 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001749 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001750
1751 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1752 ThrowException("Invalid number of parameters detected!");
1753
Chris Lattner6cdb0112001-11-26 16:54:11 +00001754 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001755 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001756 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001757 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001758 }
1759 | UNWIND {
1760 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001761 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001762
1763
Chris Lattner00950542001-06-06 20:29:01 +00001764
1765JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1766 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001767 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001768 if (V == 0)
1769 ThrowException("May only switch on a constant pool value!");
1770
Chris Lattner64116f92004-07-14 01:33:11 +00001771 $$->push_back(std::make_pair(V, getBBVal($6)));
Chris Lattner00950542001-06-06 20:29:01 +00001772 }
1773 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001774 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001775 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001776
1777 if (V == 0)
1778 ThrowException("May only switch on a constant pool value!");
1779
Chris Lattner64116f92004-07-14 01:33:11 +00001780 $$->push_back(std::make_pair(V, getBBVal($5)));
Chris Lattner51727be2002-06-04 21:58:56 +00001781 };
Chris Lattner00950542001-06-06 20:29:01 +00001782
1783Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001784 // Is this definition named?? if so, assign the name...
Chris Lattner8ab406d2004-07-13 07:59:27 +00001785 setValueName($2, $1);
Chris Lattner00950542001-06-06 20:29:01 +00001786 InsertValue($2);
1787 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001788};
Chris Lattner00950542001-06-06 20:29:01 +00001789
Chris Lattnerc24d2082001-06-11 15:04:20 +00001790PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001791 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
Chris Lattner64116f92004-07-14 01:33:11 +00001792 $$->push_back(std::make_pair(getVal(*$1, $3), getBBVal($5)));
Chris Lattner30c89792001-09-07 16:35:17 +00001793 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001794 }
1795 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1796 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001797 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
Chris Lattner64116f92004-07-14 01:33:11 +00001798 getBBVal($6)));
Chris Lattner51727be2002-06-04 21:58:56 +00001799 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001800
1801
Chris Lattner30c89792001-09-07 16:35:17 +00001802ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001803 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001804 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001805 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001806 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001807 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001808 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001809 };
Chris Lattner00950542001-06-06 20:29:01 +00001810
1811// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001812ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001813
Chris Lattner4a6482b2002-09-10 19:57:26 +00001814InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1815 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1816 ThrowException("Arithmetic operator requires integer or FP operands!");
1817 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1818 if ($$ == 0)
1819 ThrowException("binary operator returned null!");
1820 delete $2;
1821 }
1822 | LogicalOps Types ValueRef ',' ValueRef {
1823 if (!(*$2)->isIntegral())
1824 ThrowException("Logical operator requires integral operands!");
1825 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1826 if ($$ == 0)
1827 ThrowException("binary operator returned null!");
1828 delete $2;
1829 }
1830 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001831 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001832 if ($$ == 0)
1833 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001834 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001835 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001836 | NOT ResolvedVal {
1837 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1838 << " Replacing with 'xor'.\n";
1839
1840 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1841 if (Ones == 0)
1842 ThrowException("Expected integral type for not instruction!");
1843
1844 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001845 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001846 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001847 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001848 | ShiftOps ResolvedVal ',' ResolvedVal {
1849 if ($4->getType() != Type::UByteTy)
1850 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001851 if (!$2->getType()->isInteger())
1852 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001853 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001854 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001855 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001856 if (!$4->get()->isFirstClassType())
1857 ThrowException("cast instruction to a non-primitive type: '" +
1858 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001859 $$ = new CastInst($2, *$4);
1860 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001861 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001862 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1863 if ($2->getType() != Type::BoolTy)
1864 ThrowException("select condition must be boolean!");
1865 if ($4->getType() != $6->getType())
1866 ThrowException("select value types should match!");
1867 $$ = new SelectInst($2, $4, $6);
1868 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001869 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001870 // FIXME: This is emulation code for an obsolete syntax. This should be
1871 // removed at some point.
1872 if (!ObsoleteVarArgs) {
1873 std::cerr << "WARNING: this file uses obsolete features. "
1874 << "Assemble and disassemble to update it.\n";
1875 ObsoleteVarArgs = true;
1876 }
1877
1878 // First, load the valist...
1879 Instruction *CurVAList = new LoadInst($2, "");
1880 CurBB->getInstList().push_back(CurVAList);
1881
1882 // Emit the vaarg instruction.
1883 $$ = new VAArgInst(CurVAList, *$4);
1884
1885 // Now we must advance the pointer and update it in memory.
1886 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1887 CurBB->getInstList().push_back(TheVANext);
1888
1889 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1890 delete $4;
1891 }
1892 | VAARG ResolvedVal ',' Types {
1893 $$ = new VAArgInst($2, *$4);
1894 delete $4;
1895 }
1896 | VANEXT ResolvedVal ',' Types {
1897 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001898 delete $4;
1899 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001900 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001901 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001902 if (!Ty->isFirstClassType())
1903 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001904 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001905 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001906 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001907 if ($2->front().first->getType() != Ty)
1908 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001909 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001910 $2->pop_front();
1911 }
1912 delete $2; // Free the list...
1913 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001914 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001915 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001916 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001917
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001918 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1919 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001920 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001921 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001922 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001923 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1924 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001925 ParamTypes.push_back((*I)->getType());
1926 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001927
1928 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1929 if (isVarArg) ParamTypes.pop_back();
1930
Chris Lattner79df7c02002-03-26 18:01:55 +00001931 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001932 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001933 }
Chris Lattner00950542001-06-06 20:29:01 +00001934
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001935 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001936
Chris Lattner8b81bf52001-07-25 22:47:46 +00001937 // Create the call node...
1938 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001939 // Make sure no arguments is a good thing!
1940 if (Ty->getNumParams() != 0)
1941 ThrowException("No arguments passed to a function that "
1942 "expects arguments!");
1943
Chris Lattner9232b992003-04-22 18:42:41 +00001944 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001945 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001946 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001947 // correctly!
1948 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001949 FunctionType::param_iterator I = Ty->param_begin();
1950 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001951 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001952
1953 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1954 if ((*ArgI)->getType() != *I)
1955 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001956 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001957
Chris Lattner8b81bf52001-07-25 22:47:46 +00001958 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001959 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001960
Chris Lattner6cdb0112001-11-26 16:54:11 +00001961 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001962 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001963 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001964 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001965 }
1966 | MemoryInst {
1967 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001968 };
Chris Lattner00950542001-06-06 20:29:01 +00001969
Chris Lattner6cdb0112001-11-26 16:54:11 +00001970
1971// IndexList - List of indices for GEP based instructions...
1972IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001973 $$ = $2;
1974 } | /* empty */ {
1975 $$ = new std::vector<Value*>();
1976 };
1977
1978OptVolatile : VOLATILE {
1979 $$ = true;
1980 }
1981 | /* empty */ {
1982 $$ = false;
1983 };
1984
Chris Lattner027dcc52001-07-08 21:10:27 +00001985
Chris Lattner00950542001-06-06 20:29:01 +00001986MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001987 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001988 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001989 }
1990 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001991 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001992 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001993 }
1994 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001995 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001996 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001997 }
1998 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001999 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00002000 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00002001 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002002 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00002003 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002004 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00002005 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00002006 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00002007 }
2008
Chris Lattner15c9c032003-09-08 18:20:29 +00002009 | OptVolatile LOAD Types ValueRef {
2010 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00002011 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002012 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00002013 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002014 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00002015 }
Chris Lattner15c9c032003-09-08 18:20:29 +00002016 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2017 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002018 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00002019 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00002020 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00002021 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00002022 if (ElTy != $3->getType())
2023 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00002024 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00002025
Chris Lattner79ad13802003-09-08 20:29:46 +00002026 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00002027 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002028 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00002029 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00002030 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00002031 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00002032
2033 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
2034 // indices to uint struct indices for compatibility.
2035 generic_gep_type_iterator<std::vector<Value*>::iterator>
2036 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2037 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2038 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2039 if (isa<StructType>(*GTI)) // Only change struct indices
2040 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2041 if (CUI->getType() == Type::UByteTy)
2042 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2043
Chris Lattner30c89792001-09-07 16:35:17 +00002044 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002045 ThrowException("Invalid getelementptr indices for type '" +
2046 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002047 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2048 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002049 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002050
Brian Gaeked0fde302003-11-11 22:41:34 +00002051
Chris Lattner00950542001-06-06 20:29:01 +00002052%%
Chris Lattner09083092001-07-08 04:57:15 +00002053int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002054 std::string where
2055 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002056 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002057 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002058 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002059 errMsg += "end-of-file.";
2060 else
Chris Lattner9232b992003-04-22 18:42:41 +00002061 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002062 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002063 return 0;
2064}