blob: c8304d75cc3f0a3c8cde9693964ed71ffe382c9f [file] [log] [blame]
Chris Lattnercf3056d2003-10-13 03:32:08 +00001//===-- llvmAsmParser.y - Parser for llvm assembly files --------*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements the bison parser for LLVM assembly languages files.
11//
Chris Lattnercf3056d2003-10-13 03:32:08 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattner00950542001-06-06 20:29:01 +000014%{
15#include "ParserInternals.h"
Chris Lattner00950542001-06-06 20:29:01 +000016#include "llvm/SymbolTable.h"
17#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/iTerminators.h"
19#include "llvm/iMemory.h"
Chris Lattner1cff96a2002-09-10 22:37:46 +000020#include "llvm/iOperators.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000021#include "llvm/iPHINode.h"
Chris Lattner830b6f92004-04-05 01:30:04 +000022#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000023#include "Support/STLExtras.h"
Chris Lattner00950542001-06-06 20:29:01 +000024#include <list>
Chris Lattnerc188eeb2002-07-30 18:54:25 +000025#include <utility>
Chris Lattner30c89792001-09-07 16:35:17 +000026#include <algorithm>
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner386a3b72001-10-16 19:54:17 +000028int yyerror(const char *ErrorMsg); // Forward declarations to prevent "implicit
Chris Lattner09083092001-07-08 04:57:15 +000029int yylex(); // declaration" of xxx warnings.
Chris Lattner00950542001-06-06 20:29:01 +000030int yyparse();
31
Brian Gaeked0fde302003-11-11 22:41:34 +000032namespace llvm {
33
Chris Lattner00950542001-06-06 20:29:01 +000034static Module *ParserResult;
Chris Lattner9232b992003-04-22 18:42:41 +000035std::string CurFilename;
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattner30c89792001-09-07 16:35:17 +000037// DEBUG_UPREFS - Define this symbol if you want to enable debugging output
38// relating to upreferences in the input stream.
39//
40//#define DEBUG_UPREFS 1
41#ifdef DEBUG_UPREFS
Chris Lattner699f1eb2002-08-14 17:12:33 +000042#define UR_OUT(X) std::cerr << X
Chris Lattner30c89792001-09-07 16:35:17 +000043#else
44#define UR_OUT(X)
45#endif
46
Vikram S. Adved3f7eb02002-07-14 22:59:28 +000047#define YYERROR_VERBOSE 1
48
Chris Lattner99e7ab72003-10-18 05:53:13 +000049// HACK ALERT: This variable is used to implement the automatic conversion of
50// variable argument instructions from their old to new forms. When this
51// compatiblity "Feature" is removed, this should be too.
52//
53static BasicBlock *CurBB;
54static bool ObsoleteVarArgs;
55
56
Chris Lattner7e708292002-06-25 16:13:24 +000057// This contains info used when building the body of a function. It is
58// destroyed when the function is completed.
Chris Lattner00950542001-06-06 20:29:01 +000059//
Chris Lattner9232b992003-04-22 18:42:41 +000060typedef std::vector<Value *> ValueList; // Numbered defs
Chris Lattner16af11d2004-02-09 21:03:38 +000061static void ResolveDefinitions(std::map<unsigned,ValueList> &LateResolvers,
62 std::map<unsigned,ValueList> *FutureLateResolvers = 0);
Chris Lattner00950542001-06-06 20:29:01 +000063
64static struct PerModuleInfo {
65 Module *CurrentModule;
Chris Lattner16af11d2004-02-09 21:03:38 +000066 std::map<unsigned,ValueList> Values; // Module level numbered definitions
67 std::map<unsigned,ValueList> LateResolveValues;
68 std::vector<PATypeHolder> Types;
Chris Lattner9232b992003-04-22 18:42:41 +000069 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner00950542001-06-06 20:29:01 +000070
Chris Lattner2079fde2001-10-13 06:41:08 +000071 // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward
72 // references to global values. Global values may be referenced before they
73 // are defined, and if so, the temporary object that they represent is held
Chris Lattnere9bb2df2001-12-03 22:26:30 +000074 // here. This is used for forward references of ConstantPointerRefs.
Chris Lattner2079fde2001-10-13 06:41:08 +000075 //
Chris Lattner9232b992003-04-22 18:42:41 +000076 typedef std::map<std::pair<const PointerType *,
Chris Lattnerbc207592004-03-08 06:09:57 +000077 ValID>, GlobalValue*> GlobalRefsType;
Chris Lattner2079fde2001-10-13 06:41:08 +000078 GlobalRefsType GlobalRefs;
79
Chris Lattner00950542001-06-06 20:29:01 +000080 void ModuleDone() {
Chris Lattner7e708292002-06-25 16:13:24 +000081 // If we could not resolve some functions at function compilation time
82 // (calls to functions before they are defined), resolve them now... Types
83 // are resolved when the constant pool has been completely parsed.
Chris Lattner30c89792001-09-07 16:35:17 +000084 //
Chris Lattner00950542001-06-06 20:29:01 +000085 ResolveDefinitions(LateResolveValues);
86
Chris Lattner2079fde2001-10-13 06:41:08 +000087 // Check to make sure that all global value forward references have been
88 // resolved!
89 //
90 if (!GlobalRefs.empty()) {
Chris Lattner9232b992003-04-22 18:42:41 +000091 std::string UndefinedReferences = "Unresolved global references exist:\n";
Chris Lattner749ce032002-03-11 22:12:39 +000092
93 for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end();
94 I != E; ++I) {
95 UndefinedReferences += " " + I->first.first->getDescription() + " " +
96 I->first.second.getName() + "\n";
97 }
98 ThrowException(UndefinedReferences);
Chris Lattner2079fde2001-10-13 06:41:08 +000099 }
100
Chris Lattner7e708292002-06-25 16:13:24 +0000101 Values.clear(); // Clear out function local definitions
Chris Lattner30c89792001-09-07 16:35:17 +0000102 Types.clear();
Chris Lattner00950542001-06-06 20:29:01 +0000103 CurrentModule = 0;
104 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000105
106
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000107 // DeclareNewGlobalValue - Called every time a new GV has been defined. This
Chris Lattner2079fde2001-10-13 06:41:08 +0000108 // is used to remove things from the forward declaration map, resolving them
109 // to the correct thing as needed.
110 //
111 void DeclareNewGlobalValue(GlobalValue *GV, ValID D) {
112 // Check to see if there is a forward reference to this global variable...
113 // if there is, eliminate it and patch the reference to use the new def'n.
Chris Lattner9232b992003-04-22 18:42:41 +0000114 GlobalRefsType::iterator I =
115 GlobalRefs.find(std::make_pair(GV->getType(), D));
Chris Lattner2079fde2001-10-13 06:41:08 +0000116
117 if (I != GlobalRefs.end()) {
Chris Lattnerbc207592004-03-08 06:09:57 +0000118 GlobalValue *OldGV = I->second; // Get the placeholder...
Misha Brukman5560c9d2003-08-18 14:43:39 +0000119 I->first.second.destroy(); // Free string memory if necessary
Chris Lattner2079fde2001-10-13 06:41:08 +0000120
121 // Loop over all of the uses of the GlobalValue. The only thing they are
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000122 // allowed to be is ConstantPointerRef's.
Chris Lattnerfd059242003-10-15 16:48:29 +0000123 assert(OldGV->hasOneUse() && "Only one reference should exist!");
Chris Lattner9e932bd2002-10-14 03:28:42 +0000124 User *U = OldGV->use_back(); // Must be a ConstantPointerRef...
125 ConstantPointerRef *CPR = cast<ConstantPointerRef>(U);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000126
Chris Lattner9e932bd2002-10-14 03:28:42 +0000127 // Change the const pool reference to point to the real global variable
128 // now. This should drop a use from the OldGV.
Chris Lattnerbc207592004-03-08 06:09:57 +0000129 CPR->replaceUsesOfWithOnConstant(OldGV, GV);
Chris Lattner9e932bd2002-10-14 03:28:42 +0000130 assert(OldGV->use_empty() && "All uses should be gone now!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000131
132 // Remove OldGV from the module...
Chris Lattnerbc207592004-03-08 06:09:57 +0000133 if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(OldGV))
134 CurrentModule->getGlobalList().erase(GVar);
135 else
136 CurrentModule->getFunctionList().erase(cast<Function>(OldGV));
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000137
Chris Lattner2079fde2001-10-13 06:41:08 +0000138 // Remove the map entry for the global now that it has been created...
139 GlobalRefs.erase(I);
140 }
141 }
142
Chris Lattner00950542001-06-06 20:29:01 +0000143} CurModule;
144
Chris Lattner79df7c02002-03-26 18:01:55 +0000145static struct PerFunctionInfo {
Chris Lattner7e708292002-06-25 16:13:24 +0000146 Function *CurrentFunction; // Pointer to current function being created
Chris Lattner00950542001-06-06 20:29:01 +0000147
Chris Lattner16af11d2004-02-09 21:03:38 +0000148 std::map<unsigned,ValueList> Values; // Keep track of numbered definitions
149 std::map<unsigned,ValueList> LateResolveValues;
Chris Lattner9232b992003-04-22 18:42:41 +0000150 std::vector<PATypeHolder> Types;
151 std::map<ValID, PATypeHolder> LateResolveTypes;
Chris Lattner15b69722003-11-12 04:40:30 +0000152 SymbolTable LocalSymtab;
Chris Lattner7e708292002-06-25 16:13:24 +0000153 bool isDeclare; // Is this function a forward declararation?
Chris Lattner00950542001-06-06 20:29:01 +0000154
Chris Lattner79df7c02002-03-26 18:01:55 +0000155 inline PerFunctionInfo() {
156 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000157 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000158 }
159
Chris Lattner79df7c02002-03-26 18:01:55 +0000160 inline void FunctionStart(Function *M) {
161 CurrentFunction = M;
Chris Lattner00950542001-06-06 20:29:01 +0000162 }
163
Chris Lattner79df7c02002-03-26 18:01:55 +0000164 void FunctionDone() {
Chris Lattner00950542001-06-06 20:29:01 +0000165 // If we could not resolve some blocks at parsing time (forward branches)
166 // resolve the branches now...
Chris Lattner386a3b72001-10-16 19:54:17 +0000167 ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000168
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000169 // Make sure to resolve any constant expr references that might exist within
170 // the function we just declared itself.
171 ValID FID;
172 if (CurrentFunction->hasName()) {
173 FID = ValID::create((char*)CurrentFunction->getName().c_str());
174 } else {
175 unsigned Slot = CurrentFunction->getType()->getUniqueID();
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000176 // Figure out which slot number if is...
Chris Lattner16af11d2004-02-09 21:03:38 +0000177 ValueList &List = CurModule.Values[Slot];
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000178 for (unsigned i = 0; ; ++i) {
Chris Lattner16af11d2004-02-09 21:03:38 +0000179 assert(i < List.size() && "Function not found!");
180 if (List[i] == CurrentFunction) {
Chris Lattnerb8fdd8b2003-04-25 21:47:33 +0000181 FID = ValID::create((int)i);
182 break;
183 }
184 }
185 }
186 CurModule.DeclareNewGlobalValue(CurrentFunction, FID);
187
Chris Lattner7e708292002-06-25 16:13:24 +0000188 Values.clear(); // Clear out function local definitions
Chris Lattner15b69722003-11-12 04:40:30 +0000189 Types.clear(); // Clear out function local types
190 LocalSymtab.clear(); // Clear out function local symbol table
Chris Lattner79df7c02002-03-26 18:01:55 +0000191 CurrentFunction = 0;
Chris Lattnere1815642001-07-15 06:35:53 +0000192 isDeclare = false;
Chris Lattner00950542001-06-06 20:29:01 +0000193 }
Chris Lattner394f2eb2003-10-16 20:12:13 +0000194} CurFun; // Info for the current function...
Chris Lattner00950542001-06-06 20:29:01 +0000195
Chris Lattner394f2eb2003-10-16 20:12:13 +0000196static bool inFunctionScope() { return CurFun.CurrentFunction != 0; }
Chris Lattnerb7474512001-10-03 15:39:04 +0000197
Chris Lattner00950542001-06-06 20:29:01 +0000198
199//===----------------------------------------------------------------------===//
200// Code to handle definitions of all the types
201//===----------------------------------------------------------------------===//
202
Chris Lattner9232b992003-04-22 18:42:41 +0000203static int InsertValue(Value *D,
Chris Lattner16af11d2004-02-09 21:03:38 +0000204 std::map<unsigned,ValueList> &ValueTab = CurFun.Values) {
Chris Lattner2079fde2001-10-13 06:41:08 +0000205 if (D->hasName()) return -1; // Is this a numbered definition?
206
207 // Yes, insert the value into the value table...
208 unsigned type = D->getType()->getUniqueID();
Chris Lattner2079fde2001-10-13 06:41:08 +0000209 //printf("Values[%d][%d] = %d\n", type, ValueTab[type].size(), D);
Chris Lattner16af11d2004-02-09 21:03:38 +0000210 ValueList &List = ValueTab[type];
211 List.push_back(D);
212 return List.size()-1;
Chris Lattner00950542001-06-06 20:29:01 +0000213}
214
Chris Lattner30c89792001-09-07 16:35:17 +0000215// TODO: FIXME when Type are not const
Chris Lattner9232b992003-04-22 18:42:41 +0000216static void InsertType(const Type *Ty, std::vector<PATypeHolder> &Types) {
Chris Lattner30c89792001-09-07 16:35:17 +0000217 Types.push_back(Ty);
218}
219
220static const Type *getTypeVal(const ValID &D, bool DoNotImprovise = false) {
Chris Lattner00950542001-06-06 20:29:01 +0000221 switch (D.Type) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000222 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000223 unsigned Num = (unsigned)D.Num;
224
225 // Module constants occupy the lowest numbered slots...
226 if (Num < CurModule.Types.size())
227 return CurModule.Types[Num];
228
229 Num -= CurModule.Types.size();
230
231 // Check that the number is within bounds...
Chris Lattner394f2eb2003-10-16 20:12:13 +0000232 if (Num <= CurFun.Types.size())
233 return CurFun.Types[Num];
Chris Lattner42c9e772001-10-20 09:32:59 +0000234 break;
Chris Lattner30c89792001-09-07 16:35:17 +0000235 }
Chris Lattnerf8dff732002-07-18 05:18:37 +0000236 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000237 std::string Name(D.Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000238 SymbolTable *SymTab = 0;
Chris Lattner6e6026b2002-11-20 18:36:02 +0000239 Value *N = 0;
240 if (inFunctionScope()) {
Chris Lattner394f2eb2003-10-16 20:12:13 +0000241 SymTab = &CurFun.CurrentFunction->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000242 N = SymTab->lookup(Type::TypeTy, Name);
243 }
Chris Lattner30c89792001-09-07 16:35:17 +0000244
245 if (N == 0) {
Chris Lattner7e708292002-06-25 16:13:24 +0000246 // Symbol table doesn't automatically chain yet... because the function
Chris Lattner30c89792001-09-07 16:35:17 +0000247 // hasn't been added to the module...
248 //
Chris Lattner6e6026b2002-11-20 18:36:02 +0000249 SymTab = &CurModule.CurrentModule->getSymbolTable();
250 N = SymTab->lookup(Type::TypeTy, Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000251 if (N == 0) break;
252 }
253
254 D.destroy(); // Free old strdup'd memory...
Chris Lattner949a3622003-07-23 15:30:06 +0000255 return cast<Type>(N);
Chris Lattner30c89792001-09-07 16:35:17 +0000256 }
257 default:
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000258 ThrowException("Internal parser error: Invalid symbol type reference!");
Chris Lattner30c89792001-09-07 16:35:17 +0000259 }
260
261 // If we reached here, we referenced either a symbol that we don't know about
262 // or an id number that hasn't been read yet. We may be referencing something
263 // forward, so just create an entry to be resolved later and get to it...
264 //
265 if (DoNotImprovise) return 0; // Do we just want a null to be returned?
266
Chris Lattner9232b992003-04-22 18:42:41 +0000267 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000268 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000269
Chris Lattner9232b992003-04-22 18:42:41 +0000270 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000271 if (I != LateResolver.end()) {
272 return I->second;
273 }
Chris Lattner30c89792001-09-07 16:35:17 +0000274
Chris Lattner82269592001-10-22 06:01:08 +0000275 Type *Typ = OpaqueType::get();
Chris Lattner9232b992003-04-22 18:42:41 +0000276 LateResolver.insert(std::make_pair(D, Typ));
Chris Lattner30c89792001-09-07 16:35:17 +0000277 return Typ;
278}
279
Chris Lattner9232b992003-04-22 18:42:41 +0000280static Value *lookupInSymbolTable(const Type *Ty, const std::string &Name) {
Chris Lattner6e6026b2002-11-20 18:36:02 +0000281 SymbolTable &SymTab =
Chris Lattner394f2eb2003-10-16 20:12:13 +0000282 inFunctionScope() ? CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner9705a152002-05-02 19:27:42 +0000283 CurModule.CurrentModule->getSymbolTable();
Chris Lattner6e6026b2002-11-20 18:36:02 +0000284 return SymTab.lookup(Ty, Name);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +0000285}
286
Chris Lattner2079fde2001-10-13 06:41:08 +0000287// getValNonImprovising - Look up the value specified by the provided type and
288// the provided ValID. If the value exists and has already been defined, return
289// it. Otherwise return null.
290//
291static Value *getValNonImprovising(const Type *Ty, const ValID &D) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000292 if (isa<FunctionType>(Ty))
293 ThrowException("Functions are not values and "
294 "must be referenced as pointers");
Chris Lattner386a3b72001-10-16 19:54:17 +0000295
Chris Lattner30c89792001-09-07 16:35:17 +0000296 switch (D.Type) {
Chris Lattner1a1cb112001-09-30 22:46:54 +0000297 case ValID::NumberVal: { // Is it a numbered definition?
Chris Lattner30c89792001-09-07 16:35:17 +0000298 unsigned type = Ty->getUniqueID();
Chris Lattner00950542001-06-06 20:29:01 +0000299 unsigned Num = (unsigned)D.Num;
300
301 // Module constants occupy the lowest numbered slots...
Chris Lattner16af11d2004-02-09 21:03:38 +0000302 std::map<unsigned,ValueList>::iterator VI = CurModule.Values.find(type);
303 if (VI != CurModule.Values.end()) {
304 if (Num < VI->second.size())
305 return VI->second[Num];
306 Num -= VI->second.size();
Chris Lattner00950542001-06-06 20:29:01 +0000307 }
308
309 // Make sure that our type is within bounds
Chris Lattner16af11d2004-02-09 21:03:38 +0000310 VI = CurFun.Values.find(type);
311 if (VI == CurFun.Values.end()) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000312
313 // Check that the number is within bounds...
Chris Lattner16af11d2004-02-09 21:03:38 +0000314 if (VI->second.size() <= Num) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000315
Chris Lattner16af11d2004-02-09 21:03:38 +0000316 return VI->second[Num];
Chris Lattner00950542001-06-06 20:29:01 +0000317 }
Chris Lattner2079fde2001-10-13 06:41:08 +0000318
Chris Lattner1a1cb112001-09-30 22:46:54 +0000319 case ValID::NameVal: { // Is it a named definition?
Chris Lattner9232b992003-04-22 18:42:41 +0000320 Value *N = lookupInSymbolTable(Ty, std::string(D.Name));
Chris Lattner2079fde2001-10-13 06:41:08 +0000321 if (N == 0) return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000322
323 D.destroy(); // Free old strdup'd memory...
324 return N;
325 }
326
Chris Lattner2079fde2001-10-13 06:41:08 +0000327 // Check to make sure that "Ty" is an integral type, and that our
328 // value will fit into the specified type...
329 case ValID::ConstSIntVal: // Is it a constant pool reference??
Chris Lattnerd78700d2002-08-16 21:14:40 +0000330 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64))
331 ThrowException("Signed integral constant '" +
332 itostr(D.ConstPool64) + "' is invalid for type '" +
333 Ty->getDescription() + "'!");
334 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000335
336 case ValID::ConstUIntVal: // Is it an unsigned const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000337 if (!ConstantUInt::isValueValidForType(Ty, D.UConstPool64)) {
338 if (!ConstantSInt::isValueValidForType(Ty, D.ConstPool64)) {
Chris Lattnerf8dff732002-07-18 05:18:37 +0000339 ThrowException("Integral constant '" + utostr(D.UConstPool64) +
340 "' is invalid or out of range!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000341 } else { // This is really a signed reference. Transmogrify.
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000342 return ConstantSInt::get(Ty, D.ConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000343 }
344 } else {
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000345 return ConstantUInt::get(Ty, D.UConstPool64);
Chris Lattner2079fde2001-10-13 06:41:08 +0000346 }
347
Chris Lattner2079fde2001-10-13 06:41:08 +0000348 case ValID::ConstFPVal: // Is it a floating point const pool reference?
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000349 if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP))
Chris Lattner2079fde2001-10-13 06:41:08 +0000350 ThrowException("FP constant invalid for type!!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000351 return ConstantFP::get(Ty, D.ConstPoolFP);
Chris Lattner2079fde2001-10-13 06:41:08 +0000352
353 case ValID::ConstNullVal: // Is it a null value?
Chris Lattner9b625032002-05-06 16:15:30 +0000354 if (!isa<PointerType>(Ty))
Chris Lattner2079fde2001-10-13 06:41:08 +0000355 ThrowException("Cannot create a a non pointer null!");
Chris Lattnere9bb2df2001-12-03 22:26:30 +0000356 return ConstantPointerNull::get(cast<PointerType>(Ty));
Chris Lattner2079fde2001-10-13 06:41:08 +0000357
Chris Lattnerd78700d2002-08-16 21:14:40 +0000358 case ValID::ConstantVal: // Fully resolved constant?
359 if (D.ConstantValue->getType() != Ty)
360 ThrowException("Constant expression type different from required type!");
361 return D.ConstantValue;
362
Chris Lattner30c89792001-09-07 16:35:17 +0000363 default:
364 assert(0 && "Unhandled case!");
Chris Lattner2079fde2001-10-13 06:41:08 +0000365 return 0;
Chris Lattner00950542001-06-06 20:29:01 +0000366 } // End of switch
367
Chris Lattner2079fde2001-10-13 06:41:08 +0000368 assert(0 && "Unhandled case!");
369 return 0;
370}
371
372
373// getVal - This function is identical to getValNonImprovising, except that if a
374// value is not already defined, it "improvises" by creating a placeholder var
375// that looks and acts just like the requested variable. When the value is
376// defined later, all uses of the placeholder variable are replaced with the
377// real thing.
378//
379static Value *getVal(const Type *Ty, const ValID &D) {
380 assert(Ty != Type::TypeTy && "Should use getTypeVal for types!");
381
382 // See if the value has already been defined...
383 Value *V = getValNonImprovising(Ty, D);
384 if (V) return V;
Chris Lattner00950542001-06-06 20:29:01 +0000385
386 // If we reached here, we referenced either a symbol that we don't know about
387 // or an id number that hasn't been read yet. We may be referencing something
388 // forward, so just create an entry to be resolved later and get to it...
389 //
Chris Lattner00950542001-06-06 20:29:01 +0000390 Value *d = 0;
Chris Lattner30c89792001-09-07 16:35:17 +0000391 switch (Ty->getPrimitiveID()) {
392 case Type::LabelTyID: d = new BBPlaceHolder(Ty, D); break;
Chris Lattner30c89792001-09-07 16:35:17 +0000393 default: d = new ValuePlaceHolder(Ty, D); break;
Chris Lattner00950542001-06-06 20:29:01 +0000394 }
395
396 assert(d != 0 && "How did we not make something?");
Chris Lattner79df7c02002-03-26 18:01:55 +0000397 if (inFunctionScope())
Chris Lattner394f2eb2003-10-16 20:12:13 +0000398 InsertValue(d, CurFun.LateResolveValues);
Chris Lattner386a3b72001-10-16 19:54:17 +0000399 else
400 InsertValue(d, CurModule.LateResolveValues);
Chris Lattner00950542001-06-06 20:29:01 +0000401 return d;
402}
403
404
405//===----------------------------------------------------------------------===//
406// Code to handle forward references in instructions
407//===----------------------------------------------------------------------===//
408//
409// This code handles the late binding needed with statements that reference
410// values not defined yet... for example, a forward branch, or the PHI node for
411// a loop body.
412//
Chris Lattner394f2eb2003-10-16 20:12:13 +0000413// This keeps a table (CurFun.LateResolveValues) of all such forward references
Chris Lattner00950542001-06-06 20:29:01 +0000414// and back patchs after we are done.
415//
416
417// ResolveDefinitions - If we could not resolve some defs at parsing
418// time (forward branches, phi functions for loops, etc...) resolve the
419// defs now...
420//
Chris Lattner16af11d2004-02-09 21:03:38 +0000421static void ResolveDefinitions(std::map<unsigned,ValueList> &LateResolvers,
422 std::map<unsigned,ValueList> *FutureLateResolvers) {
Chris Lattner00950542001-06-06 20:29:01 +0000423 // Loop over LateResolveDefs fixing up stuff that couldn't be resolved
Chris Lattner16af11d2004-02-09 21:03:38 +0000424 for (std::map<unsigned,ValueList>::iterator LRI = LateResolvers.begin(),
425 E = LateResolvers.end(); LRI != E; ++LRI) {
426 ValueList &List = LRI->second;
427 while (!List.empty()) {
428 Value *V = List.back();
429 List.pop_back();
Chris Lattner386a3b72001-10-16 19:54:17 +0000430 assert(!isa<Type>(V) && "Types should be in LateResolveTypes!");
Chris Lattner00950542001-06-06 20:29:01 +0000431 ValID &DID = getValIDFromPlaceHolder(V);
432
Chris Lattner16af11d2004-02-09 21:03:38 +0000433 Value *TheRealValue =
434 getValNonImprovising(Type::getUniqueIDType(LRI->first), DID);
Chris Lattner386a3b72001-10-16 19:54:17 +0000435 if (TheRealValue) {
436 V->replaceAllUsesWith(TheRealValue);
437 delete V;
438 } else if (FutureLateResolvers) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000439 // Functions have their unresolved items forwarded to the module late
Chris Lattner386a3b72001-10-16 19:54:17 +0000440 // resolver table
441 InsertValue(V, *FutureLateResolvers);
442 } else {
Chris Lattner9705a152002-05-02 19:27:42 +0000443 if (DID.Type == ValID::NameVal)
Chris Lattner30c89792001-09-07 16:35:17 +0000444 ThrowException("Reference to an invalid definition: '" +DID.getName()+
445 "' of type '" + V->getType()->getDescription() + "'",
446 getLineNumFromPlaceHolder(V));
447 else
448 ThrowException("Reference to an invalid definition: #" +
449 itostr(DID.Num) + " of type '" +
450 V->getType()->getDescription() + "'",
451 getLineNumFromPlaceHolder(V));
452 }
Chris Lattner00950542001-06-06 20:29:01 +0000453 }
454 }
455
456 LateResolvers.clear();
457}
458
Chris Lattner4a42e902001-10-22 05:56:09 +0000459// ResolveTypeTo - A brand new type was just declared. This means that (if
460// name is not null) things referencing Name can be resolved. Otherwise, things
461// refering to the number can be resolved. Do this now.
Chris Lattner00950542001-06-06 20:29:01 +0000462//
Chris Lattner4a42e902001-10-22 05:56:09 +0000463static void ResolveTypeTo(char *Name, const Type *ToTy) {
Chris Lattner9232b992003-04-22 18:42:41 +0000464 std::vector<PATypeHolder> &Types = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000465 CurFun.Types : CurModule.Types;
Chris Lattner00950542001-06-06 20:29:01 +0000466
Chris Lattner4a42e902001-10-22 05:56:09 +0000467 ValID D;
468 if (Name) D = ValID::create(Name);
469 else D = ValID::create((int)Types.size());
Chris Lattner30c89792001-09-07 16:35:17 +0000470
Chris Lattner9232b992003-04-22 18:42:41 +0000471 std::map<ValID, PATypeHolder> &LateResolver = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000472 CurFun.LateResolveTypes : CurModule.LateResolveTypes;
Chris Lattner4a42e902001-10-22 05:56:09 +0000473
Chris Lattner9232b992003-04-22 18:42:41 +0000474 std::map<ValID, PATypeHolder>::iterator I = LateResolver.find(D);
Chris Lattner4a42e902001-10-22 05:56:09 +0000475 if (I != LateResolver.end()) {
Chris Lattner51727be2002-06-04 21:58:56 +0000476 ((DerivedType*)I->second.get())->refineAbstractTypeTo(ToTy);
Chris Lattner4a42e902001-10-22 05:56:09 +0000477 LateResolver.erase(I);
478 }
479}
480
481// ResolveTypes - At this point, all types should be resolved. Any that aren't
482// are errors.
483//
Chris Lattner9232b992003-04-22 18:42:41 +0000484static void ResolveTypes(std::map<ValID, PATypeHolder> &LateResolveTypes) {
Chris Lattner4a42e902001-10-22 05:56:09 +0000485 if (!LateResolveTypes.empty()) {
Chris Lattner82269592001-10-22 06:01:08 +0000486 const ValID &DID = LateResolveTypes.begin()->first;
Chris Lattner4a42e902001-10-22 05:56:09 +0000487
488 if (DID.Type == ValID::NameVal)
Chris Lattner82269592001-10-22 06:01:08 +0000489 ThrowException("Reference to an invalid type: '" +DID.getName() + "'");
Chris Lattner4a42e902001-10-22 05:56:09 +0000490 else
Chris Lattner82269592001-10-22 06:01:08 +0000491 ThrowException("Reference to an invalid type: #" + itostr(DID.Num));
Chris Lattner30c89792001-09-07 16:35:17 +0000492 }
493}
494
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000495
Chris Lattner1781aca2001-09-18 04:00:54 +0000496// setValueName - Set the specified value to the name given. The name may be
497// null potentially, in which case this is a noop. The string passed in is
498// assumed to be a malloc'd string buffer, and is freed by this function.
499//
Chris Lattnerb7474512001-10-03 15:39:04 +0000500// This function returns true if the value has already been defined, but is
501// allowed to be redefined in the specified context. If the name is a new name
502// for the typeplane, false is returned.
503//
504static bool setValueName(Value *V, char *NameStr) {
505 if (NameStr == 0) return false;
Chris Lattner386a3b72001-10-16 19:54:17 +0000506
Chris Lattner9232b992003-04-22 18:42:41 +0000507 std::string Name(NameStr); // Copy string
Chris Lattner1781aca2001-09-18 04:00:54 +0000508 free(NameStr); // Free old string
509
Chris Lattner2079fde2001-10-13 06:41:08 +0000510 if (V->getType() == Type::VoidTy)
511 ThrowException("Can't assign name '" + Name +
512 "' to a null valued instruction!");
513
Chris Lattner6e6026b2002-11-20 18:36:02 +0000514 SymbolTable &ST = inFunctionScope() ?
Chris Lattner394f2eb2003-10-16 20:12:13 +0000515 CurFun.CurrentFunction->getSymbolTable() :
Chris Lattner6e6026b2002-11-20 18:36:02 +0000516 CurModule.CurrentModule->getSymbolTable();
Chris Lattner30c89792001-09-07 16:35:17 +0000517
Chris Lattner6e6026b2002-11-20 18:36:02 +0000518 Value *Existing = ST.lookup(V->getType(), Name);
Chris Lattner30c89792001-09-07 16:35:17 +0000519 if (Existing) { // Inserting a name that is already defined???
520 // There is only one case where this is allowed: when we are refining an
521 // opaque type. In this case, Existing will be an opaque type.
Chris Lattner949a3622003-07-23 15:30:06 +0000522 if (const Type *Ty = dyn_cast<Type>(Existing)) {
Chris Lattner51727be2002-06-04 21:58:56 +0000523 if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Ty)) {
Chris Lattner30c89792001-09-07 16:35:17 +0000524 // We ARE replacing an opaque type!
Chris Lattner51727be2002-06-04 21:58:56 +0000525 ((OpaqueType*)OpTy)->refineAbstractTypeTo(cast<Type>(V));
Chris Lattnerb7474512001-10-03 15:39:04 +0000526 return true;
Chris Lattner30c89792001-09-07 16:35:17 +0000527 }
Chris Lattnerdf7306f2001-10-03 01:49:25 +0000528 }
Chris Lattner30c89792001-09-07 16:35:17 +0000529
Chris Lattner9636a912001-10-01 16:18:37 +0000530 // Otherwise, we are a simple redefinition of a value, check to see if it
531 // is defined the same as the old one...
Chris Lattneradf99702003-03-03 23:28:55 +0000532 if (const Type *Ty = dyn_cast<Type>(Existing)) {
533 if (Ty == cast<Type>(V)) return true; // Yes, it's equal.
Chris Lattner699f1eb2002-08-14 17:12:33 +0000534 // std::cerr << "Type: " << Ty->getDescription() << " != "
Chris Lattner949a3622003-07-23 15:30:06 +0000535 // << cast<Type>(V)->getDescription() << "!\n";
Chris Lattneradf99702003-03-03 23:28:55 +0000536 } else if (const Constant *C = dyn_cast<Constant>(Existing)) {
537 if (C == V) return true; // Constants are equal to themselves
Chris Lattnerb7474512001-10-03 15:39:04 +0000538 } else if (GlobalVariable *EGV = dyn_cast<GlobalVariable>(Existing)) {
Chris Lattner43efcbf2001-10-03 19:35:57 +0000539 // We are allowed to redefine a global variable in two circumstances:
540 // 1. If at least one of the globals is uninitialized or
541 // 2. If both initializers have the same value.
542 //
Chris Lattner89219832001-10-03 19:35:04 +0000543 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000544 if (!EGV->hasInitializer() || !GV->hasInitializer() ||
545 EGV->getInitializer() == GV->getInitializer()) {
Chris Lattnerb7474512001-10-03 15:39:04 +0000546
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000547 // Make sure the existing global version gets the initializer! Make
548 // sure that it also gets marked const if the new version is.
Chris Lattner89219832001-10-03 19:35:04 +0000549 if (GV->hasInitializer() && !EGV->hasInitializer())
550 EGV->setInitializer(GV->getInitializer());
Chris Lattnerbb3e5d42003-02-02 16:40:20 +0000551 if (GV->isConstant())
552 EGV->setConstant(true);
Chris Lattner4ad02e72003-04-16 20:28:45 +0000553 EGV->setLinkage(GV->getLinkage());
Chris Lattner89219832001-10-03 19:35:04 +0000554
Chris Lattner2079fde2001-10-13 06:41:08 +0000555 delete GV; // Destroy the duplicate!
Chris Lattner89219832001-10-03 19:35:04 +0000556 return true; // They are equivalent!
557 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000558 }
Chris Lattner9636a912001-10-01 16:18:37 +0000559 }
Chris Lattner0b268fb2003-11-25 03:54:16 +0000560
Chris Lattner2079fde2001-10-13 06:41:08 +0000561 ThrowException("Redefinition of value named '" + Name + "' in the '" +
Chris Lattner30c89792001-09-07 16:35:17 +0000562 V->getType()->getDescription() + "' type plane!");
Chris Lattner93750fa2001-07-28 17:48:55 +0000563 }
Chris Lattner00950542001-06-06 20:29:01 +0000564
Chris Lattner15b69722003-11-12 04:40:30 +0000565 // Set the name
Chris Lattner6e6026b2002-11-20 18:36:02 +0000566 V->setName(Name, &ST);
Chris Lattner15b69722003-11-12 04:40:30 +0000567
568 // If we're in function scope
569 if (inFunctionScope()) {
570 // Look up the symbol in the function's local symboltable
571 Existing = CurFun.LocalSymtab.lookup(V->getType(),Name);
572
573 // If it already exists
574 if (Existing) {
Chris Lattner15b69722003-11-12 04:40:30 +0000575 // Bail
576 ThrowException("Redefinition of value named '" + Name + "' in the '" +
577 V->getType()->getDescription() + "' type plane!");
578
579 // otherwise, since it doesn't exist
580 } else {
581 // Insert it.
582 CurFun.LocalSymtab.insert(V);
583 }
584 }
Chris Lattnerb7474512001-10-03 15:39:04 +0000585 return false;
Chris Lattner00950542001-06-06 20:29:01 +0000586}
587
Chris Lattner8896eda2001-07-09 19:38:36 +0000588
Chris Lattner30c89792001-09-07 16:35:17 +0000589//===----------------------------------------------------------------------===//
590// Code for handling upreferences in type names...
Chris Lattner8896eda2001-07-09 19:38:36 +0000591//
Chris Lattner8896eda2001-07-09 19:38:36 +0000592
Chris Lattner3b073862004-02-09 03:19:29 +0000593// TypeContains - Returns true if Ty directly contains E in it.
Chris Lattner30c89792001-09-07 16:35:17 +0000594//
595static bool TypeContains(const Type *Ty, const Type *E) {
Chris Lattner3b073862004-02-09 03:19:29 +0000596 return find(Ty->subtype_begin(), Ty->subtype_end(), E) != Ty->subtype_end();
597}
598
599namespace {
600 struct UpRefRecord {
601 // NestingLevel - The number of nesting levels that need to be popped before
602 // this type is resolved.
603 unsigned NestingLevel;
604
605 // LastContainedTy - This is the type at the current binding level for the
606 // type. Every time we reduce the nesting level, this gets updated.
607 const Type *LastContainedTy;
608
609 // UpRefTy - This is the actual opaque type that the upreference is
610 // represented with.
611 OpaqueType *UpRefTy;
612
613 UpRefRecord(unsigned NL, OpaqueType *URTy)
614 : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) {}
615 };
Chris Lattner30c89792001-09-07 16:35:17 +0000616}
Chris Lattner698b56e2001-07-20 19:15:08 +0000617
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000618// UpRefs - A list of the outstanding upreferences that need to be resolved.
Chris Lattner3b073862004-02-09 03:19:29 +0000619static std::vector<UpRefRecord> UpRefs;
Chris Lattner30c89792001-09-07 16:35:17 +0000620
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000621/// HandleUpRefs - Every time we finish a new layer of types, this function is
622/// called. It loops through the UpRefs vector, which is a list of the
623/// currently active types. For each type, if the up reference is contained in
624/// the newly completed type, we decrement the level count. When the level
625/// count reaches zero, the upreferenced type is the type that is passed in:
626/// thus we can complete the cycle.
627///
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000628static PATypeHolder HandleUpRefs(const Type *ty) {
Chris Lattner2c37c182004-02-09 03:03:10 +0000629 if (!ty->isAbstract()) return ty;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000630 PATypeHolder Ty(ty);
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000631 UR_OUT("Type '" << Ty->getDescription() <<
Chris Lattner5084d032001-11-02 07:46:26 +0000632 "' newly formed. Resolving upreferences.\n" <<
633 UpRefs.size() << " upreferences active!\n");
Chris Lattner026a8ce2004-02-09 18:53:54 +0000634
635 // If we find any resolvable upreferences (i.e., those whose NestingLevel goes
636 // to zero), we resolve them all together before we resolve them to Ty. At
637 // the end of the loop, if there is anything to resolve to Ty, it will be in
638 // this variable.
639 OpaqueType *TypeToResolve = 0;
640
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000641 for (unsigned i = 0; i != UpRefs.size(); ++i) {
Chris Lattner5084d032001-11-02 07:46:26 +0000642 UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", "
Chris Lattner30c89792001-09-07 16:35:17 +0000643 << UpRefs[i].second->getDescription() << ") = "
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000644 << (TypeContains(Ty, UpRefs[i].second) ? "true" : "false") << "\n");
Chris Lattner3b073862004-02-09 03:19:29 +0000645 if (TypeContains(Ty, UpRefs[i].LastContainedTy)) {
646 // Decrement level of upreference
647 unsigned Level = --UpRefs[i].NestingLevel;
648 UpRefs[i].LastContainedTy = Ty;
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000649 UR_OUT(" Uplevel Ref Level = " << Level << "\n");
Chris Lattner30c89792001-09-07 16:35:17 +0000650 if (Level == 0) { // Upreference should be resolved!
Chris Lattner026a8ce2004-02-09 18:53:54 +0000651 if (!TypeToResolve) {
652 TypeToResolve = UpRefs[i].UpRefTy;
653 } else {
654 UR_OUT(" * Resolving upreference for "
655 << UpRefs[i].second->getDescription() << "\n";
656 std::string OldName = UpRefs[i].UpRefTy->getDescription());
657 UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve);
658 UR_OUT(" * Type '" << OldName << "' refined upreference to: "
659 << (const void*)Ty << ", " << Ty->getDescription() << "\n");
660 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000661 UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list...
662 --i; // Do not skip the next element...
Chris Lattner30c89792001-09-07 16:35:17 +0000663 }
664 }
Chris Lattner8896eda2001-07-09 19:38:36 +0000665 }
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000666
Chris Lattner026a8ce2004-02-09 18:53:54 +0000667 if (TypeToResolve) {
668 UR_OUT(" * Resolving upreference for "
669 << UpRefs[i].second->getDescription() << "\n";
Chris Lattner16af11d2004-02-09 21:03:38 +0000670 std::string OldName = TypeToResolve->getDescription());
Chris Lattner026a8ce2004-02-09 18:53:54 +0000671 TypeToResolve->refineAbstractTypeTo(Ty);
672 }
673
Chris Lattner8896eda2001-07-09 19:38:36 +0000674 return Ty;
675}
676
Chris Lattner30c89792001-09-07 16:35:17 +0000677
Chris Lattner00950542001-06-06 20:29:01 +0000678//===----------------------------------------------------------------------===//
679// RunVMAsmParser - Define an interface to this parser
680//===----------------------------------------------------------------------===//
681//
Chris Lattner9232b992003-04-22 18:42:41 +0000682Module *RunVMAsmParser(const std::string &Filename, FILE *F) {
Chris Lattner00950542001-06-06 20:29:01 +0000683 llvmAsmin = F;
Chris Lattnera2850432001-07-22 18:36:00 +0000684 CurFilename = Filename;
Chris Lattner00950542001-06-06 20:29:01 +0000685 llvmAsmlineno = 1; // Reset the current line number...
Chris Lattner99e7ab72003-10-18 05:53:13 +0000686 ObsoleteVarArgs = false;
Chris Lattner00950542001-06-06 20:29:01 +0000687
Chris Lattner75f20532003-04-22 18:02:52 +0000688 // Allocate a new module to read
689 CurModule.CurrentModule = new Module(Filename);
Chris Lattner42570982003-11-26 07:24:58 +0000690
691 try {
692 yyparse(); // Parse the file.
693 } catch (...) {
694 // Clear the symbol table so it doesn't complain when it
695 // gets destructed
696 CurFun.LocalSymtab.clear();
697 throw;
698 }
Chris Lattner99e7ab72003-10-18 05:53:13 +0000699
Chris Lattner00950542001-06-06 20:29:01 +0000700 Module *Result = ParserResult;
Chris Lattner99e7ab72003-10-18 05:53:13 +0000701
702 // Check to see if they called va_start but not va_arg..
703 if (!ObsoleteVarArgs)
704 if (Function *F = Result->getNamedFunction("llvm.va_start"))
705 if (F->asize() == 1) {
706 std::cerr << "WARNING: this file uses obsolete features. "
707 << "Assemble and disassemble to update it.\n";
708 ObsoleteVarArgs = true;
709 }
710
711
712 if (ObsoleteVarArgs) {
713 // If the user is making use of obsolete varargs intrinsics, adjust them for
714 // the user.
715 if (Function *F = Result->getNamedFunction("llvm.va_start")) {
716 assert(F->asize() == 1 && "Obsolete va_start takes 1 argument!");
717
718 const Type *RetTy = F->getFunctionType()->getParamType(0);
719 RetTy = cast<PointerType>(RetTy)->getElementType();
720 Function *NF = Result->getOrInsertFunction("llvm.va_start", RetTy, 0);
721
722 while (!F->use_empty()) {
723 CallInst *CI = cast<CallInst>(F->use_back());
724 Value *V = new CallInst(NF, "", CI);
725 new StoreInst(V, CI->getOperand(1), CI);
726 CI->getParent()->getInstList().erase(CI);
727 }
728 Result->getFunctionList().erase(F);
729 }
730
731 if (Function *F = Result->getNamedFunction("llvm.va_end")) {
732 assert(F->asize() == 1 && "Obsolete va_end takes 1 argument!");
733 const Type *ArgTy = F->getFunctionType()->getParamType(0);
734 ArgTy = cast<PointerType>(ArgTy)->getElementType();
735 Function *NF = Result->getOrInsertFunction("llvm.va_end", Type::VoidTy,
736 ArgTy, 0);
737
738 while (!F->use_empty()) {
739 CallInst *CI = cast<CallInst>(F->use_back());
740 Value *V = new LoadInst(CI->getOperand(1), "", CI);
741 new CallInst(NF, V, "", CI);
742 CI->getParent()->getInstList().erase(CI);
743 }
744 Result->getFunctionList().erase(F);
745 }
746
747 if (Function *F = Result->getNamedFunction("llvm.va_copy")) {
748 assert(F->asize() == 2 && "Obsolete va_copy takes 2 argument!");
749 const Type *ArgTy = F->getFunctionType()->getParamType(0);
750 ArgTy = cast<PointerType>(ArgTy)->getElementType();
751 Function *NF = Result->getOrInsertFunction("llvm.va_copy", ArgTy,
752 ArgTy, 0);
753
754 while (!F->use_empty()) {
755 CallInst *CI = cast<CallInst>(F->use_back());
756 Value *V = new CallInst(NF, CI->getOperand(2), "", CI);
757 new StoreInst(V, CI->getOperand(1), CI);
758 CI->getParent()->getInstList().erase(CI);
759 }
760 Result->getFunctionList().erase(F);
761 }
762 }
763
Chris Lattner00950542001-06-06 20:29:01 +0000764 llvmAsmin = stdin; // F is about to go away, don't use it anymore...
765 ParserResult = 0;
766
767 return Result;
768}
769
Brian Gaeked0fde302003-11-11 22:41:34 +0000770} // End llvm namespace
771
772using namespace llvm;
773
Chris Lattner00950542001-06-06 20:29:01 +0000774%}
775
776%union {
Brian Gaeked0fde302003-11-11 22:41:34 +0000777 llvm::Module *ModuleVal;
778 llvm::Function *FunctionVal;
779 std::pair<llvm::PATypeHolder*, char*> *ArgVal;
780 llvm::BasicBlock *BasicBlockVal;
781 llvm::TerminatorInst *TermInstVal;
782 llvm::Instruction *InstVal;
783 llvm::Constant *ConstVal;
Chris Lattner00950542001-06-06 20:29:01 +0000784
Brian Gaeked0fde302003-11-11 22:41:34 +0000785 const llvm::Type *PrimType;
786 llvm::PATypeHolder *TypeVal;
787 llvm::Value *ValueVal;
Chris Lattner30c89792001-09-07 16:35:17 +0000788
Brian Gaeked0fde302003-11-11 22:41:34 +0000789 std::vector<std::pair<llvm::PATypeHolder*,char*> > *ArgList;
790 std::vector<llvm::Value*> *ValueList;
791 std::list<llvm::PATypeHolder> *TypeList;
792 std::list<std::pair<llvm::Value*,
793 llvm::BasicBlock*> > *PHIList; // Represent the RHS of PHI node
794 std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable;
795 std::vector<llvm::Constant*> *ConstVector;
Chris Lattner00950542001-06-06 20:29:01 +0000796
Brian Gaeked0fde302003-11-11 22:41:34 +0000797 llvm::GlobalValue::LinkageTypes Linkage;
Chris Lattner30c89792001-09-07 16:35:17 +0000798 int64_t SInt64Val;
799 uint64_t UInt64Val;
800 int SIntVal;
801 unsigned UIntVal;
802 double FPVal;
Chris Lattner1781aca2001-09-18 04:00:54 +0000803 bool BoolVal;
Chris Lattner00950542001-06-06 20:29:01 +0000804
Chris Lattner30c89792001-09-07 16:35:17 +0000805 char *StrVal; // This memory is strdup'd!
Brian Gaeked0fde302003-11-11 22:41:34 +0000806 llvm::ValID ValIDVal; // strdup'd memory maybe!
Chris Lattner00950542001-06-06 20:29:01 +0000807
Brian Gaeked0fde302003-11-11 22:41:34 +0000808 llvm::Instruction::BinaryOps BinaryOpVal;
809 llvm::Instruction::TermOps TermOpVal;
810 llvm::Instruction::MemoryOps MemOpVal;
811 llvm::Instruction::OtherOps OtherOpVal;
812 llvm::Module::Endianness Endianness;
Chris Lattner00950542001-06-06 20:29:01 +0000813}
814
Chris Lattner79df7c02002-03-26 18:01:55 +0000815%type <ModuleVal> Module FunctionList
816%type <FunctionVal> Function FunctionProto FunctionHeader BasicBlockList
Chris Lattner00950542001-06-06 20:29:01 +0000817%type <BasicBlockVal> BasicBlock InstructionList
818%type <TermInstVal> BBTerminatorInst
819%type <InstVal> Inst InstVal MemoryInst
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000820%type <ConstVal> ConstVal ConstExpr
Chris Lattner6cdb0112001-11-26 16:54:11 +0000821%type <ConstVector> ConstVector
Chris Lattner46748042002-04-09 19:41:42 +0000822%type <ArgList> ArgList ArgListH
823%type <ArgVal> ArgVal
Chris Lattnerc24d2082001-06-11 15:04:20 +0000824%type <PHIList> PHIList
Chris Lattnerab5ac6b2001-07-08 23:22:50 +0000825%type <ValueList> ValueRefList ValueRefListE // For call param lists
Chris Lattner6cdb0112001-11-26 16:54:11 +0000826%type <ValueList> IndexList // For GEP derived indices
Chris Lattner30c89792001-09-07 16:35:17 +0000827%type <TypeList> TypeListI ArgTypeListI
Chris Lattner00950542001-06-06 20:29:01 +0000828%type <JumpTable> JumpTable
Chris Lattner4ad02e72003-04-16 20:28:45 +0000829%type <BoolVal> GlobalType // GLOBAL or CONSTANT?
Chris Lattner15c9c032003-09-08 18:20:29 +0000830%type <BoolVal> OptVolatile // 'volatile' or not
Chris Lattner4ad02e72003-04-16 20:28:45 +0000831%type <Linkage> OptLinkage
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000832%type <Endianness> BigOrLittle
Chris Lattner00950542001-06-06 20:29:01 +0000833
Chris Lattner2079fde2001-10-13 06:41:08 +0000834// ValueRef - Unresolved reference to a definition or BB
835%type <ValIDVal> ValueRef ConstValueRef SymbolicValueRef
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +0000836%type <ValueVal> ResolvedVal // <type> <valref> pair
Chris Lattner00950542001-06-06 20:29:01 +0000837// Tokens and types for handling constant integer values
838//
839// ESINT64VAL - A negative number within long long range
840%token <SInt64Val> ESINT64VAL
841
842// EUINT64VAL - A positive number within uns. long long range
843%token <UInt64Val> EUINT64VAL
844%type <SInt64Val> EINT64VAL
845
846%token <SIntVal> SINTVAL // Signed 32 bit ints...
847%token <UIntVal> UINTVAL // Unsigned 32 bit ints...
848%type <SIntVal> INTVAL
Chris Lattner3d52b2f2001-07-15 00:17:01 +0000849%token <FPVal> FPVAL // Float or Double constant
Chris Lattner00950542001-06-06 20:29:01 +0000850
851// Built in types...
Chris Lattner30c89792001-09-07 16:35:17 +0000852%type <TypeVal> Types TypesV UpRTypes UpRTypesV
853%type <PrimType> SIntType UIntType IntType FPType PrimType // Classifications
Chris Lattner30c89792001-09-07 16:35:17 +0000854%token <PrimType> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
855%token <PrimType> FLOAT DOUBLE TYPE LABEL
Chris Lattner00950542001-06-06 20:29:01 +0000856
Chris Lattner6c23f572003-08-22 05:42:10 +0000857%token <StrVal> VAR_ID LABELSTR STRINGCONSTANT
858%type <StrVal> Name OptName OptAssign
Chris Lattner00950542001-06-06 20:29:01 +0000859
860
Chris Lattner91ef4602004-03-31 03:49:47 +0000861%token IMPLEMENTATION ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK
Chris Lattner15c9c032003-09-08 18:20:29 +0000862%token DECLARE GLOBAL CONSTANT VOLATILE
Chris Lattnera58e3a12004-02-08 21:48:25 +0000863%token TO DOTDOTDOT NULL_TOK CONST INTERNAL LINKONCE WEAK APPENDING
Chris Lattnerb9bcbb52003-04-22 19:07:06 +0000864%token OPAQUE NOT EXTERNAL TARGET ENDIAN POINTERSIZE LITTLE BIG
Chris Lattner00950542001-06-06 20:29:01 +0000865
866// Basic Block Terminating Operators
Chris Lattner36143fc2003-09-08 18:54:55 +0000867%token <TermOpVal> RET BR SWITCH INVOKE UNWIND
Chris Lattner00950542001-06-06 20:29:01 +0000868
Chris Lattner00950542001-06-06 20:29:01 +0000869// Binary Operators
870%type <BinaryOpVal> BinaryOps // all the binary operators
Chris Lattner4a6482b2002-09-10 19:57:26 +0000871%type <BinaryOpVal> ArithmeticOps LogicalOps SetCondOps // Binops Subcatagories
Chris Lattner42c9e772001-10-20 09:32:59 +0000872%token <BinaryOpVal> ADD SUB MUL DIV REM AND OR XOR
Chris Lattner027dcc52001-07-08 21:10:27 +0000873%token <BinaryOpVal> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comarators
Chris Lattner00950542001-06-06 20:29:01 +0000874
875// Memory Instructions
Vikram S. Adved3f7eb02002-07-14 22:59:28 +0000876%token <MemOpVal> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Chris Lattner00950542001-06-06 20:29:01 +0000877
Chris Lattner027dcc52001-07-08 21:10:27 +0000878// Other Operators
879%type <OtherOpVal> ShiftOps
Chris Lattner76f0ff32004-03-12 05:51:36 +0000880%token <OtherOpVal> PHI_TOK CALL CAST SELECT SHL SHR VAARG VANEXT
Chris Lattner99e7ab72003-10-18 05:53:13 +0000881%token VA_ARG // FIXME: OBSOLETE
Chris Lattner027dcc52001-07-08 21:10:27 +0000882
Chris Lattner00950542001-06-06 20:29:01 +0000883%start Module
884%%
885
886// Handle constant integer size restriction and conversion...
887//
Chris Lattner51727be2002-06-04 21:58:56 +0000888INTVAL : SINTVAL;
Chris Lattner00950542001-06-06 20:29:01 +0000889INTVAL : UINTVAL {
890 if ($1 > (uint32_t)INT32_MAX) // Outside of my range!
891 ThrowException("Value too large for type!");
892 $$ = (int32_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000893};
Chris Lattner00950542001-06-06 20:29:01 +0000894
895
Chris Lattner51727be2002-06-04 21:58:56 +0000896EINT64VAL : ESINT64VAL; // These have same type and can't cause problems...
Chris Lattner00950542001-06-06 20:29:01 +0000897EINT64VAL : EUINT64VAL {
898 if ($1 > (uint64_t)INT64_MAX) // Outside of my range!
899 ThrowException("Value too large for type!");
900 $$ = (int64_t)$1;
Chris Lattner51727be2002-06-04 21:58:56 +0000901};
Chris Lattner00950542001-06-06 20:29:01 +0000902
Chris Lattner00950542001-06-06 20:29:01 +0000903// Operations that are notably excluded from this list include:
904// RET, BR, & SWITCH because they end basic blocks and are treated specially.
905//
Chris Lattner4a6482b2002-09-10 19:57:26 +0000906ArithmeticOps: ADD | SUB | MUL | DIV | REM;
907LogicalOps : AND | OR | XOR;
908SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
909BinaryOps : ArithmeticOps | LogicalOps | SetCondOps;
910
Chris Lattner51727be2002-06-04 21:58:56 +0000911ShiftOps : SHL | SHR;
Chris Lattner00950542001-06-06 20:29:01 +0000912
Chris Lattnere98dda62001-07-14 06:10:16 +0000913// These are some types that allow classification if we only want a particular
914// thing... for example, only a signed, unsigned, or integral type.
Chris Lattner51727be2002-06-04 21:58:56 +0000915SIntType : LONG | INT | SHORT | SBYTE;
916UIntType : ULONG | UINT | USHORT | UBYTE;
917IntType : SIntType | UIntType;
918FPType : FLOAT | DOUBLE;
Chris Lattner00950542001-06-06 20:29:01 +0000919
Chris Lattnere98dda62001-07-14 06:10:16 +0000920// OptAssign - Value producing statements have an optional assignment component
Chris Lattner6c23f572003-08-22 05:42:10 +0000921OptAssign : Name '=' {
Chris Lattner00950542001-06-06 20:29:01 +0000922 $$ = $1;
923 }
924 | /*empty*/ {
925 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +0000926 };
Chris Lattner00950542001-06-06 20:29:01 +0000927
Chris Lattner4ad02e72003-04-16 20:28:45 +0000928OptLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } |
929 LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } |
Chris Lattner72ac148d2003-10-16 18:29:00 +0000930 WEAK { $$ = GlobalValue::WeakLinkage; } |
Chris Lattner4ad02e72003-04-16 20:28:45 +0000931 APPENDING { $$ = GlobalValue::AppendingLinkage; } |
932 /*empty*/ { $$ = GlobalValue::ExternalLinkage; };
Chris Lattner30c89792001-09-07 16:35:17 +0000933
934//===----------------------------------------------------------------------===//
935// Types includes all predefined types... except void, because it can only be
Chris Lattner7e708292002-06-25 16:13:24 +0000936// used in specific contexts (function returning void for example). To have
Chris Lattner30c89792001-09-07 16:35:17 +0000937// access to it, a user must explicitly use TypesV.
938//
939
940// TypesV includes all of 'Types', but it also includes the void type.
Chris Lattner51727be2002-06-04 21:58:56 +0000941TypesV : Types | VOID { $$ = new PATypeHolder($1); };
942UpRTypesV : UpRTypes | VOID { $$ = new PATypeHolder($1); };
Chris Lattner30c89792001-09-07 16:35:17 +0000943
944Types : UpRTypes {
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000945 if (!UpRefs.empty())
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000946 ThrowException("Invalid upreference in type: " + (*$1)->getDescription());
947 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +0000948 };
Chris Lattner30c89792001-09-07 16:35:17 +0000949
950
951// Derived types are added later...
952//
Chris Lattner51727be2002-06-04 21:58:56 +0000953PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
954PrimType : LONG | ULONG | FLOAT | DOUBLE | TYPE | LABEL;
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000955UpRTypes : OPAQUE {
956 $$ = new PATypeHolder(OpaqueType::get());
957 }
958 | PrimType {
959 $$ = new PATypeHolder($1);
Chris Lattner51727be2002-06-04 21:58:56 +0000960 };
Chris Lattnerd78700d2002-08-16 21:14:40 +0000961UpRTypes : SymbolicValueRef { // Named types are also simple types...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000962 $$ = new PATypeHolder(getTypeVal($1));
Chris Lattner51727be2002-06-04 21:58:56 +0000963};
Chris Lattner30c89792001-09-07 16:35:17 +0000964
Chris Lattner30c89792001-09-07 16:35:17 +0000965// Include derived types in the Types production.
966//
967UpRTypes : '\\' EUINT64VAL { // Type UpReference
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000968 if ($2 > (uint64_t)~0U) ThrowException("Value out of range!");
Chris Lattner30c89792001-09-07 16:35:17 +0000969 OpaqueType *OT = OpaqueType::get(); // Use temporary placeholder
Chris Lattner3b073862004-02-09 03:19:29 +0000970 UpRefs.push_back(UpRefRecord((unsigned)$2, OT)); // Add to vector...
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000971 $$ = new PATypeHolder(OT);
Chris Lattner30c89792001-09-07 16:35:17 +0000972 UR_OUT("New Upreference!\n");
973 }
Chris Lattner79df7c02002-03-26 18:01:55 +0000974 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Chris Lattner9232b992003-04-22 18:42:41 +0000975 std::vector<const Type*> Params;
Chris Lattner697954c2002-01-20 22:54:45 +0000976 mapto($3->begin(), $3->end(), std::back_inserter(Params),
Chris Lattner876dc572003-10-02 19:00:34 +0000977 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner2079fde2001-10-13 06:41:08 +0000978 bool isVarArg = Params.size() && Params.back() == Type::VoidTy;
979 if (isVarArg) Params.pop_back();
980
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000981 $$ = new PATypeHolder(HandleUpRefs(FunctionType::get(*$1,Params,isVarArg)));
Chris Lattner30c89792001-09-07 16:35:17 +0000982 delete $3; // Delete the argument list
Chris Lattnerc7d3f6b2003-12-31 02:18:11 +0000983 delete $1; // Delete the return type handle
Chris Lattner30c89792001-09-07 16:35:17 +0000984 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000985 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000986 $$ = new PATypeHolder(HandleUpRefs(ArrayType::get(*$4, (unsigned)$2)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000987 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +0000988 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000989 | '{' TypeListI '}' { // Structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000990 std::vector<const Type*> Elements;
Chris Lattner697954c2002-01-20 22:54:45 +0000991 mapto($2->begin(), $2->end(), std::back_inserter(Elements),
Chris Lattner876dc572003-10-02 19:00:34 +0000992 std::mem_fun_ref(&PATypeHolder::get));
Chris Lattner30c89792001-09-07 16:35:17 +0000993
Chris Lattner8b88b3b2002-04-04 19:23:55 +0000994 $$ = new PATypeHolder(HandleUpRefs(StructType::get(Elements)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000995 delete $2;
996 }
997 | '{' '}' { // Empty structure type?
Chris Lattner9232b992003-04-22 18:42:41 +0000998 $$ = new PATypeHolder(StructType::get(std::vector<const Type*>()));
Chris Lattnerd05adbc2001-10-03 03:19:33 +0000999 }
1000 | UpRTypes '*' { // Pointer type?
Chris Lattner8b88b3b2002-04-04 19:23:55 +00001001 $$ = new PATypeHolder(HandleUpRefs(PointerType::get(*$1)));
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001002 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001003 };
Chris Lattner30c89792001-09-07 16:35:17 +00001004
Chris Lattner7e708292002-06-25 16:13:24 +00001005// TypeList - Used for struct declarations and as a basis for function type
Chris Lattner30c89792001-09-07 16:35:17 +00001006// declaration type lists
1007//
1008TypeListI : UpRTypes {
Chris Lattner9232b992003-04-22 18:42:41 +00001009 $$ = new std::list<PATypeHolder>();
Chris Lattner30c89792001-09-07 16:35:17 +00001010 $$->push_back(*$1); delete $1;
1011 }
1012 | TypeListI ',' UpRTypes {
1013 ($$=$1)->push_back(*$3); delete $3;
Chris Lattner51727be2002-06-04 21:58:56 +00001014 };
Chris Lattner30c89792001-09-07 16:35:17 +00001015
Chris Lattner7e708292002-06-25 16:13:24 +00001016// ArgTypeList - List of types for a function type declaration...
Chris Lattner30c89792001-09-07 16:35:17 +00001017ArgTypeListI : TypeListI
1018 | TypeListI ',' DOTDOTDOT {
1019 ($$=$1)->push_back(Type::VoidTy);
1020 }
1021 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001022 ($$ = new std::list<PATypeHolder>())->push_back(Type::VoidTy);
Chris Lattner30c89792001-09-07 16:35:17 +00001023 }
1024 | /*empty*/ {
Chris Lattner9232b992003-04-22 18:42:41 +00001025 $$ = new std::list<PATypeHolder>();
Chris Lattner51727be2002-06-04 21:58:56 +00001026 };
Chris Lattner30c89792001-09-07 16:35:17 +00001027
Chris Lattnere98dda62001-07-14 06:10:16 +00001028// ConstVal - The various declarations that go into the constant pool. This
Chris Lattnerd78700d2002-08-16 21:14:40 +00001029// production is used ONLY to represent constants that show up AFTER a 'const',
1030// 'constant' or 'global' token at global scope. Constants that can be inlined
1031// into other expressions (such as integers and constexprs) are handled by the
1032// ResolvedVal, ValueRef and ConstValueRef productions.
Chris Lattnere98dda62001-07-14 06:10:16 +00001033//
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001034ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Chris Lattner949a3622003-07-23 15:30:06 +00001035 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001036 if (ATy == 0)
1037 ThrowException("Cannot make array constant with type: '" +
1038 (*$1)->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001039 const Type *ETy = ATy->getElementType();
1040 int NumElements = ATy->getNumElements();
Chris Lattner00950542001-06-06 20:29:01 +00001041
Chris Lattner30c89792001-09-07 16:35:17 +00001042 // Verify that we have the correct size...
1043 if (NumElements != -1 && NumElements != (int)$3->size())
Chris Lattner00950542001-06-06 20:29:01 +00001044 ThrowException("Type mismatch: constant sized array initialized with " +
Chris Lattner30c89792001-09-07 16:35:17 +00001045 utostr($3->size()) + " arguments, but has size of " +
1046 itostr(NumElements) + "!");
Chris Lattner00950542001-06-06 20:29:01 +00001047
Chris Lattner30c89792001-09-07 16:35:17 +00001048 // Verify all elements are correct type!
1049 for (unsigned i = 0; i < $3->size(); i++) {
1050 if (ETy != (*$3)[i]->getType())
Chris Lattner00950542001-06-06 20:29:01 +00001051 ThrowException("Element #" + utostr(i) + " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001052 ETy->getDescription() +"' as required!\nIt is of type '"+
1053 (*$3)[i]->getType()->getDescription() + "'.");
Chris Lattner00950542001-06-06 20:29:01 +00001054 }
1055
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001056 $$ = ConstantArray::get(ATy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001057 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001058 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001059 | Types '[' ']' {
Chris Lattner949a3622003-07-23 15:30:06 +00001060 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001061 if (ATy == 0)
1062 ThrowException("Cannot make array constant with type: '" +
1063 (*$1)->getDescription() + "'!");
1064
1065 int NumElements = ATy->getNumElements();
Chris Lattner30c89792001-09-07 16:35:17 +00001066 if (NumElements != -1 && NumElements != 0)
Chris Lattner00950542001-06-06 20:29:01 +00001067 ThrowException("Type mismatch: constant sized array initialized with 0"
Chris Lattner30c89792001-09-07 16:35:17 +00001068 " arguments, but has size of " + itostr(NumElements) +"!");
Chris Lattner9232b992003-04-22 18:42:41 +00001069 $$ = ConstantArray::get(ATy, std::vector<Constant*>());
Chris Lattner30c89792001-09-07 16:35:17 +00001070 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001071 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001072 | Types 'c' STRINGCONSTANT {
Chris Lattner949a3622003-07-23 15:30:06 +00001073 const ArrayType *ATy = dyn_cast<ArrayType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001074 if (ATy == 0)
1075 ThrowException("Cannot make array constant with type: '" +
1076 (*$1)->getDescription() + "'!");
1077
Chris Lattner30c89792001-09-07 16:35:17 +00001078 int NumElements = ATy->getNumElements();
1079 const Type *ETy = ATy->getElementType();
1080 char *EndStr = UnEscapeLexed($3, true);
1081 if (NumElements != -1 && NumElements != (EndStr-$3))
Chris Lattner93750fa2001-07-28 17:48:55 +00001082 ThrowException("Can't build string constant of size " +
Chris Lattner30c89792001-09-07 16:35:17 +00001083 itostr((int)(EndStr-$3)) +
1084 " when array has size " + itostr(NumElements) + "!");
Chris Lattner9232b992003-04-22 18:42:41 +00001085 std::vector<Constant*> Vals;
Chris Lattner30c89792001-09-07 16:35:17 +00001086 if (ETy == Type::SByteTy) {
1087 for (char *C = $3; C != EndStr; ++C)
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001088 Vals.push_back(ConstantSInt::get(ETy, *C));
Chris Lattner30c89792001-09-07 16:35:17 +00001089 } else if (ETy == Type::UByteTy) {
1090 for (char *C = $3; C != EndStr; ++C)
Chris Lattnerbae362f2003-01-30 22:24:26 +00001091 Vals.push_back(ConstantUInt::get(ETy, (unsigned char)*C));
Chris Lattner93750fa2001-07-28 17:48:55 +00001092 } else {
Chris Lattner30c89792001-09-07 16:35:17 +00001093 free($3);
Chris Lattner93750fa2001-07-28 17:48:55 +00001094 ThrowException("Cannot build string arrays of non byte sized elements!");
1095 }
Chris Lattner30c89792001-09-07 16:35:17 +00001096 free($3);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001097 $$ = ConstantArray::get(ATy, Vals);
Chris Lattner30c89792001-09-07 16:35:17 +00001098 delete $1;
Chris Lattner93750fa2001-07-28 17:48:55 +00001099 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001100 | Types '{' ConstVector '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001101 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001102 if (STy == 0)
1103 ThrowException("Cannot make struct constant with type: '" +
1104 (*$1)->getDescription() + "'!");
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001105
Chris Lattnerc6212f12003-05-21 16:06:56 +00001106 if ($3->size() != STy->getNumContainedTypes())
1107 ThrowException("Illegal number of initializers for structure type!");
1108
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001109 // Check to ensure that constants are compatible with the type initializer!
1110 for (unsigned i = 0, e = $3->size(); i != e; ++i)
Chris Lattnerd21cd802004-02-09 04:37:31 +00001111 if ((*$3)[i]->getType() != STy->getElementType(i))
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001112 ThrowException("Expected type '" +
Chris Lattnerd21cd802004-02-09 04:37:31 +00001113 STy->getElementType(i)->getDescription() +
Chris Lattneraf76d0e2003-04-15 16:09:31 +00001114 "' for element #" + utostr(i) +
1115 " of structure initializer!");
1116
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001117 $$ = ConstantStruct::get(STy, *$3);
Chris Lattner30c89792001-09-07 16:35:17 +00001118 delete $1; delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001119 }
Chris Lattnerc6212f12003-05-21 16:06:56 +00001120 | Types '{' '}' {
Chris Lattner949a3622003-07-23 15:30:06 +00001121 const StructType *STy = dyn_cast<StructType>($1->get());
Chris Lattnerc6212f12003-05-21 16:06:56 +00001122 if (STy == 0)
1123 ThrowException("Cannot make struct constant with type: '" +
1124 (*$1)->getDescription() + "'!");
1125
1126 if (STy->getNumContainedTypes() != 0)
1127 ThrowException("Illegal number of initializers for structure type!");
1128
1129 $$ = ConstantStruct::get(STy, std::vector<Constant*>());
1130 delete $1;
1131 }
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001132 | Types NULL_TOK {
Chris Lattner949a3622003-07-23 15:30:06 +00001133 const PointerType *PTy = dyn_cast<PointerType>($1->get());
Chris Lattnerd05adbc2001-10-03 03:19:33 +00001134 if (PTy == 0)
1135 ThrowException("Cannot make null pointer constant with type: '" +
1136 (*$1)->getDescription() + "'!");
1137
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001138 $$ = ConstantPointerNull::get(PTy);
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001139 delete $1;
1140 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001141 | Types SymbolicValueRef {
Chris Lattner949a3622003-07-23 15:30:06 +00001142 const PointerType *Ty = dyn_cast<PointerType>($1->get());
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001143 if (Ty == 0)
1144 ThrowException("Global const reference must be a pointer type!");
1145
Chris Lattner3101c252002-08-15 17:58:33 +00001146 // ConstExprs can exist in the body of a function, thus creating
1147 // ConstantPointerRefs whenever they refer to a variable. Because we are in
1148 // the context of a function, getValNonImprovising will search the functions
1149 // symbol table instead of the module symbol table for the global symbol,
1150 // which throws things all off. To get around this, we just tell
1151 // getValNonImprovising that we are at global scope here.
1152 //
Chris Lattner394f2eb2003-10-16 20:12:13 +00001153 Function *SavedCurFn = CurFun.CurrentFunction;
1154 CurFun.CurrentFunction = 0;
Chris Lattner3101c252002-08-15 17:58:33 +00001155
Chris Lattner2079fde2001-10-13 06:41:08 +00001156 Value *V = getValNonImprovising(Ty, $2);
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001157
Chris Lattner394f2eb2003-10-16 20:12:13 +00001158 CurFun.CurrentFunction = SavedCurFn;
Chris Lattner3101c252002-08-15 17:58:33 +00001159
Chris Lattner2079fde2001-10-13 06:41:08 +00001160 // If this is an initializer for a constant pointer, which is referencing a
1161 // (currently) undefined variable, create a stub now that shall be replaced
1162 // in the future with the right type of variable.
1163 //
1164 if (V == 0) {
1165 assert(isa<PointerType>(Ty) && "Globals may only be used as pointers!");
1166 const PointerType *PT = cast<PointerType>(Ty);
1167
1168 // First check to see if the forward references value is already created!
1169 PerModuleInfo::GlobalRefsType::iterator I =
Chris Lattner9232b992003-04-22 18:42:41 +00001170 CurModule.GlobalRefs.find(std::make_pair(PT, $2));
Chris Lattner2079fde2001-10-13 06:41:08 +00001171
1172 if (I != CurModule.GlobalRefs.end()) {
1173 V = I->second; // Placeholder already exists, use it...
Chris Lattnera989b232003-12-23 20:05:15 +00001174 $2.destroy();
Chris Lattner2079fde2001-10-13 06:41:08 +00001175 } else {
Chris Lattner2079fde2001-10-13 06:41:08 +00001176 // Create a placeholder for the global variable reference...
Chris Lattner7a176752001-12-04 00:03:30 +00001177 GlobalVariable *GV = new GlobalVariable(PT->getElementType(),
Chris Lattner4ad02e72003-04-16 20:28:45 +00001178 false,
1179 GlobalValue::ExternalLinkage);
Chris Lattner2079fde2001-10-13 06:41:08 +00001180 // Keep track of the fact that we have a forward ref to recycle it
Chris Lattner9232b992003-04-22 18:42:41 +00001181 CurModule.GlobalRefs.insert(std::make_pair(std::make_pair(PT, $2), GV));
Chris Lattner2079fde2001-10-13 06:41:08 +00001182
1183 // Must temporarily push this value into the module table...
1184 CurModule.CurrentModule->getGlobalList().push_back(GV);
1185 V = GV;
1186 }
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001187 }
1188
Chris Lattner2079fde2001-10-13 06:41:08 +00001189 GlobalValue *GV = cast<GlobalValue>(V);
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001190 $$ = ConstantPointerRef::get(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001191 delete $1; // Free the type handle
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001192 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001193 | Types ConstExpr {
1194 if ($1->get() != $2->getType())
1195 ThrowException("Mismatched types for constant expression!");
1196 $$ = $2;
1197 delete $1;
Chris Lattnerf4600482003-06-28 20:01:34 +00001198 }
1199 | Types ZEROINITIALIZER {
1200 $$ = Constant::getNullValue($1->get());
1201 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001202 };
Chris Lattnerf4ba6c72001-10-03 06:12:09 +00001203
Chris Lattnere43f40b2002-10-09 00:25:32 +00001204ConstVal : SIntType EINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001205 if (!ConstantSInt::isValueValidForType($1, $2))
1206 ThrowException("Constant value doesn't fit in type!");
1207 $$ = ConstantSInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001208 }
1209 | UIntType EUINT64VAL { // integral constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001210 if (!ConstantUInt::isValueValidForType($1, $2))
1211 ThrowException("Constant value doesn't fit in type!");
1212 $$ = ConstantUInt::get($1, $2);
Chris Lattnere43f40b2002-10-09 00:25:32 +00001213 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001214 | BOOL TRUETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001215 $$ = ConstantBool::True;
1216 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001217 | BOOL FALSETOK { // Boolean constants
Chris Lattnerd05e3592002-08-15 18:17:28 +00001218 $$ = ConstantBool::False;
1219 }
1220 | FPType FPVAL { // Float & Double constants
1221 $$ = ConstantFP::get($1, $2);
1222 };
1223
Chris Lattner00950542001-06-06 20:29:01 +00001224
Chris Lattnerd78700d2002-08-16 21:14:40 +00001225ConstExpr: CAST '(' ConstVal TO Types ')' {
Chris Lattnerae711a82003-11-21 20:27:35 +00001226 if (!$3->getType()->isFirstClassType())
1227 ThrowException("cast constant expression from a non-primitive type: '" +
1228 $3->getType()->getDescription() + "'!");
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001229 if (!$5->get()->isFirstClassType())
1230 ThrowException("cast constant expression to a non-primitive type: '" +
1231 $5->get()->getDescription() + "'!");
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001232 $$ = ConstantExpr::getCast($3, $5->get());
Chris Lattnerec1b8a02002-08-15 19:37:11 +00001233 delete $5;
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001234 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001235 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1236 if (!isa<PointerType>($3->getType()))
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001237 ThrowException("GetElementPtr requires a pointer operand!");
1238
Chris Lattner830b6f92004-04-05 01:30:04 +00001239 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1240 // indices to uint struct indices for compatibility.
1241 generic_gep_type_iterator<std::vector<Value*>::iterator>
1242 GTI = gep_type_begin($3->getType(), $4->begin(), $4->end()),
1243 GTE = gep_type_end($3->getType(), $4->begin(), $4->end());
1244 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
1245 if (isa<StructType>(*GTI)) // Only change struct indices
1246 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
1247 if (CUI->getType() == Type::UByteTy)
1248 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
1249
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001250 const Type *IdxTy =
Chris Lattnerd78700d2002-08-16 21:14:40 +00001251 GetElementPtrInst::getIndexedType($3->getType(), *$4, true);
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001252 if (!IdxTy)
1253 ThrowException("Index list invalid for constant getelementptr!");
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001254
Chris Lattner9232b992003-04-22 18:42:41 +00001255 std::vector<Constant*> IdxVec;
Chris Lattnerd78700d2002-08-16 21:14:40 +00001256 for (unsigned i = 0, e = $4->size(); i != e; ++i)
1257 if (Constant *C = dyn_cast<Constant>((*$4)[i]))
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001258 IdxVec.push_back(C);
1259 else
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001260 ThrowException("Indices to constant getelementptr must be constants!");
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001261
Chris Lattnerd78700d2002-08-16 21:14:40 +00001262 delete $4;
Chris Lattnercc4b6ec2002-07-18 00:14:27 +00001263
Chris Lattnerd78700d2002-08-16 21:14:40 +00001264 $$ = ConstantExpr::getGetElementPtr($3, IdxVec);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001265 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001266 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1267 if ($3->getType() != Type::BoolTy)
1268 ThrowException("Select condition must be of boolean type!");
1269 if ($5->getType() != $7->getType())
1270 ThrowException("Select operand types must match!");
1271 $$ = ConstantExpr::getSelect($3, $5, $7);
1272 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001273 | BinaryOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001274 if ($3->getType() != $5->getType())
1275 ThrowException("Binary operator types must match!");
Chris Lattnerd78700d2002-08-16 21:14:40 +00001276 $$ = ConstantExpr::get($1, $3, $5);
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001277 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001278 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Chris Lattnerc188eeb2002-07-30 18:54:25 +00001279 if ($5->getType() != Type::UByteTy)
1280 ThrowException("Shift count for shift constant must be unsigned byte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001281 if (!$3->getType()->isInteger())
1282 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerb16689b2004-01-12 19:08:43 +00001283 $$ = ConstantExpr::get($1, $3, $5);
Chris Lattner699f1eb2002-08-14 17:12:33 +00001284 };
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001285
1286
Misha Brukmanbc0e9982003-07-14 17:20:40 +00001287// ConstVector - A list of comma separated constants.
Chris Lattner00950542001-06-06 20:29:01 +00001288ConstVector : ConstVector ',' ConstVal {
Chris Lattner30c89792001-09-07 16:35:17 +00001289 ($$ = $1)->push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001290 }
1291 | ConstVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001292 $$ = new std::vector<Constant*>();
Chris Lattner30c89792001-09-07 16:35:17 +00001293 $$->push_back($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001294 };
Chris Lattner00950542001-06-06 20:29:01 +00001295
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001296
Chris Lattner1781aca2001-09-18 04:00:54 +00001297// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Chris Lattner51727be2002-06-04 21:58:56 +00001298GlobalType : GLOBAL { $$ = false; } | CONSTANT { $$ = true; };
Chris Lattner1781aca2001-09-18 04:00:54 +00001299
Chris Lattner00950542001-06-06 20:29:01 +00001300
Chris Lattner0e73ce62002-05-02 19:11:13 +00001301//===----------------------------------------------------------------------===//
1302// Rules to match Modules
1303//===----------------------------------------------------------------------===//
1304
1305// Module rule: Capture the result of parsing the whole file into a result
1306// variable...
1307//
1308Module : FunctionList {
1309 $$ = ParserResult = $1;
1310 CurModule.ModuleDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001311};
Chris Lattner0e73ce62002-05-02 19:11:13 +00001312
Chris Lattner7e708292002-06-25 16:13:24 +00001313// FunctionList - A list of functions, preceeded by a constant pool.
Chris Lattner0e73ce62002-05-02 19:11:13 +00001314//
1315FunctionList : FunctionList Function {
1316 $$ = $1;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001317 CurFun.FunctionDone();
Chris Lattner0e73ce62002-05-02 19:11:13 +00001318 }
1319 | FunctionList FunctionProto {
1320 $$ = $1;
1321 }
1322 | FunctionList IMPLEMENTATION {
1323 $$ = $1;
1324 }
1325 | ConstPool {
1326 $$ = CurModule.CurrentModule;
1327 // Resolve circular types before we parse the body of the module
1328 ResolveTypes(CurModule.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001329 };
Chris Lattner0e73ce62002-05-02 19:11:13 +00001330
Chris Lattnere98dda62001-07-14 06:10:16 +00001331// ConstPool - Constants with optional names assigned to them.
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001332ConstPool : ConstPool OptAssign CONST ConstVal {
Chris Lattneradf99702003-03-03 23:28:55 +00001333 if (!setValueName($4, $2))
1334 InsertValue($4);
Chris Lattner00950542001-06-06 20:29:01 +00001335 }
Chris Lattner30c89792001-09-07 16:35:17 +00001336 | ConstPool OptAssign TYPE TypesV { // Types can be defined in the const pool
Chris Lattner4a42e902001-10-22 05:56:09 +00001337 // Eagerly resolve types. This is not an optimization, this is a
1338 // requirement that is due to the fact that we could have this:
1339 //
1340 // %list = type { %list * }
1341 // %list = type { %list * } ; repeated type decl
1342 //
1343 // If types are not resolved eagerly, then the two types will not be
1344 // determined to be the same type!
1345 //
1346 ResolveTypeTo($2, $4->get());
1347
Chris Lattner1781aca2001-09-18 04:00:54 +00001348 // TODO: FIXME when Type are not const
Chris Lattnerb7474512001-10-03 15:39:04 +00001349 if (!setValueName(const_cast<Type*>($4->get()), $2)) {
1350 // If this is not a redefinition of a type...
1351 if (!$2) {
1352 InsertType($4->get(),
Chris Lattner394f2eb2003-10-16 20:12:13 +00001353 inFunctionScope() ? CurFun.Types : CurModule.Types);
Chris Lattnerb7474512001-10-03 15:39:04 +00001354 }
Chris Lattner30c89792001-09-07 16:35:17 +00001355 }
Chris Lattnerc9a21b52001-10-21 23:02:41 +00001356
1357 delete $4;
Chris Lattner30c89792001-09-07 16:35:17 +00001358 }
Chris Lattner79df7c02002-03-26 18:01:55 +00001359 | ConstPool FunctionProto { // Function prototypes can be in const pool
Chris Lattner93750fa2001-07-28 17:48:55 +00001360 }
Chris Lattner4ad02e72003-04-16 20:28:45 +00001361 | ConstPool OptAssign OptLinkage GlobalType ConstVal {
Chris Lattnerdda71962001-11-26 18:54:16 +00001362 const Type *Ty = $5->getType();
Chris Lattner1781aca2001-09-18 04:00:54 +00001363 // Global declarations appear in Constant Pool
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001364 Constant *Initializer = $5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001365 if (Initializer == 0)
1366 ThrowException("Global value initializer is not a constant!");
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001367
Chris Lattnerdda71962001-11-26 18:54:16 +00001368 GlobalVariable *GV = new GlobalVariable(Ty, $4, $3, Initializer);
Chris Lattnerb7474512001-10-03 15:39:04 +00001369 if (!setValueName(GV, $2)) { // If not redefining...
1370 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001371 int Slot = InsertValue(GV, CurModule.Values);
1372
1373 if (Slot != -1) {
1374 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1375 } else {
1376 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1377 (char*)GV->getName().c_str()));
1378 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001379 }
Chris Lattner1781aca2001-09-18 04:00:54 +00001380 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001381 | ConstPool OptAssign EXTERNAL GlobalType Types {
1382 const Type *Ty = *$5;
Chris Lattner1781aca2001-09-18 04:00:54 +00001383 // Global declarations appear in Constant Pool
Chris Lattner4ad02e72003-04-16 20:28:45 +00001384 GlobalVariable *GV = new GlobalVariable(Ty,$4,GlobalValue::ExternalLinkage);
Chris Lattnerb7474512001-10-03 15:39:04 +00001385 if (!setValueName(GV, $2)) { // If not redefining...
1386 CurModule.CurrentModule->getGlobalList().push_back(GV);
Chris Lattner2079fde2001-10-13 06:41:08 +00001387 int Slot = InsertValue(GV, CurModule.Values);
1388
1389 if (Slot != -1) {
1390 CurModule.DeclareNewGlobalValue(GV, ValID::create(Slot));
1391 } else {
1392 assert(GV->hasName() && "Not named and not numbered!?");
1393 CurModule.DeclareNewGlobalValue(GV, ValID::create(
1394 (char*)GV->getName().c_str()));
1395 }
Chris Lattnerb7474512001-10-03 15:39:04 +00001396 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001397 delete $5;
Chris Lattnere98dda62001-07-14 06:10:16 +00001398 }
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001399 | ConstPool TARGET TargetDefinition {
1400 }
Chris Lattner00950542001-06-06 20:29:01 +00001401 | /* empty: end of list */ {
Chris Lattner51727be2002-06-04 21:58:56 +00001402 };
Chris Lattner00950542001-06-06 20:29:01 +00001403
1404
Chris Lattnerb9bcbb52003-04-22 19:07:06 +00001405
1406BigOrLittle : BIG { $$ = Module::BigEndian; };
1407BigOrLittle : LITTLE { $$ = Module::LittleEndian; };
1408
1409TargetDefinition : ENDIAN '=' BigOrLittle {
1410 CurModule.CurrentModule->setEndianness($3);
1411 }
1412 | POINTERSIZE '=' EUINT64VAL {
1413 if ($3 == 32)
1414 CurModule.CurrentModule->setPointerSize(Module::Pointer32);
1415 else if ($3 == 64)
1416 CurModule.CurrentModule->setPointerSize(Module::Pointer64);
1417 else
1418 ThrowException("Invalid pointer size: '" + utostr($3) + "'!");
1419 };
1420
1421
Chris Lattner00950542001-06-06 20:29:01 +00001422//===----------------------------------------------------------------------===//
Chris Lattner79df7c02002-03-26 18:01:55 +00001423// Rules to match Function Headers
Chris Lattner00950542001-06-06 20:29:01 +00001424//===----------------------------------------------------------------------===//
1425
Chris Lattner6c23f572003-08-22 05:42:10 +00001426Name : VAR_ID | STRINGCONSTANT;
1427OptName : Name | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001428
Chris Lattner6c23f572003-08-22 05:42:10 +00001429ArgVal : Types OptName {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001430 if (*$1 == Type::VoidTy)
1431 ThrowException("void typed arguments are invalid!");
Chris Lattner9232b992003-04-22 18:42:41 +00001432 $$ = new std::pair<PATypeHolder*, char*>($1, $2);
Chris Lattner51727be2002-06-04 21:58:56 +00001433};
Chris Lattner00950542001-06-06 20:29:01 +00001434
Chris Lattner69da5cf2002-10-13 20:57:00 +00001435ArgListH : ArgListH ',' ArgVal {
1436 $$ = $1;
1437 $1->push_back(*$3);
1438 delete $3;
Chris Lattner00950542001-06-06 20:29:01 +00001439 }
1440 | ArgVal {
Chris Lattner9232b992003-04-22 18:42:41 +00001441 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001442 $$->push_back(*$1);
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001443 delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001444 };
Chris Lattner00950542001-06-06 20:29:01 +00001445
1446ArgList : ArgListH {
1447 $$ = $1;
1448 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001449 | ArgListH ',' DOTDOTDOT {
1450 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001451 $$->push_back(std::pair<PATypeHolder*,
1452 char*>(new PATypeHolder(Type::VoidTy), 0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001453 }
1454 | DOTDOTDOT {
Chris Lattner9232b992003-04-22 18:42:41 +00001455 $$ = new std::vector<std::pair<PATypeHolder*,char*> >();
1456 $$->push_back(std::make_pair(new PATypeHolder(Type::VoidTy), (char*)0));
Chris Lattner69da5cf2002-10-13 20:57:00 +00001457 }
Chris Lattner00950542001-06-06 20:29:01 +00001458 | /* empty */ {
1459 $$ = 0;
Chris Lattner51727be2002-06-04 21:58:56 +00001460 };
Chris Lattner00950542001-06-06 20:29:01 +00001461
Chris Lattner6c23f572003-08-22 05:42:10 +00001462FunctionHeaderH : TypesV Name '(' ArgList ')' {
Chris Lattner1f862af2003-04-16 18:13:57 +00001463 UnEscapeLexed($2);
Chris Lattner9232b992003-04-22 18:42:41 +00001464 std::string FunctionName($2);
Chris Lattnerdda71962001-11-26 18:54:16 +00001465
Chris Lattnerc282f5a2003-11-21 22:32:23 +00001466 if (!(*$1)->isFirstClassType() && *$1 != Type::VoidTy)
1467 ThrowException("LLVM functions cannot return aggregate types!");
1468
Chris Lattner9232b992003-04-22 18:42:41 +00001469 std::vector<const Type*> ParamTypeList;
Chris Lattner1f862af2003-04-16 18:13:57 +00001470 if ($4) { // If there are arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001471 for (std::vector<std::pair<PATypeHolder*,char*> >::iterator I = $4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001472 I != $4->end(); ++I)
Chris Lattner69da5cf2002-10-13 20:57:00 +00001473 ParamTypeList.push_back(I->first->get());
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001474 }
Chris Lattner00950542001-06-06 20:29:01 +00001475
Chris Lattner2079fde2001-10-13 06:41:08 +00001476 bool isVarArg = ParamTypeList.size() && ParamTypeList.back() == Type::VoidTy;
1477 if (isVarArg) ParamTypeList.pop_back();
1478
Chris Lattner1f862af2003-04-16 18:13:57 +00001479 const FunctionType *FT = FunctionType::get(*$1, ParamTypeList, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001480 const PointerType *PFT = PointerType::get(FT);
Chris Lattner1f862af2003-04-16 18:13:57 +00001481 delete $1;
Chris Lattner00950542001-06-06 20:29:01 +00001482
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001483 Function *Fn = 0;
1484 // Is the function already in symtab?
1485 if ((Fn = CurModule.CurrentModule->getFunction(FunctionName, FT))) {
1486 // Yes it is. If this is the case, either we need to be a forward decl,
1487 // or it needs to be.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001488 if (!CurFun.isDeclare && !Fn->isExternal())
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001489 ThrowException("Redefinition of function '" + FunctionName + "'!");
1490
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001491 // Make sure to strip off any argument names so we can't get conflicts...
1492 for (Function::aiterator AI = Fn->abegin(), AE = Fn->aend(); AI != AE; ++AI)
1493 AI->setName("");
Chris Lattner5659dd12002-07-15 00:10:33 +00001494
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001495 } else { // Not already defined?
Chris Lattner63a22502004-03-08 16:14:19 +00001496 Fn = new Function(FT, GlobalValue::ExternalLinkage, FunctionName,
1497 CurModule.CurrentModule);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001498 InsertValue(Fn, CurModule.Values);
Chris Lattner1f862af2003-04-16 18:13:57 +00001499 CurModule.DeclareNewGlobalValue(Fn, ValID::create($2));
Chris Lattnere1815642001-07-15 06:35:53 +00001500 }
Chris Lattner1f862af2003-04-16 18:13:57 +00001501 free($2); // Free strdup'd memory!
Chris Lattner00950542001-06-06 20:29:01 +00001502
Chris Lattner394f2eb2003-10-16 20:12:13 +00001503 CurFun.FunctionStart(Fn);
Chris Lattner00950542001-06-06 20:29:01 +00001504
Chris Lattner7e708292002-06-25 16:13:24 +00001505 // Add all of the arguments we parsed to the function...
Chris Lattner1f862af2003-04-16 18:13:57 +00001506 if ($4) { // Is null if empty...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001507 if (isVarArg) { // Nuke the last entry
Chris Lattner1f862af2003-04-16 18:13:57 +00001508 assert($4->back().first->get() == Type::VoidTy && $4->back().second == 0&&
Chris Lattner69da5cf2002-10-13 20:57:00 +00001509 "Not a varargs marker!");
Chris Lattner1f862af2003-04-16 18:13:57 +00001510 delete $4->back().first;
1511 $4->pop_back(); // Delete the last entry
Chris Lattner69da5cf2002-10-13 20:57:00 +00001512 }
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001513 Function::aiterator ArgIt = Fn->abegin();
Chris Lattner9232b992003-04-22 18:42:41 +00001514 for (std::vector<std::pair<PATypeHolder*, char*> >::iterator I =$4->begin();
Chris Lattner1f862af2003-04-16 18:13:57 +00001515 I != $4->end(); ++I, ++ArgIt) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001516 delete I->first; // Delete the typeholder...
1517
1518 if (setValueName(ArgIt, I->second)) // Insert arg into symtab...
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001519 assert(0 && "No arg redef allowed!");
Chris Lattnerf28d6c92002-03-08 18:41:32 +00001520
Chris Lattner69da5cf2002-10-13 20:57:00 +00001521 InsertValue(ArgIt);
Chris Lattner00950542001-06-06 20:29:01 +00001522 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001523
Chris Lattner1f862af2003-04-16 18:13:57 +00001524 delete $4; // We're now done with the argument list
Chris Lattner00950542001-06-06 20:29:01 +00001525 }
Chris Lattner51727be2002-06-04 21:58:56 +00001526};
Chris Lattner00950542001-06-06 20:29:01 +00001527
Chris Lattner9b02cc32002-05-03 18:23:48 +00001528BEGIN : BEGINTOK | '{'; // Allow BEGIN or '{' to start a function
1529
Chris Lattner4ad02e72003-04-16 20:28:45 +00001530FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
Chris Lattner394f2eb2003-10-16 20:12:13 +00001531 $$ = CurFun.CurrentFunction;
Chris Lattner30c89792001-09-07 16:35:17 +00001532
Chris Lattner4ad02e72003-04-16 20:28:45 +00001533 // Make sure that we keep track of the linkage type even if there was a
1534 // previous "declare".
1535 $$->setLinkage($1);
Chris Lattner1f862af2003-04-16 18:13:57 +00001536
Chris Lattner7e708292002-06-25 16:13:24 +00001537 // Resolve circular types before we parse the body of the function.
Chris Lattner394f2eb2003-10-16 20:12:13 +00001538 ResolveTypes(CurFun.LateResolveTypes);
Chris Lattner51727be2002-06-04 21:58:56 +00001539};
Chris Lattner00950542001-06-06 20:29:01 +00001540
Chris Lattner9b02cc32002-05-03 18:23:48 +00001541END : ENDTOK | '}'; // Allow end of '}' to end a function
1542
Chris Lattner79df7c02002-03-26 18:01:55 +00001543Function : BasicBlockList END {
Chris Lattner00950542001-06-06 20:29:01 +00001544 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001545};
Chris Lattner00950542001-06-06 20:29:01 +00001546
Chris Lattner394f2eb2003-10-16 20:12:13 +00001547FunctionProto : DECLARE { CurFun.isDeclare = true; } FunctionHeaderH {
1548 $$ = CurFun.CurrentFunction;
Chris Lattner394f2eb2003-10-16 20:12:13 +00001549 CurFun.FunctionDone();
Chris Lattner51727be2002-06-04 21:58:56 +00001550};
Chris Lattner00950542001-06-06 20:29:01 +00001551
1552//===----------------------------------------------------------------------===//
1553// Rules to match Basic Blocks
1554//===----------------------------------------------------------------------===//
1555
1556ConstValueRef : ESINT64VAL { // A reference to a direct constant
1557 $$ = ValID::create($1);
1558 }
1559 | EUINT64VAL {
1560 $$ = ValID::create($1);
1561 }
Chris Lattner3d52b2f2001-07-15 00:17:01 +00001562 | FPVAL { // Perhaps it's an FP constant?
1563 $$ = ValID::create($1);
1564 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001565 | TRUETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001566 $$ = ValID::create(ConstantBool::True);
Chris Lattner00950542001-06-06 20:29:01 +00001567 }
Chris Lattner91ef4602004-03-31 03:49:47 +00001568 | FALSETOK {
Chris Lattnerd78700d2002-08-16 21:14:40 +00001569 $$ = ValID::create(ConstantBool::False);
Chris Lattner00950542001-06-06 20:29:01 +00001570 }
Chris Lattner1a1cb112001-09-30 22:46:54 +00001571 | NULL_TOK {
1572 $$ = ValID::createNull();
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00001573 }
Chris Lattnerd78700d2002-08-16 21:14:40 +00001574 | ConstExpr {
1575 $$ = ValID::create($1);
1576 };
Chris Lattner1a1cb112001-09-30 22:46:54 +00001577
Chris Lattner2079fde2001-10-13 06:41:08 +00001578// SymbolicValueRef - Reference to one of two ways of symbolically refering to
1579// another value.
1580//
1581SymbolicValueRef : INTVAL { // Is it an integer reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001582 $$ = ValID::create($1);
1583 }
Chris Lattner6c23f572003-08-22 05:42:10 +00001584 | Name { // Is it a named reference...?
Chris Lattner00950542001-06-06 20:29:01 +00001585 $$ = ValID::create($1);
Chris Lattner51727be2002-06-04 21:58:56 +00001586 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001587
1588// ValueRef - A reference to a definition... either constant or symbolic
Chris Lattner51727be2002-06-04 21:58:56 +00001589ValueRef : SymbolicValueRef | ConstValueRef;
Chris Lattner2079fde2001-10-13 06:41:08 +00001590
Chris Lattner00950542001-06-06 20:29:01 +00001591
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001592// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1593// type immediately preceeds the value reference, and allows complex constant
1594// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
Chris Lattnerdf7306f2001-10-03 01:49:25 +00001595ResolvedVal : Types ValueRef {
Chris Lattner30c89792001-09-07 16:35:17 +00001596 $$ = getVal(*$1, $2); delete $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001597 };
Chris Lattner8b81bf52001-07-25 22:47:46 +00001598
Chris Lattner00950542001-06-06 20:29:01 +00001599BasicBlockList : BasicBlockList BasicBlock {
Chris Lattner7e708292002-06-25 16:13:24 +00001600 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner00950542001-06-06 20:29:01 +00001601 }
Chris Lattner7e708292002-06-25 16:13:24 +00001602 | FunctionHeader BasicBlock { // Do not allow functions with 0 basic blocks
1603 ($$ = $1)->getBasicBlockList().push_back($2);
Chris Lattner51727be2002-06-04 21:58:56 +00001604 };
Chris Lattner00950542001-06-06 20:29:01 +00001605
1606
1607// Basic blocks are terminated by branching instructions:
1608// br, br/cc, switch, ret
1609//
Chris Lattner2079fde2001-10-13 06:41:08 +00001610BasicBlock : InstructionList OptAssign BBTerminatorInst {
1611 if (setValueName($3, $2)) { assert(0 && "No redefn allowed!"); }
1612 InsertValue($3);
1613
1614 $1->getInstList().push_back($3);
Chris Lattner00950542001-06-06 20:29:01 +00001615 InsertValue($1);
1616 $$ = $1;
1617 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001618 | LABELSTR InstructionList OptAssign BBTerminatorInst {
1619 if (setValueName($4, $3)) { assert(0 && "No redefn allowed!"); }
1620 InsertValue($4);
1621
1622 $2->getInstList().push_back($4);
Chris Lattnerb7474512001-10-03 15:39:04 +00001623 if (setValueName($2, $1)) { assert(0 && "No label redef allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001624
1625 InsertValue($2);
1626 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001627 };
Chris Lattner00950542001-06-06 20:29:01 +00001628
1629InstructionList : InstructionList Inst {
1630 $1->getInstList().push_back($2);
1631 $$ = $1;
1632 }
1633 | /* empty */ {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001634 $$ = CurBB = new BasicBlock();
Chris Lattner51727be2002-06-04 21:58:56 +00001635 };
Chris Lattner00950542001-06-06 20:29:01 +00001636
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001637BBTerminatorInst : RET ResolvedVal { // Return with a result...
1638 $$ = new ReturnInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001639 }
1640 | RET VOID { // Return with no result...
1641 $$ = new ReturnInst();
1642 }
1643 | BR LABEL ValueRef { // Unconditional Branch...
Chris Lattner9636a912001-10-01 16:18:37 +00001644 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $3)));
Chris Lattner00950542001-06-06 20:29:01 +00001645 } // Conditional Branch...
1646 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Chris Lattner9636a912001-10-01 16:18:37 +00001647 $$ = new BranchInst(cast<BasicBlock>(getVal(Type::LabelTy, $6)),
1648 cast<BasicBlock>(getVal(Type::LabelTy, $9)),
Chris Lattner00950542001-06-06 20:29:01 +00001649 getVal(Type::BoolTy, $3));
1650 }
1651 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1652 SwitchInst *S = new SwitchInst(getVal($2, $3),
Chris Lattner9636a912001-10-01 16:18:37 +00001653 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
Chris Lattner00950542001-06-06 20:29:01 +00001654 $$ = S;
1655
Chris Lattner9232b992003-04-22 18:42:41 +00001656 std::vector<std::pair<Constant*,BasicBlock*> >::iterator I = $8->begin(),
Chris Lattner46748042002-04-09 19:41:42 +00001657 E = $8->end();
1658 for (; I != E; ++I)
Chris Lattner4354f562003-08-23 23:14:52 +00001659 S->addCase(I->first, I->second);
Chris Lattnerf57a43d2004-04-17 23:49:15 +00001660 delete $8;
Chris Lattner00950542001-06-06 20:29:01 +00001661 }
Chris Lattner196850c2003-05-15 21:30:00 +00001662 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1663 SwitchInst *S = new SwitchInst(getVal($2, $3),
1664 cast<BasicBlock>(getVal(Type::LabelTy, $6)));
1665 $$ = S;
1666 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001667 | INVOKE TypesV ValueRef '(' ValueRefListE ')' TO ResolvedVal
Chris Lattnera58e3a12004-02-08 21:48:25 +00001668 UNWIND ResolvedVal {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001669 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001670 const FunctionType *Ty;
Chris Lattner2079fde2001-10-13 06:41:08 +00001671
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001672 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1673 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner2079fde2001-10-13 06:41:08 +00001674 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001675 std::vector<const Type*> ParamTypes;
Chris Lattner2079fde2001-10-13 06:41:08 +00001676 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001677 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1678 I != E; ++I)
Chris Lattner2079fde2001-10-13 06:41:08 +00001679 ParamTypes.push_back((*I)->getType());
1680 }
1681
1682 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1683 if (isVarArg) ParamTypes.pop_back();
1684
Chris Lattner79df7c02002-03-26 18:01:55 +00001685 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001686 PFTy = PointerType::get(Ty);
Chris Lattner2079fde2001-10-13 06:41:08 +00001687 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001688
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001689 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner2079fde2001-10-13 06:41:08 +00001690
1691 BasicBlock *Normal = dyn_cast<BasicBlock>($8);
1692 BasicBlock *Except = dyn_cast<BasicBlock>($10);
1693
1694 if (Normal == 0 || Except == 0)
1695 ThrowException("Invoke instruction without label destinations!");
1696
1697 // Create the call node...
1698 if (!$5) { // Has no arguments?
Chris Lattner9232b992003-04-22 18:42:41 +00001699 $$ = new InvokeInst(V, Normal, Except, std::vector<Value*>());
Chris Lattner2079fde2001-10-13 06:41:08 +00001700 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001701 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner2079fde2001-10-13 06:41:08 +00001702 // correctly!
1703 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001704 FunctionType::param_iterator I = Ty->param_begin();
1705 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001706 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner2079fde2001-10-13 06:41:08 +00001707
1708 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1709 if ((*ArgI)->getType() != *I)
1710 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001711 (*I)->getDescription() + "'!");
Chris Lattner2079fde2001-10-13 06:41:08 +00001712
1713 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
1714 ThrowException("Invalid number of parameters detected!");
1715
Chris Lattner6cdb0112001-11-26 16:54:11 +00001716 $$ = new InvokeInst(V, Normal, Except, *$5);
Chris Lattner2079fde2001-10-13 06:41:08 +00001717 }
Chris Lattner9cd42572003-12-23 22:18:36 +00001718 delete $2;
Chris Lattner2079fde2001-10-13 06:41:08 +00001719 delete $5;
Chris Lattner36143fc2003-09-08 18:54:55 +00001720 }
1721 | UNWIND {
1722 $$ = new UnwindInst();
Chris Lattner51727be2002-06-04 21:58:56 +00001723 };
Chris Lattner2079fde2001-10-13 06:41:08 +00001724
1725
Chris Lattner00950542001-06-06 20:29:01 +00001726
1727JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1728 $$ = $1;
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001729 Constant *V = cast<Constant>(getValNonImprovising($2, $3));
Chris Lattner00950542001-06-06 20:29:01 +00001730 if (V == 0)
1731 ThrowException("May only switch on a constant pool value!");
1732
Chris Lattner9232b992003-04-22 18:42:41 +00001733 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($5, $6))));
Chris Lattner00950542001-06-06 20:29:01 +00001734 }
1735 | IntType ConstValueRef ',' LABEL ValueRef {
Chris Lattner9232b992003-04-22 18:42:41 +00001736 $$ = new std::vector<std::pair<Constant*, BasicBlock*> >();
Chris Lattnere9bb2df2001-12-03 22:26:30 +00001737 Constant *V = cast<Constant>(getValNonImprovising($1, $2));
Chris Lattner00950542001-06-06 20:29:01 +00001738
1739 if (V == 0)
1740 ThrowException("May only switch on a constant pool value!");
1741
Chris Lattner9232b992003-04-22 18:42:41 +00001742 $$->push_back(std::make_pair(V, cast<BasicBlock>(getVal($4, $5))));
Chris Lattner51727be2002-06-04 21:58:56 +00001743 };
Chris Lattner00950542001-06-06 20:29:01 +00001744
1745Inst : OptAssign InstVal {
Chris Lattnerb7474512001-10-03 15:39:04 +00001746 // Is this definition named?? if so, assign the name...
1747 if (setValueName($2, $1)) { assert(0 && "No redefin allowed!"); }
Chris Lattner00950542001-06-06 20:29:01 +00001748 InsertValue($2);
1749 $$ = $2;
Chris Lattner51727be2002-06-04 21:58:56 +00001750};
Chris Lattner00950542001-06-06 20:29:01 +00001751
Chris Lattnerc24d2082001-06-11 15:04:20 +00001752PHIList : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Chris Lattner9232b992003-04-22 18:42:41 +00001753 $$ = new std::list<std::pair<Value*, BasicBlock*> >();
1754 $$->push_back(std::make_pair(getVal(*$1, $3),
1755 cast<BasicBlock>(getVal(Type::LabelTy, $5))));
Chris Lattner30c89792001-09-07 16:35:17 +00001756 delete $1;
Chris Lattnerc24d2082001-06-11 15:04:20 +00001757 }
1758 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1759 $$ = $1;
Chris Lattner9232b992003-04-22 18:42:41 +00001760 $1->push_back(std::make_pair(getVal($1->front().first->getType(), $4),
1761 cast<BasicBlock>(getVal(Type::LabelTy, $6))));
Chris Lattner51727be2002-06-04 21:58:56 +00001762 };
Chris Lattnerc24d2082001-06-11 15:04:20 +00001763
1764
Chris Lattner30c89792001-09-07 16:35:17 +00001765ValueRefList : ResolvedVal { // Used for call statements, and memory insts...
Chris Lattner9232b992003-04-22 18:42:41 +00001766 $$ = new std::vector<Value*>();
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001767 $$->push_back($1);
Chris Lattner00950542001-06-06 20:29:01 +00001768 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001769 | ValueRefList ',' ResolvedVal {
Chris Lattner00950542001-06-06 20:29:01 +00001770 $$ = $1;
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001771 $1->push_back($3);
Chris Lattner51727be2002-06-04 21:58:56 +00001772 };
Chris Lattner00950542001-06-06 20:29:01 +00001773
1774// ValueRefListE - Just like ValueRefList, except that it may also be empty!
Chris Lattner51727be2002-06-04 21:58:56 +00001775ValueRefListE : ValueRefList | /*empty*/ { $$ = 0; };
Chris Lattner00950542001-06-06 20:29:01 +00001776
Chris Lattner4a6482b2002-09-10 19:57:26 +00001777InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1778 if (!(*$2)->isInteger() && !(*$2)->isFloatingPoint())
1779 ThrowException("Arithmetic operator requires integer or FP operands!");
1780 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1781 if ($$ == 0)
1782 ThrowException("binary operator returned null!");
1783 delete $2;
1784 }
1785 | LogicalOps Types ValueRef ',' ValueRef {
1786 if (!(*$2)->isIntegral())
1787 ThrowException("Logical operator requires integral operands!");
1788 $$ = BinaryOperator::create($1, getVal(*$2, $3), getVal(*$2, $5));
1789 if ($$ == 0)
1790 ThrowException("binary operator returned null!");
1791 delete $2;
1792 }
1793 | SetCondOps Types ValueRef ',' ValueRef {
Chris Lattner1cff96a2002-09-10 22:37:46 +00001794 $$ = new SetCondInst($1, getVal(*$2, $3), getVal(*$2, $5));
Chris Lattner00950542001-06-06 20:29:01 +00001795 if ($$ == 0)
1796 ThrowException("binary operator returned null!");
Chris Lattner30c89792001-09-07 16:35:17 +00001797 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001798 }
Chris Lattner699f1eb2002-08-14 17:12:33 +00001799 | NOT ResolvedVal {
1800 std::cerr << "WARNING: Use of eliminated 'not' instruction:"
1801 << " Replacing with 'xor'.\n";
1802
1803 Value *Ones = ConstantIntegral::getAllOnesValue($2->getType());
1804 if (Ones == 0)
1805 ThrowException("Expected integral type for not instruction!");
1806
1807 $$ = BinaryOperator::create(Instruction::Xor, $2, Ones);
Chris Lattner00950542001-06-06 20:29:01 +00001808 if ($$ == 0)
Chris Lattner699f1eb2002-08-14 17:12:33 +00001809 ThrowException("Could not create a xor instruction!");
Chris Lattner09083092001-07-08 04:57:15 +00001810 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001811 | ShiftOps ResolvedVal ',' ResolvedVal {
1812 if ($4->getType() != Type::UByteTy)
1813 ThrowException("Shift amount must be ubyte!");
Chris Lattner888d3bc2003-10-17 05:11:44 +00001814 if (!$2->getType()->isInteger())
1815 ThrowException("Shift constant expression requires integer operand!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001816 $$ = new ShiftInst($1, $2, $4);
Chris Lattner027dcc52001-07-08 21:10:27 +00001817 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001818 | CAST ResolvedVal TO Types {
Chris Lattner0f3bc5e2003-10-10 03:56:01 +00001819 if (!$4->get()->isFirstClassType())
1820 ThrowException("cast instruction to a non-primitive type: '" +
1821 $4->get()->getDescription() + "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00001822 $$ = new CastInst($2, *$4);
1823 delete $4;
Chris Lattner09083092001-07-08 04:57:15 +00001824 }
Chris Lattner76f0ff32004-03-12 05:51:36 +00001825 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1826 if ($2->getType() != Type::BoolTy)
1827 ThrowException("select condition must be boolean!");
1828 if ($4->getType() != $6->getType())
1829 ThrowException("select value types should match!");
1830 $$ = new SelectInst($2, $4, $6);
1831 }
Chris Lattner8f77dae2003-05-08 02:44:12 +00001832 | VA_ARG ResolvedVal ',' Types {
Chris Lattner99e7ab72003-10-18 05:53:13 +00001833 // FIXME: This is emulation code for an obsolete syntax. This should be
1834 // removed at some point.
1835 if (!ObsoleteVarArgs) {
1836 std::cerr << "WARNING: this file uses obsolete features. "
1837 << "Assemble and disassemble to update it.\n";
1838 ObsoleteVarArgs = true;
1839 }
1840
1841 // First, load the valist...
1842 Instruction *CurVAList = new LoadInst($2, "");
1843 CurBB->getInstList().push_back(CurVAList);
1844
1845 // Emit the vaarg instruction.
1846 $$ = new VAArgInst(CurVAList, *$4);
1847
1848 // Now we must advance the pointer and update it in memory.
1849 Instruction *TheVANext = new VANextInst(CurVAList, *$4);
1850 CurBB->getInstList().push_back(TheVANext);
1851
1852 CurBB->getInstList().push_back(new StoreInst(TheVANext, $2));
1853 delete $4;
1854 }
1855 | VAARG ResolvedVal ',' Types {
1856 $$ = new VAArgInst($2, *$4);
1857 delete $4;
1858 }
1859 | VANEXT ResolvedVal ',' Types {
1860 $$ = new VANextInst($2, *$4);
Chris Lattner8f77dae2003-05-08 02:44:12 +00001861 delete $4;
1862 }
Chris Lattner3b237fc2003-10-19 21:34:28 +00001863 | PHI_TOK PHIList {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001864 const Type *Ty = $2->front().first->getType();
Chris Lattner8ea8f362003-10-30 01:38:18 +00001865 if (!Ty->isFirstClassType())
1866 ThrowException("PHI node operands must be of first class type!");
Chris Lattnerc24d2082001-06-11 15:04:20 +00001867 $$ = new PHINode(Ty);
Chris Lattnerbd2531f2003-10-10 16:34:58 +00001868 $$->op_reserve($2->size()*2);
Chris Lattner00950542001-06-06 20:29:01 +00001869 while ($2->begin() != $2->end()) {
Chris Lattnerc24d2082001-06-11 15:04:20 +00001870 if ($2->front().first->getType() != Ty)
1871 ThrowException("All elements of a PHI node must be of the same type!");
Chris Lattnerb00c5822001-10-02 03:41:24 +00001872 cast<PHINode>($$)->addIncoming($2->front().first, $2->front().second);
Chris Lattner00950542001-06-06 20:29:01 +00001873 $2->pop_front();
1874 }
1875 delete $2; // Free the list...
1876 }
Chris Lattner93750fa2001-07-28 17:48:55 +00001877 | CALL TypesV ValueRef '(' ValueRefListE ')' {
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001878 const PointerType *PFTy;
Chris Lattner79df7c02002-03-26 18:01:55 +00001879 const FunctionType *Ty;
Chris Lattner00950542001-06-06 20:29:01 +00001880
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001881 if (!(PFTy = dyn_cast<PointerType>($2->get())) ||
1882 !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) {
Chris Lattner8b81bf52001-07-25 22:47:46 +00001883 // Pull out the types of all of the arguments...
Chris Lattner9232b992003-04-22 18:42:41 +00001884 std::vector<const Type*> ParamTypes;
Chris Lattneref9c23f2001-10-03 14:53:21 +00001885 if ($5) {
Chris Lattner9232b992003-04-22 18:42:41 +00001886 for (std::vector<Value*>::iterator I = $5->begin(), E = $5->end();
1887 I != E; ++I)
Chris Lattneref9c23f2001-10-03 14:53:21 +00001888 ParamTypes.push_back((*I)->getType());
1889 }
Chris Lattner2079fde2001-10-13 06:41:08 +00001890
1891 bool isVarArg = ParamTypes.size() && ParamTypes.back() == Type::VoidTy;
1892 if (isVarArg) ParamTypes.pop_back();
1893
Chris Lattner79df7c02002-03-26 18:01:55 +00001894 Ty = FunctionType::get($2->get(), ParamTypes, isVarArg);
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001895 PFTy = PointerType::get(Ty);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001896 }
Chris Lattner00950542001-06-06 20:29:01 +00001897
Chris Lattnerbf0a37b2002-10-15 21:41:14 +00001898 Value *V = getVal(PFTy, $3); // Get the function we're calling...
Chris Lattner00950542001-06-06 20:29:01 +00001899
Chris Lattner8b81bf52001-07-25 22:47:46 +00001900 // Create the call node...
1901 if (!$5) { // Has no arguments?
Chris Lattnera4e25182002-07-25 20:52:56 +00001902 // Make sure no arguments is a good thing!
1903 if (Ty->getNumParams() != 0)
1904 ThrowException("No arguments passed to a function that "
1905 "expects arguments!");
1906
Chris Lattner9232b992003-04-22 18:42:41 +00001907 $$ = new CallInst(V, std::vector<Value*>());
Chris Lattner8b81bf52001-07-25 22:47:46 +00001908 } else { // Has arguments?
Chris Lattner79df7c02002-03-26 18:01:55 +00001909 // Loop through FunctionType's arguments and ensure they are specified
Chris Lattner00950542001-06-06 20:29:01 +00001910 // correctly!
1911 //
Chris Lattnerd5d89962004-02-09 04:14:01 +00001912 FunctionType::param_iterator I = Ty->param_begin();
1913 FunctionType::param_iterator E = Ty->param_end();
Chris Lattner9232b992003-04-22 18:42:41 +00001914 std::vector<Value*>::iterator ArgI = $5->begin(), ArgE = $5->end();
Chris Lattner8b81bf52001-07-25 22:47:46 +00001915
1916 for (; ArgI != ArgE && I != E; ++ArgI, ++I)
1917 if ((*ArgI)->getType() != *I)
1918 ThrowException("Parameter " +(*ArgI)->getName()+ " is not of type '" +
Chris Lattner72e00252001-12-14 16:28:42 +00001919 (*I)->getDescription() + "'!");
Chris Lattner00950542001-06-06 20:29:01 +00001920
Chris Lattner8b81bf52001-07-25 22:47:46 +00001921 if (I != E || (ArgI != ArgE && !Ty->isVarArg()))
Chris Lattner00950542001-06-06 20:29:01 +00001922 ThrowException("Invalid number of parameters detected!");
Chris Lattner00950542001-06-06 20:29:01 +00001923
Chris Lattner6cdb0112001-11-26 16:54:11 +00001924 $$ = new CallInst(V, *$5);
Chris Lattner8b81bf52001-07-25 22:47:46 +00001925 }
Chris Lattnerf64d57a2003-12-23 20:39:17 +00001926 delete $2;
Chris Lattner8b81bf52001-07-25 22:47:46 +00001927 delete $5;
Chris Lattner00950542001-06-06 20:29:01 +00001928 }
1929 | MemoryInst {
1930 $$ = $1;
Chris Lattner51727be2002-06-04 21:58:56 +00001931 };
Chris Lattner00950542001-06-06 20:29:01 +00001932
Chris Lattner6cdb0112001-11-26 16:54:11 +00001933
1934// IndexList - List of indices for GEP based instructions...
1935IndexList : ',' ValueRefList {
Chris Lattner15c9c032003-09-08 18:20:29 +00001936 $$ = $2;
1937 } | /* empty */ {
1938 $$ = new std::vector<Value*>();
1939 };
1940
1941OptVolatile : VOLATILE {
1942 $$ = true;
1943 }
1944 | /* empty */ {
1945 $$ = false;
1946 };
1947
Chris Lattner027dcc52001-07-08 21:10:27 +00001948
Chris Lattner00950542001-06-06 20:29:01 +00001949MemoryInst : MALLOC Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001950 $$ = new MallocInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001951 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001952 }
1953 | MALLOC Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001954 $$ = new MallocInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001955 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001956 }
1957 | ALLOCA Types {
Chris Lattner05804b72002-09-13 22:28:45 +00001958 $$ = new AllocaInst(*$2);
Chris Lattner30c89792001-09-07 16:35:17 +00001959 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001960 }
1961 | ALLOCA Types ',' UINT ValueRef {
Chris Lattner05804b72002-09-13 22:28:45 +00001962 $$ = new AllocaInst(*$2, getVal($4, $5));
Chris Lattner30c89792001-09-07 16:35:17 +00001963 delete $2;
Chris Lattner00950542001-06-06 20:29:01 +00001964 }
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001965 | FREE ResolvedVal {
Chris Lattner9b625032002-05-06 16:15:30 +00001966 if (!isa<PointerType>($2->getType()))
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001967 ThrowException("Trying to free nonpointer type " +
Chris Lattner72e00252001-12-14 16:28:42 +00001968 $2->getType()->getDescription() + "!");
Chris Lattnerbcbf6ba2001-07-26 16:29:15 +00001969 $$ = new FreeInst($2);
Chris Lattner00950542001-06-06 20:29:01 +00001970 }
1971
Chris Lattner15c9c032003-09-08 18:20:29 +00001972 | OptVolatile LOAD Types ValueRef {
1973 if (!isa<PointerType>($3->get()))
Chris Lattner2079fde2001-10-13 06:41:08 +00001974 ThrowException("Can't load from nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001975 (*$3)->getDescription());
Chris Lattner79ad13802003-09-08 20:29:46 +00001976 $$ = new LoadInst(getVal(*$3, $4), "", $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001977 delete $3;
Chris Lattner027dcc52001-07-08 21:10:27 +00001978 }
Chris Lattner15c9c032003-09-08 18:20:29 +00001979 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1980 const PointerType *PT = dyn_cast<PointerType>($5->get());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001981 if (!PT)
Chris Lattner72e00252001-12-14 16:28:42 +00001982 ThrowException("Can't store to a nonpointer type: " +
Chris Lattner15c9c032003-09-08 18:20:29 +00001983 (*$5)->getDescription());
Chris Lattner8c8418d2003-09-01 16:31:28 +00001984 const Type *ElTy = PT->getElementType();
Chris Lattner15c9c032003-09-08 18:20:29 +00001985 if (ElTy != $3->getType())
1986 ThrowException("Can't store '" + $3->getType()->getDescription() +
Chris Lattner72e00252001-12-14 16:28:42 +00001987 "' into space of type '" + ElTy->getDescription() + "'!");
Chris Lattner0383cc42002-08-21 23:51:21 +00001988
Chris Lattner79ad13802003-09-08 20:29:46 +00001989 $$ = new StoreInst($3, getVal(*$5, $6), $1);
Chris Lattner15c9c032003-09-08 18:20:29 +00001990 delete $5;
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001991 }
Chris Lattner6cdb0112001-11-26 16:54:11 +00001992 | GETELEMENTPTR Types ValueRef IndexList {
Chris Lattner51727be2002-06-04 21:58:56 +00001993 if (!isa<PointerType>($2->get()))
Chris Lattnerab5ac6b2001-07-08 23:22:50 +00001994 ThrowException("getelementptr insn requires pointer operand!");
Chris Lattner830b6f92004-04-05 01:30:04 +00001995
1996 // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte struct
1997 // indices to uint struct indices for compatibility.
1998 generic_gep_type_iterator<std::vector<Value*>::iterator>
1999 GTI = gep_type_begin($2->get(), $4->begin(), $4->end()),
2000 GTE = gep_type_end($2->get(), $4->begin(), $4->end());
2001 for (unsigned i = 0, e = $4->size(); i != e && GTI != GTE; ++i, ++GTI)
2002 if (isa<StructType>(*GTI)) // Only change struct indices
2003 if (ConstantUInt *CUI = dyn_cast<ConstantUInt>((*$4)[i]))
2004 if (CUI->getType() == Type::UByteTy)
2005 (*$4)[i] = ConstantExpr::getCast(CUI, Type::UIntTy);
2006
Chris Lattner30c89792001-09-07 16:35:17 +00002007 if (!GetElementPtrInst::getIndexedType(*$2, *$4, true))
Chris Lattner830b6f92004-04-05 01:30:04 +00002008 ThrowException("Invalid getelementptr indices for type '" +
2009 (*$2)->getDescription()+ "'!");
Chris Lattner30c89792001-09-07 16:35:17 +00002010 $$ = new GetElementPtrInst(getVal(*$2, $3), *$4);
2011 delete $2; delete $4;
Chris Lattner51727be2002-06-04 21:58:56 +00002012 };
Chris Lattner027dcc52001-07-08 21:10:27 +00002013
Brian Gaeked0fde302003-11-11 22:41:34 +00002014
Chris Lattner00950542001-06-06 20:29:01 +00002015%%
Chris Lattner09083092001-07-08 04:57:15 +00002016int yyerror(const char *ErrorMsg) {
Chris Lattner9232b992003-04-22 18:42:41 +00002017 std::string where
2018 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002019 + ":" + utostr((unsigned) llvmAsmlineno) + ": ";
Chris Lattner9232b992003-04-22 18:42:41 +00002020 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
Chris Lattnerbc280ab2004-03-30 20:58:25 +00002021 if (yychar == YYEMPTY || yychar == 0)
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002022 errMsg += "end-of-file.";
2023 else
Chris Lattner9232b992003-04-22 18:42:41 +00002024 errMsg += "token: '" + std::string(llvmAsmtext, llvmAsmleng) + "'";
Vikram S. Adved3f7eb02002-07-14 22:59:28 +00002025 ThrowException(errMsg);
Chris Lattner00950542001-06-06 20:29:01 +00002026 return 0;
2027}